|
| 1 | +import type { Operation } from '../../../../src/http/Operation'; |
| 2 | +import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation'; |
| 3 | +import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError'; |
| 4 | +import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler'; |
| 5 | +import { |
| 6 | + MethodFilterHandler, |
| 7 | +} from '../../../../src/util/handlers/MethodFilterHandler'; |
| 8 | + |
| 9 | +describe('A MethodFilterHandler', (): void => { |
| 10 | + const modes = [ 'PATCH', 'POST' ]; |
| 11 | + const result = 'RESULT'; |
| 12 | + let operation: Operation; |
| 13 | + let source: jest.Mocked<AsyncHandler<Operation, string>>; |
| 14 | + let handler: MethodFilterHandler<Operation, string>; |
| 15 | + |
| 16 | + beforeEach(async(): Promise<void> => { |
| 17 | + operation = { |
| 18 | + method: 'PATCH', |
| 19 | + preferences: {}, |
| 20 | + permissionSet: {}, |
| 21 | + target: { path: 'http://example.com/foo' }, |
| 22 | + body: new BasicRepresentation(), |
| 23 | + }; |
| 24 | + |
| 25 | + source = { |
| 26 | + canHandle: jest.fn(), |
| 27 | + handle: jest.fn().mockResolvedValue(result), |
| 28 | + } as any; |
| 29 | + |
| 30 | + handler = new MethodFilterHandler(modes, source); |
| 31 | + }); |
| 32 | + |
| 33 | + it('rejects unknown methods.', async(): Promise<void> => { |
| 34 | + operation.method = 'GET'; |
| 35 | + await expect(handler.canHandle(operation)).rejects.toThrow(NotImplementedHttpError); |
| 36 | + }); |
| 37 | + |
| 38 | + it('checks if the source handle supports the request.', async(): Promise<void> => { |
| 39 | + operation.method = 'PATCH'; |
| 40 | + await expect(handler.canHandle(operation)).resolves.toBeUndefined(); |
| 41 | + operation.method = 'POST'; |
| 42 | + await expect(handler.canHandle(operation)).resolves.toBeUndefined(); |
| 43 | + source.canHandle.mockRejectedValueOnce(new Error('not supported')); |
| 44 | + await expect(handler.canHandle(operation)).rejects.toThrow('not supported'); |
| 45 | + expect(source.canHandle).toHaveBeenLastCalledWith(operation); |
| 46 | + }); |
| 47 | + |
| 48 | + it('calls the source extractor.', async(): Promise<void> => { |
| 49 | + await expect(handler.handle(operation)).resolves.toBe(result); |
| 50 | + expect(source.handle).toHaveBeenLastCalledWith(operation); |
| 51 | + }); |
| 52 | +}); |
0 commit comments