parent
4abd6f333c
commit
d6c1ccaf14
|
@ -78,7 +78,31 @@ describe('DocumentService', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should emit the not-found document if the document is not found on the server', () => {
|
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', () => {
|
it('should not make a request to the server if the doc is in the cache already', () => {
|
||||||
|
|
|
@ -8,7 +8,7 @@ import 'rxjs/add/operator/switchMap';
|
||||||
import { LocationService } from 'app/shared/location.service';
|
import { LocationService } from 'app/shared/location.service';
|
||||||
import { Logger } from 'app/shared/logger.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 {
|
export interface DocumentContents {
|
||||||
title: string;
|
title: string;
|
||||||
|
@ -19,6 +19,7 @@ export interface DocumentContents {
|
||||||
export class DocumentService {
|
export class DocumentService {
|
||||||
|
|
||||||
private cache = new Map<string, Observable<DocumentContents>>();
|
private cache = new Map<string, Observable<DocumentContents>>();
|
||||||
|
private fileNotFoundPath = this.computePath(FILE_NOT_FOUND_URL);
|
||||||
|
|
||||||
currentDocument: Observable<DocumentContents>;
|
currentDocument: Observable<DocumentContents>;
|
||||||
|
|
||||||
|
@ -43,10 +44,14 @@ export class DocumentService {
|
||||||
.get(path)
|
.get(path)
|
||||||
.map(res => res.json())
|
.map(res => res.json())
|
||||||
.catch((error: Response) => {
|
.catch((error: Response) => {
|
||||||
if (error.status === 404 && path !== FILE_NOT_FOUND_DOC) {
|
if (error.status === 404) {
|
||||||
this.logger.error(`Document file not found at '${path}'`);
|
if (path !== this.fileNotFoundPath) {
|
||||||
// using `getDocument` means that we can fetch the 404 doc contents from the server and cache it
|
this.logger.error(`Document file not found at '${path}'`);
|
||||||
return this.getDocument(FILE_NOT_FOUND_DOC);
|
// 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 {
|
} else {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue