feat: Create a RoutingResourceStore that takes routing rules

This commit is contained in:
Joachim Van Herwegen
2020-10-26 09:17:03 +01:00
parent 86de805daa
commit 5287cd1e41
13 changed files with 437 additions and 101 deletions

View File

@@ -0,0 +1,77 @@
import type { ResourceStore } from '../../../src/storage/ResourceStore';
import { RoutingResourceStore } from '../../../src/storage/RoutingResourceStore';
import { NotFoundHttpError } from '../../../src/util/errors/NotFoundHttpError';
import { UnsupportedHttpError } from '../../../src/util/errors/UnsupportedHttpError';
import { StaticAsyncHandler } from '../../util/StaticAsyncHandler';
describe('A RoutingResourceStore', (): void => {
let store: RoutingResourceStore;
let source: ResourceStore;
let rule: StaticAsyncHandler<ResourceStore>;
const identifier = { path: 'identifier' };
beforeEach(async(): Promise<void> => {
source = {
getRepresentation: jest.fn(),
addResource: jest.fn(),
setRepresentation: jest.fn(),
modifyResource: jest.fn(),
deleteResource: jest.fn(),
};
rule = new StaticAsyncHandler(true, source);
store = new RoutingResourceStore(rule);
});
it('calls getRepresentation on the resulting store.', async(): Promise<void> => {
await expect(store.getRepresentation(identifier, 'preferences' as any, 'conditions' as any))
.resolves.toBeUndefined();
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(identifier, 'preferences', 'conditions');
});
it('calls addRepresentation on the resulting store.', async(): Promise<void> => {
await expect(store.addResource(identifier, 'representation' as any, 'conditions' as any))
.resolves.toBeUndefined();
expect(source.addResource).toHaveBeenCalledTimes(1);
expect(source.addResource).toHaveBeenLastCalledWith(identifier, 'representation', 'conditions');
});
it('calls setRepresentation on the resulting store.', async(): Promise<void> => {
await expect(store.setRepresentation(identifier, 'representation' as any, 'conditions' as any))
.resolves.toBeUndefined();
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
expect(source.setRepresentation).toHaveBeenLastCalledWith(identifier, 'representation', 'conditions');
});
it('calls modifyResource on the resulting store.', async(): Promise<void> => {
await expect(store.modifyResource(identifier, 'patch' as any, 'conditions' as any))
.resolves.toBeUndefined();
expect(source.modifyResource).toHaveBeenCalledTimes(1);
expect(source.modifyResource).toHaveBeenLastCalledWith(identifier, 'patch', 'conditions');
});
it('calls deleteResource on the resulting store.', async(): Promise<void> => {
await expect(store.deleteResource(identifier, 'conditions' as any))
.resolves.toBeUndefined();
expect(source.deleteResource).toHaveBeenCalledTimes(1);
expect(source.deleteResource).toHaveBeenLastCalledWith(identifier, 'conditions');
});
it('throws a 404 if there is no body and no store was found.', async(): Promise<void> => {
rule.canHandle = (): any => {
throw new UnsupportedHttpError();
};
await expect(store.getRepresentation(identifier, 'preferences' as any, 'conditions' as any))
.rejects.toThrow(NotFoundHttpError);
});
it('re-throws the error if something went wrong.', async(): Promise<void> => {
rule.canHandle = (): any => {
throw new Error('error');
};
await expect(store.getRepresentation(identifier, 'preferences' as any, 'conditions' as any))
.rejects.toThrow(new Error('error'));
});
});

View File

@@ -0,0 +1,81 @@
import type { Readable } from 'stream';
import type { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
import { ConvertingRouterRule } from '../../../../src/storage/routing/ConvertingRouterRule';
import type { PreferenceSupport } from '../../../../src/storage/routing/PreferenceSupport';
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
describe('A ConvertingRouterRule', (): void => {
let store1: ResourceStore;
let store2: ResourceStore;
let defaultStore: ResourceStore;
let checker1: PreferenceSupport;
let checker2: PreferenceSupport;
let rule: ConvertingRouterRule;
let representation: Representation;
let metadata: RepresentationMetadata;
beforeEach(async(): Promise<void> => {
store1 = { name: 'turtleStore' } as any;
store2 = { name: 'textStore' } as any;
defaultStore = { name: 'defaultStore' } as any;
checker1 = {
async supports(input: { representation: Representation }): Promise<boolean> {
return input.representation.metadata.contentType === 'text/turtle';
},
} as any;
checker2 = {
async supports(input: { representation: Representation }): Promise<boolean> {
return input.representation.metadata.contentType === 'application/ld+json';
},
} as any;
rule = new ConvertingRouterRule([{ store: store1, supportChecker: checker1 },
{ store: store2, supportChecker: checker2 }], defaultStore);
metadata = new RepresentationMetadata();
representation = { binary: true, data: 'data!' as any, metadata };
});
it('returns the corresponding store if it supports the input.', async(): Promise<void> => {
metadata.contentType = 'text/turtle';
await expect(rule.handle({ identifier: { path: 'identifier' }, representation })).resolves.toBe(store1);
metadata.contentType = 'application/ld+json';
await expect(rule.handle({ identifier: { path: 'identifier' }, representation })).resolves.toBe(store2);
});
it('returns the defaultStore if the converter does not support the input.', async(): Promise<void> => {
await expect(rule.handle({ identifier: { path: 'identifier' }, representation })).resolves.toBe(defaultStore);
});
it('checks if the stores contain the identifier if there is no data.', async(): Promise<void> => {
const data: Readable = { destroy: jest.fn() } as any;
store1.getRepresentation = (): any => {
throw new NotFoundHttpError();
};
store2.getRepresentation = async(): Promise<Representation> => ({ data } as any);
await expect(rule.handle({ identifier: { path: 'identifier' }})).resolves.toBe(store2);
expect(data.destroy).toHaveBeenCalledTimes(1);
});
it('returns the defaultStore if no other store has the resource.', async(): Promise<void> => {
store1.getRepresentation = (): any => {
throw new NotFoundHttpError();
};
store2.getRepresentation = (): any => {
throw new NotFoundHttpError();
};
await expect(rule.handle({ identifier: { path: 'identifier' }})).resolves.toBe(defaultStore);
});
it('throws the error if a store had a non-404 error.', async(): Promise<void> => {
store1.getRepresentation = (): any => {
throw new InternalServerError();
};
await expect(rule.handle({ identifier: { path: 'identifier' }})).rejects.toThrow(InternalServerError);
});
});

View File

@@ -0,0 +1,35 @@
import type { Representation } from '../../../../src/ldp/representation/Representation';
import type { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import type { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import type { RepresentationConverter } from '../../../../src/storage/conversion/RepresentationConverter';
import { PreferenceSupport } from '../../../../src/storage/routing/PreferenceSupport';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('A PreferenceSupport', (): void => {
let preferences: RepresentationPreferences;
let converter: RepresentationConverter;
let support: PreferenceSupport;
const identifier: ResourceIdentifier = 'identifier' as any;
const representation: Representation = 'representation' as any;
beforeEach(async(): Promise<void> => {
preferences = { type: []};
converter = { canHandle: jest.fn() } as any;
support = new PreferenceSupport(preferences, converter);
});
it('returns true if the converter supports the input.', async(): Promise<void> => {
await expect(support.supports({ identifier, representation })).resolves.toBe(true);
expect(converter.canHandle).toHaveBeenCalledTimes(1);
expect(converter.canHandle).toHaveBeenLastCalledWith({ identifier, representation, preferences });
});
it('returns false if the converter does not support the input.', async(): Promise<void> => {
converter.canHandle = jest.fn((): any => {
throw new UnsupportedHttpError();
});
await expect(support.supports({ identifier, representation })).resolves.toBe(false);
expect(converter.canHandle).toHaveBeenCalledTimes(1);
expect(converter.canHandle).toHaveBeenLastCalledWith({ identifier, representation, preferences });
});
});

View File

@@ -0,0 +1,32 @@
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
import { RegexRouterRule } from '../../../../src/storage/routing/RegexRouterRule';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('A RegexRouterRule', (): void => {
const base = 'http://test.com/';
const store: ResourceStore = 'resourceStore' as any;
it('rejects identifiers not containing the base.', async(): Promise<void> => {
const router = new RegexRouterRule(base, {});
await expect(router.canHandle({ identifier: { path: 'http://notTest.com/apple' }}))
.rejects.toThrow(new UnsupportedHttpError(`Identifiers need to start with http://test.com`));
});
it('rejects identifiers not matching any regex.', async(): Promise<void> => {
const router = new RegexRouterRule(base, { pear: store });
await expect(router.canHandle({ identifier: { path: `${base}apple/` }}))
.rejects.toThrow(new UnsupportedHttpError(`No stored regexes match http://test.com/apple/`));
});
it('accepts identifiers matching any regex.', async(): Promise<void> => {
const router = new RegexRouterRule(base, { '^/apple': store });
await expect(router.canHandle({ identifier: { path: `${base}apple/` }}))
.resolves.toBeUndefined();
});
it('returns the corresponding store.', async(): Promise<void> => {
const store2: ResourceStore = 'resourceStore2' as any;
const router = new RegexRouterRule(base, { '^/apple': store2, '/pear/': store });
await expect(router.handle({ identifier: { path: `${base}apple/` }})).resolves.toBe(store2);
});
});