@miqro/core 1.7.1 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/dist/api-router.js +0 -1
  2. package/dist/app-utils.js +0 -1
  3. package/dist/app.js +0 -1
  4. package/dist/common/api-router-utils.js +0 -1
  5. package/dist/common/api-router.js +0 -1
  6. package/dist/common/config/config.js +0 -1
  7. package/dist/common/config/featuretoggle.js +0 -1
  8. package/dist/common/config/index.js +0 -1
  9. package/dist/common/config/loader.js +0 -1
  10. package/dist/common/context.js +0 -1
  11. package/dist/common/error/badrequesterror.js +0 -1
  12. package/dist/common/error/configfilenotfound.js +0 -1
  13. package/dist/common/error/forbiddenerror.js +0 -1
  14. package/dist/common/error/index.js +0 -1
  15. package/dist/common/error/notfound.js +0 -1
  16. package/dist/common/error/notimplemented.js +0 -1
  17. package/dist/common/error/unauthorizederror.js +0 -1
  18. package/dist/common/helpers.js +0 -1
  19. package/dist/common/index.js +0 -1
  20. package/dist/common/logger/constants.js +0 -1
  21. package/dist/common/logger/factory.js +0 -1
  22. package/dist/common/logger/index.js +0 -1
  23. package/dist/common/logger/interfaces.js +0 -1
  24. package/dist/common/logger/logger.js +0 -1
  25. package/dist/common/logger/transport/console.js +0 -1
  26. package/dist/common/logger/transport/file.js +0 -1
  27. package/dist/common/router-handler.js +0 -1
  28. package/dist/common/router-helpers.js +0 -1
  29. package/dist/common/router-utils.js +0 -1
  30. package/dist/common/session/common.js +0 -1
  31. package/dist/common/session/index.js +0 -1
  32. package/dist/common/session/service.js +0 -1
  33. package/dist/common/tokenize-match.js +0 -1
  34. package/dist/index.js +0 -1
  35. package/dist/middleware/buffer.js +0 -1
  36. package/dist/middleware/cookie.js +0 -1
  37. package/dist/middleware/cors.js +0 -1
  38. package/dist/middleware/form.js +0 -1
  39. package/dist/middleware/group.js +0 -1
  40. package/dist/middleware/index.js +0 -1
  41. package/dist/middleware/json.js +0 -1
  42. package/dist/middleware/logger.js +0 -1
  43. package/dist/middleware/parse.js +0 -1
  44. package/dist/middleware/result.js +0 -1
  45. package/dist/middleware/session.js +0 -1
  46. package/dist/middleware/tag.js +0 -1
  47. package/dist/middleware/text.js +0 -1
  48. package/dist/proxy.js +0 -1
  49. package/dist/router.js +0 -1
  50. package/dist/static.js +0 -1
  51. package/package.json +6 -6
  52. package/dist/api-router.js.map +0 -1
  53. package/dist/app-utils.js.map +0 -1
  54. package/dist/app.js.map +0 -1
  55. package/dist/common/api-router-utils.js.map +0 -1
  56. package/dist/common/api-router.js.map +0 -1
  57. package/dist/common/config/config.js.map +0 -1
  58. package/dist/common/config/featuretoggle.js.map +0 -1
  59. package/dist/common/config/index.js.map +0 -1
  60. package/dist/common/config/loader.js.map +0 -1
  61. package/dist/common/context.js.map +0 -1
  62. package/dist/common/error/badrequesterror.js.map +0 -1
  63. package/dist/common/error/configfilenotfound.js.map +0 -1
  64. package/dist/common/error/forbiddenerror.js.map +0 -1
  65. package/dist/common/error/index.js.map +0 -1
  66. package/dist/common/error/notfound.js.map +0 -1
  67. package/dist/common/error/notimplemented.js.map +0 -1
  68. package/dist/common/error/unauthorizederror.js.map +0 -1
  69. package/dist/common/helpers.js.map +0 -1
  70. package/dist/common/index.js.map +0 -1
  71. package/dist/common/logger/constants.js.map +0 -1
  72. package/dist/common/logger/factory.js.map +0 -1
  73. package/dist/common/logger/index.js.map +0 -1
  74. package/dist/common/logger/interfaces.js.map +0 -1
  75. package/dist/common/logger/logger.js.map +0 -1
  76. package/dist/common/logger/transport/console.js.map +0 -1
  77. package/dist/common/logger/transport/file.js.map +0 -1
  78. package/dist/common/router-handler.js.map +0 -1
  79. package/dist/common/router-helpers.js.map +0 -1
  80. package/dist/common/router-utils.js.map +0 -1
  81. package/dist/common/session/common.js.map +0 -1
  82. package/dist/common/session/index.js.map +0 -1
  83. package/dist/common/session/service.js.map +0 -1
  84. package/dist/common/tokenize-match.js.map +0 -1
  85. package/dist/index.js.map +0 -1
  86. package/dist/middleware/buffer.js.map +0 -1
  87. package/dist/middleware/cookie.js.map +0 -1
  88. package/dist/middleware/cors.js.map +0 -1
  89. package/dist/middleware/form.js.map +0 -1
  90. package/dist/middleware/group.js.map +0 -1
  91. package/dist/middleware/index.js.map +0 -1
  92. package/dist/middleware/json.js.map +0 -1
  93. package/dist/middleware/logger.js.map +0 -1
  94. package/dist/middleware/parse.js.map +0 -1
  95. package/dist/middleware/result.js.map +0 -1
  96. package/dist/middleware/session.js.map +0 -1
  97. package/dist/middleware/tag.js.map +0 -1
  98. package/dist/middleware/text.js.map +0 -1
  99. package/dist/proxy.js.map +0 -1
  100. package/dist/router.js.map +0 -1
  101. package/dist/static.js.map +0 -1
  102. package/src/api-router.ts +0 -60
  103. package/src/api-router.unit.test.js +0 -273
  104. package/src/app-utils.ts +0 -82
  105. package/src/app-utils.unit.test.js +0 -100
  106. package/src/app.ts +0 -51
  107. package/src/app.unit.test.js +0 -333
  108. package/src/common/api-router-utils.ts +0 -193
  109. package/src/common/api-router.ts +0 -19
  110. package/src/common/config/config.ts +0 -111
  111. package/src/common/config/featuretoggle.ts +0 -6
  112. package/src/common/config/index.ts +0 -3
  113. package/src/common/config/loader.ts +0 -80
  114. package/src/common/context.ts +0 -167
  115. package/src/common/context.unit.test.js +0 -288
  116. package/src/common/error/badrequesterror.ts +0 -6
  117. package/src/common/error/configfilenotfound.ts +0 -6
  118. package/src/common/error/forbiddenerror.ts +0 -6
  119. package/src/common/error/index.ts +0 -6
  120. package/src/common/error/notfound.ts +0 -6
  121. package/src/common/error/notimplemented.ts +0 -6
  122. package/src/common/error/unauthorizederror.ts +0 -6
  123. package/src/common/helpers.ts +0 -11
  124. package/src/common/index.ts +0 -7
  125. package/src/common/logger/constants.ts +0 -27
  126. package/src/common/logger/factory.ts +0 -53
  127. package/src/common/logger/index.ts +0 -6
  128. package/src/common/logger/interfaces.ts +0 -19
  129. package/src/common/logger/logger.ts +0 -136
  130. package/src/common/logger/transport/console.ts +0 -34
  131. package/src/common/logger/transport/file.ts +0 -26
  132. package/src/common/router-handler.ts +0 -159
  133. package/src/common/router-helpers.ts +0 -20
  134. package/src/common/router-utils.ts +0 -95
  135. package/src/common/session/common.ts +0 -40
  136. package/src/common/session/index.ts +0 -2
  137. package/src/common/session/service.ts +0 -160
  138. package/src/common/tokenize-match.ts +0 -130
  139. package/src/index.ts +0 -7
  140. package/src/middleware/buffer.ts +0 -80
  141. package/src/middleware/cookie.ts +0 -20
  142. package/src/middleware/cors.ts +0 -96
  143. package/src/middleware/form.ts +0 -59
  144. package/src/middleware/group.ts +0 -70
  145. package/src/middleware/index.ts +0 -48
  146. package/src/middleware/json.ts +0 -55
  147. package/src/middleware/logger.ts +0 -18
  148. package/src/middleware/parse.ts +0 -93
  149. package/src/middleware/result.ts +0 -43
  150. package/src/middleware/session.ts +0 -136
  151. package/src/middleware/tag.ts +0 -15
  152. package/src/middleware/text.ts +0 -45
  153. package/src/proxy.ts +0 -77
  154. package/src/proxy.unit.test.js +0 -159
  155. package/src/router.ts +0 -112
  156. package/src/router.unit.test.js +0 -396
  157. package/src/setup-test.js +0 -5
  158. package/src/static.ts +0 -238
package/src/router.ts DELETED
@@ -1,112 +0,0 @@
1
- import {IncomingMessage, RequestListener} from "http";
2
- import {Logger} from "./common/logger/index.js";
3
- import {Context, ErrorHandler, Handler, Method} from "./common/context";
4
- import {HandlerWithOptions, RouterHandlerOptions, RouterJSONDoc} from "./common/router-utils";
5
- import {normalizePath, PathPart, PathPartToken, splitPath} from "./common/tokenize-match";
6
- import {defaultLoggerFactory} from "./common/router-helpers";
7
- import {RouterHandler} from "./common/router-handler";
8
- import {randomUUID} from "crypto";
9
-
10
-
11
- export interface RouterOptions {
12
- loggerFactory?: (uuid: string, req: IncomingMessage) => Logger;
13
- }
14
-
15
- export class Router {
16
- protected readonly handlers: RouterHandler[] = [];
17
- protected readonly errorHandlers: ErrorHandler[] = [];
18
- public readonly listener: RequestListener;
19
-
20
- constructor(protected config?: RouterOptions) {
21
- const loggerFactory = this.config && this.config.loggerFactory ? this.config.loggerFactory : defaultLoggerFactory;
22
- this.listener = async (req, res) => {
23
- const uuid = randomUUID();
24
- const logger = loggerFactory(uuid, req);
25
- const ctx = new Context(uuid, req, res, logger);
26
- ctx.logger.trace(`request received`);
27
- await this.run(ctx);
28
- };
29
- }
30
-
31
- public get(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
32
- return this.use(handler, path, "get", options);
33
- }
34
-
35
- public post(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
36
- return this.use(handler, path, "post", options);
37
- }
38
-
39
- public patch(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
40
- return this.use(handler, path, "patch", options);
41
- }
42
-
43
- public delete(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
44
- return this.use(handler, path, "delete", options);
45
- }
46
-
47
- public put(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
48
- return this.use(handler, path, "put", options);
49
- }
50
-
51
- public options(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
52
- return this.use(handler, path, "options", options);
53
- }
54
-
55
- public use(handler: Array<Handler> | HandlerWithOptions | Handler | Router, path?: string, method?: Method, options?: RouterHandlerOptions): Router {
56
- const nPath = path !== undefined ? normalizePath(path) : undefined;
57
- const nMethod = method ? method.toUpperCase() as Method : method;
58
- this.handlers.push(new RouterHandler(handler, nPath, nMethod, options));
59
- return this;
60
- }
61
-
62
- public catch(errorHandler: Array<ErrorHandler> | ErrorHandler): Router {
63
- const errorHandlers = errorHandler instanceof Array ? errorHandler : [errorHandler];
64
- for (const e of errorHandlers) {
65
- if (typeof e !== "function") {
66
- throw new Error(`errorHandler ${e} not a function!`);
67
- }
68
- this.errorHandlers.push(e);
69
- }
70
- return this;
71
- }
72
-
73
- public async run(ctx: Context, pathTokens?: PathPart[], prePathTokens: PathPartToken[] = []): Promise<boolean> {
74
- pathTokens = pathTokens ? pathTokens : splitPath(ctx.path);
75
- try {
76
- for (const h of this.handlers) {
77
- if (ctx.res.headersSent) {
78
- break;
79
- }
80
- const isMatch = h.isMatch(ctx.method, pathTokens, prePathTokens);
81
- if (isMatch.match) {
82
- if (isMatch.params) {
83
- ctx.params = ctx.params ? {
84
- ...ctx.params,
85
- ...isMatch.params
86
- } : isMatch.params;
87
- }
88
- const shouldContinue = await h.invoke(ctx, pathTokens, prePathTokens);
89
- if (!shouldContinue) {
90
- break;
91
- }
92
- }
93
- }
94
- return !ctx.res.headersSent;
95
- } catch (e: any) {
96
- for (const eH of this.errorHandlers) {
97
- const ret = await eH(e, ctx);
98
- if (ret === false || ctx.res.headersSent) {
99
- return false;
100
- }
101
- }
102
- throw e; // pass the error along
103
- }
104
- }
105
-
106
- public getJSONDoc(doc: RouterJSONDoc = {}, prePath = "/"): RouterJSONDoc {
107
- for (const h of this.handlers) {
108
- doc = h.getJSONDoc(doc, prePath);
109
- }
110
- return doc;
111
- }
112
- }
@@ -1,396 +0,0 @@
1
- const {fake, requireMock} = require("@miqro/test");
2
- const {resolve} = require("path");
3
- const {strictEqual} = require("assert");
4
- const {format} = require("util");
5
- const {distPath, basePath} = require("./setup-test.js");
6
-
7
- const testFilePath = resolve(distPath, "router.js");
8
-
9
- describe("router unit tests", () => {
10
-
11
- describe("this.run", () => {
12
- [
13
- {prePathTokens: undefined},
14
- {prePathTokens: ["prePathTokens1"]}
15
- ].forEach(({prePathTokens}) => {
16
- [
17
- {
18
- pathTokens: undefined
19
- },
20
- {
21
- pathTokens: "pathTokens"
22
- }
23
- ].forEach(({pathTokens}) => {
24
- let router, splitPath;
25
- const splitPathRet = "splitPathRet";
26
-
27
- function createFakeHandler({isMatch, invokeEnds, throws}) {
28
- return {
29
- invoke: fake((ctx) => {
30
- if (throws) {
31
- throw new Error("fakeHandlerError");
32
- }
33
- if (invokeEnds) {
34
- ctx.res.headersSent = true;
35
- }
36
- return invokeEnds ? false : true;
37
- }),
38
- isMatch: fake(() => {
39
- return {
40
- match: isMatch
41
- };
42
- })
43
- };
44
- }
45
-
46
- function createFakeContext() {
47
- return {
48
- method: "method",
49
- res: {
50
- headersSent: false
51
- }
52
- };
53
- }
54
-
55
-
56
- splitPath = fake(() => {
57
- return splitPathRet;
58
- });
59
- before(() => {
60
- splitPath.reset();
61
- const {Router} = requireMock(testFilePath, {
62
- "http": {},
63
-
64
- "./common/logger": {},
65
- "./common/context": {},
66
- "./common/router-utils": {},
67
- "./common/router-helpers": {},
68
- "./common/router-handler": {},
69
- "./common/tokenize-match": {
70
- splitPath
71
- }
72
- }, distPath);
73
- router = new Router();
74
- });
75
- describe("some handlers match with " + prePathTokens + " " + pathTokens, () => {
76
- it("one handler ends response", async () => {
77
- router.handlers = [
78
- createFakeHandler({invokeEnds: false, isMatch: true}),
79
- createFakeHandler({invokeEnds: false, isMatch: true}),
80
- createFakeHandler({invokeEnds: false, isMatch: false}),
81
- createFakeHandler({invokeEnds: true, isMatch: true}),
82
- createFakeHandler({invokeEnds: true, isMatch: true})
83
- ];
84
- const context = createFakeContext();
85
- const ret = await router.run(context, pathTokens, prePathTokens);
86
- strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, router.handlers.length - 1);
87
- strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
88
- strictEqual(router.handlers[0].isMatch.callArgs[0][0], context.method);
89
- strictEqual(router.handlers[0].isMatch.callArgs[0][1], pathTokens ? pathTokens : splitPathRet);
90
- strictEqual(router.handlers[0].isMatch.callArgs[0][2].length, prePathTokens ? prePathTokens.length : 0);
91
- if (prePathTokens) {
92
- strictEqual(router.handlers[0].isMatch.callArgs[0][2], prePathTokens);
93
- }
94
- strictEqual(router.handlers[0].invoke.callCount, 1);
95
- strictEqual(router.handlers[0].invoke.callArgs[0][0], context);
96
- strictEqual(router.handlers[0].invoke.callArgs[0][1], pathTokens ? pathTokens : splitPathRet);
97
- strictEqual(router.handlers[0].invoke.callArgs[0][2].length, prePathTokens ? prePathTokens.length : 0);
98
- if (prePathTokens) {
99
- strictEqual(router.handlers[0].invoke.callArgs[0][2], prePathTokens);
100
- }
101
- strictEqual(router.handlers[1].invoke.callCount, 1);
102
- strictEqual(router.handlers[2].invoke.callCount, 0);
103
- strictEqual(router.handlers[3].invoke.callCount, 1);
104
- strictEqual(router.handlers[4].invoke.callCount, 0);
105
- });
106
-
107
- it("no handler ends response", async () => {
108
- router.handlers = [
109
- createFakeHandler({invokeEnds: false, isMatch: true}),
110
- createFakeHandler({invokeEnds: false, isMatch: true}),
111
- createFakeHandler({invokeEnds: false, isMatch: false}),
112
- createFakeHandler({invokeEnds: false, isMatch: true}),
113
- createFakeHandler({invokeEnds: false, isMatch: true})
114
- ];
115
- const context = createFakeContext();
116
- const ret = await router.run(context, pathTokens, prePathTokens);
117
- strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, router.handlers.length);
118
- strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
119
- strictEqual(router.handlers[0].invoke.callCount, 1);
120
- strictEqual(router.handlers[1].invoke.callCount, 1);
121
- strictEqual(router.handlers[2].invoke.callCount, 0);
122
- strictEqual(router.handlers[3].invoke.callCount, 1);
123
- strictEqual(router.handlers[4].invoke.callCount, 1);
124
- });
125
-
126
- it("one handler throws second errorHandler catches", async () => {
127
- router.handlers = [
128
- createFakeHandler({invokeEnds: false, isMatch: true}),
129
- createFakeHandler({invokeEnds: false, isMatch: true, throws: true}),
130
- createFakeHandler({invokeEnds: false, isMatch: false}),
131
- createFakeHandler({invokeEnds: true, isMatch: true}),
132
- createFakeHandler({invokeEnds: false, isMatch: true})
133
- ];
134
- const errorHandlerMisses = fake(() => {
135
-
136
- });
137
- const errorHandler = fake((e, ctx) => {
138
- ctx.res.headersSent = true;
139
- });
140
- const context = createFakeContext();
141
- router.catch([errorHandlerMisses, errorHandler]);
142
- const ret = await router.run(context, pathTokens, prePathTokens);
143
- strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, 2);
144
- strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
145
- strictEqual(router.handlers[0].invoke.callCount, 1);
146
- strictEqual(router.handlers[1].invoke.callCount, 1);
147
- strictEqual(router.handlers[2].invoke.callCount, 0);
148
- strictEqual(router.handlers[3].invoke.callCount, 0);
149
- strictEqual(router.handlers[4].invoke.callCount, 0);
150
- strictEqual(errorHandlerMisses.callCount, 1);
151
- strictEqual(errorHandler.callCount, 1);
152
- });
153
-
154
- it("one handler throws first errorHandler catches", async () => {
155
- router.handlers = [
156
- createFakeHandler({invokeEnds: false, isMatch: true}),
157
- createFakeHandler({invokeEnds: false, isMatch: true, throws: true}),
158
- createFakeHandler({invokeEnds: false, isMatch: false}),
159
- createFakeHandler({invokeEnds: true, isMatch: true}),
160
- createFakeHandler({invokeEnds: false, isMatch: true})
161
- ];
162
- const errorHandlerMisses = fake(() => {
163
-
164
- });
165
- const errorHandler = fake((e, ctx) => {
166
- ctx.res.headersSent = true;
167
- });
168
- const context = createFakeContext();
169
- router.catch([errorHandler, errorHandlerMisses]);
170
- const ret = await router.run(context, pathTokens, prePathTokens);
171
- strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, 2);
172
- strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
173
- strictEqual(router.handlers[0].invoke.callCount, 1);
174
- strictEqual(router.handlers[1].invoke.callCount, 1);
175
- strictEqual(router.handlers[2].invoke.callCount, 0);
176
- strictEqual(router.handlers[3].invoke.callCount, 0);
177
- strictEqual(router.handlers[4].invoke.callCount, 0);
178
- strictEqual(errorHandlerMisses.callCount, 0);
179
- strictEqual(errorHandler.callCount, 1);
180
- });
181
-
182
- it("one handler throws errorHandler doesn't catch", async () => {
183
- router.handlers = [
184
- createFakeHandler({invokeEnds: false, isMatch: true}),
185
- createFakeHandler({invokeEnds: false, isMatch: true, throws: true}),
186
- createFakeHandler({invokeEnds: false, isMatch: false}),
187
- createFakeHandler({invokeEnds: true, isMatch: true}),
188
- createFakeHandler({invokeEnds: false, isMatch: true})
189
- ];
190
- const context = createFakeContext();
191
- try {
192
- const ret = await router.run(context, pathTokens, prePathTokens);
193
- strictEqual(false, true);
194
- } catch (e) {
195
- strictEqual(e.message, "fakeHandlerError")
196
- }
197
- strictEqual(router.handlers.filter(r => r.isMatch.callCount === 1).length, 2);
198
- strictEqual(splitPath.callCount, pathTokens ? 0 : 1);
199
- strictEqual(router.handlers[0].invoke.callCount, 1);
200
- strictEqual(router.handlers[1].invoke.callCount, 1);
201
- strictEqual(router.handlers[2].invoke.callCount, 0);
202
- strictEqual(router.handlers[3].invoke.callCount, 0);
203
- strictEqual(router.handlers[4].invoke.callCount, 0);
204
- });
205
- });
206
- });
207
- });
208
- });
209
-
210
- [
211
- {method: "options"},
212
- {method: "put"},
213
- {method: "patch"},
214
- {method: "delete"},
215
- {method: "post"},
216
- {method: "get"}
217
- ].forEach(({method}) => {
218
- let router;
219
- before(() => {
220
- const {Router} = requireMock(testFilePath, {
221
- "http": {},
222
-
223
- "./common/logger": {},
224
- "./common/context": {},
225
- "./common/router-utils": {},
226
- "./common/router-helpers": {},
227
- "./common/router-handler": {},
228
- "./common/tokenize-match": {}
229
- }, distPath);
230
- router = new Router();
231
- });
232
- it(method + " just alias for use", async () => {
233
- router.use = fake(() => {
234
- });
235
- const path = "path";
236
- const handler = "handler";
237
- const options = "options";
238
- router[method](path, handler, options);
239
- strictEqual(router.use.callCount, 1);
240
- strictEqual(router.use.callArgs[0][0], handler);
241
- strictEqual(router.use.callArgs[0][1], path);
242
- strictEqual(router.use.callArgs[0][2], method);
243
- strictEqual(router.use.callArgs[0][3], options);
244
- });
245
- });
246
-
247
- describe("this.catch", () => {
248
-
249
- let router;
250
- before(() => {
251
- const {Router} = requireMock(testFilePath, {
252
- "http": {},
253
-
254
- "./common/logger": {},
255
- "./common/context": {},
256
- "./common/router-utils": {},
257
- "./common/router-helpers": {},
258
- "./common/router-handler": {},
259
- "./common/tokenize-match": {}
260
- }, distPath);
261
- router = new Router();
262
- });
263
- [
264
- {
265
- errorHandler: [
266
- () => {
267
- },
268
- () => {
269
- }
270
- ]
271
- },
272
- {
273
- errorHandler: () => {
274
- }
275
- },
276
- {
277
- errorHandler: undefined,
278
- throws: true,
279
- },
280
- {
281
- errorHandler: [
282
- () => {
283
- },
284
- undefined
285
- ],
286
- throws: true
287
- }
288
- ].forEach(({errorHandler, throws}) => {
289
- it(format("errorHandler %o throws %o", errorHandler, throws), () => {
290
- router.errorHandlers = {
291
- push: fake(() => {
292
-
293
- })
294
- };
295
- try {
296
- const ret = router.catch(errorHandler);
297
- strictEqual(undefined, throws);
298
- strictEqual(ret, router);
299
- } catch (e) {
300
- strictEqual(true, throws);
301
- } finally {
302
- const count = (!errorHandler ? [] : (errorHandler instanceof Array ? errorHandler : [errorHandler])).filter(m => typeof m === "function").length;
303
- strictEqual(router.errorHandlers.push.callCount, count);
304
- }
305
- });
306
- });
307
- });
308
-
309
- describe("this.use", () => {
310
- let RouterHandler, router, routerHandlerCtr, normalizePath, splitPath;
311
- const normalizePathRet = "normalizePathRet";
312
-
313
- before(() => {
314
- routerHandlerCtr = fake(() => {
315
-
316
- });
317
-
318
- normalizePath = fake(() => {
319
- return normalizePathRet;
320
- });
321
-
322
- splitPath = fake(() => {
323
-
324
- });
325
-
326
- RouterHandler = class {
327
- constructor() {
328
- routerHandlerCtr(...arguments);
329
- }
330
- }
331
-
332
- const {Router} = requireMock(testFilePath, {
333
- "http": {},
334
- "./common/logger": {},
335
- "./common/context": {},
336
- "./common/router-utils": {},
337
- "./common/router-helpers": {},
338
- "./common/router-handler": {
339
- RouterHandler
340
- },
341
- "./common/tokenize-match": {
342
- normalizePath,
343
- splitPath
344
- }
345
- }, distPath);
346
- router = new Router();
347
- });
348
-
349
- [
350
- {handler: "handler"},
351
- {handler: undefined},
352
- {handler: ["handler1", "handler2"]}
353
- ].forEach(({handler}) => {
354
- [
355
- {options: "options"},
356
- {options: undefined}
357
- ].forEach(({options}) => {
358
- [
359
- {
360
- path: "path",
361
- method: "method"
362
- },
363
- {
364
- path: "path"
365
- },
366
- {}
367
- ].forEach(
368
- ({
369
- path,
370
- method
371
- }) => {
372
- it(`use with handler ${handler} path ${path} method ${method} options ${options}`, async () => {
373
- router.handlers = {
374
- push: fake(() => {
375
-
376
- })
377
- }
378
- const ret = router.use(handler, path, method, options);
379
- strictEqual(ret, router);
380
- strictEqual(normalizePath.callCount, path !== undefined ? 1 : 0);
381
- if (path) {
382
- strictEqual(normalizePath.callArgs[0][0], path);
383
- }
384
- strictEqual(routerHandlerCtr.callCount, 1);
385
- strictEqual(routerHandlerCtr.callArgs[0][0], handler);
386
- strictEqual(routerHandlerCtr.callArgs[0][1], path ? normalizePathRet : path);
387
- strictEqual(routerHandlerCtr.callArgs[0][2], method ? method.toUpperCase() : method);
388
- strictEqual(routerHandlerCtr.callArgs[0][3], options);
389
- strictEqual(router.handlers.push.callCount, 1);
390
- strictEqual(router.handlers.push.callArgs[0][0] instanceof RouterHandler, true);
391
- });
392
- });
393
- });
394
- });
395
- });
396
- });
package/src/setup-test.js DELETED
@@ -1,5 +0,0 @@
1
- const {resolve} = require("path");
2
-
3
- module.exports.basePath = resolve(__dirname, "..");
4
-
5
- module.exports.distPath = resolve(module.exports.basePath, "dist");