fix: Test error classes correctly

This commit is contained in:
Joachim Van Herwegen
2021-01-28 17:16:11 +01:00
parent a57105be8e
commit c29928c32c
18 changed files with 110 additions and 78 deletions

View File

@@ -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');
});
});