@contrast/route-coverage 1.20.7 → 1.22.0

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,152 @@
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
+ const { METHODS } = require('../utils/methods');
9
+
10
+ describe('route-coverage koa', function () {
11
+ let core, Router, framework, Koa, app, handlerFn;
12
+
13
+ beforeEach(function () {
14
+ core = mocks.core();
15
+ core.logger = mocks.logger();
16
+ core.routeCoverage = mocks.routeCoverage();
17
+ core.scopes = scopes(core);
18
+ core.depHooks = mocks.depHooks();
19
+ core.patcher = patcher(core);
20
+
21
+ framework = 'koa';
22
+ Router = function Router() { };
23
+ Router.router = {
24
+ stack: []
25
+ };
26
+
27
+ handlerFn = sinon.stub();
28
+ Router.prototype.use = (path, fn) => Router.router.stack.push({ methods: [], path, stack: [fn] });
29
+ Router.prototype.get = (path, fn) => Router.router.stack.push({ methods: ['GET'], path, stack: [fn] });
30
+ Router.prototype.all = (path, fn) => Router.router.stack.push({ methods: METHODS, path, stack: [fn] });
31
+ Router.prototype.routes = () => Router;
32
+
33
+ Koa = function() {};
34
+ Koa.prototype.use = sinon.stub();
35
+ core.depHooks.resolve.withArgs({ name: 'koa', version: '>=2.3.0' }).yields(Koa);
36
+ require('./koa')(core).install();
37
+ });
38
+
39
+ it('does not report when called with nothing', function () {
40
+ app = new Koa();
41
+ app.use();
42
+ expect(core.routeCoverage.discover).not.to.have.been.called;
43
+ });
44
+
45
+ it('does not report when called with non-router', function () {
46
+ app = new Koa();
47
+ app.use({ not: 'a router' });
48
+ expect(core.routeCoverage.discover).not.to.have.been.called;
49
+ });
50
+
51
+ it('does not report unmounted routes', function () {
52
+ const router = new Router();
53
+ router.get('/test/route', handlerFn);
54
+
55
+ expect(core.routeCoverage.discover).not.to.have.been.called;
56
+ });
57
+
58
+ it('does not report a non-existent route', function () {
59
+ const router = new Router();
60
+ router.get();
61
+
62
+ app = new Koa();
63
+ app.use(router.routes());
64
+ expect(core.routeCoverage.discover).not.to.have.been.called;
65
+ });
66
+
67
+ it('does not report when path is an array', function () {
68
+ const router = new Router();
69
+ router.get(['/test/route'], handlerFn);
70
+
71
+ app = new Koa();
72
+ app.use(router.routes());
73
+ expect(core.routeCoverage.discover).not.to.have.been.called;
74
+ });
75
+
76
+ it('discovers a route', function () {
77
+ const router = new Router();
78
+ router.get('/test/route', handlerFn);
79
+
80
+ app = new Koa();
81
+ app.use(router.routes());
82
+ expect(core.routeCoverage.discover).to.have.been.calledWith({
83
+ signature: "Router.get('/test/route', [Function])",
84
+ url: '/test/route',
85
+ normalizedUrl: '/test/route',
86
+ method: 'get',
87
+ framework
88
+ });
89
+ });
90
+
91
+ it('discovers a .use route', function () {
92
+ const router = new Router();
93
+ router.use('/test/route', handlerFn);
94
+
95
+ app = new Koa();
96
+ app.use(router.routes());
97
+ expect(core.routeCoverage.discover).to.have.been.calledWith({
98
+ signature: "Router.use('/test/route', [Function])",
99
+ url: '/test/route',
100
+ normalizedUrl: '/test/route',
101
+ method: 'use',
102
+ framework
103
+ });
104
+ });
105
+
106
+ it('discovers a .all route', function () {
107
+ const router = new Router();
108
+ router.all('/test/route', handlerFn);
109
+
110
+ app = new Koa();
111
+ app.use(router.routes());
112
+ expect(core.routeCoverage.discover).to.have.been.calledWith({
113
+ signature: "Router.all('/test/route', [Function])",
114
+ url: '/test/route',
115
+ normalizedUrl: '/test/route',
116
+ method: 'all',
117
+ framework
118
+ });
119
+ });
120
+
121
+ it('does not observe a route without a valid request', async function () {
122
+ const router = new Router();
123
+ router.get('/test/route', handlerFn);
124
+
125
+ app = new Koa();
126
+ app.use(router.routes());
127
+
128
+ Router.router.stack[0].stack[0]({});
129
+ expect(core.routeCoverage.observe).to.not.have.been.called;
130
+ });
131
+
132
+ it('observes a route when a route handler is exercised', async function () {
133
+ const router = new Router();
134
+ router.get('/test/route', handlerFn);
135
+
136
+ app = new Koa();
137
+ app.use(router.routes());
138
+
139
+ const request = {
140
+ url: '/test/route',
141
+ method: 'get'
142
+ };
143
+ Router.router.stack[0].stack[0]({ request });
144
+ expect(core.routeCoverage.observe).to.have.been.calledWith({
145
+ signature: "Router.get('/test/route', [Function])",
146
+ url: '/test/route',
147
+ normalizedUrl: '/test/route',
148
+ method: 'get',
149
+ framework: 'koa'
150
+ });
151
+ });
152
+ });
@@ -17,6 +17,7 @@
17
17
  const { StringPrototypeToLowerCase, isString } = require('@contrast/common');
18
18
  const { createSignature, patchType } = require('../utils/route-info');
19
19
 
20
+ // Spec: https://contrast.atlassian.net/wiki/spaces/NOD/pages/3454861621/Node.js+Agent+Route+Signatures#Restify
20
21
  module.exports = function init(core) {
21
22
  const { patcher, depHooks, routeCoverage } = core;
22
23
  const discover = (route) => routeCoverage.discover(route);
@@ -25,7 +26,7 @@ module.exports = function init(core) {
25
26
  function createRoute(url, method) {
26
27
  method = StringPrototypeToLowerCase.call(method);
27
28
  return {
28
- signature: createSignature(url, method, 'Server'),
29
+ signature: createSignature(url, method, 'server'),
29
30
  method,
30
31
  url,
31
32
  normalizedUrl: url,
@@ -0,0 +1,92 @@
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
+ describe('route-coverage restify', function () {
10
+ let core, restify, serverMock, handler, framework;
11
+
12
+ beforeEach(function () {
13
+ core = mocks.core();
14
+ core.logger = mocks.logger();
15
+ core.routeCoverage = mocks.routeCoverage();
16
+ core.scopes = scopes(core);
17
+ core.depHooks = mocks.depHooks();
18
+ core.patcher = patcher(core);
19
+
20
+ handler = sinon.stub();
21
+ serverMock = {
22
+ router: {
23
+ mount (path, method) {
24
+ return {
25
+ path,
26
+ method,
27
+ chain: {
28
+ _stack: [
29
+ handler
30
+ ]
31
+ }
32
+ };
33
+ }
34
+ }
35
+ };
36
+
37
+ framework = 'restify';
38
+ restify = {
39
+ createServer: () => serverMock
40
+ };
41
+ core.depHooks.resolve.withArgs({ name: 'restify', version: '>= 8.0.0' }).yields(restify);
42
+ require('./restify')(core).install();
43
+ });
44
+
45
+ it('does not report a non-existent route', function () {
46
+ const { router } = restify.createServer();
47
+ router.mount();
48
+ expect(core.routeCoverage.discover).not.to.have.been.called;
49
+ });
50
+
51
+ it('does not report an incomplete route', function () {
52
+ const { router } = restify.createServer();
53
+ router.mount('/foo');
54
+ expect(core.routeCoverage.discover).not.to.have.been.called;
55
+ });
56
+
57
+ it('does not report an invalid route', function () {
58
+ const { router } = restify.createServer();
59
+ router.mount(42, 'GET');
60
+ expect(core.routeCoverage.discover).not.to.have.been.called;
61
+ });
62
+
63
+ it('discovers a route', function () {
64
+ const { router } = restify.createServer();
65
+ router.mount('/foo', 'GET');
66
+
67
+ expect(core.routeCoverage.discover).to.have.been.calledWith({
68
+ signature: "server.get('/foo', [Function])",
69
+ url: '/foo',
70
+ normalizedUrl: '/foo',
71
+ method: 'get',
72
+ framework
73
+ });
74
+ });
75
+
76
+ it('observes a route', async function () {
77
+ const { router } = restify.createServer();
78
+ const route = router.mount('/foo', 'GET');
79
+ const handler = route.chain._stack[0];
80
+ handler({
81
+ url: '/foo?input=bar',
82
+ method: 'GET'
83
+ });
84
+ expect(core.routeCoverage.observe).to.have.been.calledWith({
85
+ signature: "server.get('/foo', [Function])",
86
+ url: '/foo',
87
+ normalizedUrl: '/foo',
88
+ method: 'get',
89
+ framework
90
+ });
91
+ });
92
+ });
@@ -0,0 +1,45 @@
1
+ /*
2
+ * Copyright: 2024 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+ 'use strict';
16
+
17
+ // eslint-disable-next-line node/no-extraneous-require
18
+ const semver = require('semver');
19
+ const { METHODS } = require('http');
20
+
21
+ function getFastifyMethods(version) {
22
+ return [
23
+ 'DELETE',
24
+ 'GET',
25
+ 'HEAD',
26
+ 'PATCH',
27
+ 'POST',
28
+ 'PUT',
29
+ 'OPTIONS',
30
+ ...semver.gte(version, '4.4.0') ?
31
+ [
32
+ 'PROPFIND',
33
+ 'PROPPATCH',
34
+ 'MKCOL',
35
+ 'COPY',
36
+ 'MOVE',
37
+ 'LOCK',
38
+ 'UNLOCK',
39
+ 'TRACE',
40
+ 'SEARCH'
41
+ ] : []
42
+ ];
43
+ }
44
+
45
+ module.exports = { getFastifyMethods, METHODS };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/route-coverage",
3
- "version": "1.20.7",
3
+ "version": "1.22.0",
4
4
  "description": "Handles route discovery and observation",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
@@ -17,7 +17,7 @@
17
17
  "test": "../scripts/test.sh"
18
18
  },
19
19
  "dependencies": {
20
- "@contrast/common": "1.21.3",
20
+ "@contrast/common": "1.23.0",
21
21
  "@contrast/fn-inspect": "^4.0.0"
22
22
  }
23
23
  }