mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
49 lines
2.5 KiB
TypeScript
49 lines
2.5 KiB
TypeScript
import type { Operation } from '../../../../src/ldp/operations/Operation';
|
|
import { PostOperationHandler } from '../../../../src/ldp/operations/PostOperationHandler';
|
|
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
|
|
import type { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
|
|
import { BasicConditions } from '../../../../src/storage/BasicConditions';
|
|
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
|
|
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
|
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
|
import { SOLID_HTTP } from '../../../../src/util/Vocabularies';
|
|
|
|
describe('A PostOperationHandler', (): void => {
|
|
const conditions = new BasicConditions({});
|
|
let store: ResourceStore;
|
|
let handler: PostOperationHandler;
|
|
|
|
beforeEach(async(): Promise<void> => {
|
|
store = {
|
|
addResource: jest.fn(async(): Promise<ResourceIdentifier> => ({ path: 'newPath' } as ResourceIdentifier)),
|
|
} as unknown as ResourceStore;
|
|
handler = new PostOperationHandler(store);
|
|
});
|
|
|
|
it('only supports POST operations.', async(): Promise<void> => {
|
|
await expect(handler.canHandle({ method: 'POST', body: { }} as Operation))
|
|
.resolves.toBeUndefined();
|
|
await expect(handler.canHandle({ method: 'GET', body: { }} as Operation))
|
|
.rejects.toThrow(NotImplementedHttpError);
|
|
});
|
|
|
|
it('errors if there is no body or content-type.', async(): Promise<void> => {
|
|
await expect(handler.handle({ } as Operation)).rejects.toThrow(BadRequestHttpError);
|
|
await expect(handler.handle({ body: { metadata: new RepresentationMetadata() }} as Operation))
|
|
.rejects.toThrow(BadRequestHttpError);
|
|
});
|
|
|
|
it('adds the given representation to the store and returns the correct response.', async(): Promise<void> => {
|
|
const metadata = new RepresentationMetadata('text/turtle');
|
|
const result = await handler.handle(
|
|
{ method: 'POST', target: { path: 'url' }, body: { metadata }, conditions } as Operation,
|
|
);
|
|
expect(result.statusCode).toBe(201);
|
|
expect(result.metadata).toBeInstanceOf(RepresentationMetadata);
|
|
expect(result.metadata?.get(SOLID_HTTP.location)?.value).toBe('newPath');
|
|
expect(result.data).toBeUndefined();
|
|
expect(store.addResource).toHaveBeenCalledTimes(1);
|
|
expect(store.addResource).toHaveBeenLastCalledWith({ path: 'url' }, { metadata }, conditions);
|
|
});
|
|
});
|