2020-11-13 15:32:48 +01:00

76 lines
3.7 KiB
TypeScript

import type { Representation } from '../../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
import type { RepresentationPreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
import type { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
import { checkRequest, matchingTypes } from '../../../../src/storage/conversion/ConversionUtil';
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
describe('A ConversionUtil', (): void => {
const identifier: ResourceIdentifier = { path: 'path' };
let representation: Representation;
let metadata: RepresentationMetadata;
beforeEach(async(): Promise<void> => {
metadata = new RepresentationMetadata();
representation = { metadata } as Representation;
});
describe('#checkRequest', (): void => {
it('requires an input type.', async(): Promise<void> => {
const preferences: RepresentationPreferences = {};
expect((): any => checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ '*/*' ]))
.toThrow('Input type required for conversion.');
});
it('requires a matching input type.', async(): Promise<void> => {
metadata.contentType = 'a/x';
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect((): any => checkRequest({ identifier, representation, preferences }, [ 'c/x' ], [ '*/*' ]))
.toThrow('Can only convert from c/x to */*.');
});
it('requires a matching output type.', async(): Promise<void> => {
metadata.contentType = 'a/x';
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect((): any => checkRequest({ identifier, representation, preferences }, [ '*/*' ], [ 'c/x' ]))
.toThrow('Can only convert from */* to c/x.');
});
it('succeeds with a valid input and output type.', async(): Promise<void> => {
metadata.contentType = 'a/x';
const preferences: RepresentationPreferences = { type: [{ value: 'b/x', weight: 1 }]};
expect(checkRequest({ identifier, representation, preferences }, [ 'a/x' ], [ 'b/x' ]))
.toBeUndefined();
});
});
describe('#matchingTypes', (): void => {
it('requires type preferences.', async(): Promise<void> => {
const preferences: RepresentationPreferences = {};
expect((): any => matchingTypes(preferences, [ '*/*' ]))
.toThrow('Output type required for conversion.');
});
it('returns matching types if weight > 0.', async(): Promise<void> => {
const preferences: RepresentationPreferences = { type:
[{ value: 'a/x', weight: 1 }, { value: 'b/x', weight: 0.5 }, { value: 'c/x', weight: 0 }]};
expect(matchingTypes(preferences, [ 'b/x', 'c/x' ])).toEqual([{ value: 'b/x', weight: 0.5 }]);
});
it('errors if there are duplicate preferences.', async(): Promise<void> => {
const preferences: RepresentationPreferences =
{ type: [{ value: 'b/x', weight: 1 }, { value: 'b/x', weight: 0 }]};
expect((): any => matchingTypes(preferences, [ 'b/x' ]))
.toThrow(new UnsupportedHttpError(`Duplicate type preference found: b/x`));
});
it('errors if there invalid types.', async(): Promise<void> => {
const preferences: RepresentationPreferences =
{ type: [{ value: 'b/x', weight: 1 }]};
expect((): any => matchingTypes(preferences, [ 'noType' ]))
.toThrow(new InternalServerError(`Unexpected type preference: noType`));
});
});
});