-
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: Add OperationHandler for PATCH
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Operation } from './Operation'; | ||
import { OperationHandler } from './OperationHandler'; | ||
import { Patch } from '../http/Patch'; | ||
import { ResourceStore } from '../../storage/ResourceStore'; | ||
import { ResponseDescription } from './ResponseDescription'; | ||
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError'; | ||
|
||
export class SimplePatchOperationHandler extends OperationHandler { | ||
private readonly store: ResourceStore; | ||
|
||
public constructor(store: ResourceStore) { | ||
super(); | ||
this.store = store; | ||
} | ||
|
||
public async canHandle(input: Operation): Promise<void> { | ||
if (input.method !== 'PATCH') { | ||
throw new UnsupportedHttpError('This handler only supports PATCH operations.'); | ||
} | ||
} | ||
|
||
public async handle(input: Operation): Promise<ResponseDescription> { | ||
await this.store.modifyResource(input.target, input.body as Patch); | ||
return { identifier: input.target }; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
test/unit/ldp/operations/SimplePatchOperationHandler.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Operation } from '../../../../src/ldp/operations/Operation'; | ||
import { ResourceStore } from '../../../../src/storage/ResourceStore'; | ||
import { SimplePatchOperationHandler } from '../../../../src/ldp/operations/SimplePatchOperationHandler'; | ||
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; | ||
|
||
describe('A SimplePatchOperationHandler', (): void => { | ||
const store = {} as unknown as ResourceStore; | ||
const handler = new SimplePatchOperationHandler(store); | ||
beforeEach(async(): Promise<void> => { | ||
store.modifyResource = jest.fn(async(): Promise<void> => undefined); | ||
}); | ||
|
||
it('only supports GET operations.', async(): Promise<void> => { | ||
await expect(handler.canHandle({ method: 'PATCH' } as Operation)).resolves.toBeUndefined(); | ||
await expect(handler.canHandle({ method: 'GET' } as Operation)).rejects.toThrow(UnsupportedHttpError); | ||
}); | ||
|
||
it('deletes the resource from the store and returns its identifier.', async(): Promise<void> => { | ||
await expect(handler.handle({ target: { path: 'url' }, body: { dataType: 'patch' }} as Operation)) | ||
.resolves.toEqual({ identifier: { path: 'url' }}); | ||
expect(store.modifyResource).toHaveBeenCalledTimes(1); | ||
expect(store.modifyResource).toHaveBeenLastCalledWith({ path: 'url' }, { dataType: 'patch' }); | ||
}); | ||
}); |