feat: Support link and slug headers in SimpleBodyParser

This commit is contained in:
Joachim Van Herwegen
2020-08-27 14:59:42 +02:00
parent 48740e5cba
commit 86d5f367d5
3 changed files with 88 additions and 16 deletions

View File

@@ -38,7 +38,6 @@ describe('A SimpleRequestParser with simple input parsers', (): void => {
dataType: DATA_TYPE_BINARY,
metadata: {
contentType: 'text/turtle',
profiles: [],
raw: [],
},
},

View File

@@ -1,9 +1,9 @@
import { Readable } from 'stream';
import arrayifyStream from 'arrayify-stream';
import streamifyArray from 'streamify-array';
import { SimpleBodyParser } from '../../../../src/ldp/http/SimpleBodyParser';
import { HttpRequest } from '../../../../src/server/HttpRequest';
import { DATA_TYPE_BINARY } from '../../../../src/util/ContentTypes';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import 'jest-rdf';
describe('A SimpleBodyparser', (): void => {
@@ -22,11 +22,10 @@ describe('A SimpleBodyparser', (): void => {
input.headers = { 'content-type': 'text/turtle' };
const result = (await bodyParser.handle(input))!;
expect(result).toEqual({
data: expect.any(Readable),
data: input,
dataType: DATA_TYPE_BINARY,
metadata: {
contentType: 'text/turtle',
profiles: [],
raw: [],
},
});
@@ -34,4 +33,48 @@ describe('A SimpleBodyparser', (): void => {
[ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ],
);
});
it('adds the slug header to the metadata.', async(): Promise<void> => {
const input = {} as HttpRequest;
input.headers = { 'content-type': 'text/turtle', slug: 'slugText' };
const result = (await bodyParser.handle(input))!;
expect(result.metadata).toEqual({
contentType: 'text/turtle',
raw: [],
slug: 'slugText',
});
});
it('errors if there are multiple slugs.', async(): Promise<void> => {
const input = {} as HttpRequest;
input.headers = { 'content-type': 'text/turtle', slug: [ 'slugTextA', 'slugTextB' ]};
await expect(bodyParser.handle(input)).rejects.toThrow(UnsupportedHttpError);
});
it('adds the link headers to the metadata.', async(): Promise<void> => {
const input = {} as HttpRequest;
input.headers = { 'content-type': 'text/turtle', link: '<http://www.w3.org/ns/ldp#Container>; rel="type"' };
const result = (await bodyParser.handle(input))!;
expect(result.metadata).toEqual({
contentType: 'text/turtle',
raw: [],
linkRel: { type: new Set([ 'http://www.w3.org/ns/ldp#Container' ]) },
});
});
it('supports multiple link headers.', async(): Promise<void> => {
const input = {} as HttpRequest;
input.headers = { 'content-type': 'text/turtle',
link: [ '<http://www.w3.org/ns/ldp#Container>; rel="type"',
'<http://www.w3.org/ns/ldp#Resource>; rel="type"',
'<unrelatedLink>',
'badLink',
]};
const result = (await bodyParser.handle(input))!;
expect(result.metadata).toEqual({
contentType: 'text/turtle',
raw: [],
linkRel: { type: new Set([ 'http://www.w3.org/ns/ldp#Container', 'http://www.w3.org/ns/ldp#Resource' ]) },
});
});
});