change: Refactor AllVoidCompositeHandler into SequenceHandler.

This commit is contained in:
Ruben Verborgh
2020-12-08 19:18:08 +00:00
committed by Joachim Van Herwegen
parent 7cae14acf7
commit ba47ce7951
11 changed files with 112 additions and 69 deletions

View File

@@ -13,7 +13,7 @@ import type {
OperationHandler,
} from '../../src/index';
import {
AcceptPreferenceParser, AllVoidCompositeHandler,
AcceptPreferenceParser,
BasicMetadataExtractor,
BasicRequestParser,
BasicResponseWriter,
@@ -35,6 +35,7 @@ import {
PutOperationHandler,
RawBodyParser,
RepresentationConvertingStore,
SequenceHandler,
SingleThreadedResourceLocker,
SlugParser,
SparqlUpdatePatchHandler,
@@ -117,7 +118,7 @@ export const getOperationHandler = (store: ResourceStore): OperationHandler => {
};
export const getResponseWriter = (): ResponseWriter => {
const serializer = new AllVoidCompositeHandler([
const serializer = new SequenceHandler([
new MappedMetadataWriter({
[CONTENT_TYPE]: 'content-type',
[HTTP.location]: 'location',

View File

@@ -1,30 +0,0 @@
import { AllVoidCompositeHandler } from '../../../src/util/AllVoidCompositeHandler';
import type { AsyncHandler } from '../../../src/util/AsyncHandler';
describe('An AllVoidCompositeHandler', (): void => {
let handler1: AsyncHandler<string>;
let handler2: AsyncHandler<string>;
let composite: AllVoidCompositeHandler<string>;
beforeEach(async(): Promise<void> => {
handler1 = { handleSafe: jest.fn() } as any;
handler2 = { handleSafe: jest.fn() } as any;
composite = new AllVoidCompositeHandler<string>([ handler1, handler2 ]);
});
it('can handle all input.', async(): Promise<void> => {
await expect(composite.canHandle('test')).resolves.toBeUndefined();
});
it('runs all handlers without caring about their result.', async(): Promise<void> => {
handler1.handleSafe = jest.fn(async(): Promise<void> => {
throw new Error('error');
});
await expect(composite.handleSafe('test')).resolves.toBeUndefined();
expect(handler1.handleSafe).toHaveBeenCalledTimes(1);
expect(handler1.handleSafe).toHaveBeenLastCalledWith('test');
expect(handler2.handleSafe).toHaveBeenCalledTimes(1);
expect(handler2.handleSafe).toHaveBeenLastCalledWith('test');
});
});

View File

@@ -0,0 +1,64 @@
import type { AsyncHandler } from '../../../src/util/AsyncHandler';
import { SequenceHandler } from '../../../src/util/SequenceHandler';
describe('A SequenceHandler', (): void => {
const handlers: jest.Mocked<AsyncHandler<string, string>>[] = [
{
canHandle: jest.fn(),
handle: jest.fn().mockResolvedValue('0'),
} as any,
{
canHandle: jest.fn().mockRejectedValue(new Error('not supported')),
handle: jest.fn().mockRejectedValue(new Error('should not be called')),
} as any,
{
canHandle: jest.fn(),
handle: jest.fn().mockResolvedValue('2'),
} as any,
];
let composite: SequenceHandler<string, string>;
beforeEach(async(): Promise<void> => {
composite = new SequenceHandler<string, string>(handlers);
});
it('can handle all input.', async(): Promise<void> => {
await expect(composite.canHandle('test')).resolves.toBeUndefined();
});
it('runs all supported handlers.', async(): Promise<void> => {
await composite.handleSafe('test');
expect(handlers[0].canHandle).toHaveBeenCalledTimes(1);
expect(handlers[0].canHandle).toHaveBeenLastCalledWith('test');
expect(handlers[0].handle).toHaveBeenCalledTimes(1);
expect(handlers[0].handle).toHaveBeenLastCalledWith('test');
expect(handlers[1].canHandle).toHaveBeenCalledTimes(1);
expect(handlers[1].canHandle).toHaveBeenLastCalledWith('test');
expect(handlers[1].handle).toHaveBeenCalledTimes(0);
expect(handlers[2].canHandle).toHaveBeenCalledTimes(1);
expect(handlers[2].canHandle).toHaveBeenLastCalledWith('test');
expect(handlers[2].handle).toHaveBeenCalledTimes(1);
expect(handlers[2].handle).toHaveBeenLastCalledWith('test');
});
it('returns the result of the last supported handler.', async(): Promise<void> => {
await expect(composite.handleSafe('test')).resolves.toBe('2');
handlers[2].canHandle.mockRejectedValueOnce(new Error('not supported'));
await expect(composite.handleSafe('test')).resolves.toBe('0');
});
it('returns undefined if no handler is supported.', async(): Promise<void> => {
handlers[0].canHandle.mockRejectedValueOnce(new Error('not supported'));
handlers[2].canHandle.mockRejectedValueOnce(new Error('not supported'));
await expect(composite.handleSafe('test')).resolves.toBeUndefined();
});
it('errors if a handler errors.', async(): Promise<void> => {
handlers[2].handle.mockRejectedValueOnce(new Error('failure'));
await expect(composite.handleSafe('test')).rejects.toThrow('failure');
});
});