|
@@ -2,9 +2,9 @@ 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 {HookType} 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 {RouterHookType} from './hook-registry.js';
|
|
|
import {createResponseMock} from '../utils/index.js';
|
|
import {createResponseMock} from '../utils/index.js';
|
|
|
|
|
|
|
|
describe('HookInvoker', function () {
|
|
describe('HookInvoker', function () {
|
|
@@ -13,7 +13,11 @@ 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, HookType.PRE_HANDLER, res);
|
|
|
|
|
|
|
+ s.invokeAndContinueUntilValueReceived(
|
|
|
|
|
+ v,
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
|
|
+ res,
|
|
|
|
|
+ );
|
|
|
const error = v =>
|
|
const error = v =>
|
|
|
format(
|
|
format(
|
|
|
'The parameter "route" of ' +
|
|
'The parameter "route" of ' +
|
|
@@ -66,7 +70,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(HookType.PRE_HANDLER)();
|
|
|
|
|
|
|
+ throwable(RouterHookType.PRE_HANDLER)();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('requires the parameter "hookType" to be a supported hook', function () {
|
|
it('requires the parameter "hookType" to be a supported hook', function () {
|
|
@@ -77,7 +81,7 @@ describe('HookInvoker', function () {
|
|
|
handler: () => undefined,
|
|
handler: () => undefined,
|
|
|
});
|
|
});
|
|
|
const res = createResponseMock();
|
|
const res = createResponseMock();
|
|
|
- Object.values(HookType).forEach(type =>
|
|
|
|
|
|
|
+ Object.values(RouterHookType).forEach(type =>
|
|
|
s.invokeAndContinueUntilValueReceived(route, type, res),
|
|
s.invokeAndContinueUntilValueReceived(route, type, res),
|
|
|
);
|
|
);
|
|
|
const throwable = () =>
|
|
const throwable = () =>
|
|
@@ -93,7 +97,11 @@ describe('HookInvoker', function () {
|
|
|
handler: () => undefined,
|
|
handler: () => undefined,
|
|
|
});
|
|
});
|
|
|
const throwable = v => () =>
|
|
const throwable = v => () =>
|
|
|
- s.invokeAndContinueUntilValueReceived(route, HookType.PRE_HANDLER, v);
|
|
|
|
|
|
|
+ s.invokeAndContinueUntilValueReceived(
|
|
|
|
|
+ route,
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
|
|
+ v,
|
|
|
|
|
+ );
|
|
|
const error = v =>
|
|
const error = v =>
|
|
|
format(
|
|
format(
|
|
|
'The parameter "response" of ' +
|
|
'The parameter "response" of ' +
|
|
@@ -117,10 +125,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(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -138,7 +146,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
s.invokeAndContinueUntilValueReceived(
|
|
s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(order).to.be.eql([
|
|
expect(order).to.be.eql([
|
|
@@ -153,14 +161,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(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
return ret;
|
|
return ret;
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook3');
|
|
order.push('globalHook3');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -178,7 +186,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(result).to.be.eq(ret);
|
|
expect(result).to.be.eq(ret);
|
|
@@ -189,10 +197,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(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -214,7 +222,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(result).to.be.eq(ret);
|
|
expect(result).to.be.eq(ret);
|
|
@@ -230,14 +238,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(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
res._headersSent = true;
|
|
res._headersSent = true;
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook3');
|
|
order.push('globalHook3');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -255,7 +263,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
res,
|
|
res,
|
|
|
);
|
|
);
|
|
|
expect(result).to.be.eq(res);
|
|
expect(result).to.be.eq(res);
|
|
@@ -266,10 +274,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(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -291,7 +299,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
const result = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
res,
|
|
res,
|
|
|
);
|
|
);
|
|
|
expect(result).to.be.eq(res);
|
|
expect(result).to.be.eq(res);
|
|
@@ -306,13 +314,16 @@ 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(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, async () => {
|
|
|
|
|
- order.push('globalHook2');
|
|
|
|
|
- });
|
|
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
|
|
+ async () => {
|
|
|
|
|
+ order.push('globalHook2');
|
|
|
|
|
+ },
|
|
|
|
|
+ );
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook3');
|
|
order.push('globalHook3');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -330,7 +341,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(promise).to.be.instanceof(Promise);
|
|
expect(promise).to.be.instanceof(Promise);
|
|
@@ -347,12 +358,18 @@ 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(HookType.PRE_HANDLER, async () => {
|
|
|
|
|
- order.push('globalHook1');
|
|
|
|
|
- });
|
|
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, async () => {
|
|
|
|
|
- order.push('globalHook2');
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
|
|
+ async () => {
|
|
|
|
|
+ order.push('globalHook1');
|
|
|
|
|
+ },
|
|
|
|
|
+ );
|
|
|
|
|
+ s.getService(HookRegistry).addHook(
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
|
|
+ async () => {
|
|
|
|
|
+ order.push('globalHook2');
|
|
|
|
|
+ },
|
|
|
|
|
+ );
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
|
method: HttpMethod.GET,
|
|
method: HttpMethod.GET,
|
|
|
path: '/',
|
|
path: '/',
|
|
@@ -368,7 +385,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(promise).to.be.instanceof(Promise);
|
|
expect(promise).to.be.instanceof(Promise);
|
|
@@ -384,10 +401,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(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -408,7 +425,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(promise).to.be.instanceof(Promise);
|
|
expect(promise).to.be.instanceof(Promise);
|
|
@@ -425,10 +442,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(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook1');
|
|
order.push('globalHook1');
|
|
|
});
|
|
});
|
|
|
- s.getService(HookRegistry).addHook(HookType.PRE_HANDLER, () => {
|
|
|
|
|
|
|
+ s.getService(HookRegistry).addHook(RouterHookType.PRE_HANDLER, () => {
|
|
|
order.push('globalHook2');
|
|
order.push('globalHook2');
|
|
|
});
|
|
});
|
|
|
const route = new Route({
|
|
const route = new Route({
|
|
@@ -446,7 +463,7 @@ describe('HookInvoker', function () {
|
|
|
});
|
|
});
|
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
const promise = s.invokeAndContinueUntilValueReceived(
|
|
|
route,
|
|
route,
|
|
|
- HookType.PRE_HANDLER,
|
|
|
|
|
|
|
+ RouterHookType.PRE_HANDLER,
|
|
|
createResponseMock(),
|
|
createResponseMock(),
|
|
|
);
|
|
);
|
|
|
expect(promise).to.be.instanceof(Promise);
|
|
expect(promise).to.be.instanceof(Promise);
|