import type { Authorization } from '../../../../src/authorization/Authorization'; import { GetOperationHandler } from '../../../../src/ldp/operations/GetOperationHandler'; import type { Operation } from '../../../../src/ldp/operations/Operation'; import type { Representation } from '../../../../src/ldp/representation/Representation'; import { BasicConditions } from '../../../../src/storage/BasicConditions'; import type { ResourceStore } from '../../../../src/storage/ResourceStore'; import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError'; describe('A GetOperationHandler', (): void => { const conditions = new BasicConditions({}); const preferences = {}; let store: ResourceStore; let handler: GetOperationHandler; beforeEach(async(): Promise => { store = { getRepresentation: jest.fn(async(): Promise => ({ binary: false, data: 'data', metadata: 'metadata' } as any)), } as unknown as ResourceStore; handler = new GetOperationHandler(store); }); it('only supports GET operations.', async(): Promise => { await expect(handler.canHandle({ method: 'GET' } as Operation)).resolves.toBeUndefined(); await expect(handler.canHandle({ method: 'POST' } as Operation)).rejects.toThrow(NotImplementedHttpError); }); it('returns the representation from the store with the correct response.', async(): Promise => { const result = await handler.handle({ target: { path: 'url' }, preferences, conditions } as Operation); expect(result.statusCode).toBe(200); expect(result.metadata).toBe('metadata'); expect(result.data).toBe('data'); expect(store.getRepresentation).toHaveBeenCalledTimes(1); expect(store.getRepresentation).toHaveBeenLastCalledWith({ path: 'url' }, preferences, conditions); }); it('adds authorization metadata in case the operation is an AuthorizedOperation.', async(): Promise => { const authorization: Authorization = { addMetadata: jest.fn() }; const result = await handler.handle( { target: { path: 'url' }, preferences, conditions, authorization } as Operation, ); expect(result.statusCode).toBe(200); expect(store.getRepresentation).toHaveBeenCalledTimes(1); expect(store.getRepresentation).toHaveBeenLastCalledWith({ path: 'url' }, preferences, conditions); expect(authorization.addMetadata).toHaveBeenCalledTimes(1); expect(authorization.addMetadata).toHaveBeenLastCalledWith('metadata'); }); });