feat: Handle OPTIONS requests in OperationHandler

This commit is contained in:
Joachim Van Herwegen
2022-03-17 15:34:03 +01:00
parent 6f83ac5ead
commit ad3edcf1a8
10 changed files with 110 additions and 13 deletions

View File

@@ -70,7 +70,7 @@ describe('An http server with middleware', (): void => {
.set('Access-Control-Request-Headers', 'content-type')
.set('Access-Control-Request-Method', 'POST')
.set('Host', 'test.com')
.expect(204);
.expect(200);
expect(res.header).toEqual(expect.objectContaining({
'access-control-allow-origin': '*',
'access-control-allow-headers': 'content-type',

View File

@@ -44,10 +44,9 @@ const allModes = [ AM.read, AM.append, AM.create, AM.write, AM.delete ];
// For PUT/PATCH/DELETE we return 205 instead of 200/204
/* eslint-disable no-multi-spaces */
const table: [string, string, AM[], AM[] | undefined, string, string, number, number][] = [
// We currently handle OPTIONS before authorization
// [ 'OPTIONS', 'C/R', [], undefined, '', '', 401, 401 ],
// [ 'OPTIONS', 'C/R', [], [ AM.read ], '', '', 200, 404 ],
// [ 'OPTIONS', 'C/R', [ AM.read ], undefined, '', '', 200, 404 ],
[ 'OPTIONS', 'C/R', [], undefined, '', '', 401, 401 ],
[ 'OPTIONS', 'C/R', [], [ AM.read ], '', '', 204, 404 ],
[ 'OPTIONS', 'C/R', [ AM.read ], undefined, '', '', 204, 404 ],
[ 'HEAD', 'C/R', [], undefined, '', '', 401, 401 ],
[ 'HEAD', 'C/R', [], [ AM.read ], '', '', 200, 404 ],

View File

@@ -0,0 +1,44 @@
import { OptionsOperationHandler } from '../../../../src/http/ldp/OptionsOperationHandler';
import type { Operation } from '../../../../src/http/Operation';
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation';
import { BasicConditions } from '../../../../src/storage/BasicConditions';
import type { ResourceSet } from '../../../../src/storage/ResourceSet';
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
describe('An OptionsOperationHandler', (): void => {
let operation: Operation;
const conditions = new BasicConditions({});
const preferences = {};
const body = new BasicRepresentation();
let resourceSet: jest.Mocked<ResourceSet>;
let handler: OptionsOperationHandler;
beforeEach(async(): Promise<void> => {
operation = { method: 'OPTIONS', target: { path: 'http://test.com/foo' }, preferences, conditions, body };
resourceSet = {
hasResource: jest.fn().mockResolvedValue(true),
};
handler = new OptionsOperationHandler(resourceSet);
});
it('only supports Options operations.', async(): Promise<void> => {
await expect(handler.canHandle({ operation })).resolves.toBeUndefined();
operation.method = 'GET';
await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError);
operation.method = 'HEAD';
await expect(handler.canHandle({ operation })).rejects.toThrow(NotImplementedHttpError);
});
it('returns a 204 response.', async(): Promise<void> => {
const result = await handler.handle({ operation });
expect(result.statusCode).toBe(204);
expect(result.metadata).toBeUndefined();
expect(result.data).toBeUndefined();
});
it('returns a 404 if the target resource does not exist.', async(): Promise<void> => {
resourceSet.hasResource.mockResolvedValueOnce(false);
await expect(handler.handle({ operation })).rejects.toThrow(NotFoundHttpError);
});
});