-
Notifications
You must be signed in to change notification settings - Fork 131
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
12 changed files
with
213 additions
and
1 deletion.
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,3 +7,5 @@ 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
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,3 +9,4 @@ coverage | |
!.eslintrc.js | ||
!test/eslintrc.js | ||
!jest.config.js | ||
!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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
declare module 'arrayify-stream' { | ||
import { Readable } from 'stream'; | ||
|
||
function arrayifyStream(input: Readable): Promise<any[]>; | ||
export = arrayifyStream; | ||
} |
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,6 @@ | ||
declare module 'streamify-array' { | ||
import { Readable } from 'stream'; | ||
|
||
function streamifyArray(input: any[]): Readable; | ||
export = streamifyArray; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { BodyParser } from './BodyParser'; | ||
import { HttpRequest } from '../../server/HttpRequest'; | ||
import { Quad } from 'rdf-js'; | ||
import { QuadRepresentation } from '../representation/QuadRepresentation'; | ||
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 = [ | ||
'application/n-quads', | ||
'application/trig', | ||
'application/n-triples', | ||
'text/turtle', | ||
'text/n3', | ||
]; | ||
|
||
public async canHandle(input: HttpRequest): Promise<void> { | ||
const contentType = input.headers['content-type']; | ||
|
||
if (contentType && !SimpleBodyParser.contentTypes.some((type): boolean => contentType.includes(type))) { | ||
throw new UnsupportedMediaTypeHttpError('This parser only supports RDF data.'); | ||
} | ||
} | ||
|
||
public async handle(input: HttpRequest): Promise<QuadRepresentation> { | ||
const contentType = input.headers['content-type']; | ||
|
||
if (!contentType) { | ||
return undefined; | ||
} | ||
|
||
const specificType = contentType.split(';')[0]; | ||
|
||
const metadata: RepresentationMetadata = { | ||
raw: [], | ||
profiles: [], | ||
contentType: specificType, | ||
}; | ||
|
||
// StreamParser is a Readable but typings are incorrect at time of writing | ||
const quads: TypedReadable<Quad> = input.pipe(new StreamParser()) as unknown as TypedReadable<Quad>; | ||
|
||
return { | ||
dataType: 'quad', | ||
data: quads, | ||
metadata, | ||
}; | ||
} | ||
} |
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,7 @@ | ||
import { HttpError } from './HttpError'; | ||
|
||
export class UnsupportedMediaTypeHttpError extends HttpError { | ||
public constructor(message?: string) { | ||
super(415, 'UnsupportedHttpError', message); | ||
} | ||
} |
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,58 @@ | ||
import arrayifyStream from 'arrayify-stream'; | ||
import { HttpRequest } from '../../../../src/server/HttpRequest'; | ||
import { SimpleBodyParser } from '../../../../src/ldp/http/SimpleBodyParser'; | ||
import streamifyArray from 'streamify-array'; | ||
import { StreamParser } from 'n3'; | ||
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError'; | ||
import { namedNode, triple } from '@rdfjs/data-model'; | ||
|
||
const contentTypes = [ | ||
'application/n-quads', | ||
'application/trig', | ||
'application/n-triples', | ||
'text/turtle', | ||
'text/n3', | ||
]; | ||
|
||
describe('A SimpleBodyparser', (): void => { | ||
const bodyParser = new SimpleBodyParser(); | ||
|
||
it('rejects input with unsupported content type.', async(): Promise<void> => { | ||
await expect(bodyParser.canHandle({ headers: { 'content-type': 'application/rdf+xml' }} as HttpRequest)) | ||
.rejects.toThrow(new UnsupportedMediaTypeHttpError('This parser only supports RDF data.')); | ||
}); | ||
|
||
it('accepts input with no content type.', async(): Promise<void> => { | ||
await expect(bodyParser.canHandle({ headers: { }} as HttpRequest)).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('accepts turtle and similar content types.', async(): Promise<void> => { | ||
for (const type of contentTypes) { | ||
await expect(bodyParser.canHandle({ headers: { 'content-type': type }} as HttpRequest)).resolves.toBeUndefined(); | ||
} | ||
}); | ||
|
||
it('returns empty output if there was no content-type.', async(): Promise<void> => { | ||
await expect(bodyParser.handle({ headers: { }} as HttpRequest)).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('returns a stream of quads if there was data.', async(): Promise<void> => { | ||
const input = streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]) as HttpRequest; | ||
input.headers = { 'content-type': 'text/turtle' }; | ||
const result = await bodyParser.handle(input); | ||
expect(result).toEqual({ | ||
data: expect.any(StreamParser), | ||
dataType: 'quad', | ||
metadata: { | ||
contentType: 'text/turtle', | ||
profiles: [], | ||
raw: [], | ||
}, | ||
}); | ||
await expect(arrayifyStream(result.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