fix: Prevent server from crashing if requested data can't be handled

This commit is contained in:
Joachim Van Herwegen
2020-07-13 10:41:10 +02:00
parent ea788ba406
commit ccd3f1738c
6 changed files with 31 additions and 5 deletions

View File

@@ -42,7 +42,7 @@ describe('An AuthenticatedLdpHandler', (): void => {
it('can handle input.', async(): Promise<void> => {
const handler = new AuthenticatedLdpHandler(args);
await expect(handler.handle({ request: 'request' as any, response: 'response' as any })).resolves.toEqual('response');
await expect(handler.handle({ request: 'request' as any, response: 'response' as any })).resolves.toEqual(undefined);
expect(responseFn).toHaveBeenCalledTimes(1);
expect(responseFn).toHaveBeenLastCalledWith({ response: 'response', description: 'operation' as any });
});
@@ -51,7 +51,7 @@ describe('An AuthenticatedLdpHandler', (): void => {
args.requestParser = new StaticAsyncHandler(false, null);
const handler = new AuthenticatedLdpHandler(args);
await expect(handler.handle({ request: 'request' as any, response: null })).resolves.toEqual('response');
await expect(handler.handle({ request: 'request' as any, response: null })).resolves.toEqual(undefined);
expect(responseFn).toHaveBeenCalledTimes(1);
expect(responseFn.mock.calls[0][0].error).toBeInstanceOf(Error);
});

View File

@@ -20,6 +20,12 @@ describe('A SimpleResponseWriter', (): void => {
await expect(writer.canHandle({ response })).rejects.toThrow(UnsupportedHttpError);
});
it('requires the description body to be a string or binary stream if present.', async(): Promise<void> => {
await expect(writer.canHandle({ response, description: { body: { dataType: 'quad' }} as ResponseDescription })).rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, description: { body: { dataType: 'string' }} as ResponseDescription })).resolves.toBeUndefined();
await expect(writer.canHandle({ response, description: { body: { dataType: 'binary' }} as ResponseDescription })).resolves.toBeUndefined();
});
it('responds with status code 200 and a location header if there is a description.', async(): Promise<void> => {
await writer.handle({ response, description: { identifier: { path: 'path' }}});
expect(response._isEndCalled()).toBeTruthy();

View File

@@ -24,8 +24,9 @@ describe('ExpressHttpServer', (): void => {
let server: Server;
let canHandleJest: jest.Mock<Promise<void>, []>;
let handleJest: jest.Mock<Promise<void>, [any]>;
let handler: SimpleHttpHandler;
beforeEach(async(): Promise<void> => {
const handler = new SimpleHttpHandler();
handler = new SimpleHttpHandler();
canHandleJest = jest.fn(async(): Promise<void> => undefined);
handleJest = jest.fn(async(input): Promise<void> => handle(input));
@@ -74,4 +75,12 @@ describe('ExpressHttpServer', (): void => {
response: expect.objectContaining({}),
});
});
it('catches errors thrown by its handler.', async(): Promise<void> => {
handler.handle = async(): Promise<void> => {
throw new Error('dummyError');
};
const res = await request(server).get('/').expect(500);
expect(res.text).toContain('dummyError');
});
});