change: Support all PUT operations, error later

This commit is contained in:
Ruben Verborgh 2020-09-02 17:27:32 +02:00 committed by Joachim Van Herwegen
parent 4965b476c9
commit 385e1a4cdf
2 changed files with 10 additions and 7 deletions

View File

@ -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<ResponseDescription> {
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 };
}
}

View File

@ -11,10 +11,9 @@ describe('A PutOperationHandler', (): void => {
store.setRepresentation = jest.fn(async(): Promise<void> => {});
});
it('only supports PUT operations with a body.', async(): Promise<void> => {
await expect(handler.canHandle({ method: 'PUT' } as Operation)).rejects.toThrow(UnsupportedHttpError);
it('only supports PUT operations.', async(): Promise<void> => {
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<void> => {
@ -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<void> => {
await expect(handler.handle({ method: 'PUT' } as Operation)).rejects.toThrow(UnsupportedHttpError);
});
});