Co-authored-by: Andrew Eisenberg <aeisenberg@github.com> Co-authored-by: Henry Mercer <henrymercer@github.com>
76 lines
2.5 KiB
JavaScript
76 lines
2.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const hooks_1 = require("../hooks");
|
|
const errors_1 = require("../errors");
|
|
const request_1 = require("../request");
|
|
describe("Hooks behaviour", () => {
|
|
it("can chain multiple hooks together", () => {
|
|
const hook1Spy = jest.fn();
|
|
const hooks1 = {
|
|
requestReceived: (ctx) => {
|
|
hook1Spy("received");
|
|
},
|
|
requestRouted: (ctx) => {
|
|
hook1Spy("routed");
|
|
},
|
|
requestPrepared: (ctx) => {
|
|
hook1Spy("prepared");
|
|
},
|
|
requestSent: (ctx) => {
|
|
hook1Spy("sent");
|
|
},
|
|
responseSent: (ctx) => {
|
|
hook1Spy("sent");
|
|
},
|
|
responsePrepared: (ctx) => {
|
|
hook1Spy("prepared");
|
|
},
|
|
error: (ctx, err) => {
|
|
hook1Spy("error");
|
|
}
|
|
};
|
|
const hook2Spy = jest.fn();
|
|
const hooks2 = {
|
|
requestReceived: (ctx) => {
|
|
hook2Spy("received");
|
|
},
|
|
requestRouted: (ctx) => {
|
|
hook2Spy("routed");
|
|
},
|
|
requestPrepared: (ctx) => {
|
|
hook2Spy("prepared");
|
|
},
|
|
requestSent: (ctx) => {
|
|
hook2Spy("sent");
|
|
},
|
|
responseSent: (ctx) => {
|
|
hook2Spy("sent");
|
|
},
|
|
responsePrepared: (ctx) => {
|
|
hook2Spy("prepared");
|
|
},
|
|
error: (ctx, err) => {
|
|
hook2Spy("error");
|
|
}
|
|
};
|
|
const emptyHook = {};
|
|
const chainedHook = hooks_1.chainHooks(hooks1, hooks2, emptyHook);
|
|
expect(chainedHook).not.toBeNull();
|
|
const hookNames = ["requestReceived", "requestRouted", "requestPrepared", "requestSent", "responseSent", "responsePrepared", "error"];
|
|
hookNames.map(hookName => {
|
|
const ctx = {
|
|
req: jest.fn(),
|
|
res: jest.fn(),
|
|
contentType: request_1.TwirpContentType.Unknown,
|
|
packageName: "",
|
|
serviceName: "",
|
|
methodName: "",
|
|
};
|
|
const hook = chainedHook[hookName];
|
|
if (!hook) {
|
|
throw new Error(`hook ${hookName} must be present`);
|
|
}
|
|
hook(ctx, new errors_1.InternalServerError("test"));
|
|
});
|
|
});
|
|
});
|