From 385e1a4cdf362b8b34bc0651fc382e18c99b9963 Mon Sep 17 00:00:00 2001 From: Ruben Verborgh Date: Wed, 2 Sep 2020 17:27:32 +0200 Subject: [PATCH] change: Support all PUT operations, error later --- src/ldp/operations/PutOperationHandler.ts | 8 ++++---- test/unit/ldp/operations/PutOperationHandler.test.ts | 9 ++++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/ldp/operations/PutOperationHandler.ts b/src/ldp/operations/PutOperationHandler.ts index 841939234..7aceacc1e 100644 --- a/src/ldp/operations/PutOperationHandler.ts +++ b/src/ldp/operations/PutOperationHandler.ts @@ -20,13 +20,13 @@ export class PutOperationHandler extends OperationHandler { if (input.method !== 'PUT') { throw new UnsupportedHttpError('This handler only supports PUT operations.'); } - if (typeof input.body !== 'object') { - throw new UnsupportedHttpError('PUT operations require a body.'); - } } public async handle(input: Operation): Promise { - await this.store.setRepresentation(input.target, input.body!); + if (typeof input.body !== 'object') { + throw new UnsupportedHttpError('PUT operations require a body.'); + } + await this.store.setRepresentation(input.target, input.body); return { identifier: input.target }; } } diff --git a/test/unit/ldp/operations/PutOperationHandler.test.ts b/test/unit/ldp/operations/PutOperationHandler.test.ts index 68ff8ef66..6f9abea01 100644 --- a/test/unit/ldp/operations/PutOperationHandler.test.ts +++ b/test/unit/ldp/operations/PutOperationHandler.test.ts @@ -11,10 +11,9 @@ describe('A PutOperationHandler', (): void => { store.setRepresentation = jest.fn(async(): Promise => {}); }); - it('only supports PUT operations with a body.', async(): Promise => { - await expect(handler.canHandle({ method: 'PUT' } as Operation)).rejects.toThrow(UnsupportedHttpError); + it('only supports PUT operations.', async(): Promise => { await expect(handler.canHandle({ method: 'GET' } as Operation)).rejects.toThrow(UnsupportedHttpError); - await expect(handler.canHandle({ method: 'PUT', body: { dataType: 'test' }} as Operation)).resolves.toBeUndefined(); + await expect(handler.canHandle({ method: 'PUT' } as Operation)).resolves.toBeUndefined(); }); it('sets the representation in the store and returns its identifier.', async(): Promise => { @@ -23,4 +22,8 @@ describe('A PutOperationHandler', (): void => { expect(store.setRepresentation).toHaveBeenCalledTimes(1); expect(store.setRepresentation).toHaveBeenLastCalledWith({ path: 'url' }, { dataType: 'test' }); }); + + it('errors when there is no body.', async(): Promise => { + await expect(handler.handle({ method: 'PUT' } as Operation)).rejects.toThrow(UnsupportedHttpError); + }); });