@senacor/azure-function-middleware
Version:
Middleware for azure functions to handle authentication, authorization, error handling and logging
38 lines (37 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const functions_1 = require("@azure/functions");
const ApplicationError_1 = require("./ApplicationError");
const errorHandler_1 = require("./errorHandler");
describe('Error-Handler should', () => {
const contextMock = new functions_1.InvocationContext();
beforeEach(() => {
jest.restoreAllMocks();
});
test('return an provided ApplicationError', () => {
const res = (0, errorHandler_1.errorHandler)(new ApplicationError_1.ApplicationError('', 400, { error: 'critical' }), contextMock);
expect(res.status).toStrictEqual(400);
expect(res.jsonBody).toStrictEqual({ error: 'critical' });
});
test('return an default error-message, if no errorResponseHandler was provided', () => {
const res = (0, errorHandler_1.errorHandler)('Error!', contextMock);
expect(res.status).toStrictEqual(500);
expect(res.jsonBody).toStrictEqual({ message: 'Internal server error' });
});
test('return error as body, if body of the ApplicationError is not an object', () => {
const res = (0, errorHandler_1.errorHandler)(new ApplicationError_1.ApplicationError('', 400, 'critical'), contextMock);
expect(res.status).toStrictEqual(400);
expect(res.body).toStrictEqual('critical');
});
test('use the errorResponseHandler, if an errorResponseHandler was provided', () => {
const errorResponseHandler = () => ({
status: 409,
body: {
message: 'conflict!',
},
});
const res = (0, errorHandler_1.errorHandler)('Error!', contextMock, { errorResponseHandler });
expect(res.status).toStrictEqual(409);
expect(res.body).toStrictEqual({ message: 'conflict!' });
});
});