import arrayifyStream from 'arrayify-stream'; import streamifyArray from 'streamify-array'; import { RawBodyParser } from '../../../../src/ldp/http/RawBodyParser'; import { HttpRequest } from '../../../../src/server/HttpRequest'; import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError'; import 'jest-rdf'; describe('A RawBodyparser', (): void => { const bodyParser = new RawBodyParser(); it('accepts all input.', async(): Promise => { await expect(bodyParser.canHandle()).resolves.toBeUndefined(); }); it('returns empty output if there was no content-type.', async(): Promise => { await expect(bodyParser.handle({ headers: { }} as HttpRequest)).resolves.toBeUndefined(); }); it('returns a Representation if there was data.', async(): Promise => { const input = streamifyArray([ ' .' ]) as HttpRequest; input.headers = { 'content-type': 'text/turtle' }; const result = (await bodyParser.handle(input))!; expect(result).toEqual({ binary: true, data: input, metadata: { contentType: 'text/turtle', raw: [], }, }); await expect(arrayifyStream(result.data)).resolves.toEqual( [ ' .' ], ); }); it('adds the slug header to the metadata.', async(): Promise => { 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 => { 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 => { const input = {} as HttpRequest; input.headers = { 'content-type': 'text/turtle', link: '; 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 => { const input = {} as HttpRequest; input.headers = { 'content-type': 'text/turtle', link: [ '; rel="type"', '; rel="type"', '', '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' ]) }, }); }); });