-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Return correct status codes for invalid requests
- Loading branch information
Showing
12 changed files
with
179 additions
and
17 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
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,30 @@ | ||
import { NotImplementedHttpError } from '../errors/NotImplementedHttpError'; | ||
import { AsyncHandler } from './AsyncHandler'; | ||
|
||
/** | ||
* Only accepts requests where the input has a `method` field that matches any one of the given methods. | ||
* In case of a match, the input will be sent to the source handler. | ||
*/ | ||
export class MethodFilterHandler<TIn extends { method: string }, TOut> extends AsyncHandler<TIn, TOut> { | ||
private readonly methods: string[]; | ||
private readonly source: AsyncHandler<TIn, TOut>; | ||
|
||
public constructor(methods: string[], source: AsyncHandler<TIn, TOut>) { | ||
super(); | ||
this.methods = methods; | ||
this.source = source; | ||
} | ||
|
||
public async canHandle(input: TIn): Promise<void> { | ||
if (!this.methods.includes(input.method)) { | ||
throw new NotImplementedHttpError( | ||
`Cannot determine permissions of ${input.method}, only ${this.methods.join(',')}.`, | ||
); | ||
} | ||
await this.source.canHandle(input); | ||
} | ||
|
||
public async handle(input: TIn): Promise<TOut> { | ||
return this.source.handle(input); | ||
} | ||
} |
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,18 @@ | ||
import type { HttpError } from '../errors/HttpError'; | ||
import { AsyncHandler } from './AsyncHandler'; | ||
|
||
/** | ||
* Utility handler that can handle all input and always throws the given error. | ||
*/ | ||
export class StaticThrowHandler extends AsyncHandler<any, never> { | ||
private readonly error: HttpError; | ||
|
||
public constructor(error: HttpError) { | ||
super(); | ||
this.error = error; | ||
} | ||
|
||
public async handle(): Promise<never> { | ||
throw this.error; | ||
} | ||
} |
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
14 changes: 5 additions & 9 deletions
14
...issions/SparqlPatchModesExtractor.test.ts → ...ssions/SparqlUpdateModesExtractor.test.ts
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,52 @@ | ||
import type { Operation } from '../../../../src/http/Operation'; | ||
import { BasicRepresentation } from '../../../../src/http/representation/BasicRepresentation'; | ||
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError'; | ||
import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler'; | ||
import { | ||
MethodFilterHandler, | ||
} from '../../../../src/util/handlers/MethodFilterHandler'; | ||
|
||
describe('A MethodFilterHandler', (): void => { | ||
const modes = [ 'PATCH', 'POST' ]; | ||
const result = 'RESULT'; | ||
let operation: Operation; | ||
let source: jest.Mocked<AsyncHandler<Operation, string>>; | ||
let handler: MethodFilterHandler<Operation, string>; | ||
|
||
beforeEach(async(): Promise<void> => { | ||
operation = { | ||
method: 'PATCH', | ||
preferences: {}, | ||
permissionSet: {}, | ||
target: { path: 'http://example.com/foo' }, | ||
body: new BasicRepresentation(), | ||
}; | ||
|
||
source = { | ||
canHandle: jest.fn(), | ||
handle: jest.fn().mockResolvedValue(result), | ||
} as any; | ||
|
||
handler = new MethodFilterHandler(modes, source); | ||
}); | ||
|
||
it('rejects unknown methods.', async(): Promise<void> => { | ||
operation.method = 'GET'; | ||
await expect(handler.canHandle(operation)).rejects.toThrow(NotImplementedHttpError); | ||
}); | ||
|
||
it('checks if the source handle supports the request.', async(): Promise<void> => { | ||
operation.method = 'PATCH'; | ||
await expect(handler.canHandle(operation)).resolves.toBeUndefined(); | ||
operation.method = 'POST'; | ||
await expect(handler.canHandle(operation)).resolves.toBeUndefined(); | ||
source.canHandle.mockRejectedValueOnce(new Error('not supported')); | ||
await expect(handler.canHandle(operation)).rejects.toThrow('not supported'); | ||
expect(source.canHandle).toHaveBeenLastCalledWith(operation); | ||
}); | ||
|
||
it('calls the source extractor.', async(): Promise<void> => { | ||
await expect(handler.handle(operation)).resolves.toBe(result); | ||
expect(source.handle).toHaveBeenLastCalledWith(operation); | ||
}); | ||
}); |
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,15 @@ | ||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError'; | ||
import { StaticThrowHandler } from '../../../../src/util/handlers/StaticThrowHandler'; | ||
|
||
describe('A StaticThrowHandler', (): void => { | ||
const error = new BadRequestHttpError(); | ||
const handler = new StaticThrowHandler(error); | ||
|
||
it('can handle all requests.', async(): Promise<void> => { | ||
await expect(handler.canHandle({})).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('always throws the given error.', async(): Promise<void> => { | ||
await expect(handler.handle()).rejects.toThrow(error); | ||
}); | ||
}); |