@miqro/core 1.5.15 → 1.5.16

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.
@@ -0,0 +1,157 @@
1
+ const {fake, requireMock} = require("@miqro/test");
2
+ const {resolve} = require("path");
3
+ const {strictEqual} = require("assert");
4
+ const {distPath, basePath} = require("./setup-test.js");
5
+
6
+ const testFilePath = resolve(distPath, "proxy.js");
7
+
8
+ describe("proxy unit tests", () => {
9
+ [{
10
+ fakeOptions: {
11
+ url: "http://localhost"
12
+ }
13
+ }, {
14
+ fakeOptions: {
15
+ url: "http://localhost",
16
+ rejectUnauthorized: true
17
+ }
18
+ }, {
19
+ fakeOptions: {
20
+ url: "http://localhost",
21
+ rejectUnauthorized: false
22
+ }
23
+ }].forEach(({fakeOptions}) => {
24
+ it("ctor with " + JSON.stringify(fakeOptions), async () => {
25
+ const routerCtr = fake(() => {
26
+
27
+ });
28
+
29
+ const normalizePathRet = "normalizePathRet";
30
+
31
+ const urlCtr = fake(() => {
32
+ return "fakePath";
33
+ });
34
+
35
+ class Router {
36
+ constructor(options) {
37
+ routerCtr(options);
38
+ }
39
+ }
40
+
41
+ class URL {
42
+ constructor(options) {
43
+ this.pathname = urlCtr(options);
44
+ }
45
+ }
46
+
47
+ const normalizePath = fake((args) => {
48
+ return normalizePathRet;
49
+ });
50
+
51
+ const {Proxy} = requireMock(testFilePath, {
52
+ "./common": {},
53
+ "@miqro/request": {},
54
+ "./router": {
55
+ Router
56
+ },
57
+ "./common/tokenize-match": {
58
+ normalizePath
59
+ },
60
+ "url": {
61
+ URL
62
+ },
63
+ }, basePath);
64
+ // console.dir(Object.keys(require.cache));
65
+ const router = Proxy(fakeOptions);
66
+ strictEqual(router.proxyURL.pathname, normalizePathRet);
67
+ strictEqual(router.rejectUnauthorized, fakeOptions.rejectUnauthorized);
68
+ strictEqual(router.proxyURL instanceof URL, true);
69
+ strictEqual(routerCtr.callCount, 1);
70
+ strictEqual(routerCtr.callArgs[0][0], fakeOptions);
71
+ strictEqual(urlCtr.callCount, 1);
72
+ strictEqual(urlCtr.callArgs[0][0], fakeOptions.url);
73
+ strictEqual(normalizePath.callCount, 1);
74
+ strictEqual(normalizePath.callArgs[0][0], "fakePath");
75
+ });
76
+
77
+ [{
78
+ context: {
79
+ path: "/",
80
+ res: {
81
+ headersSent: false
82
+ },
83
+ logger: {
84
+ info: fake(() => {
85
+
86
+ })
87
+ },
88
+ end: fake(()=>{
89
+
90
+ })
91
+ }
92
+ }].forEach(({context}) => {
93
+ it("this.run: super.run returns true with options " + JSON.stringify(fakeOptions) + " and context " + context.path, async () => {
94
+ const routerCtr = fake(() => {
95
+
96
+ });
97
+ const routerRun = fake(async () => {
98
+ return true;
99
+ });
100
+
101
+ const normalizePathRet = "normalizePathRet";
102
+
103
+ const urlCtr = fake(() => {
104
+ return "fakePath";
105
+ });
106
+
107
+ class Router {
108
+ constructor(options) {
109
+ routerCtr(options);
110
+ }
111
+
112
+ run() {
113
+ return routerRun(...arguments);
114
+ }
115
+ }
116
+
117
+ class URL {
118
+ constructor(options) {
119
+ this.pathname = urlCtr(options);
120
+ }
121
+ }
122
+
123
+ const normalizePath = fake((args) => {
124
+ return normalizePathRet;
125
+ });
126
+ const requestRet = {
127
+
128
+ };
129
+ const request = fake(async () => {
130
+ return requestRet;
131
+ });
132
+
133
+ const {Proxy} = requireMock(testFilePath, {
134
+ "./common": {},
135
+ "@miqro/request": {
136
+ request
137
+ },
138
+ "./router": {
139
+ Router
140
+ },
141
+ "./common/tokenize-match": {
142
+ normalizePath
143
+ },
144
+ "url": {
145
+ URL
146
+ },
147
+ }, basePath);
148
+ //console.dir(Object.keys(require.cache))
149
+ const router = Proxy(fakeOptions);
150
+ const ret = await router.run(context);
151
+ strictEqual(request.callCount, 1);
152
+ // TODO assert path
153
+ });
154
+ });
155
+ });
156
+
157
+ });
package/src/router.ts CHANGED
@@ -62,6 +62,9 @@ export class Router {
62
62
  public catch(errorHandler: Array<ErrorHandler> | ErrorHandler): Router {
63
63
  const errorHandlers = errorHandler instanceof Array ? errorHandler : [errorHandler];
64
64
  for (const e of errorHandlers) {
65
+ if (typeof e !== "function") {
66
+ throw new Error(`errorHandler ${e} not a function!`);
67
+ }
65
68
  this.errorHandlers.push(e);
66
69
  }
67
70
  return this;
@@ -0,0 +1,397 @@
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
+ "uuid": {},
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
+ "uuid": {},
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
+ "uuid": {},
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
+ "uuid": {},
335
+ "./common/logger": {},
336
+ "./common/context": {},
337
+ "./common/router-utils": {},
338
+ "./common/router-helpers": {},
339
+ "./common/router-handler": {
340
+ RouterHandler
341
+ },
342
+ "./common/tokenize-match": {
343
+ normalizePath,
344
+ splitPath
345
+ }
346
+ }, distPath);
347
+ router = new Router();
348
+ });
349
+
350
+ [
351
+ {handler: "handler"},
352
+ {handler: undefined},
353
+ {handler: ["handler1", "handler2"]}
354
+ ].forEach(({handler}) => {
355
+ [
356
+ {options: "options"},
357
+ {options: undefined}
358
+ ].forEach(({options}) => {
359
+ [
360
+ {
361
+ path: "path",
362
+ method: "method"
363
+ },
364
+ {
365
+ path: "path"
366
+ },
367
+ {}
368
+ ].forEach(
369
+ ({
370
+ path,
371
+ method
372
+ }) => {
373
+ it(`use with handler ${handler} path ${path} method ${method} options ${options}`, async () => {
374
+ router.handlers = {
375
+ push: fake(() => {
376
+
377
+ })
378
+ }
379
+ const ret = router.use(handler, path, method, options);
380
+ strictEqual(ret, router);
381
+ strictEqual(normalizePath.callCount, path !== undefined ? 1 : 0);
382
+ if (path) {
383
+ strictEqual(normalizePath.callArgs[0][0], path);
384
+ }
385
+ strictEqual(routerHandlerCtr.callCount, 1);
386
+ strictEqual(routerHandlerCtr.callArgs[0][0], handler);
387
+ strictEqual(routerHandlerCtr.callArgs[0][1], path ? normalizePathRet : path);
388
+ strictEqual(routerHandlerCtr.callArgs[0][2], method ? method.toUpperCase() : method);
389
+ strictEqual(routerHandlerCtr.callArgs[0][3], options);
390
+ strictEqual(router.handlers.push.callCount, 1);
391
+ strictEqual(router.handlers.push.callArgs[0][0] instanceof RouterHandler, true);
392
+ });
393
+ });
394
+ });
395
+ });
396
+ });
397
+ });
package/src/setup-test.js CHANGED
@@ -1,3 +1,5 @@
1
1
  const {resolve} = require("path");
2
2
 
3
- module.exports.distPath = resolve(__dirname, "..", "dist");
3
+ module.exports.basePath = resolve(__dirname, "..");
4
+
5
+ module.exports.distPath = resolve(module.exports.basePath, "dist");