feat: Create SubdomainIdentifierStrategy

This strategy interprets all subdomains of the base to also be root containers.
This commit is contained in:
Joachim Van Herwegen
2021-02-12 11:52:18 +01:00
parent bdb3621ee3
commit 29df380396
7 changed files with 136 additions and 39 deletions

View File

@@ -0,0 +1,31 @@
import type { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { BaseIdentifierStrategy } from '../../../../src/util/identifiers/BaseIdentifierStrategy';
class DummyStrategy extends BaseIdentifierStrategy {
public supportsIdentifier(identifier: ResourceIdentifier): boolean {
return !identifier.path.endsWith('unsupported');
}
public isRootContainer(identifier: ResourceIdentifier): boolean {
return identifier.path.endsWith('root');
}
}
describe('A BaseIdentifierStrategy', (): void => {
const strategy = new DummyStrategy();
it('returns the parent identifier.', async(): Promise<void> => {
expect(strategy.getParentContainer({ path: 'http://test.com/foo/bar' })).toEqual({ path: 'http://test.com/foo/' });
expect(strategy.getParentContainer({ path: 'http://test.com/foo/bar/' })).toEqual({ path: 'http://test.com/foo/' });
});
it('errors when attempting to get the parent of an unsupported identifier.', async(): Promise<void> => {
expect((): any => strategy.getParentContainer({ path: '/unsupported' }))
.toThrow('/unsupported is not supported');
});
it('errors when attempting to get the parent of a root container.', async(): Promise<void> => {
expect((): any => strategy.getParentContainer({ path: 'http://test.com/root' }))
.toThrow('http://test.com/root is a root container and has no parent');
});
});

View File

@@ -2,32 +2,17 @@ import { SingleRootIdentifierStrategy } from '../../../../src/util/identifiers/S
describe('A SingleRootIdentifierStrategy', (): void => {
const baseUrl = 'http://test.com/';
const manager = new SingleRootIdentifierStrategy(baseUrl);
const strategy = new SingleRootIdentifierStrategy(baseUrl);
it('verifies if identifiers are in its domain.', async(): Promise<void> => {
expect(manager.supportsIdentifier({ path: 'http://notest.com/' })).toBe(false);
expect(manager.supportsIdentifier({ path: baseUrl })).toBe(true);
expect(manager.supportsIdentifier({ path: `${baseUrl}foo/bar` })).toBe(true);
});
it('returns the parent identifier.', async(): Promise<void> => {
expect(manager.getParentContainer({ path: 'http://test.com/foo/bar' })).toEqual({ path: 'http://test.com/foo/' });
expect(manager.getParentContainer({ path: 'http://test.com/foo/bar/' })).toEqual({ path: 'http://test.com/foo/' });
});
it('errors when attempting to get the parent of an unsupported identifier.', async(): Promise<void> => {
expect((): any => manager.getParentContainer({ path: 'http://nottest.com/' }))
.toThrow('http://nottest.com/ is not supported');
});
it('errors when attempting to get the parent of a root container.', async(): Promise<void> => {
expect((): any => manager.getParentContainer({ path: 'http://test.com/' }))
.toThrow('http://test.com/ is a root container and has no parent');
expect(strategy.supportsIdentifier({ path: 'http://notest.com/' })).toBe(false);
expect(strategy.supportsIdentifier({ path: baseUrl })).toBe(true);
expect(strategy.supportsIdentifier({ path: `${baseUrl}foo/bar` })).toBe(true);
});
it('checks for the root container by comparing with the base URL.', async(): Promise<void> => {
expect(manager.isRootContainer({ path: 'http://notest.com/' })).toBe(false);
expect(manager.isRootContainer({ path: baseUrl })).toBe(true);
expect(manager.isRootContainer({ path: `${baseUrl}foo/bar` })).toBe(false);
expect(strategy.isRootContainer({ path: 'http://notest.com/' })).toBe(false);
expect(strategy.isRootContainer({ path: baseUrl })).toBe(true);
expect(strategy.isRootContainer({ path: `${baseUrl}foo/bar` })).toBe(false);
});
});

View File

@@ -0,0 +1,32 @@
import { SubdomainIdentifierStrategy } from '../../../../src/util/identifiers/SubdomainIdentifierStrategy';
describe('A SubdomainIdentifierStrategy', (): void => {
const baseUrl = 'http://test.com/foo/';
const strategy = new SubdomainIdentifierStrategy(baseUrl);
it('supports URLs in its domain.', async(): Promise<void> => {
expect(strategy.supportsIdentifier({ path: 'http://test.com/foo/' })).toBe(true);
expect(strategy.supportsIdentifier({ path: 'http://alice.test.com/foo/' })).toBe(true);
expect(strategy.supportsIdentifier({ path: 'http://a.b.c.test.com/foo/' })).toBe(true);
expect(strategy.supportsIdentifier({ path: 'http://test.com/foo/bar' })).toBe(true);
expect(strategy.supportsIdentifier({ path: 'http://alice.test.com/foo/bar' })).toBe(true);
expect(strategy.supportsIdentifier({ path: 'http://a.b.c.test.com/foo/bar' })).toBe(true);
});
it('does not support URLs outside of its domain.', async(): Promise<void> => {
expect(strategy.supportsIdentifier({ path: 'http://fake.com/http://test.com/foo/' })).toBe(false);
expect(strategy.supportsIdentifier({ path: 'http://fake.com/test.com/foo/' })).toBe(false);
expect(strategy.supportsIdentifier({ path: 'http://faketest.com/foo/' })).toBe(false);
expect(strategy.supportsIdentifier({ path: 'http://test.com/foo' })).toBe(false);
expect(strategy.supportsIdentifier({ path: 'ftp://test.com/foo/' })).toBe(false);
});
it('identifiers the base and all subdomains as root containers.', async(): Promise<void> => {
expect(strategy.isRootContainer({ path: 'http://test.com/foo/' })).toBe(true);
expect(strategy.isRootContainer({ path: 'http://alice.test.com/foo/' })).toBe(true);
expect(strategy.isRootContainer({ path: 'http://test.com/foo/bar' })).toBe(false);
expect(strategy.isRootContainer({ path: 'http://alice.test.com/foo/bar' })).toBe(false);
});
});