diff --git a/aio/src/app/documents/document.service.spec.ts b/aio/src/app/documents/document.service.spec.ts index 9381aa019b..4f32ced7ea 100644 --- a/aio/src/app/documents/document.service.spec.ts +++ b/aio/src/app/documents/document.service.spec.ts @@ -78,7 +78,31 @@ describe('DocumentService', () => { }); it('should emit the not-found document if the document is not found on the server', () => { + const { service, backend } = getServices('missing/url'); + const connections = backend.connectionsArray; + service.currentDocument.subscribe(); + connections[0].mockError(new Response(new ResponseOptions({ status: 404, statusText: 'NOT FOUND'})) as any); + expect(connections.length).toEqual(2); + expect(connections[1].request.url).toEqual(CONTENT_URL_PREFIX + 'file-not-found.json'); + }); + + it('should emit a hard-coded not-found document if the not-found document is not found on the server', () => { + let currentDocument: DocumentContents; + const notFoundDoc = { title: 'Not Found', contents: 'Document not found' }; + const nextDoc = { title: 'Next Doc' }; + const { service, backend, location } = getServices('file-not-found'); + const connections = backend.connectionsArray; + service.currentDocument.subscribe(doc => currentDocument = doc); + + connections[0].mockError(new Response(new ResponseOptions({ status: 404, statusText: 'NOT FOUND'})) as any); + expect(connections.length).toEqual(1); + expect(currentDocument).toEqual(notFoundDoc); + + // now check that we haven't killed the currentDocument observable sequence + location.urlSubject.next('new/url'); + connections[1].mockRespond(createResponse(nextDoc)); + expect(currentDocument).toEqual(nextDoc); }); it('should not make a request to the server if the doc is in the cache already', () => { diff --git a/aio/src/app/documents/document.service.ts b/aio/src/app/documents/document.service.ts index e0774dd21a..fb12bfc157 100644 --- a/aio/src/app/documents/document.service.ts +++ b/aio/src/app/documents/document.service.ts @@ -8,7 +8,7 @@ import 'rxjs/add/operator/switchMap'; import { LocationService } from 'app/shared/location.service'; import { Logger } from 'app/shared/logger.service'; -const FILE_NOT_FOUND_DOC = 'file-not-found'; +const FILE_NOT_FOUND_URL = 'file-not-found'; export interface DocumentContents { title: string; @@ -19,6 +19,7 @@ export interface DocumentContents { export class DocumentService { private cache = new Map>(); + private fileNotFoundPath = this.computePath(FILE_NOT_FOUND_URL); currentDocument: Observable; @@ -43,10 +44,14 @@ export class DocumentService { .get(path) .map(res => res.json()) .catch((error: Response) => { - if (error.status === 404 && path !== FILE_NOT_FOUND_DOC) { - this.logger.error(`Document file not found at '${path}'`); - // using `getDocument` means that we can fetch the 404 doc contents from the server and cache it - return this.getDocument(FILE_NOT_FOUND_DOC); + if (error.status === 404) { + if (path !== this.fileNotFoundPath) { + this.logger.error(`Document file not found at '${path}'`); + // using `getDocument` means that we can fetch the 404 doc contents from the server and cache it + return this.getDocument(FILE_NOT_FOUND_URL); + } else { + return Observable.of({ title: 'Not Found', contents: 'Document not found' }); + } } else { throw error; }