Skip to content

Commit

Permalink
feat: add simple request parser
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Jun 17, 2020
1 parent 09eb665 commit cf258d0
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 27 deletions.
2 changes: 0 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ coverage
**/*.js
**/*.d.ts
**/*.js.map

!external-types/*.d.ts
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ coverage
!.eslintrc.js
!test/eslintrc.js
!jest.config.js
!external-types/*.d.ts
6 changes: 0 additions & 6 deletions external-types/arrayifyStream.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions external-types/streamifyArray.d.ts

This file was deleted.

18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
"n3": "^1.3.7"
},
"devDependencies": {
"@types/arrayify-stream": "^1.0.0",
"@types/jest": "^25.2.1",
"@types/streamify-array": "^1.0.0",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"arrayify-stream": "^1.0.0",
Expand Down
5 changes: 2 additions & 3 deletions src/ldp/http/SimpleBodyParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { RepresentationMetadata } from '../representation/RepresentationMetadata
import { StreamParser } from 'n3';
import { TypedReadable } from '../../util/TypedReadable';
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError';
import 'jest-rdf';

export class SimpleBodyParser extends BodyParser {
private static readonly contentTypes = [
Expand All @@ -32,12 +31,12 @@ export class SimpleBodyParser extends BodyParser {
return undefined;
}

const specificType = contentType.split(';')[0];
const mediaType = contentType.split(';')[0];

const metadata: RepresentationMetadata = {
raw: [],
profiles: [],
contentType: specificType,
contentType: mediaType,
};

// StreamParser is a Readable but typings are incorrect at time of writing
Expand Down
4 changes: 2 additions & 2 deletions src/ldp/http/SimplePreferenceParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class SimplePreferenceParser extends PreferenceParser {
// Datetime can have commas so requires separate rules
let datetime;
if (input.headers['accept-datetime']) {
datetime = [{ value: input.headers['accept-datetime'] as string }];
datetime = [{ value: input.headers['accept-datetime'] as string, weight: 1 }];
}

return { type, charset, datetime, language };
Expand All @@ -34,7 +34,7 @@ export class SimplePreferenceParser extends PreferenceParser {
return header.split(',').map((preference): RepresentationPreference => {
const parts = preference.split(';');
if (parts.length === 1) {
return { value: parts[0].trim() };
return { value: parts[0].trim(), weight: 1 };
}
return { value: parts[0].trim(), weight: parseFloat(parts[1].trim().slice('q='.length)) };
});
Expand Down
43 changes: 43 additions & 0 deletions src/ldp/http/SimpleRequestParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { BodyParser } from './BodyParser';
import { HttpRequest } from '../../server/HttpRequest';
import { Operation } from '../operations/Operation';
import { PreferenceParser } from './PreferenceParser';
import { RequestParser } from './RequestParser';
import { TargetExtractor } from './TargetExtractor';

/**
* Input parsers required for a {@link SimpleRequestParser}.
*/
export interface SimpleRequestParserArgs {
targetExtractor: TargetExtractor;
preferenceParser: PreferenceParser;
bodyParser: BodyParser;
}

export class SimpleRequestParser extends RequestParser {
private readonly targetExtractor: TargetExtractor;
private readonly preferenceParser: PreferenceParser;
private readonly bodyParser: BodyParser;

public constructor(args: SimpleRequestParserArgs) {
super();
Object.assign(this, args);
}

public async canHandle(input: HttpRequest): Promise<void> {
if (!input.url) {
throw new Error('Missing URL.');
}
if (!input.method) {
throw new Error('Missing method.');
}
}

public async handle(input: HttpRequest): Promise<Operation> {
const target = await this.targetExtractor.handleSafe(input);
const preferences = await this.preferenceParser.handleSafe(input);
const body = await this.bodyParser.handleSafe(input);

return { method: input.method, target, preferences, body };
}
}
2 changes: 1 addition & 1 deletion src/ldp/representation/RepresentationPreference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export interface RepresentationPreference {
/**
* How important this preference is in a value going from 0 to 1.
*/
weight?: number;
weight: number;
}
2 changes: 1 addition & 1 deletion src/ldp/representation/ResourceIdentifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
export interface ResourceIdentifier {
/**
* Path to the relevant resource. Usually this would be an URL.
* Path to the relevant resource.
*/
path: string;
}
52 changes: 52 additions & 0 deletions test/integration/RequestParser.test.ts
Original file line number Diff line number Diff line change
@@ -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'),
) ]);
});
});
1 change: 1 addition & 0 deletions test/unit/ldp/http/SimpleBodyParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions test/unit/ldp/http/SimplePreferenceParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 40 additions & 0 deletions test/unit/ldp/http/SimpleRequestParser.test.ts
Original file line number Diff line number Diff line change
@@ -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',
});
});
});
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"stripInternal": true
},
"include": [
"external-types/**/*.ts",
"src/**/*.ts",
"test/**/*.ts"
],
Expand Down

0 comments on commit cf258d0

Please sign in to comment.