mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Add BooleanHandler
Co-Authored-By: Ludovico Granata <Ludogranata@gmail.com>
This commit is contained in:
committed by
Joachim Van Herwegen
parent
0355673a0f
commit
73867f0827
56
test/unit/util/handlers/BooleanHandler.test.ts
Normal file
56
test/unit/util/handlers/BooleanHandler.test.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
|
||||
import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler';
|
||||
import { BooleanHandler } from '../../../../src/util/handlers/BooleanHandler';
|
||||
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';
|
||||
|
||||
describe('A BooleanHandler', (): void => {
|
||||
let handlerFalse: AsyncHandler<any, false>;
|
||||
let handlerTrue: AsyncHandler<any, true>;
|
||||
let handlerError: AsyncHandler<any, never>;
|
||||
let handlerCanNotHandle: AsyncHandler<any, any>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
handlerFalse = new StaticAsyncHandler(true, false);
|
||||
handlerTrue = new StaticAsyncHandler(true, true);
|
||||
handlerError = new StaticAsyncHandler(true, null) as any;
|
||||
handlerError.handle = (): never => {
|
||||
throw new InternalServerError();
|
||||
};
|
||||
handlerCanNotHandle = new StaticAsyncHandler(false, null);
|
||||
});
|
||||
|
||||
it('can handle the input if any of its handlers can.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerFalse, handlerCanNotHandle ]);
|
||||
await expect(handler.canHandle(null)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it('errors if none of its handlers supports the input.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerCanNotHandle, handlerCanNotHandle ]);
|
||||
await expect(handler.canHandle(null)).rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
|
||||
it('returns true if any of its handlers returns true.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerFalse, handlerTrue, handlerCanNotHandle ]);
|
||||
await expect(handler.handle(null)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('returns false if none of its handlers returns true.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerFalse, handlerError, handlerCanNotHandle ]);
|
||||
await expect(handler.handle(null)).resolves.toBe(false);
|
||||
});
|
||||
|
||||
it('throw an internal error when calling handle with unsupported input.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerCanNotHandle, handlerCanNotHandle ]);
|
||||
await expect(handler.handle(null)).rejects.toThrow(InternalServerError);
|
||||
});
|
||||
|
||||
it('returns the same handle results with handleSafe.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerFalse, handlerTrue, handlerCanNotHandle ]);
|
||||
await expect(handler.handleSafe(null)).resolves.toBe(true);
|
||||
});
|
||||
|
||||
it('throws the canHandle error when calling handleSafe with unsupported input.', async(): Promise<void> => {
|
||||
const handler = new BooleanHandler([ handlerCanNotHandle, handlerCanNotHandle ]);
|
||||
await expect(handler.handleSafe(null)).rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
});
|
||||
84
test/unit/util/handlers/HandlerUtil.test.ts
Normal file
84
test/unit/util/handlers/HandlerUtil.test.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { HttpError } from '../../../../src/util/errors/HttpError';
|
||||
import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler';
|
||||
import { createAggregateError, filterHandlers, findHandler } from '../../../../src/util/handlers/HandlerUtil';
|
||||
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';
|
||||
|
||||
describe('HandlerUtil', (): void => {
|
||||
describe('createAggregateError', (): void => {
|
||||
const error401 = new HttpError(401, 'UnauthorizedHttpError');
|
||||
const error415 = new HttpError(415, 'UnsupportedMediaTypeHttpError');
|
||||
const error501 = new HttpError(501, 'NotImplementedHttpError');
|
||||
const error = new Error('noStatusCode');
|
||||
|
||||
it('throws an error with matching status code if all errors have the same.', async(): Promise<void> => {
|
||||
expect(createAggregateError([ error401, error401 ])).toMatchObject({
|
||||
statusCode: 401,
|
||||
name: 'UnauthorizedHttpError',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an InternalServerError if one of the errors has status code 5xx.', async(): Promise<void> => {
|
||||
expect(createAggregateError([ error401, error501 ])).toMatchObject({
|
||||
statusCode: 500,
|
||||
name: 'InternalServerError',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an BadRequestHttpError if all handlers have 4xx status codes.', async(): Promise<void> => {
|
||||
expect(createAggregateError([ error401, error415 ])).toMatchObject({
|
||||
statusCode: 400,
|
||||
name: 'BadRequestHttpError',
|
||||
});
|
||||
});
|
||||
|
||||
it('interprets non-HTTP errors as internal errors.', async(): Promise<void> => {
|
||||
expect(createAggregateError([ error ])).toMatchObject({
|
||||
statusCode: 500,
|
||||
name: 'InternalServerError',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('findHandler', (): void => {
|
||||
let handlerTrue: AsyncHandler<any, any>;
|
||||
let handlerFalse: AsyncHandler<any, any>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
handlerTrue = new StaticAsyncHandler(true, null);
|
||||
handlerFalse = new StaticAsyncHandler(false, null);
|
||||
});
|
||||
|
||||
it('finds a matching handler.', async(): Promise<void> => {
|
||||
await expect(findHandler([ handlerFalse, handlerTrue ], null)).resolves.toBe(handlerTrue);
|
||||
});
|
||||
|
||||
it('errors if there is no matching handler.', async(): Promise<void> => {
|
||||
await expect(findHandler([ handlerFalse, handlerFalse ], null)).rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
|
||||
it('supports non-native Errors.', async(): Promise<void> => {
|
||||
handlerFalse.canHandle = jest.fn().mockRejectedValue('apple');
|
||||
await expect(findHandler([ handlerFalse ], null)).rejects.toThrow('[Unknown error: apple]');
|
||||
});
|
||||
});
|
||||
|
||||
describe('filterHandlers', (): void => {
|
||||
let handlerTrue: AsyncHandler<any, any>;
|
||||
let handlerFalse: AsyncHandler<any, any>;
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
handlerTrue = new StaticAsyncHandler(true, null);
|
||||
handlerFalse = new StaticAsyncHandler(false, null);
|
||||
});
|
||||
|
||||
it('finds matching handlers.', async(): Promise<void> => {
|
||||
await expect(filterHandlers([ handlerTrue, handlerFalse, handlerTrue ], null))
|
||||
.resolves.toEqual([ handlerTrue, handlerTrue ]);
|
||||
});
|
||||
|
||||
it('errors if there is no matching handler.', async(): Promise<void> => {
|
||||
await expect(filterHandlers([ handlerFalse, handlerFalse ], null))
|
||||
.rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,3 @@
|
||||
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
|
||||
import { HttpError } from '../../../../src/util/errors/HttpError';
|
||||
import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler';
|
||||
import { WaterfallHandler } from '../../../../src/util/handlers/WaterfallHandler';
|
||||
import { StaticAsyncHandler } from '../../../util/StaticAsyncHandler';
|
||||
@@ -83,44 +81,5 @@ describe('A WaterfallHandler', (): void => {
|
||||
|
||||
await expect(handler.handleSafe(null)).rejects.toThrow('[Not supported, Not supported]');
|
||||
});
|
||||
|
||||
it('throws an error with matching status code if all handlers threw the same.', async(): Promise<void> => {
|
||||
handlerTrue.canHandle = async(): Promise<void> => {
|
||||
throw new HttpError(401, 'UnauthorizedHttpError');
|
||||
};
|
||||
const handler = new WaterfallHandler([ handlerTrue, handlerTrue ]);
|
||||
|
||||
await expect(handler.canHandle(null)).rejects.toMatchObject({
|
||||
statusCode: 401,
|
||||
name: 'UnauthorizedHttpError',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an internal server error if one of the handlers threw one.', async(): Promise<void> => {
|
||||
handlerTrue.canHandle = async(): Promise<void> => {
|
||||
throw new HttpError(401, 'UnauthorizedHttpError');
|
||||
};
|
||||
handlerFalse.canHandle = async(): Promise<void> => {
|
||||
throw new Error('Server is crashing!');
|
||||
};
|
||||
const handler = new WaterfallHandler([ handlerTrue, handlerFalse ]);
|
||||
|
||||
await expect(handler.canHandle(null)).rejects.toMatchObject({
|
||||
statusCode: 500,
|
||||
name: 'InternalServerError',
|
||||
});
|
||||
});
|
||||
|
||||
it('throws an BadRequestHttpError if handlers throw different errors.', async(): Promise<void> => {
|
||||
handlerTrue.canHandle = async(): Promise<void> => {
|
||||
throw new HttpError(401, 'UnauthorizedHttpError');
|
||||
};
|
||||
handlerFalse.canHandle = async(): Promise<void> => {
|
||||
throw new HttpError(415, 'UnsupportedMediaTypeHttpError');
|
||||
};
|
||||
const handler = new WaterfallHandler([ handlerTrue, handlerFalse ]);
|
||||
|
||||
await expect(handler.canHandle(null)).rejects.toThrow(BadRequestHttpError);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user