feat: add simple request parser

This commit is contained in:
Joachim Van Herwegen
2020-06-10 11:47:43 +02:00
parent 09eb665c12
commit cf258d0317
16 changed files with 166 additions and 27 deletions

View 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',
});
});
});