mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Store content type parameters
* feat: support storage and retrievel of content-type parameters * test: extra unit tests for parseContentTypeWithParameters * refactor: simplify set contentType() Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com> * refactor: simplify for loop because of unique blankNodes Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com> * refactor: ContentTypeParameter should be contentTypeParameter Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com> * refactor: remove undefined type in favor of var? syntax Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com> * refactor: use new parseContentType internally * chore: remove commented code * docs: code documentation line changed Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com> * refactor: Check for faulty metadata in contentType rdf structure Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com> * refactor: remove all instances of blanknodes Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com> * refactor: use full contentType when parsing header Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com> * refactor: use quads() method instead of store.getQuads() * refactor: .value needed for type correctness * feat: ReprMetadata constructor now supports full content-type string Co-authored-by: Joachim Van Herwegen <joachimvh@gmail.com>
This commit is contained in:
@@ -25,9 +25,9 @@ describe('A SparqlUpdateBodyParser', (): void => {
|
||||
input.metadata.contentType = 'text/plain';
|
||||
await expect(bodyParser.canHandle(input)).rejects.toThrow(UnsupportedMediaTypeHttpError);
|
||||
input.metadata.contentType = 'application/sparql-update;charset=utf-8';
|
||||
await expect(bodyParser.canHandle(input)).rejects.toThrow(UnsupportedMediaTypeHttpError);
|
||||
await expect(bodyParser.canHandle(input)).resolves.toBeUndefined();
|
||||
input.metadata.contentType = 'application/sparql-update ; foo=bar';
|
||||
await expect(bodyParser.canHandle(input)).rejects.toThrow(UnsupportedMediaTypeHttpError);
|
||||
await expect(bodyParser.canHandle(input)).resolves.toBeUndefined();
|
||||
input.metadata.contentType = 'application/sparql-update';
|
||||
await expect(bodyParser.canHandle(input)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
@@ -20,7 +20,13 @@ describe('A ContentTypeParser', (): void => {
|
||||
it('sets the given content-type as metadata.', async(): Promise<void> => {
|
||||
request.headers['content-type'] = 'text/plain;charset=UTF-8';
|
||||
await expect(parser.handle({ request, metadata })).resolves.toBeUndefined();
|
||||
expect(metadata.quads()).toHaveLength(1);
|
||||
expect(metadata.quads()).toHaveLength(4);
|
||||
expect(metadata.contentType).toBe('text/plain');
|
||||
expect(metadata.contentTypeObject).toEqual({
|
||||
value: 'text/plain',
|
||||
parameters: {
|
||||
charset: 'UTF-8',
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import 'jest-rdf';
|
||||
import type { BlankNode } from 'n3';
|
||||
import { DataFactory } from 'n3';
|
||||
import type { NamedNode, Quad } from 'rdf-js';
|
||||
import { RepresentationMetadata } from '../../../../src/http/representation/RepresentationMetadata';
|
||||
import { CONTENT_TYPE } from '../../../../src/util/Vocabularies';
|
||||
import { CONTENT_TYPE, SOLID_META, RDFS } from '../../../../src/util/Vocabularies';
|
||||
const { defaultGraph, literal, namedNode, quad } = DataFactory;
|
||||
|
||||
// Helper functions to filter quads
|
||||
@@ -296,5 +297,74 @@ describe('A RepresentationMetadata', (): void => {
|
||||
metadata.add(CONTENT_TYPE, 'c/d');
|
||||
expect((): any => metadata.contentType).toThrow();
|
||||
});
|
||||
|
||||
it('has a shorthand for Content-Type as string.', async(): Promise<void> => {
|
||||
expect(metadata.contentType).toBeUndefined();
|
||||
expect(metadata.contentTypeObject).toBeUndefined();
|
||||
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
|
||||
expect(metadata.contentTypeObject).toEqual({
|
||||
value: 'text/plain',
|
||||
parameters: {
|
||||
charset: 'utf-8',
|
||||
test: 'value1',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('has a shorthand for Content-Type as object.', async(): Promise<void> => {
|
||||
expect(metadata.contentType).toBeUndefined();
|
||||
expect(metadata.contentTypeObject).toBeUndefined();
|
||||
metadata.contentTypeObject = {
|
||||
value: 'text/plain',
|
||||
parameters: {
|
||||
charset: 'utf-8',
|
||||
test: 'value1',
|
||||
},
|
||||
};
|
||||
expect(metadata.contentTypeObject).toEqual({
|
||||
value: 'text/plain',
|
||||
parameters: {
|
||||
charset: 'utf-8',
|
||||
test: 'value1',
|
||||
},
|
||||
});
|
||||
expect(metadata.contentType).toBe('text/plain');
|
||||
});
|
||||
|
||||
it('can properly clear the Content-Type parameters explicitly.', async(): Promise<void> => {
|
||||
expect(metadata.contentType).toBeUndefined();
|
||||
expect(metadata.contentTypeObject).toBeUndefined();
|
||||
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
|
||||
metadata.contentType = undefined;
|
||||
expect(metadata.contentType).toBeUndefined();
|
||||
expect(metadata.contentTypeObject).toBeUndefined();
|
||||
expect(metadata.quads(null, SOLID_META.terms.contentTypeParameter, null, null)).toHaveLength(0);
|
||||
expect(metadata.quads(null, SOLID_META.terms.value, null, null)).toHaveLength(0);
|
||||
expect(metadata.quads(null, RDFS.terms.label, null, null)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('can properly clear the Content-Type parameters implicitly.', async(): Promise<void> => {
|
||||
expect(metadata.contentType).toBeUndefined();
|
||||
expect(metadata.contentTypeObject).toBeUndefined();
|
||||
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
|
||||
metadata.contentType = 'text/turtle';
|
||||
expect(metadata.contentType).toBe('text/turtle');
|
||||
expect(metadata.contentTypeObject).toEqual({
|
||||
value: 'text/turtle',
|
||||
parameters: {},
|
||||
});
|
||||
expect(metadata.quads(null, SOLID_META.terms.contentTypeParameter, null, null)).toHaveLength(0);
|
||||
expect(metadata.quads(null, SOLID_META.terms.value, null, null)).toHaveLength(0);
|
||||
expect(metadata.quads(null, RDFS.terms.label, null, null)).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('can return invalid parameters when too many quads are present.', async(): Promise<void> => {
|
||||
expect(metadata.contentType).toBeUndefined();
|
||||
expect(metadata.contentTypeObject).toBeUndefined();
|
||||
metadata.contentType = 'text/plain; charset=utf-8; test=value1';
|
||||
const param = metadata.quads(null, SOLID_META.terms.value)[0].subject;
|
||||
metadata.addQuad(param as BlankNode, SOLID_META.terms.value, 'anomaly');
|
||||
expect(metadata.contentTypeObject?.parameters).toMatchObject({ invalid: '' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -190,16 +190,31 @@ describe('HeaderUtil', (): void => {
|
||||
|
||||
describe('#parseContentType', (): void => {
|
||||
const contentTypeTurtle = 'text/turtle';
|
||||
const contentTypePlain: any = {
|
||||
value: 'text/plain',
|
||||
parameters: {
|
||||
charset: 'utf-8',
|
||||
},
|
||||
};
|
||||
it('handles single content-type parameter (with leading and trailing whitespaces).', (): void => {
|
||||
expect(parseContentType('text/turtle').type).toEqual(contentTypeTurtle);
|
||||
expect(parseContentType('text/turtle ').type).toEqual(contentTypeTurtle);
|
||||
expect(parseContentType(' text/turtle').type).toEqual(contentTypeTurtle);
|
||||
expect(parseContentType('text/turtle').value).toEqual(contentTypeTurtle);
|
||||
expect(parseContentType('text/turtle ').value).toEqual(contentTypeTurtle);
|
||||
expect(parseContentType(' text/turtle').value).toEqual(contentTypeTurtle);
|
||||
expect(parseContentType('text/plain; charset=utf-8')).toEqual(contentTypePlain);
|
||||
expect(parseContentType(' text/plain; charset=utf-8')).toEqual(contentTypePlain);
|
||||
expect(parseContentType('text/plain ; charset=utf-8')).toEqual(contentTypePlain);
|
||||
expect(parseContentType(' text/plain ; charset=utf-8')).toEqual(contentTypePlain);
|
||||
expect(parseContentType(' text/plain ; charset="utf-8"')).toEqual(contentTypePlain);
|
||||
expect(parseContentType(' text/plain ; charset = "utf-8"')).toEqual(contentTypePlain);
|
||||
});
|
||||
|
||||
it('handles multiple content-type parameters.', (): void => {
|
||||
expect(parseContentType('text/turtle; charset=UTF-8').type).toEqual(contentTypeTurtle);
|
||||
expect(parseContentType('text/turtle; charset=UTF-8').value).toEqual(contentTypeTurtle);
|
||||
contentTypePlain.parameters.test = 'value1';
|
||||
expect(parseContentType('text/plain; charset=utf-8;test="value1"')).toEqual(contentTypePlain);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#parseForwarded', (): void => {
|
||||
it('handles an empty set of headers.', (): void => {
|
||||
expect(parseForwarded({})).toEqual({});
|
||||
|
||||
Reference in New Issue
Block a user