mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Reject unacceptable content types
This commit is contained in:
@@ -3,6 +3,7 @@ import { RepresentationMetadata } from '../../../src/ldp/representation/Represen
|
||||
import type { RepresentationConverter } from '../../../src/storage/conversion/RepresentationConverter';
|
||||
import { RepresentationConvertingStore } from '../../../src/storage/RepresentationConvertingStore';
|
||||
import type { ResourceStore } from '../../../src/storage/ResourceStore';
|
||||
import { InternalServerError } from '../../../src/util/errors/InternalServerError';
|
||||
import { CONTENT_TYPE } from '../../../src/util/UriConstants';
|
||||
|
||||
describe('A RepresentationConvertingStore', (): void => {
|
||||
@@ -30,7 +31,7 @@ describe('A RepresentationConvertingStore', (): void => {
|
||||
|
||||
it('returns the Representation from the source if no changes are required.', async(): Promise<void> => {
|
||||
const result = await store.getRepresentation({ path: 'path' }, { type: [
|
||||
{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 },
|
||||
{ value: 'application/*', weight: 0 }, { value: 'text/turtle', weight: 1 },
|
||||
]});
|
||||
expect(result).toEqual({
|
||||
data: 'data',
|
||||
@@ -39,7 +40,9 @@ describe('A RepresentationConvertingStore', (): void => {
|
||||
expect(result.metadata.contentType).toEqual('text/turtle');
|
||||
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
expect(source.getRepresentation).toHaveBeenLastCalledWith(
|
||||
{ path: 'path' }, { type: [{ value: 'text/*', weight: 0 }, { value: 'text/turtle', weight: 1 }]}, undefined,
|
||||
{ path: 'path' },
|
||||
{ type: [{ value: 'application/*', weight: 0 }, { value: 'text/turtle', weight: 1 }]},
|
||||
undefined,
|
||||
);
|
||||
expect(outConverter.handleSafe).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
@@ -98,4 +101,11 @@ describe('A RepresentationConvertingStore', (): void => {
|
||||
expect(inConverter.handleSafe).toHaveBeenCalledTimes(2);
|
||||
expect(source.setRepresentation).toHaveBeenLastCalledWith(id, 'inConvert', 'conditions');
|
||||
});
|
||||
|
||||
it('throws an error if no content-type is provided.', async(): Promise<void> => {
|
||||
metadata.removeAll(CONTENT_TYPE);
|
||||
const id = { path: 'identifier' };
|
||||
|
||||
await expect(store.addResource(id, representation, 'conditions' as any)).rejects.toThrow(InternalServerError);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,6 +3,8 @@ import { RepresentationMetadata } from '../../../../src/ldp/representation/Repre
|
||||
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' };
|
||||
@@ -38,7 +40,7 @@ describe('A ConversionUtil', (): void => {
|
||||
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 }, [ '*/*' ], [ '*/*' ]))
|
||||
expect(checkRequest({ identifier, representation, preferences }, [ 'a/x' ], [ 'b/x' ]))
|
||||
.toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -55,5 +57,19 @@ describe('A ConversionUtil', (): void => {
|
||||
[{ 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`));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user