-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Handle OPTIONS requests in OperationHandler
- Loading branch information
Showing
10 changed files
with
110 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { ResourceSet } from '../../storage/ResourceSet'; | ||
import { NotFoundHttpError } from '../../util/errors/NotFoundHttpError'; | ||
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError'; | ||
import { NoContentResponseDescription } from '../output/response/NoContentResponseDescription'; | ||
import type { ResponseDescription } from '../output/response/ResponseDescription'; | ||
import type { OperationHandlerInput } from './OperationHandler'; | ||
import { OperationHandler } from './OperationHandler'; | ||
|
||
/** | ||
* Handles OPTIONS {@link Operation}s by always returning a 204. | ||
*/ | ||
export class OptionsOperationHandler extends OperationHandler { | ||
private readonly resourceSet: ResourceSet; | ||
|
||
/** | ||
* Uses a {@link ResourceSet} to determine the existence of the target resource which impacts the response code. | ||
* @param resourceSet - {@link ResourceSet} that knows if the target resource exists or not. | ||
*/ | ||
public constructor(resourceSet: ResourceSet) { | ||
super(); | ||
this.resourceSet = resourceSet; | ||
} | ||
|
||
public async canHandle({ operation }: OperationHandlerInput): Promise<void> { | ||
if (operation.method !== 'OPTIONS') { | ||
throw new NotImplementedHttpError('This handler only supports OPTIONS operations'); | ||
} | ||
} | ||
|
||
public async handle({ operation }: OperationHandlerInput): Promise<ResponseDescription> { | ||
if (!await this.resourceSet.hasResource(operation.target)) { | ||
throw new NotFoundHttpError(); | ||
} | ||
return new NoContentResponseDescription(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ResponseDescription } from './ResponseDescription'; | ||
|
||
/** | ||
* Corresponds to a 204 response. | ||
*/ | ||
export class NoContentResponseDescription extends ResponseDescription { | ||
public constructor() { | ||
super(204); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |