-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
166 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,3 @@ coverage | |
**/*.js | ||
**/*.d.ts | ||
**/*.js.map | ||
|
||
!external-types/*.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,3 @@ coverage | |
!.eslintrc.js | ||
!test/eslintrc.js | ||
!jest.config.js | ||
!external-types/*.d.ts |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
) ]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
"stripInternal": true | ||
}, | ||
"include": [ | ||
"external-types/**/*.ts", | ||
"src/**/*.ts", | ||
"test/**/*.ts" | ||
], | ||
|