|
| 1 | +import arrayifyStream from 'arrayify-stream'; |
| 2 | +import { HttpRequest } from '../../../../src/server/HttpRequest'; |
| 3 | +import { SimpleBodyParser } from '../../../../src/ldp/http/SimpleBodyParser'; |
| 4 | +import streamifyArray from 'streamify-array'; |
| 5 | +import { StreamParser } from 'n3'; |
| 6 | +import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError'; |
| 7 | +import { namedNode, triple } from '@rdfjs/data-model'; |
| 8 | + |
| 9 | +const contentTypes = [ |
| 10 | + 'application/n-quads', |
| 11 | + 'application/trig', |
| 12 | + 'application/n-triples', |
| 13 | + 'text/turtle', |
| 14 | + 'text/n3', |
| 15 | +]; |
| 16 | + |
| 17 | +describe('A SimpleBodyparser', (): void => { |
| 18 | + const bodyParser = new SimpleBodyParser(); |
| 19 | + |
| 20 | + it('rejects input with unsupported content type.', async(): Promise<void> => { |
| 21 | + await expect(bodyParser.canHandle({ headers: { 'content-type': 'application/rdf+xml' }} as HttpRequest)) |
| 22 | + .rejects.toThrow(new UnsupportedMediaTypeHttpError('This parser only supports RDF data.')); |
| 23 | + }); |
| 24 | + |
| 25 | + it('accepts input with no content type.', async(): Promise<void> => { |
| 26 | + await expect(bodyParser.canHandle({ headers: { }} as HttpRequest)).resolves.toBeUndefined(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('accepts turtle and similar content types.', async(): Promise<void> => { |
| 30 | + for (const type of contentTypes) { |
| 31 | + await expect(bodyParser.canHandle({ headers: { 'content-type': type }} as HttpRequest)).resolves.toBeUndefined(); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + it('returns empty output if there was no content-type.', async(): Promise<void> => { |
| 36 | + await expect(bodyParser.handle({ headers: { }} as HttpRequest)).resolves.toBeUndefined(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('returns a stream of quads if there was data.', async(): Promise<void> => { |
| 40 | + const input = streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]) as HttpRequest; |
| 41 | + input.headers = { 'content-type': 'text/turtle' }; |
| 42 | + const result = await bodyParser.handle(input); |
| 43 | + expect(result).toEqual({ |
| 44 | + data: expect.any(StreamParser), |
| 45 | + dataType: 'quad', |
| 46 | + metadata: { |
| 47 | + contentType: 'text/turtle', |
| 48 | + profiles: [], |
| 49 | + raw: [], |
| 50 | + }, |
| 51 | + }); |
| 52 | + await expect(arrayifyStream(result.data)).resolves.toEqualRdfQuadArray([ triple( |
| 53 | + namedNode('http://test.com/s'), |
| 54 | + namedNode('http://test.com/p'), |
| 55 | + namedNode('http://test.com/o'), |
| 56 | + ) ]); |
| 57 | + }); |
| 58 | +}); |
0 commit comments