mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add simple operation handlers
This commit is contained in:
parent
12fd00e3b8
commit
fe8749390c
25
src/ldp/operations/SimpleDeleteOperationHandler.ts
Normal file
25
src/ldp/operations/SimpleDeleteOperationHandler.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { Operation } from './Operation';
|
||||||
|
import { OperationHandler } from './OperationHandler';
|
||||||
|
import { ResourceStore } from '../../storage/ResourceStore';
|
||||||
|
import { ResponseDescription } from './ResponseDescription';
|
||||||
|
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||||
|
|
||||||
|
export class SimpleDeleteOperationHandler extends OperationHandler {
|
||||||
|
private readonly store: ResourceStore;
|
||||||
|
|
||||||
|
public constructor(store: ResourceStore) {
|
||||||
|
super();
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async canHandle(input: Operation): Promise<void> {
|
||||||
|
if (input.method !== 'DELETE') {
|
||||||
|
throw new UnsupportedHttpError('This handler only supports DELETE operations.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handle(input: Operation): Promise<ResponseDescription> {
|
||||||
|
await this.store.deleteResource(input.target);
|
||||||
|
return { identifier: input.target };
|
||||||
|
}
|
||||||
|
}
|
25
src/ldp/operations/SimpleGetOperationHandler.ts
Normal file
25
src/ldp/operations/SimpleGetOperationHandler.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import { Operation } from './Operation';
|
||||||
|
import { OperationHandler } from './OperationHandler';
|
||||||
|
import { ResourceStore } from '../../storage/ResourceStore';
|
||||||
|
import { ResponseDescription } from './ResponseDescription';
|
||||||
|
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||||
|
|
||||||
|
export class SimpleGetOperationHandler extends OperationHandler {
|
||||||
|
private readonly store: ResourceStore;
|
||||||
|
|
||||||
|
public constructor(store: ResourceStore) {
|
||||||
|
super();
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async canHandle(input: Operation): Promise<void> {
|
||||||
|
if (input.method !== 'GET') {
|
||||||
|
throw new UnsupportedHttpError('This handler only supports GET operations.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handle(input: Operation): Promise<ResponseDescription> {
|
||||||
|
const body = await this.store.getRepresentation(input.target, input.preferences);
|
||||||
|
return { identifier: input.target, body };
|
||||||
|
}
|
||||||
|
}
|
28
src/ldp/operations/SimplePostOperationHandler.ts
Normal file
28
src/ldp/operations/SimplePostOperationHandler.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { Operation } from './Operation';
|
||||||
|
import { OperationHandler } from './OperationHandler';
|
||||||
|
import { ResourceStore } from '../../storage/ResourceStore';
|
||||||
|
import { ResponseDescription } from './ResponseDescription';
|
||||||
|
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
|
||||||
|
|
||||||
|
export class SimplePostOperationHandler extends OperationHandler {
|
||||||
|
private readonly store: ResourceStore;
|
||||||
|
|
||||||
|
public constructor(store: ResourceStore) {
|
||||||
|
super();
|
||||||
|
this.store = store;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async canHandle(input: Operation): Promise<void> {
|
||||||
|
if (input.method !== 'POST') {
|
||||||
|
throw new UnsupportedHttpError('This handler only supports POST operations.');
|
||||||
|
}
|
||||||
|
if (!input.body) {
|
||||||
|
throw new UnsupportedHttpError('POST operations require a body.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async handle(input: Operation): Promise<ResponseDescription> {
|
||||||
|
const identifier = await this.store.addResource(input.target, input.body);
|
||||||
|
return { identifier };
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,24 @@
|
|||||||
|
import { Operation } from '../../../../src/ldp/operations/Operation';
|
||||||
|
import { ResourceStore } from '../../../../src/storage/ResourceStore';
|
||||||
|
import { SimpleDeleteOperationHandler } from '../../../../src/ldp/operations/SimpleDeleteOperationHandler';
|
||||||
|
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
|
||||||
|
|
||||||
|
describe('A SimpleDeleteOperationHandler', (): void => {
|
||||||
|
const store = {} as unknown as ResourceStore;
|
||||||
|
const handler = new SimpleDeleteOperationHandler(store);
|
||||||
|
beforeEach(async(): Promise<void> => {
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
|
store.deleteResource = jest.fn(async(): Promise<void> => {});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('only supports GET operations.', async(): Promise<void> => {
|
||||||
|
await expect(handler.canHandle({ method: 'DELETE' } 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' }} as Operation)).resolves.toEqual({ identifier: { path: 'url' }});
|
||||||
|
expect(store.deleteResource).toHaveBeenCalledTimes(1);
|
||||||
|
expect(store.deleteResource).toHaveBeenLastCalledWith({ path: 'url' });
|
||||||
|
});
|
||||||
|
});
|
23
test/unit/ldp/operations/SimpleGetOperationHandler.test.ts
Normal file
23
test/unit/ldp/operations/SimpleGetOperationHandler.test.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { Operation } from '../../../../src/ldp/operations/Operation';
|
||||||
|
import { Representation } from '../../../../src/ldp/representation/Representation';
|
||||||
|
import { ResourceStore } from '../../../../src/storage/ResourceStore';
|
||||||
|
import { SimpleGetOperationHandler } from '../../../../src/ldp/operations/SimpleGetOperationHandler';
|
||||||
|
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
|
||||||
|
|
||||||
|
describe('A SimpleGetOperationHandler', (): void => {
|
||||||
|
const store = {
|
||||||
|
getRepresentation: async(): Promise<Representation> => ({ dataType: 'quad' } as Representation),
|
||||||
|
} as unknown as ResourceStore;
|
||||||
|
const handler = new SimpleGetOperationHandler(store);
|
||||||
|
|
||||||
|
it('only supports GET operations.', async(): Promise<void> => {
|
||||||
|
await expect(handler.canHandle({ method: 'GET' } as Operation)).resolves.toBeUndefined();
|
||||||
|
await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the representation from the store with the input identifier.', async(): Promise<void> => {
|
||||||
|
await expect(handler.handle({ target: { path: 'url' }} as Operation)).resolves.toEqual(
|
||||||
|
{ identifier: { path: 'url' }, body: { dataType: 'quad' }},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
22
test/unit/ldp/operations/SimplePostOperationHandler.test.ts
Normal file
22
test/unit/ldp/operations/SimplePostOperationHandler.test.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { Operation } from '../../../../src/ldp/operations/Operation';
|
||||||
|
import { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
|
||||||
|
import { ResourceStore } from '../../../../src/storage/ResourceStore';
|
||||||
|
import { SimplePostOperationHandler } from '../../../../src/ldp/operations/SimplePostOperationHandler';
|
||||||
|
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
|
||||||
|
|
||||||
|
describe('A SimplePostOperationHandler', (): void => {
|
||||||
|
const store = {
|
||||||
|
addResource: async(): Promise<ResourceIdentifier> => ({ path: 'newPath' } as ResourceIdentifier),
|
||||||
|
} as unknown as ResourceStore;
|
||||||
|
const handler = new SimplePostOperationHandler(store);
|
||||||
|
|
||||||
|
it('only supports POST operations with a body.', async(): Promise<void> => {
|
||||||
|
await expect(handler.canHandle({ method: 'POST', body: { dataType: 'test' }} as Operation)).resolves.toBeUndefined();
|
||||||
|
await expect(handler.canHandle({ method: 'GET', body: { dataType: 'test' }} as Operation)).rejects.toThrow(UnsupportedHttpError);
|
||||||
|
await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(UnsupportedHttpError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('adds the given representation to the store and returns the new identifier.', async(): Promise<void> => {
|
||||||
|
await expect(handler.handle({ method: 'POST', body: { dataType: 'test' }} as Operation)).resolves.toEqual({ identifier: { path: 'newPath' }});
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user