CommunitySolidServer/test/unit/http/input/body/SparqlUpdateBodyParser.test.ts
Thomas Dupont a8602055e6
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>
2022-03-14 10:27:34 +01:00

90 lines
4.2 KiB
TypeScript

import 'jest-rdf';
import arrayifyStream from 'arrayify-stream';
import { DataFactory } from 'n3';
import { Algebra } from 'sparqlalgebrajs';
import * as algebra from 'sparqlalgebrajs';
import type { BodyParserArgs } from '../../../../../src/http/input/body/BodyParser';
import { SparqlUpdateBodyParser } from '../../../../../src/http/input/body/SparqlUpdateBodyParser';
import { RepresentationMetadata } from '../../../../../src/http/representation/RepresentationMetadata';
import type { HttpRequest } from '../../../../../src/server/HttpRequest';
import { BadRequestHttpError } from '../../../../../src/util/errors/BadRequestHttpError';
import { UnsupportedMediaTypeHttpError } from '../../../../../src/util/errors/UnsupportedMediaTypeHttpError';
import { guardedStreamFrom } from '../../../../../src/util/StreamUtil';
const { namedNode, quad } = DataFactory;
describe('A SparqlUpdateBodyParser', (): void => {
const bodyParser = new SparqlUpdateBodyParser();
let input: BodyParserArgs;
beforeEach(async(): Promise<void> => {
input = { request: { headers: {}} as HttpRequest, metadata: new RepresentationMetadata() };
});
it('only accepts application/sparql-update content.', async(): Promise<void> => {
await expect(bodyParser.canHandle(input)).rejects.toThrow(UnsupportedMediaTypeHttpError);
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)).resolves.toBeUndefined();
input.metadata.contentType = 'application/sparql-update ; foo=bar';
await expect(bodyParser.canHandle(input)).resolves.toBeUndefined();
input.metadata.contentType = 'application/sparql-update';
await expect(bodyParser.canHandle(input)).resolves.toBeUndefined();
});
it('errors when handling invalid SPARQL updates.', async(): Promise<void> => {
input.request = guardedStreamFrom([ 'VERY INVALID UPDATE' ]) as HttpRequest;
await expect(bodyParser.handle(input)).rejects.toThrow(BadRequestHttpError);
});
it('errors when receiving an unexpected error.', async(): Promise<void> => {
const mock = jest.spyOn(algebra, 'translate').mockImplementationOnce((): any => {
throw 'apple';
});
input.request = guardedStreamFrom(
[ 'DELETE DATA { <http://test.com/s> <http://test.com/p> <http://test.com/o> }' ],
) as HttpRequest;
await expect(bodyParser.handle(input)).rejects.toThrow(BadRequestHttpError);
mock.mockRestore();
});
it('converts SPARQL updates to algebra.', async(): Promise<void> => {
input.request = guardedStreamFrom(
[ 'DELETE DATA { <http://test.com/s> <http://test.com/p> <http://test.com/o> }' ],
) as HttpRequest;
const result = await bodyParser.handle(input);
expect(result.algebra.type).toBe(Algebra.types.DELETE_INSERT);
expect((result.algebra as Algebra.DeleteInsert).delete).toBeRdfIsomorphic([ quad(
namedNode('http://test.com/s'),
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]);
expect(result.binary).toBe(true);
expect(result.metadata).toBe(input.metadata);
expect(await arrayifyStream(result.data)).toEqual(
[ 'DELETE DATA { <http://test.com/s> <http://test.com/p> <http://test.com/o> }' ],
);
});
it('accepts relative references.', async(): Promise<void> => {
input.request = guardedStreamFrom(
[ 'INSERT DATA { <#it> <http://test.com/p> <http://test.com/o> }' ],
) as HttpRequest;
input.metadata.identifier = namedNode('http://test.com/my-document.ttl');
const result = await bodyParser.handle(input);
expect(result.algebra.type).toBe(Algebra.types.DELETE_INSERT);
expect((result.algebra as Algebra.DeleteInsert).insert).toBeRdfIsomorphic([ quad(
namedNode('http://test.com/my-document.ttl#it'),
namedNode('http://test.com/p'),
namedNode('http://test.com/o'),
) ]);
expect(result.binary).toBe(true);
expect(result.metadata).toBe(input.metadata);
expect(await arrayifyStream(result.data)).toEqual(
[ 'INSERT DATA { <#it> <http://test.com/p> <http://test.com/o> }' ],
);
});
});