feat: Specifiy constants in separate file

This commit is contained in:
Joachim Van Herwegen
2020-07-24 16:17:22 +02:00
parent aaba113563
commit 14db5fed91
11 changed files with 43 additions and 31 deletions

View File

@@ -1,5 +1,6 @@
import { AcceptPreferenceParser } from '../../src/ldp/http/AcceptPreferenceParser';
import arrayifyStream from 'arrayify-stream';
import { DATA_TYPE_QUAD } from '../../src/util/ContentTypes';
import { HttpRequest } from '../../src/server/HttpRequest';
import { Readable } from 'stream';
import { SimpleBodyParser } from '../../src/ldp/http/SimpleBodyParser';
@@ -35,7 +36,7 @@ describe('A SimpleRequestParser with simple input parsers', (): void => {
},
body: {
data: expect.any(Readable),
dataType: 'quad',
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: 'text/turtle',
profiles: [],

View File

@@ -1,4 +1,5 @@
import arrayifyStream from 'arrayify-stream';
import { DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { HttpRequest } from '../../../../src/server/HttpRequest';
import { Readable } from 'stream';
import { SimpleBodyParser } from '../../../../src/ldp/http/SimpleBodyParser';
@@ -44,7 +45,7 @@ describe('A SimpleBodyparser', (): void => {
const result = (await bodyParser.handle(input))!;
expect(result).toEqual({
data: expect.any(Readable),
dataType: 'quad',
dataType: DATA_TYPE_QUAD,
metadata: {
contentType: 'text/turtle',
profiles: [],

View File

@@ -5,6 +5,7 @@ import { SimpleResponseWriter } from '../../../../src/ldp/http/SimpleResponseWri
import streamifyArray from 'streamify-array';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { createResponse, MockResponse } from 'node-mocks-http';
import { DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
describe('A SimpleResponseWriter', (): void => {
const writer = new SimpleResponseWriter();
@@ -15,11 +16,9 @@ describe('A SimpleResponseWriter', (): void => {
});
it('requires the description body to be a string or binary stream if present.', async(): Promise<void> => {
await expect(writer.canHandle({ response, result: { body: { dataType: 'quad' }} as ResponseDescription }))
await expect(writer.canHandle({ response, result: { body: { dataType: DATA_TYPE_QUAD }} as ResponseDescription }))
.rejects.toThrow(UnsupportedHttpError);
await expect(writer.canHandle({ response, result: { body: { dataType: 'string' }} as ResponseDescription }))
.resolves.toBeUndefined();
await expect(writer.canHandle({ response, result: { body: { dataType: 'binary' }} as ResponseDescription }))
await expect(writer.canHandle({ response, result: { body: { dataType: DATA_TYPE_BINARY }} as ResponseDescription }))
.resolves.toBeUndefined();
});
@@ -33,7 +32,7 @@ describe('A SimpleResponseWriter', (): void => {
it('responds with a body if the description has a body.', async(done): Promise<void> => {
const body = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
dataType: 'binary',
dataType: DATA_TYPE_BINARY,
metadata: {
raw: [] as Quad[],
profiles: [] as string[],
@@ -54,7 +53,7 @@ describe('A SimpleResponseWriter', (): void => {
it('responds with a content-type if the metadata has it.', async(done): Promise<void> => {
const body = {
data: streamifyArray([ '<http://test.com/s> <http://test.com/p> <http://test.com/o>.' ]),
dataType: 'binary',
dataType: DATA_TYPE_BINARY,
metadata: {
raw: [] as Quad[],
profiles: [] as string[],

View File

@@ -1,3 +1,4 @@
import { DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { Operation } from '../../../../src/ldp/operations/Operation';
import { Representation } from '../../../../src/ldp/representation/Representation';
import { ResourceStore } from '../../../../src/storage/ResourceStore';
@@ -6,7 +7,7 @@ import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHtt
describe('A SimpleGetOperationHandler', (): void => {
const store = {
getRepresentation: async(): Promise<Representation> => ({ dataType: 'quad' } as Representation),
getRepresentation: async(): Promise<Representation> => ({ dataType: DATA_TYPE_QUAD } as Representation),
} as unknown as ResourceStore;
const handler = new SimpleGetOperationHandler(store);
@@ -17,7 +18,7 @@ describe('A SimpleGetOperationHandler', (): void => {
it('returns the representation from the store with the input identifier.', async(): Promise<void> => {
await expect(handler.handle({ target: { path: 'url' }} as Operation)).resolves.toEqual(
{ identifier: { path: 'url' }, body: { dataType: 'quad' }},
{ identifier: { path: 'url' }, body: { dataType: DATA_TYPE_QUAD }},
);
});
});

View File

@@ -6,6 +6,7 @@ import { RepresentationMetadata } from '../../../src/ldp/representation/Represen
import { SimpleResourceStore } from '../../../src/storage/SimpleResourceStore';
import streamifyArray from 'streamify-array';
import { UnsupportedMediaTypeHttpError } from '../../../src/util/errors/UnsupportedMediaTypeHttpError';
import { CONTENT_TYPE_QUADS, DATA_TYPE_BINARY, DATA_TYPE_QUAD } from '../../../src/util/ContentTypes';
import { namedNode, triple } from '@rdfjs/data-model';
const base = 'http://test.com/';
@@ -24,7 +25,7 @@ describe('A SimpleResourceStore', (): void => {
representation = {
data: streamifyArray([ quad ]),
dataType: 'quad',
dataType: DATA_TYPE_QUAD,
metadata: {} as RepresentationMetadata,
};
});
@@ -43,16 +44,16 @@ describe('A SimpleResourceStore', (): void => {
});
it('errors for wrong input data types.', async(): Promise<void> => {
(representation as any).dataType = 'binary';
(representation as any).dataType = DATA_TYPE_BINARY;
await expect(store.addResource({ path: base }, representation)).rejects.toThrow(UnsupportedMediaTypeHttpError);
});
it('can write and read data.', async(): Promise<void> => {
const identifier = await store.addResource({ path: base }, representation);
expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier, { type: [{ value: 'internal/quads', weight: 1 }]});
const result = await store.getRepresentation(identifier, { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]});
expect(result).toEqual({
dataType: 'quad',
dataType: DATA_TYPE_QUAD,
data: expect.any(Readable),
metadata: {
profiles: [],
@@ -74,7 +75,7 @@ describe('A SimpleResourceStore', (): void => {
expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier, { type: [{ value: 'text/turtle', weight: 1 }]});
expect(result).toEqual({
dataType: 'binary',
dataType: DATA_TYPE_BINARY,
data: expect.any(Readable),
metadata: {
profiles: [],
@@ -92,7 +93,7 @@ describe('A SimpleResourceStore', (): void => {
expect(identifier.path.startsWith(base)).toBeTruthy();
const result = await store.getRepresentation(identifier, { });
expect(result).toEqual({
dataType: 'binary',
dataType: DATA_TYPE_BINARY,
data: expect.any(Readable),
metadata: {
profiles: [],
@@ -107,9 +108,9 @@ describe('A SimpleResourceStore', (): void => {
it('can set data.', async(): Promise<void> => {
await store.setRepresentation({ path: base }, representation);
const result = await store.getRepresentation({ path: base }, { type: [{ value: 'internal/quads', weight: 1 }]});
const result = await store.getRepresentation({ path: base }, { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]});
expect(result).toEqual({
dataType: 'quad',
dataType: DATA_TYPE_QUAD,
data: expect.any(Readable),
metadata: {
profiles: [],

View File

@@ -8,6 +8,7 @@ import { SparqlUpdatePatch } from '../../../../src/ldp/http/SparqlUpdatePatch';
import streamifyArray from 'streamify-array';
import { translate } from 'sparqlalgebrajs';
import { UnsupportedHttpError } from '../../../../src/util/errors/UnsupportedHttpError';
import { CONTENT_TYPE_QUADS, DATA_TYPE_QUAD } from '../../../../src/util/ContentTypes';
import { namedNode, quad } from '@rdfjs/data-model';
describe('A SimpleSparqlUpdatePatchHandler', (): void => {
@@ -64,15 +65,15 @@ describe('A SimpleSparqlUpdatePatchHandler', (): void => {
const basicChecks = async(quads: Quad[]): Promise<void> => {
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(
{ path: 'path' }, { type: [{ value: 'internal/quads', weight: 1 }]},
{ path: 'path' }, { type: [{ value: CONTENT_TYPE_QUADS, weight: 1 }]},
);
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
expect(order).toEqual([ 'acquire', 'getRepresentation', 'setRepresentation', 'release' ]);
const setParams = (source.setRepresentation as jest.Mock).mock.calls[0];
expect(setParams[0]).toEqual({ path: 'path' });
expect(setParams[1]).toEqual(expect.objectContaining({
dataType: 'quad',
metadata: { raw: [], profiles: [], contentType: 'internal/quads' },
dataType: DATA_TYPE_QUAD,
metadata: { raw: [], profiles: [], contentType: CONTENT_TYPE_QUADS },
}));
await expect(arrayifyStream(setParams[1].data)).resolves.toBeRdfIsomorphic(quads);
};