mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add simple request parser
This commit is contained in:
52
test/integration/RequestParser.test.ts
Normal file
52
test/integration/RequestParser.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import arrayifyStream from 'arrayify-stream';
|
||||
import { HttpRequest } from '../../src/server/HttpRequest';
|
||||
import { SimpleBodyParser } from '../../src/ldp/http/SimpleBodyParser';
|
||||
import { SimplePreferenceParser } from '../../src/ldp/http/SimplePreferenceParser';
|
||||
import { SimpleRequestParser } from '../../src/ldp/http/SimpleRequestParser';
|
||||
import { SimpleTargetExtractor } from '../../src/ldp/http/SimpleTargetExtractor';
|
||||
import streamifyArray from 'streamify-array';
|
||||
import { StreamParser } from 'n3';
|
||||
import { namedNode, triple } from '@rdfjs/data-model';
|
||||
|
||||
describe('A SimpleRequestParser with simple input parsers', (): void => {
|
||||
const targetExtractor = new SimpleTargetExtractor();
|
||||
const bodyParser = new SimpleBodyParser();
|
||||
const preferenceParser = new SimplePreferenceParser();
|
||||
const requestParser = new SimpleRequestParser({ targetExtractor, bodyParser, preferenceParser });
|
||||
|
||||
it('can parse an incoming request.', async(): Promise<void> => {
|
||||
const request = streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]) as HttpRequest;
|
||||
request.method = 'POST';
|
||||
request.url = 'http://test.com/';
|
||||
request.headers = {
|
||||
accept: 'text/turtle; q=0.8',
|
||||
'accept-language': 'en-gb, en;q=0.5',
|
||||
'content-type': 'text/turtle',
|
||||
};
|
||||
|
||||
const result = await requestParser.handle(request);
|
||||
expect(result).toEqual({
|
||||
method: 'POST',
|
||||
target: { path: 'http://test.com/' },
|
||||
preferences: {
|
||||
type: [{ value: 'text/turtle', weight: 0.8 }],
|
||||
language: [{ value: 'en-gb', weight: 1 }, { value: 'en', weight: 0.5 }],
|
||||
},
|
||||
body: {
|
||||
data: expect.any(StreamParser),
|
||||
dataType: 'quad',
|
||||
metadata: {
|
||||
contentType: 'text/turtle',
|
||||
profiles: [],
|
||||
raw: [],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await expect(arrayifyStream(result.body.data)).resolves.toEqualRdfQuadArray([ triple(
|
||||
namedNode('http://test.com/s'),
|
||||
namedNode('http://test.com/p'),
|
||||
namedNode('http://test.com/o'),
|
||||
) ]);
|
||||
});
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import streamifyArray from 'streamify-array';
|
||||
import { StreamParser } from 'n3';
|
||||
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
|
||||
import { namedNode, triple } from '@rdfjs/data-model';
|
||||
import 'jest-rdf';
|
||||
|
||||
const contentTypes = [
|
||||
'application/n-quads',
|
||||
|
||||
@@ -14,21 +14,21 @@ describe('A SimplePreferenceParser', (): void => {
|
||||
|
||||
it('parses accept headers.', async(): Promise<void> => {
|
||||
await expect(preferenceParser.handle({ headers: { accept: 'audio/*; q=0.2, audio/basic' }} as HttpRequest))
|
||||
.resolves.toEqual({ type: [{ value: 'audio/*', weight: 0.2 }, { value: 'audio/basic' }]});
|
||||
.resolves.toEqual({ type: [{ value: 'audio/*', weight: 0.2 }, { value: 'audio/basic', weight: 1 }]});
|
||||
});
|
||||
|
||||
it('parses accept-charset headers.', async(): Promise<void> => {
|
||||
await expect(preferenceParser.handle({ headers: { 'accept-charset': 'iso-8859-5, unicode-1-1;q=0.8' }} as unknown as HttpRequest))
|
||||
.resolves.toEqual({ charset: [{ value: 'iso-8859-5' }, { value: 'unicode-1-1', weight: 0.8 }]});
|
||||
.resolves.toEqual({ charset: [{ value: 'iso-8859-5', weight: 1 }, { value: 'unicode-1-1', weight: 0.8 }]});
|
||||
});
|
||||
|
||||
it('parses accept-datetime headers.', async(): Promise<void> => {
|
||||
await expect(preferenceParser.handle({ headers: { 'accept-datetime': 'Tue, 20 Mar 2001 20:35:00 GMT' }} as unknown as HttpRequest))
|
||||
.resolves.toEqual({ datetime: [{ value: 'Tue, 20 Mar 2001 20:35:00 GMT' }]});
|
||||
.resolves.toEqual({ datetime: [{ value: 'Tue, 20 Mar 2001 20:35:00 GMT', weight: 1 }]});
|
||||
});
|
||||
|
||||
it('parses accept-language headers.', async(): Promise<void> => {
|
||||
await expect(preferenceParser.handle({ headers: { 'accept-language': 'da, en-gb;q=0.8, en;q=0.7' }} as HttpRequest))
|
||||
.resolves.toEqual({ language: [{ value: 'da' }, { value: 'en-gb', weight: 0.8 }, { value: 'en', weight: 0.7 }]});
|
||||
.resolves.toEqual({ language: [{ value: 'da', weight: 1 }, { value: 'en-gb', weight: 0.8 }, { value: 'en', weight: 0.7 }]});
|
||||
});
|
||||
});
|
||||
|
||||
40
test/unit/ldp/http/SimpleRequestParser.test.ts
Normal file
40
test/unit/ldp/http/SimpleRequestParser.test.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { BodyParser } from '../../../../src/ldp/http/BodyParser';
|
||||
import { PreferenceParser } from '../../../../src/ldp/http/PreferenceParser';
|
||||
import { SimpleRequestParser } from '../../../../src/ldp/http/SimpleRequestParser';
|
||||
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';
|
||||
import { TargetExtractor } from '../../../../src/ldp/http/TargetExtractor';
|
||||
|
||||
describe('A SimpleRequestParser', (): void => {
|
||||
let targetExtractor: TargetExtractor;
|
||||
let bodyParser: BodyParser;
|
||||
let preferenceParser: PreferenceParser;
|
||||
let requestParser: SimpleRequestParser;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
targetExtractor = new StaticAsyncHandler(true, 'target' as any);
|
||||
bodyParser = new StaticAsyncHandler(true, 'body' as any);
|
||||
preferenceParser = new StaticAsyncHandler(true, 'preference' as any);
|
||||
requestParser = new SimpleRequestParser({ targetExtractor, bodyParser, preferenceParser });
|
||||
});
|
||||
|
||||
it('can handle input with both a URL and a method.', async(): Promise<void> => {
|
||||
await expect(requestParser.canHandle({ url: 'url', method: 'GET' } as any)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('rejects input with no URL.', async(): Promise<void> => {
|
||||
await expect(requestParser.canHandle({ method: 'GET' } as any)).rejects.toThrow('Missing URL.');
|
||||
});
|
||||
|
||||
it('rejects input with no method.', async(): Promise<void> => {
|
||||
await expect(requestParser.canHandle({ url: 'url' } as any)).rejects.toThrow('Missing method.');
|
||||
});
|
||||
|
||||
it('returns the output of all input parsers after calling handle.', async(): Promise<void> => {
|
||||
await expect(requestParser.handle({ url: 'url', method: 'GET' } as any)).resolves.toEqual({
|
||||
method: 'GET',
|
||||
target: 'target',
|
||||
preferences: 'preference',
|
||||
body: 'body',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user