From c29928c32c0d2ce5c97889edb3bd73904ab6077e Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Thu, 28 Jan 2021 17:16:11 +0100 Subject: [PATCH] fix: Test error classes correctly --- .../unit/ldp/http/metadata/SlugParser.test.ts | 5 ++-- .../SparqlPatchPermissionsExtractor.test.ts | 29 ++++++++++++------- test/unit/logging/LazyLogger.test.ts | 2 +- test/unit/logging/LazyLoggerFactory.test.ts | 4 +-- test/unit/logging/LogUtil.test.ts | 2 +- .../storage/DataAccessorBasedStore.test.ts | 27 ++++++++++------- .../unit/storage/LockingResourceStore.test.ts | 2 +- .../unit/storage/RoutingResourceStore.test.ts | 2 +- .../accessors/FileDataAccessor.test.ts | 22 +++++++------- .../accessors/InMemoryDataAccessor.test.ts | 4 +-- .../accessors/SparqlDataAccessor.test.ts | 12 ++++---- .../conversion/ContentTypeReplacer.test.ts | 15 ++++++---- .../storage/conversion/ConversionUtil.test.ts | 4 +-- .../mapping/BaseFileIdentifierMapper.test.ts | 10 ++++--- .../mapping/ExtensionBasedMapper.test.ts | 23 +++++++++------ .../mapping/FixedContentTypeMapper.test.ts | 10 ++++--- .../storage/routing/RegexRouterRule.test.ts | 11 ++++--- test/unit/util/StreamUtil.test.ts | 4 +-- 18 files changed, 110 insertions(+), 78 deletions(-) diff --git a/test/unit/ldp/http/metadata/SlugParser.test.ts b/test/unit/ldp/http/metadata/SlugParser.test.ts index d0277bbf6..d91fd0c66 100644 --- a/test/unit/ldp/http/metadata/SlugParser.test.ts +++ b/test/unit/ldp/http/metadata/SlugParser.test.ts @@ -21,8 +21,9 @@ describe('A SlugParser', (): void => { it('errors if there are multiple slug headers.', async(): Promise => { 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 => { diff --git a/test/unit/ldp/permissions/SparqlPatchPermissionsExtractor.test.ts b/test/unit/ldp/permissions/SparqlPatchPermissionsExtractor.test.ts index a3f2dbeff..0d983fac9 100644 --- a/test/unit/ldp/permissions/SparqlPatchPermissionsExtractor.test.ts +++ b/test/unit/ldp/permissions/SparqlPatchPermissionsExtractor.test.ts @@ -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 => { diff --git a/test/unit/logging/LazyLogger.test.ts b/test/unit/logging/LazyLogger.test.ts index 709b68f86..19bb8fb96 100644 --- a/test/unit/logging/LazyLogger.test.ts +++ b/test/unit/logging/LazyLogger.test.ts @@ -12,7 +12,7 @@ describe('LazyLogger', (): void => { it('throws when no logger factory is set in the lazy logger factory.', async(): Promise => { 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 => { diff --git a/test/unit/logging/LazyLoggerFactory.test.ts b/test/unit/logging/LazyLoggerFactory.test.ts index 027174704..9a03265c7 100644 --- a/test/unit/logging/LazyLoggerFactory.test.ts +++ b/test/unit/logging/LazyLoggerFactory.test.ts @@ -31,7 +31,7 @@ describe('LazyLoggerFactory', (): void => { it('throws when retrieving the inner factory if none has been set.', async(): Promise => { 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 => { @@ -60,6 +60,6 @@ describe('LazyLoggerFactory', (): void => { it('errors on invoking LazyLoggers if a factory has not been set yet.', async(): Promise => { 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.'); }); }); diff --git a/test/unit/logging/LogUtil.test.ts b/test/unit/logging/LogUtil.test.ts index b5f5b4ae3..b6650a529 100644 --- a/test/unit/logging/LogUtil.test.ts +++ b/test/unit/logging/LogUtil.test.ts @@ -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.'); }); }); diff --git a/test/unit/storage/DataAccessorBasedStore.test.ts b/test/unit/storage/DataAccessorBasedStore.test.ts index 5e55d81fb..8cc5af2f6 100644 --- a/test/unit/storage/DataAccessorBasedStore.test.ts +++ b/test/unit/storage/DataAccessorBasedStore.test.ts @@ -151,14 +151,15 @@ describe('A DataAccessorBasedStore', (): void => { accessor.getMetadata = async(): Promise => { 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 => { 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 => { @@ -326,8 +327,9 @@ describe('A DataAccessorBasedStore', (): void => { representation.data = guardedStreamFrom( [ `<${`${root}resource/`}> .` ], ); - 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 => { @@ -366,8 +368,9 @@ describe('A DataAccessorBasedStore', (): void => { describe('modifying a Representation', (): void => { it('is not supported.', async(): Promise => { - 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 => { 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 => { 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 => { diff --git a/test/unit/storage/LockingResourceStore.test.ts b/test/unit/storage/LockingResourceStore.test.ts index 07c9f8f3b..f8a24c7f8 100644 --- a/test/unit/storage/LockingResourceStore.test.ts +++ b/test/unit/storage/LockingResourceStore.test.ts @@ -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); diff --git a/test/unit/storage/RoutingResourceStore.test.ts b/test/unit/storage/RoutingResourceStore.test.ts index 4051f7e15..85457edab 100644 --- a/test/unit/storage/RoutingResourceStore.test.ts +++ b/test/unit/storage/RoutingResourceStore.test.ts @@ -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'); }); }); diff --git a/test/unit/storage/accessors/FileDataAccessor.test.ts b/test/unit/storage/accessors/FileDataAccessor.test.ts index 97899aa97..c30dada88 100644 --- a/test/unit/storage/accessors/FileDataAccessor.test.ts +++ b/test/unit/storage/accessors/FileDataAccessor.test.ts @@ -39,8 +39,9 @@ describe('A FileDataAccessor', (): void => { it('can only handle binary data.', async(): Promise => { 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 => { @@ -150,8 +151,9 @@ describe('A FileDataAccessor', (): void => { }); it('throws an error when writing to a metadata path.', async(): Promise => { - 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 => { @@ -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 => { @@ -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 => { @@ -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 => { diff --git a/test/unit/storage/accessors/InMemoryDataAccessor.test.ts b/test/unit/storage/accessors/InMemoryDataAccessor.test.ts index 8804f4c89..c6efff338 100644 --- a/test/unit/storage/accessors/InMemoryDataAccessor.test.ts +++ b/test/unit/storage/accessors/InMemoryDataAccessor.test.ts @@ -42,7 +42,7 @@ describe('An InMemoryDataAccessor', (): void => { it('throws an error if part of the path matches a document.', async(): Promise => { 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 => { @@ -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.'); }); }); diff --git a/test/unit/storage/accessors/SparqlDataAccessor.test.ts b/test/unit/storage/accessors/SparqlDataAccessor.test.ts index bd4cc14d2..f1742b55b 100644 --- a/test/unit/storage/accessors/SparqlDataAccessor.test.ts +++ b/test/unit/storage/accessors/SparqlDataAccessor.test.ts @@ -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 => { - 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 => { 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 => { diff --git a/test/unit/storage/conversion/ContentTypeReplacer.test.ts b/test/unit/storage/conversion/ContentTypeReplacer.test.ts index f35a27801..be702d384 100644 --- a/test/unit/storage/conversion/ContentTypeReplacer.test.ts +++ b/test/unit/storage/conversion/ContentTypeReplacer.test.ts @@ -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 => { @@ -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 => { @@ -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 => { diff --git a/test/unit/storage/conversion/ConversionUtil.test.ts b/test/unit/storage/conversion/ConversionUtil.test.ts index f53619e22..79b6c5501 100644 --- a/test/unit/storage/conversion/ConversionUtil.test.ts +++ b/test/unit/storage/conversion/ConversionUtil.test.ts @@ -59,8 +59,8 @@ describe('ConversionUtil', (): void => { it('errors if there invalid types.', async(): Promise => { 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 => { diff --git a/test/unit/storage/mapping/BaseFileIdentifierMapper.test.ts b/test/unit/storage/mapping/BaseFileIdentifierMapper.test.ts index dd8142b8c..f1059778a 100644 --- a/test/unit/storage/mapping/BaseFileIdentifierMapper.test.ts +++ b/test/unit/storage/mapping/BaseFileIdentifierMapper.test.ts @@ -16,13 +16,15 @@ describe('An BaseFileIdentifierMapper', (): void => { }); it('throws 404 if the relative path does not start with a slash.', async(): Promise => { - 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 => { - 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 => { diff --git a/test/unit/storage/mapping/ExtensionBasedMapper.test.ts b/test/unit/storage/mapping/ExtensionBasedMapper.test.ts index 75c357f8d..dffff0d28 100644 --- a/test/unit/storage/mapping/ExtensionBasedMapper.test.ts +++ b/test/unit/storage/mapping/ExtensionBasedMapper.test.ts @@ -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 => { - 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 => { - 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 => { @@ -46,8 +49,9 @@ describe('An ExtensionBasedMapper', (): void => { }); it('rejects URLs that end with "$.{extension}".', async(): Promise => { - 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 => { @@ -104,9 +108,10 @@ describe('An ExtensionBasedMapper', (): void => { }); }); - it('throws 400 if the given content-type is not recognized.', async(): Promise => { - 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 => { + 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'); }); }); diff --git a/test/unit/storage/mapping/FixedContentTypeMapper.test.ts b/test/unit/storage/mapping/FixedContentTypeMapper.test.ts index 985b3264b..99c7946a2 100644 --- a/test/unit/storage/mapping/FixedContentTypeMapper.test.ts +++ b/test/unit/storage/mapping/FixedContentTypeMapper.test.ts @@ -17,13 +17,15 @@ describe('An FixedContentTypeMapper', (): void => { }); it('throws 404 if the relative path does not start with a slash.', async(): Promise => { - 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 => { - 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 => { diff --git a/test/unit/storage/routing/RegexRouterRule.test.ts b/test/unit/storage/routing/RegexRouterRule.test.ts index c9a4d62d5..0f1705071 100644 --- a/test/unit/storage/routing/RegexRouterRule.test.ts +++ b/test/unit/storage/routing/RegexRouterRule.test.ts @@ -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 => { 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 => { 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 => { diff --git a/test/unit/util/StreamUtil.test.ts b/test/unit/util/StreamUtil.test.ts index f98d71f15..a9ce2dc66 100644 --- a/test/unit/util/StreamUtil.test.ts +++ b/test/unit/util/StreamUtil.test.ts @@ -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 => { @@ -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'); }); });