refactor: Split off ErrorResponseWriter

This commit is contained in:
Joachim Van Herwegen
2020-10-26 12:05:39 +01:00
parent f4161d406c
commit e8fdcb0ad0
18 changed files with 150 additions and 72 deletions

View File

@@ -16,7 +16,9 @@ describe('A BasicResponseWriter', (): void => {
response = createResponse({ eventEmitter: EventEmitter });
});
it('requires the description body to be a string or binary stream if present.', async(): Promise<void> => {
it('requires a description body, with a binary stream if there is data.', async(): Promise<void> => {
await expect(writer.canHandle({ response, result: new Error('error') }))
.rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, result: { body: { binary: false }} as ResponseDescription }))
.rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, result: { body: { binary: true }} as ResponseDescription }))
@@ -72,34 +74,4 @@ describe('A BasicResponseWriter', (): void => {
await writer.handle({ response, result: { identifier: { path: 'path' }, body }});
await end;
});
it('responds with 500 if an error if there is an error.', async(): Promise<void> => {
await writer.handle({ response, result: new Error('error') });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(500);
expect(response._getData()).toMatch('Error: error');
});
it('responds with the given statuscode if there is an HttpError.', async(): Promise<void> => {
const error = new UnsupportedHttpError('error');
await writer.handle({ response, result: error });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(error.statusCode);
expect(response._getData()).toMatch('UnsupportedHttpError: error');
});
it('responds with the error name and message when no stack trace is lazily generated.', async(): Promise<void> => {
const error = new Error('error');
error.stack = undefined;
await writer.handle({ response, result: error });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(500);
expect(response._getData()).toMatch('Error: error');
});
it('ends its response with a newline if there is an error.', async(): Promise<void> => {
await writer.handle({ response, result: new Error('error') });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getData().endsWith('\n')).toBeTruthy();
});
});

View File

@@ -0,0 +1,54 @@
import { EventEmitter } from 'events';
import type { MockResponse } from 'node-mocks-http';
import { createResponse } from 'node-mocks-http';
import { ErrorResponseWriter } from '../../../../src/ldp/http/ErrorResponseWriter';
import type { ResponseDescription } from '../../../../src/ldp/operations/ResponseDescription';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('An ErrorResponseWriter', (): void => {
const writer = new ErrorResponseWriter();
let response: MockResponse<any>;
beforeEach(async(): Promise<void> => {
response = createResponse({ eventEmitter: EventEmitter });
});
it('requires the input to be an error.', async(): Promise<void> => {
await expect(writer.canHandle({ response, result: new Error('error') }))
.resolves.toBeUndefined();
await expect(writer.canHandle({ response, result: { body: { binary: false }} as ResponseDescription }))
.rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, result: { body: { binary: true }} as ResponseDescription }))
.rejects.toThrow(UnsupportedHttpError);
});
it('responds with 500 if an error if there is an error.', async(): Promise<void> => {
await writer.handle({ response, result: new Error('error') });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(500);
expect(response._getData()).toMatch('Error: error');
});
it('responds with the given statuscode if there is an HttpError.', async(): Promise<void> => {
const error = new UnsupportedHttpError('error');
await writer.handle({ response, result: error });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(error.statusCode);
expect(response._getData()).toMatch('UnsupportedHttpError: error');
});
it('responds with the error name and message when no stack trace is lazily generated.', async(): Promise<void> => {
const error = new Error('error');
error.stack = undefined;
await writer.handle({ response, result: error });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getStatusCode()).toBe(500);
expect(response._getData()).toMatch('Error: error');
});
it('ends its response with a newline if there is an error.', async(): Promise<void> => {
await writer.handle({ response, result: new Error('error') });
expect(response._isEndCalled()).toBeTruthy();
expect(response._getData().endsWith('\n')).toBeTruthy();
});
});