@contrast/route-coverage 1.35.1 → 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,218 +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
- const { getFastifyMethods } = require('../utils/methods');
9
-
10
- [
11
- { version: '>=3 <4.1.0', routeObj: false },
12
- { version: '>=4.1.0 <4.4.0', routeObj: true },
13
- { version: '>=4.4.0 <5', routeObj: true }
14
- ].forEach(({ version, routeObj }) => {
15
- describe(`route-coverage fastify (${version})`, function () {
16
- const FASTIFY_METHODS = getFastifyMethods(version);
17
- let core, RouteMock, framework, routing, fn;
18
-
19
- function routeOptions(options, fullyDeclared, routeObj) {
20
- if (fullyDeclared && routeObj) {
21
- return [{ options }];
22
- } else if ((fullyDeclared && !routeObj) || (!fullyDeclared && routeObj)) {
23
- return [options];
24
- } else if (!fullyDeclared && !routeObj) {
25
- return Object.values(options);
26
- }
27
- }
28
-
29
- beforeEach(function () {
30
- core = mocks.core();
31
- core.logger = mocks.logger();
32
- core.routeCoverage = mocks.routeCoverage();
33
- core.scopes = scopes(core);
34
- core.depHooks = mocks.depHooks();
35
- core.patcher = patcher(core);
36
- sinon.spy(core.patcher, 'patch');
37
-
38
- RouteMock = {
39
- buildRouting: () => ({
40
- prepareRoute: sinon.stub(),
41
- route: sinon.stub()
42
- })
43
- };
44
-
45
- framework = 'fastify';
46
- core.depHooks.resolve
47
- .withArgs({ name: 'fastify', version, file: 'lib/route.js' })
48
- .yields(RouteMock);
49
-
50
- require('./fastify')(core).install();
51
-
52
- routing = RouteMock.buildRouting();
53
- fn = sinon.stub();
54
- });
55
-
56
- [
57
- { method: 'prepareRoute', fullyDefined: false },
58
- { method: 'route', fullyDefined: true },
59
- ].forEach(({ method, fullyDefined }) => {
60
- describe(`.${method} base cases`, function() {
61
- it('skips instrumenting if nothing is provided', function () {
62
- routing[method]();
63
- expect(core.routeCoverage.discover).not.to.have.been.called;
64
- });
65
-
66
- it('skips instrumenting if there is no method provided', function () {
67
- routing[method](...routeOptions({ url: '/test/route', handler: fn }, fullyDefined, routeObj));
68
- expect(core.routeCoverage.discover).not.to.have.been.called;
69
- });
70
-
71
- it('skips instrumenting if there is no url provided', function () {
72
- routing[method](...routeOptions({ method: 'GET', handler: fn }, fullyDefined, routeObj));
73
- expect(core.routeCoverage.discover).not.to.have.been.called;
74
- });
75
-
76
- it('skips instrumenting if there is no handler is provided', function () {
77
- routing[method](...routeOptions({ method: 'GET', url: '/test/route' }, fullyDefined, routeObj));
78
- expect(core.routeCoverage.discover).not.to.have.been.called;
79
- });
80
- });
81
- });
82
-
83
- it('discovers a route with a single method', function () {
84
- routing.prepareRoute(...routeOptions({ method: 'GET', url: '/test/route', handler: fn }, false, routeObj));
85
- expect(core.routeCoverage.discover).to.have.been.calledWith({
86
- signature: "fastify.get('/test/route', [Function])",
87
- url: '/test/route',
88
- normalizedUrl: '/test/route',
89
- method: 'get',
90
- framework
91
- });
92
- expect(core.routeCoverage.discover).to.have.been.called;
93
- expect(core.routeCoverage.discover).to.have.callCount(1);
94
- });
95
-
96
- it('discovers a route with a prefixed path', function () {
97
- routing[Symbol('fastify.routePrefix')] = '/prefix';
98
- routing.prepareRoute(...routeOptions({ method: 'GET', url: '/test/route', handler: fn }, false, routeObj));
99
- expect(core.routeCoverage.discover).to.have.been.calledWith({
100
- signature: "fastify.get('/prefix/test/route', [Function])",
101
- url: '/prefix/test/route',
102
- normalizedUrl: '/prefix/test/route',
103
- method: 'get',
104
- framework
105
- });
106
- expect(core.routeCoverage.discover).to.have.been.called;
107
- expect(core.routeCoverage.discover).to.have.callCount(1);
108
- });
109
-
110
- it('discovers a fully declared (.route) with a prefixed path', function () {
111
- routing[Symbol('fastify.routePrefix')] = '/prefix';
112
- routing.route(...routeOptions({ method: 'GET', url: '/test/route', handler: fn }, true, routeObj));
113
- expect(core.routeCoverage.discover).to.have.been.calledWith({
114
- signature: "fastify.route({ method: 'get', url: '/prefix/test/route', handler: [Function] })",
115
- url: '/prefix/test/route',
116
- normalizedUrl: '/prefix/test/route',
117
- method: 'get',
118
- framework
119
- });
120
- expect(core.routeCoverage.discover).to.have.been.called;
121
- expect(core.routeCoverage.discover).to.have.callCount(1);
122
- });
123
-
124
- it('discovers a fully declared (.route) route with a single method', function () {
125
- routing.route(...routeOptions({ method: 'GET', url: '/test/route', handler: fn }, true, routeObj));
126
- expect(core.routeCoverage.discover).to.have.been.calledWith({
127
- signature: "fastify.route({ method: 'get', url: '/test/route', handler: [Function] })",
128
- url: '/test/route',
129
- normalizedUrl: '/test/route',
130
- method: 'get',
131
- framework
132
- });
133
- expect(core.routeCoverage.discover).to.have.been.called;
134
- expect(core.routeCoverage.discover).to.have.callCount(1);
135
- });
136
-
137
- it('discovers a route with multiple methods', function () {
138
- routing.prepareRoute(...routeOptions({ method: ['GET', 'POST'], url: '/test/route', handler: fn }, false, routeObj));
139
- expect(core.routeCoverage.discover).to.have.been.calledWith({
140
- signature: "fastify.get('/test/route', [Function])",
141
- url: '/test/route',
142
- normalizedUrl: '/test/route',
143
- method: 'get',
144
- framework
145
- });
146
-
147
- expect(core.routeCoverage.discover).to.have.been.calledWith({
148
- signature: "fastify.post('/test/route', [Function])",
149
- url: '/test/route',
150
- normalizedUrl: '/test/route',
151
- method: 'post',
152
- framework
153
- });
154
-
155
- expect(core.routeCoverage.discover).to.have.callCount(2);
156
- });
157
-
158
- it('discovers a route with all methods', function () {
159
- routing.prepareRoute(...routeOptions({ method: FASTIFY_METHODS, url: '/test/route', handler: fn }, false, routeObj));
160
- expect(core.routeCoverage.discover).to.have.been.calledWith({
161
- signature: "fastify.all('/test/route', [Function])",
162
- url: '/test/route',
163
- normalizedUrl: '/test/route',
164
- method: 'all',
165
- framework
166
- });
167
- });
168
-
169
- it('discovers a fully declared route (.route) with multiple methods', function () {
170
- routing.route(...routeOptions({ method: ['GET', 'POST'], url: '/test/route', handler: fn }, true, routeObj));
171
- expect(core.routeCoverage.discover).to.have.been.calledWith({
172
- signature: "fastify.route({ method: 'get', url: '/test/route', handler: [Function] })",
173
- url: '/test/route',
174
- normalizedUrl: '/test/route',
175
- method: 'get',
176
- framework
177
- });
178
-
179
- expect(core.routeCoverage.discover).to.have.been.calledWith({
180
- signature: "fastify.route({ method: 'post', url: '/test/route', handler: [Function] })",
181
- url: '/test/route',
182
- normalizedUrl: '/test/route',
183
- method: 'post',
184
- framework
185
- });
186
-
187
- expect(core.routeCoverage.discover).to.have.callCount(2);
188
- });
189
-
190
- it('observes a route when the route handler is called', function () {
191
- routing.prepareRoute(...routeOptions({ method: 'GET', url: '/test/route', handler: fn }, false, routeObj));
192
- const patchedFn = core.patcher.patch.getCall(3).returnValue;
193
- const handler = routeObj ? patchedFn.handler : patchedFn;
194
- handler({ raw: { method: 'GET' }, url: '/test/route' });
195
- expect(core.routeCoverage.observe).to.have.been.calledWith({
196
- signature: "fastify.get('/test/route', [Function])",
197
- url: '/test/route',
198
- method: 'get',
199
- normalizedUrl: '/test/route',
200
- framework: 'fastify'
201
- });
202
- });
203
-
204
- it('removes query string from url before reporting observation', function () {
205
- routing.prepareRoute(...routeOptions({ method: 'GET', url: '/test/route', handler: fn }, false, routeObj));
206
- const patchedFn = core.patcher.patch.getCall(3).returnValue;
207
- const handler = routeObj ? patchedFn.handler : patchedFn;
208
- handler({ raw: { method: 'GET' }, url: '/test/route' });
209
- expect(core.routeCoverage.observe).to.have.been.calledWith({
210
- signature: "fastify.get('/test/route', [Function])",
211
- url: '/test/route',
212
- method: 'get',
213
- normalizedUrl: '/test/route',
214
- framework: 'fastify'
215
- });
216
- });
217
- });
218
- });
@@ -1,175 +0,0 @@
1
- 'use strict';
2
-
3
- const { expect } = require('chai');
4
- const sinon = require('sinon');
5
- const { initCoreFixture } = require('@contrast/test/fixtures');
6
-
7
- describe('route-coverage graphql', function () {
8
- let core;
9
- let simulateRequestScope;
10
- let MockSchema;
11
-
12
- beforeEach(function () {
13
- ({ core, simulateRequestScope } = initCoreFixture());
14
- require('..')(core);
15
- sinon.spy(core.routeCoverage, 'discover');
16
- sinon.spy(core.routeCoverage, 'observe');
17
-
18
- const xport = {
19
- GraphQLSchema: class GraphQLSchema {
20
- constructor() {}
21
- }
22
- };
23
-
24
- core.depHooks.resolve.withArgs({ name: 'graphql', version: '>=14 <17', file: './type/schema.js' }).yields(xport);
25
- require('./graphql')(core).install();
26
- MockSchema = xport.GraphQLSchema;
27
- });
28
-
29
- it('reports discovery and observation by instrumenting config object passed to GraphQLSchema constructor', function () {
30
- const config = {
31
- query: {
32
- name: 'Query',
33
- _fields: {
34
- getThis: {
35
- name: 'getThis',
36
- args: [{
37
- name: 'id',
38
- type: class GetThisType {},
39
- }],
40
- resolve() {},
41
- },
42
- getThat: {
43
- name: 'getThat',
44
- resolve() {},
45
- },
46
- }
47
- },
48
- mutation: {
49
- name: 'Mutation',
50
- _fields: {
51
- createThis: {
52
- name: 'createThis',
53
- args: [{
54
- name: 'id',
55
- type: class CreateThisType {},
56
- }],
57
- resolve() {},
58
- },
59
- updateThat: {
60
- name: 'updateThat',
61
- resolve() {},
62
- }
63
- }
64
- }
65
- };
66
- new MockSchema(config);
67
-
68
- [
69
- {
70
- method: 'get',
71
- normalizedUrl: '/Query/getThis/{id}',
72
- signature: 'GraphQL Query getThis(id: GetThisType)',
73
- framework: 'graphql'
74
- },
75
- {
76
- method: 'post',
77
- normalizedUrl: '/Query/getThis/{id}',
78
- signature: 'GraphQL Query getThis(id: GetThisType)',
79
- framework: 'graphql'
80
- },
81
- {
82
- method: 'get',
83
- normalizedUrl: '/Query/getThat',
84
- signature: 'GraphQL Query getThat',
85
- framework: 'graphql'
86
- },
87
- {
88
- method: 'post',
89
- normalizedUrl: '/Query/getThat',
90
- signature: 'GraphQL Query getThat',
91
- framework: 'graphql'
92
- },
93
- {
94
- method: 'get',
95
- normalizedUrl: '/Mutation/createThis/{id}',
96
- signature: 'GraphQL Mutation createThis(id: CreateThisType)',
97
- framework: 'graphql'
98
- },
99
-
100
- {
101
- method: 'post',
102
- normalizedUrl: '/Mutation/createThis/{id}',
103
- signature: 'GraphQL Mutation createThis(id: CreateThisType)',
104
- framework: 'graphql'
105
- },
106
- {
107
- method: 'get',
108
- normalizedUrl: '/Mutation/updateThat',
109
- signature: 'GraphQL Mutation updateThat',
110
- framework: 'graphql'
111
- },
112
- {
113
- method: 'post',
114
- normalizedUrl: '/Mutation/updateThat',
115
- signature: 'GraphQL Mutation updateThat',
116
- framework: 'graphql'
117
- }
118
- ].forEach((discovery) => {
119
- try {
120
- expect(core.routeCoverage.discover).to.have.been.calledWithMatch(discovery);
121
- } catch (err) {
122
- console.log('not reported:', discovery);
123
- console.log(core.routeCoverage.discover.getCalls().map((c) => c.args[0]));
124
- throw err;
125
- }
126
- });
127
-
128
- simulateRequestScope(() => {
129
- // exercise resolvers to trigger observation
130
- config.query._fields.getThis.resolve();
131
- config.query._fields.getThat.resolve();
132
- config.mutation._fields.createThis.resolve();
133
- config.mutation._fields.updateThat.resolve();
134
-
135
- [
136
- {
137
- method: 'get',
138
- normalizedUrl: '/Query/getThis/{id}',
139
- signature: 'GraphQL Query getThis(id: GetThisType)',
140
- url: '/Query/getThis/{id}',
141
- framework: 'graphql'
142
- },
143
- {
144
- method: 'get',
145
- normalizedUrl: '/Query/getThat',
146
- signature: 'GraphQL Query getThat',
147
- url: '/Query/getThat',
148
- framework: 'graphql'
149
- },
150
- {
151
- method: 'get',
152
- normalizedUrl: '/Mutation/createThis/{id}',
153
- signature: 'GraphQL Mutation createThis(id: CreateThisType)',
154
- url: '/Mutation/createThis/{id}',
155
- framework: 'graphql'
156
- },
157
- {
158
- method: 'get',
159
- normalizedUrl: '/Mutation/updateThat',
160
- signature: 'GraphQL Mutation updateThat',
161
- url: '/Mutation/updateThat',
162
- framework: 'graphql'
163
- }
164
- ].forEach((observation) => {
165
- try {
166
- expect(core.routeCoverage.observe).to.have.been.calledWithMatch(observation);
167
- } catch (err) {
168
- console.log('not reported:', observation);
169
- console.log(core.routeCoverage.observe.getCalls().map((c) => c.args[0]));
170
- throw err;
171
- }
172
- });
173
- });
174
- });
175
- });
@@ -1,127 +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
- describe('route-coverage hapi', function () {
10
- let core, route, _core, hapi, 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
- route = {
21
- settings: {
22
- handler: sinon.stub()
23
- }
24
- };
25
-
26
- _core = {
27
- router: {
28
- add() {
29
- return { route };
30
- }
31
- }
32
- };
33
-
34
- hapi = {
35
- server() {
36
- return { _core };
37
- },
38
- Server() {
39
- return { _core };
40
- }
41
- };
42
- framework = 'hapi';
43
-
44
- core.depHooks.resolve.withArgs({ name: '@hapi/hapi', version: '>=18 <22' }).yields(hapi);
45
- require('./hapi')(core).install();
46
- });
47
-
48
-
49
- ['server', 'Server'].forEach((server) => {
50
-
51
- it('does not report a non-existent route', function () {
52
- const hapiServer = hapi[server]();
53
- hapiServer._core.router.add();
54
- expect(core.routeCoverage.discover).not.to.have.been.called;
55
- });
56
-
57
- it('does not report a route missing a method', function () {
58
- const hapiServer = hapi[server]();
59
- hapiServer._core.router.add({ path: '/foo' });
60
- expect(core.routeCoverage.discover).not.to.have.been.called;
61
- });
62
-
63
- it('does not report a route missing a path', function () {
64
- const hapiServer = hapi[server]();
65
- hapiServer._core.router.add({ method: 'GET' });
66
- expect(core.routeCoverage.discover).not.to.have.been.called;
67
- });
68
-
69
- it('discovers a route with a single method', function () {
70
- const hapiServer = hapi[server]();
71
- hapiServer._core.router.add({ method: 'GET', path: '/foo' });
72
-
73
- expect(core.routeCoverage.discover).to.have.been.calledWith({
74
- signature: "server.route({ method: 'get', path: '/foo', handler: [Function] })",
75
- url: '/foo',
76
- normalizedUrl: '/foo',
77
- method: 'get',
78
- framework
79
- });
80
- });
81
-
82
- it('discovers a route with multiple methods', function () {
83
- const hapiServer = hapi[server]();
84
- hapiServer._core.router.add({ method: ['GET', 'PUT'], path: '/foo' });
85
-
86
- expect(core.routeCoverage.discover).to.have.been.calledWith({
87
- signature: "server.route({ method: 'get', path: '/foo', handler: [Function] })",
88
- url: '/foo',
89
- normalizedUrl: '/foo',
90
- method: 'get',
91
- framework
92
- });
93
-
94
- expect(core.routeCoverage.discover).to.have.been.calledWith({
95
- signature: "server.route({ method: 'put', path: '/foo', handler: [Function] })",
96
- url: '/foo',
97
- normalizedUrl: '/foo',
98
- method: 'put',
99
- framework
100
- });
101
- });
102
-
103
- it('observes a route when a route handler is exercised', async function () {
104
- const hapiServer = hapi[server]();
105
- const { route } = hapiServer._core.router.add({ method: 'get', path: '/foo' });
106
- route.settings.handler({ method: 'get', path: '/foo', route: { path: '/foo' } });
107
- expect(core.routeCoverage.observe).to.have.been.calledWith({
108
- signature: "server.route({ method: 'get', path: '/foo', handler: [Function] })",
109
- url: '/foo',
110
- normalizedUrl: '/foo',
111
- method: 'get'
112
- });
113
- });
114
-
115
- it('observes a route when a route contains parameters', async function () {
116
- const hapiServer = hapi[server]();
117
- const { route } = hapiServer._core.router.add({ method: 'get', path: '/foo/{id}' });
118
- route.settings.handler({ method: 'get', path: '/foo/1', route: { path: '/foo/{id}' } });
119
- expect(core.routeCoverage.observe).to.have.been.calledWith({
120
- signature: "server.route({ method: 'get', path: '/foo/{id}', handler: [Function] })",
121
- url: '/foo/1',
122
- normalizedUrl: '/foo/{id}',
123
- method: 'get'
124
- });
125
- });
126
- });
127
- });
@@ -1,152 +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
- 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 <3' }).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
- });