mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
fix: Test error classes correctly
This commit is contained in:
parent
a57105be8e
commit
c29928c32c
@ -21,8 +21,9 @@ describe('A SlugParser', (): void => {
|
||||
|
||||
it('errors if there are multiple slug headers.', async(): Promise<void> => {
|
||||
request.headers.slug = [ 'slugA', 'slugB' ];
|
||||
await expect(parser.parse(request, metadata))
|
||||
.rejects.toThrow(new BadRequestHttpError('Request has multiple Slug headers'));
|
||||
const result = parser.parse(request, metadata);
|
||||
await expect(result).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(result).rejects.toThrow('Request has multiple Slug headers');
|
||||
});
|
||||
|
||||
it('stores the slug metadata.', async(): Promise<void> => {
|
||||
|
@ -2,7 +2,7 @@ import { Factory } from 'sparqlalgebrajs';
|
||||
import type { SparqlUpdatePatch } from '../../../../src/ldp/http/SparqlUpdatePatch';
|
||||
import type { Operation } from '../../../../src/ldp/operations/Operation';
|
||||
import { SparqlPatchPermissionsExtractor } from '../../../../src/ldp/permissions/SparqlPatchPermissionsExtractor';
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
||||
|
||||
describe('A SparqlPatchPermissionsExtractor', (): void => {
|
||||
const extractor = new SparqlPatchPermissionsExtractor();
|
||||
@ -13,16 +13,23 @@ describe('A SparqlPatchPermissionsExtractor', (): void => {
|
||||
await expect(extractor.canHandle(operation)).resolves.toBeUndefined();
|
||||
(operation.body as SparqlUpdatePatch).algebra = factory.createCompositeUpdate([ factory.createDeleteInsert() ]);
|
||||
await expect(extractor.canHandle(operation)).resolves.toBeUndefined();
|
||||
await expect(extractor.canHandle({ ...operation, method: 'GET' }))
|
||||
.rejects.toThrow(new BadRequestHttpError('Cannot determine permissions of GET, only PATCH.'));
|
||||
await expect(extractor.canHandle({ ...operation, body: undefined }))
|
||||
.rejects.toThrow(new BadRequestHttpError('Cannot determine permissions of PATCH operations without a body.'));
|
||||
await expect(extractor.canHandle({ ...operation, body: {} as SparqlUpdatePatch }))
|
||||
.rejects.toThrow(new BadRequestHttpError('Cannot determine permissions of non-SPARQL patches.'));
|
||||
await expect(extractor.canHandle({ ...operation,
|
||||
body: { algebra: factory.createMove('DEFAULT', 'DEFAULT') } as unknown as SparqlUpdatePatch }))
|
||||
.rejects
|
||||
.toThrow(new BadRequestHttpError('Can only determine permissions of a PATCH with DELETE/INSERT operations.'));
|
||||
|
||||
let result = extractor.canHandle({ ...operation, method: 'GET' });
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Cannot determine permissions of GET, only PATCH.');
|
||||
|
||||
result = extractor.canHandle({ ...operation, body: undefined });
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Cannot determine permissions of PATCH operations without a body.');
|
||||
|
||||
result = extractor.canHandle({ ...operation, body: {} as SparqlUpdatePatch });
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Cannot determine permissions of non-SPARQL patches.');
|
||||
|
||||
result = extractor.canHandle({ ...operation,
|
||||
body: { algebra: factory.createMove('DEFAULT', 'DEFAULT') } as unknown as SparqlUpdatePatch });
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Can only determine permissions of a PATCH with DELETE/INSERT operations.');
|
||||
});
|
||||
|
||||
it('requires append for INSERT operations.', async(): Promise<void> => {
|
||||
|
@ -12,7 +12,7 @@ describe('LazyLogger', (): void => {
|
||||
|
||||
it('throws when no logger factory is set in the lazy logger factory.', async(): Promise<void> => {
|
||||
expect((): any => logger.log('debug', 'my message', { abc: true }))
|
||||
.toThrow(new Error('No logger factory has been set. Can be caused by logger invocation during initialization.'));
|
||||
.toThrow('No logger factory has been set. Can be caused by logger invocation during initialization.');
|
||||
});
|
||||
|
||||
it('creates a new logger using the factory.', async(): Promise<void> => {
|
||||
|
@ -31,7 +31,7 @@ describe('LazyLoggerFactory', (): void => {
|
||||
|
||||
it('throws when retrieving the inner factory if none has been set.', async(): Promise<void> => {
|
||||
expect((): any => LazyLoggerFactory.getInstance().loggerFactory)
|
||||
.toThrow(new Error('No logger factory has been set. Can be caused by logger invocation during initialization.'));
|
||||
.toThrow('No logger factory has been set. Can be caused by logger invocation during initialization.');
|
||||
});
|
||||
|
||||
it('Returns the inner factory if one has been set.', async(): Promise<void> => {
|
||||
@ -60,6 +60,6 @@ describe('LazyLoggerFactory', (): void => {
|
||||
it('errors on invoking LazyLoggers if a factory has not been set yet.', async(): Promise<void> => {
|
||||
const logger = LazyLoggerFactory.getInstance().createLogger('MyLabel');
|
||||
expect((): any => logger.log('debug', 'my message', { abc: true }))
|
||||
.toThrow(new Error('No logger factory has been set. Can be caused by logger invocation during initialization.'));
|
||||
.toThrow('No logger factory has been set. Can be caused by logger invocation during initialization.');
|
||||
});
|
||||
});
|
||||
|
@ -28,6 +28,6 @@ describe('LogUtil', (): void => {
|
||||
setGlobalLoggerFactory(new VoidLoggerFactory());
|
||||
resetGlobalLoggerFactory();
|
||||
expect((): any => LazyLoggerFactory.getInstance().loggerFactory)
|
||||
.toThrow(new Error('No logger factory has been set. Can be caused by logger invocation during initialization.'));
|
||||
.toThrow('No logger factory has been set. Can be caused by logger invocation during initialization.');
|
||||
});
|
||||
});
|
||||
|
@ -151,14 +151,15 @@ describe('A DataAccessorBasedStore', (): void => {
|
||||
accessor.getMetadata = async(): Promise<any> => {
|
||||
throw new Error('randomError');
|
||||
};
|
||||
await expect(store.addResource(resourceID, representation)).rejects.toThrow(new Error('randomError'));
|
||||
await expect(store.addResource(resourceID, representation)).rejects.toThrow('randomError');
|
||||
});
|
||||
|
||||
it('does not allow adding resources to existing non-containers.', async(): Promise<void> => {
|
||||
const resourceID = { path: `${root}resource/` };
|
||||
accessor.data[resourceID.path] = representation;
|
||||
await expect(store.addResource(resourceID, representation))
|
||||
.rejects.toThrow(new MethodNotAllowedHttpError('The given path is not a container.'));
|
||||
const result = store.addResource(resourceID, representation);
|
||||
await expect(result).rejects.toThrow(MethodNotAllowedHttpError);
|
||||
await expect(result).rejects.toThrow('The given path is not a container.');
|
||||
});
|
||||
|
||||
it('errors when trying to create a container with non-RDF data.', async(): Promise<void> => {
|
||||
@ -326,8 +327,9 @@ describe('A DataAccessorBasedStore', (): void => {
|
||||
representation.data = guardedStreamFrom(
|
||||
[ `<${`${root}resource/`}> <http://www.w3.org/ns/ldp#contains> <uri>.` ],
|
||||
);
|
||||
await expect(store.setRepresentation(resourceID, representation))
|
||||
.rejects.toThrow(new ConflictHttpError('Container bodies are not allowed to have containment triples.'));
|
||||
const result = store.setRepresentation(resourceID, representation);
|
||||
await expect(result).rejects.toThrow(ConflictHttpError);
|
||||
await expect(result).rejects.toThrow('Container bodies are not allowed to have containment triples.');
|
||||
});
|
||||
|
||||
it('creates recursive containers when needed.', async(): Promise<void> => {
|
||||
@ -366,8 +368,9 @@ describe('A DataAccessorBasedStore', (): void => {
|
||||
|
||||
describe('modifying a Representation', (): void => {
|
||||
it('is not supported.', async(): Promise<void> => {
|
||||
await expect(store.modifyResource())
|
||||
.rejects.toThrow(new NotImplementedHttpError('Patches are not supported by the default store.'));
|
||||
const result = store.modifyResource();
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Patches are not supported by the default store.');
|
||||
});
|
||||
});
|
||||
|
||||
@ -380,15 +383,17 @@ describe('A DataAccessorBasedStore', (): void => {
|
||||
it('will error when deleting a root storage container.', async(): Promise<void> => {
|
||||
representation.metadata.add(RDF.type, PIM.terms.Storage);
|
||||
accessor.data[`${root}container`] = representation;
|
||||
await expect(store.deleteResource({ path: `${root}container` }))
|
||||
.rejects.toThrow(new MethodNotAllowedHttpError('Cannot delete a root storage container.'));
|
||||
const result = store.deleteResource({ path: `${root}container` });
|
||||
await expect(result).rejects.toThrow(MethodNotAllowedHttpError);
|
||||
await expect(result).rejects.toThrow('Cannot delete a root storage container.');
|
||||
});
|
||||
|
||||
it('will error when deleting non-empty containers.', async(): Promise<void> => {
|
||||
accessor.data[`${root}container`] = representation;
|
||||
accessor.data[`${root}container`].metadata.add(LDP.contains, DataFactory.namedNode(`${root}otherThing`));
|
||||
await expect(store.deleteResource({ path: `${root}container` }))
|
||||
.rejects.toThrow(new ConflictHttpError('Can only delete empty containers.'));
|
||||
const result = store.deleteResource({ path: `${root}container` });
|
||||
await expect(result).rejects.toThrow(ConflictHttpError);
|
||||
await expect(result).rejects.toThrow('Can only delete empty containers.');
|
||||
});
|
||||
|
||||
it('will delete resources.', async(): Promise<void> => {
|
||||
|
@ -216,7 +216,7 @@ describe('A LockingResourceStore', (): void => {
|
||||
|
||||
timeoutTrigger.emit('timeout');
|
||||
|
||||
await expect(prom).rejects.toThrow(new Error('timeout'));
|
||||
await expect(prom).rejects.toThrow('timeout');
|
||||
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
|
||||
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual({ path: 'path' });
|
||||
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
|
||||
|
@ -72,6 +72,6 @@ describe('A RoutingResourceStore', (): void => {
|
||||
throw new Error('error');
|
||||
};
|
||||
await expect(store.getRepresentation(identifier, 'preferences' as any, 'conditions' as any))
|
||||
.rejects.toThrow(new Error('error'));
|
||||
.rejects.toThrow('error');
|
||||
});
|
||||
});
|
||||
|
@ -39,8 +39,9 @@ describe('A FileDataAccessor', (): void => {
|
||||
|
||||
it('can only handle binary data.', async(): Promise<void> => {
|
||||
await expect(accessor.canHandle({ binary: true } as Representation)).resolves.toBeUndefined();
|
||||
await expect(accessor.canHandle({ binary: false } as Representation)).rejects
|
||||
.toThrow(new UnsupportedMediaTypeHttpError('Only binary data is supported.'));
|
||||
const result = accessor.canHandle({ binary: false } as Representation);
|
||||
await expect(result).rejects.toThrow(UnsupportedMediaTypeHttpError);
|
||||
await expect(result).rejects.toThrow('Only binary data is supported.');
|
||||
});
|
||||
|
||||
describe('getting data', (): void => {
|
||||
@ -82,7 +83,7 @@ describe('A FileDataAccessor', (): void => {
|
||||
jest.requireMock('fs').promises.lstat = (): any => {
|
||||
throw new Error('error');
|
||||
};
|
||||
await expect(accessor.getMetadata({ path: base })).rejects.toThrow(new Error('error'));
|
||||
await expect(accessor.getMetadata({ path: base })).rejects.toThrow('error');
|
||||
});
|
||||
|
||||
it('throws a 404 if the trailing slash does not match its type.', async(): Promise<void> => {
|
||||
@ -150,8 +151,9 @@ describe('A FileDataAccessor', (): void => {
|
||||
});
|
||||
|
||||
it('throws an error when writing to a metadata path.', async(): Promise<void> => {
|
||||
await expect(accessor.writeDocument({ path: `${base}resource.meta` }, data, metadata))
|
||||
.rejects.toThrow(new ConflictHttpError('Not allowed to create files with the metadata extension.'));
|
||||
const result = accessor.writeDocument({ path: `${base}resource.meta` }, data, metadata);
|
||||
await expect(result).rejects.toThrow(ConflictHttpError);
|
||||
await expect(result).rejects.toThrow('Not allowed to create files with the metadata extension.');
|
||||
});
|
||||
|
||||
it('writes the data to the corresponding file.', async(): Promise<void> => {
|
||||
@ -187,7 +189,7 @@ describe('A FileDataAccessor', (): void => {
|
||||
throw new Error('error');
|
||||
};
|
||||
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
|
||||
.rejects.toThrow(new Error('error'));
|
||||
.rejects.toThrow('error');
|
||||
});
|
||||
|
||||
it('throws if something went wrong writing a file.', async(): Promise<void> => {
|
||||
@ -196,7 +198,7 @@ describe('A FileDataAccessor', (): void => {
|
||||
return null;
|
||||
};
|
||||
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
|
||||
.rejects.toThrow(new Error('error'));
|
||||
.rejects.toThrow('error');
|
||||
});
|
||||
|
||||
it('deletes the metadata file if something went wrong writing the file.', async(): Promise<void> => {
|
||||
@ -206,7 +208,7 @@ describe('A FileDataAccessor', (): void => {
|
||||
};
|
||||
metadata.add('likes', 'apples');
|
||||
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
|
||||
.rejects.toThrow(new Error('error'));
|
||||
.rejects.toThrow('error');
|
||||
expect(cache.data['resource.meta']).toBeUndefined();
|
||||
});
|
||||
|
||||
@ -246,7 +248,7 @@ describe('A FileDataAccessor', (): void => {
|
||||
|
||||
metadata.contentType = 'text/plain';
|
||||
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata))
|
||||
.rejects.toThrow(new Error('error'));
|
||||
.rejects.toThrow('error');
|
||||
});
|
||||
});
|
||||
|
||||
@ -270,7 +272,7 @@ describe('A FileDataAccessor', (): void => {
|
||||
jest.requireMock('fs').promises.mkdir = (): any => {
|
||||
throw new Error('error');
|
||||
};
|
||||
await expect(accessor.writeContainer({ path: base }, metadata)).rejects.toThrow(new Error('error'));
|
||||
await expect(accessor.writeContainer({ path: base }, metadata)).rejects.toThrow('error');
|
||||
});
|
||||
|
||||
it('writes metadata to the corresponding metadata file.', async(): Promise<void> => {
|
||||
|
@ -42,7 +42,7 @@ describe('An InMemoryDataAccessor', (): void => {
|
||||
|
||||
it('throws an error if part of the path matches a document.', async(): Promise<void> => {
|
||||
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
|
||||
await expect(accessor.getData({ path: `${base}resource/resource2` })).rejects.toThrow(new Error('Invalid path.'));
|
||||
await expect(accessor.getData({ path: `${base}resource/resource2` })).rejects.toThrow('Invalid path.');
|
||||
});
|
||||
|
||||
it('returns the corresponding data every time.', async(): Promise<void> => {
|
||||
@ -158,7 +158,7 @@ describe('An InMemoryDataAccessor', (): void => {
|
||||
await expect(accessor.writeDocument({ path: `${base}resource` }, data, metadata)).resolves.toBeUndefined();
|
||||
|
||||
await expect(accessor.writeContainer({ path: `${base}resource/container` }, metadata))
|
||||
.rejects.toThrow(new Error('Invalid path.'));
|
||||
.rejects.toThrow('Invalid path.');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -7,9 +7,9 @@ import type { Quad } from 'rdf-js';
|
||||
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
|
||||
import { SparqlDataAccessor } from '../../../../src/storage/accessors/SparqlDataAccessor';
|
||||
import { INTERNAL_QUADS } from '../../../../src/util/ContentTypes';
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
import { ConflictHttpError } from '../../../../src/util/errors/ConflictHttpError';
|
||||
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
|
||||
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
||||
import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/UnsupportedMediaTypeHttpError';
|
||||
import type { Guarded } from '../../../../src/util/GuardedStream';
|
||||
import { SingleRootIdentifierStrategy } from '../../../../src/util/identifiers/SingleRootIdentifierStrategy';
|
||||
@ -235,16 +235,18 @@ describe('A SparqlDataAccessor', (): void => {
|
||||
});
|
||||
|
||||
it('errors when trying to write to a metadata document.', async(): Promise<void> => {
|
||||
await expect(accessor.writeDocument({ path: 'meta:http://test.com/container/resource' }, data, metadata))
|
||||
.rejects.toThrow(new ConflictHttpError('Not allowed to create NamedNodes with the metadata extension.'));
|
||||
const result = accessor.writeDocument({ path: 'meta:http://test.com/container/resource' }, data, metadata);
|
||||
await expect(result).rejects.toThrow(ConflictHttpError);
|
||||
await expect(result).rejects.toThrow('Not allowed to create NamedNodes with the metadata extension.');
|
||||
});
|
||||
|
||||
it('errors when writing triples in a non-default graph.', async(): Promise<void> => {
|
||||
data = guardedStreamFrom(
|
||||
[ quad(namedNode('http://name'), namedNode('http://pred'), literal('value'), namedNode('badGraph!')) ],
|
||||
);
|
||||
await expect(accessor.writeDocument({ path: 'http://test.com/container/resource' }, data, metadata))
|
||||
.rejects.toThrow(new BadRequestHttpError('Only triples in the default graph are supported.'));
|
||||
const result = accessor.writeDocument({ path: 'http://test.com/container/resource' }, data, metadata);
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Only triples in the default graph are supported.');
|
||||
});
|
||||
|
||||
it('errors when the SPARQL endpoint fails during reading.', async(): Promise<void> => {
|
||||
|
@ -25,8 +25,9 @@ describe('A ContentTypeReplacer', (): void => {
|
||||
const representation = { metadata };
|
||||
const preferences = { type: { 'application/json': 1 }};
|
||||
|
||||
await expect(converter.canHandle({ representation, preferences } as any))
|
||||
.rejects.toThrow(new Error('Cannot convert from text/plain to application/json'));
|
||||
const result = converter.canHandle({ representation, preferences } as any);
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Cannot convert from text/plain to application/json');
|
||||
});
|
||||
|
||||
it('throws on an unsupported output type.', async(): Promise<void> => {
|
||||
@ -34,8 +35,9 @@ describe('A ContentTypeReplacer', (): void => {
|
||||
const representation = { metadata };
|
||||
const preferences = { type: { 'application/json': 1 }};
|
||||
|
||||
await expect(converter.canHandle({ representation, preferences } as any))
|
||||
.rejects.toThrow(new Error('Cannot convert from application/n-triples to application/json'));
|
||||
const result = converter.canHandle({ representation, preferences } as any);
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Cannot convert from application/n-triples to application/json');
|
||||
});
|
||||
|
||||
it('does not replace when no content type is given.', async(): Promise<void> => {
|
||||
@ -43,8 +45,9 @@ describe('A ContentTypeReplacer', (): void => {
|
||||
const representation = { binary, data, metadata };
|
||||
const preferences = { type: { 'application/json': 1 }};
|
||||
|
||||
await expect(converter.canHandle({ representation, preferences } as any))
|
||||
.rejects.toThrow(new NotImplementedHttpError('Cannot convert from unknown to application/json'));
|
||||
const result = converter.canHandle({ representation, preferences } as any);
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Cannot convert from unknown to application/json');
|
||||
});
|
||||
|
||||
it('replaces a supported content type when no preferences are given.', async(): Promise<void> => {
|
||||
|
@ -59,8 +59,8 @@ describe('ConversionUtil', (): void => {
|
||||
|
||||
it('errors if there invalid types.', async(): Promise<void> => {
|
||||
const preferences: ValuePreferences = { 'b/x': 1 };
|
||||
expect((): any => matchingMediaTypes(preferences, { noType: 1 }))
|
||||
.toThrow(new InternalServerError(`Unexpected type preference: noType`));
|
||||
expect((): any => matchingMediaTypes(preferences, { noType: 1 })).toThrow(InternalServerError);
|
||||
expect((): any => matchingMediaTypes(preferences, { noType: 1 })).toThrow('Unexpected type preference: noType');
|
||||
});
|
||||
|
||||
it('filters out internal types.', async(): Promise<void> => {
|
||||
|
@ -16,13 +16,15 @@ describe('An BaseFileIdentifierMapper', (): void => {
|
||||
});
|
||||
|
||||
it('throws 404 if the relative path does not start with a slash.', async(): Promise<void> => {
|
||||
await expect(mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` }))
|
||||
.rejects.toThrow(new BadRequestHttpError('URL needs a / after the base'));
|
||||
const result = mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` });
|
||||
await expect(result).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(result).rejects.toThrow('URL needs a / after the base');
|
||||
});
|
||||
|
||||
it('throws 400 if the input path contains relative parts.', async(): Promise<void> => {
|
||||
await expect(mapper.mapUrlToFilePath({ path: `${base}test/../test2` }))
|
||||
.rejects.toThrow(new BadRequestHttpError('Disallowed /.. segment in URL'));
|
||||
const result = mapper.mapUrlToFilePath({ path: `${base}test/../test2` });
|
||||
await expect(result).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(result).rejects.toThrow('Disallowed /.. segment in URL');
|
||||
});
|
||||
|
||||
it('returns the corresponding file path for container identifiers.', async(): Promise<void> => {
|
||||
|
@ -5,6 +5,7 @@ import {
|
||||
} from '../../../../src/storage/mapping/ExtensionBasedMapper';
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
|
||||
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
||||
import { trimTrailingSlashes } from '../../../../src/util/PathUtil';
|
||||
|
||||
jest.mock('fs');
|
||||
@ -29,13 +30,15 @@ describe('An ExtensionBasedMapper', (): void => {
|
||||
});
|
||||
|
||||
it('throws 404 if the relative path does not start with a slash.', async(): Promise<void> => {
|
||||
await expect(mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` }))
|
||||
.rejects.toThrow(new BadRequestHttpError('URL needs a / after the base'));
|
||||
const result = mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` });
|
||||
await expect(result).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(result).rejects.toThrow('URL needs a / after the base');
|
||||
});
|
||||
|
||||
it('throws 400 if the input path contains relative parts.', async(): Promise<void> => {
|
||||
await expect(mapper.mapUrlToFilePath({ path: `${base}test/../test2` }))
|
||||
.rejects.toThrow(new BadRequestHttpError('Disallowed /.. segment in URL'));
|
||||
const result = mapper.mapUrlToFilePath({ path: `${base}test/../test2` });
|
||||
await expect(result).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(result).rejects.toThrow('Disallowed /.. segment in URL');
|
||||
});
|
||||
|
||||
it('returns the corresponding file path for container identifiers.', async(): Promise<void> => {
|
||||
@ -46,8 +49,9 @@ describe('An ExtensionBasedMapper', (): void => {
|
||||
});
|
||||
|
||||
it('rejects URLs that end with "$.{extension}".', async(): Promise<void> => {
|
||||
await expect(mapper.mapUrlToFilePath({ path: `${base}test$.txt` }))
|
||||
.rejects.toThrow(new BadRequestHttpError('Identifiers cannot contain a dollar sign before their extension'));
|
||||
const result = mapper.mapUrlToFilePath({ path: `${base}test$.txt` });
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Identifiers cannot contain a dollar sign before their extension');
|
||||
});
|
||||
|
||||
it('determines content-type by extension when looking in a folder that does not exist.', async(): Promise<void> => {
|
||||
@ -104,9 +108,10 @@ describe('An ExtensionBasedMapper', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
it('throws 400 if the given content-type is not recognized.', async(): Promise<void> => {
|
||||
await expect(mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'fake/data'))
|
||||
.rejects.toThrow(new BadRequestHttpError(`Unsupported content type fake/data`));
|
||||
it('throws 501 if the given content-type is not recognized.', async(): Promise<void> => {
|
||||
const result = mapper.mapUrlToFilePath({ path: `${base}test.txt` }, 'fake/data');
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('Unsupported content type fake/data');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -17,13 +17,15 @@ describe('An FixedContentTypeMapper', (): void => {
|
||||
});
|
||||
|
||||
it('throws 404 if the relative path does not start with a slash.', async(): Promise<void> => {
|
||||
await expect(mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` }))
|
||||
.rejects.toThrow(new BadRequestHttpError('URL needs a / after the base'));
|
||||
const result = mapper.mapUrlToFilePath({ path: `${trimTrailingSlashes(base)}test` });
|
||||
await expect(result).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(result).rejects.toThrow('URL needs a / after the base');
|
||||
});
|
||||
|
||||
it('throws 400 if the input path contains relative parts.', async(): Promise<void> => {
|
||||
await expect(mapper.mapUrlToFilePath({ path: `${base}test/../test2` }))
|
||||
.rejects.toThrow(new BadRequestHttpError('Disallowed /.. segment in URL'));
|
||||
const result = mapper.mapUrlToFilePath({ path: `${base}test/../test2` });
|
||||
await expect(result).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(result).rejects.toThrow('Disallowed /.. segment in URL');
|
||||
});
|
||||
|
||||
it('returns the corresponding file path for container identifiers.', async(): Promise<void> => {
|
||||
|
@ -1,6 +1,7 @@
|
||||
import type { ResourceStore } from '../../../../src/storage/ResourceStore';
|
||||
import { RegexRouterRule } from '../../../../src/storage/routing/RegexRouterRule';
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
import { NotImplementedHttpError } from '../../../../src/util/errors/NotImplementedHttpError';
|
||||
|
||||
describe('A RegexRouterRule', (): void => {
|
||||
const base = 'http://test.com/';
|
||||
@ -8,14 +9,16 @@ describe('A RegexRouterRule', (): void => {
|
||||
|
||||
it('rejects identifiers not containing the base.', async(): Promise<void> => {
|
||||
const router = new RegexRouterRule(base, {});
|
||||
await expect(router.canHandle({ identifier: { path: 'http://notTest.com/apple' }}))
|
||||
.rejects.toThrow(new BadRequestHttpError(`Identifiers need to start with http://test.com`));
|
||||
const result = router.canHandle({ identifier: { path: 'http://notTest.com/apple' }});
|
||||
await expect(result).rejects.toThrow(BadRequestHttpError);
|
||||
await expect(result).rejects.toThrow('Identifiers need to start with http://test.com');
|
||||
});
|
||||
|
||||
it('rejects identifiers not matching any regex.', async(): Promise<void> => {
|
||||
const router = new RegexRouterRule(base, { pear: store });
|
||||
await expect(router.canHandle({ identifier: { path: `${base}apple/` }}))
|
||||
.rejects.toThrow(new BadRequestHttpError(`No stored regexes match http://test.com/apple/`));
|
||||
const result = router.canHandle({ identifier: { path: `${base}apple/` }});
|
||||
await expect(result).rejects.toThrow(NotImplementedHttpError);
|
||||
await expect(result).rejects.toThrow('No stored regexes match http://test.com/apple/');
|
||||
});
|
||||
|
||||
it('accepts identifiers matching any regex.', async(): Promise<void> => {
|
||||
|
@ -27,7 +27,7 @@ describe('StreamUtil', (): void => {
|
||||
};
|
||||
const output = new PassThrough();
|
||||
const piped = pipeSafely(input, output);
|
||||
await expect(readableToString(piped)).rejects.toThrow(new Error('error'));
|
||||
await expect(readableToString(piped)).rejects.toThrow('error');
|
||||
});
|
||||
|
||||
it('supports mapping errors to something else.', async(): Promise<void> => {
|
||||
@ -38,7 +38,7 @@ describe('StreamUtil', (): void => {
|
||||
};
|
||||
const output = new PassThrough();
|
||||
const piped = pipeSafely(input, output, (): any => new Error('other error'));
|
||||
await expect(readableToString(piped)).rejects.toThrow(new Error('other error'));
|
||||
await expect(readableToString(piped)).rejects.toThrow('other error');
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user