mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Only convert when needed.
This commit is contained in:
97
test/unit/storage/conversion/ContentTypeReplacer.test.ts
Normal file
97
test/unit/storage/conversion/ContentTypeReplacer.test.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import 'jest-rdf';
|
||||
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
|
||||
import { ContentTypeReplacer } from '../../../../src/storage/conversion/ContentTypeReplacer';
|
||||
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
||||
|
||||
const binary = true;
|
||||
const data = { data: true };
|
||||
|
||||
describe('A ContentTypeReplacer', (): void => {
|
||||
const converter = new ContentTypeReplacer({
|
||||
'application/n-triples': [
|
||||
'text/turtle',
|
||||
'application/trig',
|
||||
'application/n-quads',
|
||||
],
|
||||
'application/ld+json': 'application/json',
|
||||
'application/json': 'application/octet-stream',
|
||||
'application/octet-stream': 'internal/anything',
|
||||
'internal/anything': 'application/octet-stream',
|
||||
'*/*': 'application/octet-stream',
|
||||
});
|
||||
|
||||
it('throws on an unsupported input type.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({ contentType: 'text/plain' });
|
||||
const representation = { metadata };
|
||||
const preferences = { type: { 'application/json': 1 }};
|
||||
|
||||
await expect(converter.canHandle({ representation, preferences } as any))
|
||||
.rejects.toThrow(new Error('Cannot convert from text/plain to application/json'));
|
||||
});
|
||||
|
||||
it('throws on an unsupported output type.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({ contentType: 'application/n-triples' });
|
||||
const representation = { metadata };
|
||||
const preferences = { type: { 'application/json': 1 }};
|
||||
|
||||
await expect(converter.canHandle({ representation, preferences } as any))
|
||||
.rejects.toThrow(new Error('Cannot convert from application/n-triples to application/json'));
|
||||
});
|
||||
|
||||
it('does not replace when no content type is given.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata();
|
||||
const representation = { binary, data, metadata };
|
||||
const preferences = { type: { 'application/json': 1 }};
|
||||
|
||||
await expect(converter.canHandle({ representation, preferences } as any))
|
||||
.rejects.toThrow(new NotImplementedHttpError('Cannot convert from unknown to application/json'));
|
||||
});
|
||||
|
||||
it('replaces a supported content type when no preferences are given.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({ contentType: 'application/n-triples' });
|
||||
const representation = { binary, data, metadata };
|
||||
const preferences = {};
|
||||
|
||||
const result = await converter.handleSafe({ representation, preferences } as any);
|
||||
expect(result.binary).toBe(binary);
|
||||
expect(result.data).toBe(data);
|
||||
expect(result.metadata.contentType).toBe('text/turtle');
|
||||
});
|
||||
|
||||
it('replaces a supported content type when preferences are given.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({ contentType: 'application/n-triples' });
|
||||
const representation = { binary, data, metadata };
|
||||
const preferences = { type: { 'application/n-quads': 1 }};
|
||||
|
||||
const result = await converter.handleSafe({ representation, preferences } as any);
|
||||
expect(result.binary).toBe(binary);
|
||||
expect(result.data).toBe(data);
|
||||
expect(result.metadata.contentType).toBe('application/n-quads');
|
||||
});
|
||||
|
||||
it('replaces a supported wildcard type.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({ contentType: 'text/plain' });
|
||||
const representation = { binary, data, metadata };
|
||||
const preferences = { type: { 'application/octet-stream': 1 }};
|
||||
|
||||
const result = await converter.handleSafe({ representation, preferences } as any);
|
||||
expect(result.binary).toBe(binary);
|
||||
expect(result.data).toBe(data);
|
||||
expect(result.metadata.contentType).toBe('application/octet-stream');
|
||||
});
|
||||
|
||||
it('picks the most preferred content type.', async(): Promise<void> => {
|
||||
const metadata = new RepresentationMetadata({ contentType: 'application/n-triples' });
|
||||
const representation = { binary, data, metadata };
|
||||
const preferences = { type: {
|
||||
'text/turtle': 0.5,
|
||||
'application/trig': 0.6,
|
||||
'application/n-quads': 0.4,
|
||||
}};
|
||||
|
||||
const result = await converter.handleSafe({ representation, preferences } as any);
|
||||
expect(result.binary).toBe(binary);
|
||||
expect(result.data).toBe(data);
|
||||
expect(result.metadata.contentType).toBe('application/trig');
|
||||
});
|
||||
});
|
||||
@@ -13,7 +13,7 @@ import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
import { CONTENT_TYPE } from '../../../../src/util/Vocabularies';
|
||||
|
||||
describe('A RdfToQuadConverter.test.ts', (): void => {
|
||||
describe('A RdfToQuadConverter', (): void => {
|
||||
const converter = new RdfToQuadConverter();
|
||||
const identifier: ResourceIdentifier = { path: 'path' };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user