|
@@ -2,7 +2,7 @@ import {expect} from '../chai.js';
|
|
|
import {Route} from '../route.js';
|
|
import {Route} from '../route.js';
|
|
|
import {HttpMethod} from '../route.js';
|
|
import {HttpMethod} from '../route.js';
|
|
|
import {format} from '@e22m4u/js-format';
|
|
import {format} from '@e22m4u/js-format';
|
|
|
-import {HOOK_NAME} from './hook-registry.js';
|
|
|
|
|
|
|
+import {HookName} from './hook-registry.js';
|
|
|
import {HookInvoker} from './hook-invoker.js';
|
|
import {HookInvoker} from './hook-invoker.js';
|
|
|
import {HookRegistry} from './hook-registry.js';
|
|
import {HookRegistry} from './hook-registry.js';
|
|
|
import {createResponseMock} from '../utils/index.js';
|
|
import {createResponseMock} from '../utils/index.js';
|
|
@@ -13,7 +13,7 @@ describe('HookInvoker', function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
const throwable = v => () =>
|
|
const throwable = v => () =>
|
|
|
- s.invokeAndContinueUntilValueReceived(v, HOOK_NAME.PRE_HANDLER, res);
|
|
|
|
|
|
|
+ s.invokeAndContinueUntilValueReceived(v, HookName.PRE_HANDLER, res);
|
|
|
const error = v =>
|
|
const error = v =>
|
|
|
format(
|
|
format(
|
|
|
'The parameter "route" of ' +
|
|
'The parameter "route" of ' +
|
|
@@ -66,7 +66,7 @@ describe('HookInvoker', function () {
|
|
|
expect(throwable({})).to.throw(error('Object'));
|
|
expect(throwable({})).to.throw(error('Object'));
|
|
|
expect(throwable([])).to.throw(error('Array'));
|
|
expect(throwable([])).to.throw(error('Array'));
|
|
|
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
|
- throwable(HOOK_NAME.PRE_HANDLER)();
|
|
|
|
|
|
|
+ throwable(HookName.PRE_HANDLER)();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('requires the parameter "hookName" to be a supported hook', function () {
|
|
it('requires the parameter "hookName" to be a supported hook', function () {
|
|
@@ -77,7 +77,7 @@ describe('HookInvoker', function () {
|
|
|
handler: () => undefined,
|
|
handler: () => undefined,
|
|
|
});
|
|
});
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
- Object.values(HOOK_NAME).forEach(name =>
|
|
|
|
|
|
|
+ Object.values(HookName).forEach(name =>
|
|
|
s.invokeAndContinueUntilValueReceived(route, name, res),
|
|
s.invokeAndContinueUntilValueReceived(route, name, res),
|
|
|
);
|
|
);
|
|
|
const throwable = () =>
|
|
const throwable = () =>
|
|
@@ -93,7 +93,7 @@ describe('HookInvoker', function () {
|
|
|
handler: () => undefined,
|
|
handler: () => undefined,
|
|
|
});
|
|
});
|
|
|
const throwable = v => () =>
|
|
const throwable = v => () =>
|
|
|
- s.invokeAndContinueUntilValueReceived(route, HOOK_NAME.PRE_HANDLER, v);
|
|
|
|
|
|
|
+ s.invokeAndContinueUntilValueReceived(route, HookName.PRE_HANDLER, v);
|
|
|
const error = v =>
|
|
const error = v =>
|
|
|
format(
|
|
format(
|
|
|
'The parameter "response" of ' +
|
|
'The parameter "response" of ' +
|
|
@@ -117,10 +117,10 @@ describe('HookInvoker', function () {
|
|
|
it('invokes global hooks in priority', function () {
|
|
it('invokes global hooks in priority', function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -138,7 +138,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
s.invokeAndContinueUntilValueReceived(
|
|
s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(order).to.be.eql([
|
|
expect(order).to.be.eql([
|
|
@@ -153,14 +153,14 @@ describe('HookInvoker', function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const ret = 'OK';
|
|
const ret = 'OK';
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
return ret;
|
|
return ret;
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook3');
|
|
order.push('globalHook3');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -178,7 +178,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(result).to.be.eq(ret);
|
|
expect(result).to.be.eq(ret);
|
|
@@ -189,10 +189,10 @@ describe('HookInvoker', function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const ret = 'OK';
|
|
const ret = 'OK';
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -214,7 +214,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(result).to.be.eq(ret);
|
|
expect(result).to.be.eq(ret);
|
|
@@ -230,14 +230,14 @@ describe('HookInvoker', function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
res._headersSent = true;
|
|
res._headersSent = true;
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook3');
|
|
order.push('globalHook3');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -255,7 +255,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
res,
|
|
res,
|
|
|
);
|
|
);
|
|
|
expect(result).to.be.eq(res);
|
|
expect(result).to.be.eq(res);
|
|
@@ -266,10 +266,10 @@ describe('HookInvoker', function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -291,7 +291,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
res,
|
|
res,
|
|
|
);
|
|
);
|
|
|
expect(result).to.be.eq(res);
|
|
expect(result).to.be.eq(res);
|
|
@@ -306,13 +306,13 @@ describe('HookInvoker', function () {
|
|
|
it('returns a Promise if any global hook is asynchronous', async function () {
|
|
it('returns a Promise if any global hook is asynchronous', async function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, async () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, async () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook3');
|
|
order.push('globalHook3');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -330,7 +330,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(promise).to.be.instanceof(Promise);
|
|
expect(promise).to.be.instanceof(Promise);
|
|
@@ -347,10 +347,10 @@ describe('HookInvoker', function () {
|
|
|
it('returns a Promise if entire global hooks are asynchronous', async function () {
|
|
it('returns a Promise if entire global hooks are asynchronous', async function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, async () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, async () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, async () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, async () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -368,7 +368,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(promise).to.be.instanceof(Promise);
|
|
expect(promise).to.be.instanceof(Promise);
|
|
@@ -384,10 +384,10 @@ describe('HookInvoker', function () {
|
|
|
it('returns a Promise if any route hook is asynchronous', async function () {
|
|
it('returns a Promise if any route hook is asynchronous', async function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -408,7 +408,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(promise).to.be.instanceof(Promise);
|
|
expect(promise).to.be.instanceof(Promise);
|
|
@@ -425,10 +425,10 @@ describe('HookInvoker', function () {
|
|
|
it('returns a Promise if entire route hooks are asynchronous', async function () {
|
|
it('returns a Promise if entire route hooks are asynchronous', async function () {
|
|
|
const s = new HookInvoker();
|
|
const s = new HookInvoker();
|
|
|
const order = [];
|
|
const order = [];
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HOOK_NAME.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(HookName.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -446,7 +446,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HOOK_NAME.PRE_HANDLER,
|
|
|
|
|
|
|
+ HookName.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(promise).to.be.instanceof(Promise);
|
|
expect(promise).to.be.instanceof(Promise);
|