@contrast/route-coverage 1.35.0 → 1.35.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.
@@ -1,418 +0,0 @@
1
- 'use strict';
2
-
3
- const { expect } = require('chai');
4
- const sinon = require('sinon');
5
- const scopes = require('@contrast/scopes');
6
- const patcher = require('@contrast/patcher');
7
- const mocks = require('@contrast/test/mocks');
8
-
9
- const METHODS = [
10
- 'all',
11
- 'get',
12
- 'post',
13
- 'put',
14
- 'delete',
15
- 'patch',
16
- 'options',
17
- 'head',
18
- ];
19
-
20
- describe('route-coverage express', function () {
21
- let core, http, Server, application, Router, Layer, express, fn, framework;
22
-
23
- beforeEach(function () {
24
- core = mocks.core();
25
- core.logger = mocks.logger();
26
- core.routeCoverage = mocks.routeCoverage();
27
- core.scopes = scopes(core);
28
- core.depHooks = mocks.depHooks();
29
- core.patcher = patcher(core);
30
-
31
- application = () => application;
32
- Router = () => Router;
33
- Layer = () => Layer;
34
- Layer.handle = sinon.stub();
35
-
36
- express = () => application;
37
- framework = 'express';
38
-
39
- Router.stack = [];
40
- Router.handle = sinon.stub();
41
- Router.use = (prefix, router) => {
42
- Router.stack.push({ handle: router, name: 'router', regexp: { source: `\\${prefix}` } });
43
- };
44
-
45
- express.application = application;
46
- express.application.use = (prefix, router) => {
47
- if (router?.use) router.use(prefix, router);
48
- return { _router: Router };
49
- };
50
- express.application._router = Router;
51
- express.Router = Router;
52
-
53
- METHODS.forEach((method) => {
54
- application[method] = () => {
55
- Router.stack.push(Layer);
56
- return { _router: Router };
57
- };
58
- Router[method] = (path, fn) => {
59
- Layer.regexp = { source: `\\${path}` };
60
- Layer.route = {
61
- path,
62
- stack: [
63
- {
64
- method
65
- }
66
- ],
67
- methods: {
68
- _all: false
69
- }
70
- };
71
- if (fn) Router.stack.push(Layer);
72
- return Router;
73
- };
74
- });
75
- Server = function () { };
76
- Server.prototype.listen = sinon.stub();
77
- http = {
78
- Server,
79
- createServer() {
80
- return new Server();
81
- }
82
- };
83
-
84
- fn = sinon.stub();
85
-
86
- core.depHooks.resolve.withArgs({ name: 'express', version: '>=4 <5' }).yields(express);
87
- core.depHooks.resolve.withArgs({ name: 'http' }).yields(http);
88
-
89
- require('./express4')(core).install();
90
- });
91
-
92
- describe('app', function () {
93
- METHODS.forEach((method) => {
94
- describe(`${method}`, function () {
95
- it('does not report a non-existent route', function () {
96
- const app = express();
97
- app[method]();
98
- expect(core.routeCoverage.discover).not.to.have.been.called;
99
- });
100
-
101
- it('does not report an invalid route', function () {
102
- const app = express();
103
- app[method](42);
104
- expect(core.routeCoverage.discover).not.to.have.been.called;
105
- });
106
-
107
- it('does not report a route missing a method', function () {
108
- const app = express();
109
- app[method]('/test/route');
110
- expect(core.routeCoverage.discover).not.to.have.been.called;
111
- });
112
-
113
- it('discovers a route', function () {
114
- const app = express();
115
- app[method]('/test/route', fn);
116
-
117
- expect(core.routeCoverage.discover).to.have.been.calledWith({
118
- signature: `App.${method}('/test/route', [Function])`,
119
- url: '/test/route',
120
- normalizedUrl: '/test/route',
121
- method,
122
- framework
123
- });
124
- });
125
-
126
- it('discovers an array route', function () {
127
- const app = express();
128
- app[method](['/test/foo', '/test/bar'], fn);
129
-
130
- expect(core.routeCoverage.discover).to.have.been.calledWith({
131
- signature: `App.${method}('/[/test/foo,/test/bar]', [Function])`,
132
- url: '/[/test/foo,/test/bar]',
133
- normalizedUrl: '/[/test/foo,/test/bar]',
134
- method,
135
- framework
136
- });
137
- });
138
-
139
- it('discovers a regEx route', function () {
140
- const app = express();
141
- app[method](/f*o/, fn);
142
-
143
- expect(core.routeCoverage.discover).to.have.been.calledWith({
144
- signature: `App.${method}('/{f*o}', [Function])`,
145
- url: '/{f*o}',
146
- normalizedUrl: '/{f*o}',
147
- method,
148
- framework
149
- });
150
- });
151
- });
152
- });
153
-
154
- it('does not discover and App.use route with no path and no router', function () {
155
- const app = express();
156
- app.use(fn);
157
- expect(core.routeCoverage.discover).not.have.been.called;
158
- });
159
-
160
- it('discovers an App.use route', function () {
161
- const app = express();
162
- app.use('/foo', fn);
163
-
164
- expect(core.routeCoverage.discover).to.have.been.calledWith({
165
- signature: 'App.use(\'/foo\', [Function])',
166
- url: '/foo',
167
- normalizedUrl: '/foo',
168
- method: 'use',
169
- framework
170
- });
171
- });
172
-
173
- it('discovers an App.use route with a non-string url', function () {
174
- const app = express();
175
- app.use(['/foo'], fn);
176
-
177
- expect(core.routeCoverage.discover).to.have.been.calledWith({
178
- signature: 'App.use(\'/[/foo]\', [Function])',
179
- url: '/[/foo]',
180
- normalizedUrl: '/[/foo]',
181
- method: 'use',
182
- framework
183
- });
184
- });
185
-
186
- it('discovers a route with middleware defined in an array', function() {
187
- const app = express();
188
- app.use('/foo', [fn]);
189
-
190
- expect(core.routeCoverage.discover).to.have.been.calledWith({
191
- signature: 'App.use(\'/foo\', [Function])',
192
- url: '/foo',
193
- normalizedUrl: '/foo',
194
- method: 'use',
195
- framework
196
- });
197
- });
198
-
199
- it('does not observe a route with an undefined method', function () {
200
- const app = express();
201
- app.get('/path/to/foo', fn);
202
-
203
- express.Router.handle({
204
- originalUrl: '/path/to/foo',
205
- route: { path: '/path/to/foo' }
206
- });
207
- expect(core.routeCoverage.observe).to.not.have.been.called;
208
- });
209
-
210
- it('observes a route when a route handler is exercised', function () {
211
- const app = express();
212
- app.get('/path/to/foo', fn);
213
-
214
- express.Router.stack[0].handle({
215
- method: 'GET',
216
- originalUrl: '/path/to/foo',
217
- route: { path: '/path/to/foo' }
218
- });
219
- expect(core.routeCoverage.observe).to.have.been.calledWith({
220
- signature: "App.get('/path/to/foo', [Function])",
221
- url: '/path/to/foo',
222
- normalizedUrl: '/path/to/foo',
223
- method: 'get',
224
- framework
225
- });
226
- });
227
-
228
- it('observes a route with a regExp path', function () {
229
- const app = express();
230
- app.get(/.*/, fn);
231
-
232
- express.Router.stack[0].handle({
233
- method: 'GET',
234
- originalUrl: '/path',
235
- route: { path: /.*/ }
236
- });
237
- expect(core.routeCoverage.observe).to.have.been.calledWith({
238
- signature: "App.get('/{.*}', [Function])",
239
- url: '/path',
240
- normalizedUrl: '/{.*}',
241
- method: 'get',
242
- framework
243
- });
244
- });
245
-
246
- it('observes a route with an Array path', function () {
247
- const app = express();
248
- app.get(['/test/foo', '/test/bar'], fn);
249
-
250
- express.Router.stack[0].handle({
251
- method: 'GET',
252
- originalUrl: '/[/test/foo,/test/bar]',
253
- route: { path: ['/test/foo', '/test/bar'] }
254
- });
255
- expect(core.routeCoverage.observe).to.have.been.calledWith({
256
- signature: "App.get('/[/test/foo,/test/bar]', [Function])",
257
- url: '/[/test/foo,/test/bar]',
258
- normalizedUrl: '/[/test/foo,/test/bar]',
259
- method: 'get',
260
- framework
261
- });
262
- });
263
-
264
- it('updates route prefixes', function () {
265
- const app = express();
266
- const router = express.Router();
267
- router.get('/foo', fn);
268
- app.use('/prefix', router);
269
- expect(core.routeCoverage.discover).to.have.been.calledWith({
270
- signature: "Router.get('/prefix/foo', [Function])",
271
- url: '/prefix/foo',
272
- normalizedUrl: '/prefix/foo',
273
- method: 'get',
274
- framework
275
- });
276
- });
277
- });
278
-
279
- describe('router', function () {
280
- METHODS.forEach((method) => {
281
- describe(`${method}`, function () {
282
- it('does not report a non-existent route', function () {
283
- const app = express();
284
- const router = express.Router();
285
- router[method]();
286
- app.use(router);
287
- expect(core.routeCoverage.discover).not.to.have.been.called;
288
- });
289
-
290
- it('does not report an invalid route', function () {
291
- const app = express();
292
- const router = express.Router();
293
- router[method](42);
294
- app.use(router);
295
- expect(core.routeCoverage.discover).not.to.have.been.called;
296
- });
297
-
298
- it('does not report a route missing a handler', function () {
299
- const app = express();
300
- const router = express.Router();
301
- router[method]('/test/route');
302
- app.use(router);
303
- expect(core.routeCoverage.discover).not.to.have.been.called;
304
- });
305
-
306
- it('discovers a route', function () {
307
- const app = express();
308
- const router = express.Router();
309
- router[method]('/test/route', fn);
310
- app.use(router);
311
-
312
- expect(core.routeCoverage.discover).to.have.been.calledWith({
313
- signature: `Router.${method}('/test/route', [Function])`,
314
- url: '/test/route',
315
- normalizedUrl: '/test/route',
316
- method,
317
- framework
318
- });
319
- });
320
-
321
- it('discovers an array route', function () {
322
- const app = express();
323
- const router = express.Router();
324
- router[method](['/test/foo', '/test/bar'], fn);
325
- app.use(router);
326
-
327
- expect(core.routeCoverage.discover).to.have.been.calledWith({
328
- signature: `Router.${method}('/[/test/foo,/test/bar]', [Function])`,
329
- url: '/[/test/foo,/test/bar]',
330
- normalizedUrl: '/[/test/foo,/test/bar]',
331
- method,
332
- framework
333
- });
334
- });
335
-
336
- it('discovers a regEx route', function () {
337
- const app = express();
338
- const router = express.Router();
339
- router[method](/f*o/, fn);
340
- app.use(router);
341
-
342
- expect(core.routeCoverage.discover).to.have.been.calledWith({
343
- signature: `Router.${method}('/{f*o}', [Function])`,
344
- url: '/{f*o}',
345
- normalizedUrl: '/{f*o}',
346
- method,
347
- framework
348
- });
349
- });
350
- });
351
- });
352
-
353
- it('does not observe a route with an undefined method', function () {
354
- const app = express();
355
- const router = express.Router();
356
- router.get('/path/to/foo', fn);
357
- app.use(router);
358
-
359
- express.Router.handle({
360
- originalUrl: '/path/to/foo',
361
- route: { path: '/path/to/foo' }
362
- });
363
- expect(core.routeCoverage.observe).to.not.have.been.called;
364
- });
365
-
366
- it('observes a route when a route handler is exercised', function () {
367
- const app = express();
368
- const router = express.Router();
369
- router.get('/path/to/foo', fn);
370
- app.use(router);
371
-
372
- express.Router.stack[0].handle({
373
- method: 'GET',
374
- originalUrl: '/path/to/foo',
375
- route: { path: '/path/to/foo' }
376
- });
377
- expect(core.routeCoverage.observe).to.have.been.calledWith({
378
- signature: "Router.get('/path/to/foo', [Function])",
379
- url: '/path/to/foo',
380
- normalizedUrl: '/path/to/foo',
381
- method: 'get',
382
- framework
383
- });
384
- });
385
-
386
- it('updates route prefixes on nested router', function () {
387
- const app = express();
388
- const router = express.Router();
389
- router.get('/foo', fn);
390
- router.use('/prefix', router);
391
- app.use(router);
392
- expect(core.routeCoverage.discover).to.have.been.calledWith({
393
- signature: "Router.get('/prefix/foo', [Function])",
394
- url: '/prefix/foo',
395
- normalizedUrl: '/prefix/foo',
396
- method: 'get',
397
- framework
398
- });
399
- });
400
-
401
- it('updates route prefixes on a non-nested router', function () {
402
- const app = express();
403
- const router = express.Router();
404
- const router2 = express.Router();
405
- router.get('/foo', fn);
406
- router2.use('/prefix', router);
407
- app.use(router);
408
- expect(core.routeCoverage.discover).to.have.been.calledWith({
409
- signature: "Router.get('/prefix/foo', [Function])",
410
- url: '/prefix/foo',
411
- normalizedUrl: '/prefix/foo',
412
- method: 'get',
413
- framework
414
- });
415
- });
416
-
417
- });
418
- });