|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { createSandbox, FakeAgent, spawnProc } = require('../helpers') |
| 4 | +const path = require('path') |
| 5 | + |
| 6 | +describe('Endpoints collection', () => { |
| 7 | + let sandbox, cwd |
| 8 | + |
| 9 | + before(async function () { |
| 10 | + this.timeout(process.platform === 'win32' ? 90000 : 30000) |
| 11 | + |
| 12 | + sandbox = await createSandbox( |
| 13 | + ['fastify'], |
| 14 | + false |
| 15 | + ) |
| 16 | + |
| 17 | + cwd = sandbox.folder |
| 18 | + }) |
| 19 | + |
| 20 | + after(async function () { |
| 21 | + this.timeout(60000) |
| 22 | + await sandbox.remove() |
| 23 | + }) |
| 24 | + |
| 25 | + function getExpectedEndpoints (framework) { |
| 26 | + const expectedEndpoints = [ |
| 27 | + // Basic routes |
| 28 | + { method: 'GET', path: '/users' }, |
| 29 | + { method: 'HEAD', path: '/users' }, |
| 30 | + { method: 'POST', path: '/users/' }, |
| 31 | + { method: 'PUT', path: '/users/:id' }, |
| 32 | + { method: 'DELETE', path: '/users/:id' }, |
| 33 | + { method: 'PATCH', path: '/users/:id/:name' }, |
| 34 | + { method: 'OPTIONS', path: '/users/:id?' }, |
| 35 | + |
| 36 | + // Route with regex |
| 37 | + { method: 'DELETE', path: '/regex/:hour(^\\d{2})h:minute(^\\d{2})m' }, |
| 38 | + |
| 39 | + // Additional methods |
| 40 | + { method: 'TRACE', path: '/trace-test' }, |
| 41 | + { method: 'HEAD', path: '/head-test' }, |
| 42 | + |
| 43 | + // Custom method |
| 44 | + { method: 'MKCOL', path: '/example/near/:lat-:lng/radius/:r' }, |
| 45 | + |
| 46 | + // Using app.route() |
| 47 | + { method: 'POST', path: '/multi-method' }, |
| 48 | + { method: 'PUT', path: '/multi-method' }, |
| 49 | + { method: 'PATCH', path: '/multi-method' }, |
| 50 | + |
| 51 | + // All supported methods route |
| 52 | + { method: 'GET', path: '/all-methods' }, |
| 53 | + { method: 'HEAD', path: '/all-methods' }, |
| 54 | + { method: 'TRACE', path: '/all-methods' }, |
| 55 | + { method: 'DELETE', path: '/all-methods' }, |
| 56 | + { method: 'OPTIONS', path: '/all-methods' }, |
| 57 | + { method: 'PATCH', path: '/all-methods' }, |
| 58 | + { method: 'PUT', path: '/all-methods' }, |
| 59 | + { method: 'POST', path: '/all-methods' }, |
| 60 | + { method: 'MKCOL', path: '/all-methods' }, // Added with addHttpMethod |
| 61 | + |
| 62 | + // Nested routes with Router |
| 63 | + { method: 'PUT', path: '/v1/nested/:id' }, |
| 64 | + |
| 65 | + // Deeply nested routes |
| 66 | + { method: 'GET', path: '/api/nested' }, |
| 67 | + { method: 'HEAD', path: '/api/nested' }, |
| 68 | + { method: 'GET', path: '/api/sub/deep' }, |
| 69 | + { method: 'HEAD', path: '/api/sub/deep' }, |
| 70 | + { method: 'POST', path: '/api/sub/deep/:id' }, |
| 71 | + |
| 72 | + // Wildcard routes |
| 73 | + { method: 'GET', path: '/wildcard/*' }, |
| 74 | + { method: 'HEAD', path: '/wildcard/*' }, |
| 75 | + { method: 'GET', path: '*' }, |
| 76 | + { method: 'HEAD', path: '*' }, |
| 77 | + |
| 78 | + { method: 'GET', path: '/later' }, |
| 79 | + { method: 'HEAD', path: '/later' }, |
| 80 | + ] |
| 81 | + |
| 82 | + return expectedEndpoints |
| 83 | + } |
| 84 | + |
| 85 | + async function runEndpointTest (framework) { |
| 86 | + let agent, proc |
| 87 | + const appFile = path.join(cwd, 'appsec', 'endpoints-collection', `${framework}.js`) |
| 88 | + |
| 89 | + try { |
| 90 | + agent = await new FakeAgent().start() |
| 91 | + |
| 92 | + const expectedEndpoints = getExpectedEndpoints(framework) |
| 93 | + const endpointsFound = [] |
| 94 | + const isFirstFlags = [] |
| 95 | + |
| 96 | + const telemetryPromise = agent.assertTelemetryReceived(({ payload }) => { |
| 97 | + isFirstFlags.push(Boolean(payload.payload.is_first)) |
| 98 | + |
| 99 | + if (payload.payload.endpoints) { |
| 100 | + payload.payload.endpoints.forEach(endpoint => { |
| 101 | + endpointsFound.push({ |
| 102 | + method: endpoint.method, |
| 103 | + path: endpoint.path, |
| 104 | + type: endpoint.type, |
| 105 | + operation_name: endpoint.operation_name, |
| 106 | + resource_name: endpoint.resource_name |
| 107 | + }) |
| 108 | + }) |
| 109 | + } |
| 110 | + }, 'app-endpoints', 5_000, 4) |
| 111 | + |
| 112 | + proc = await spawnProc(appFile, { |
| 113 | + cwd, |
| 114 | + env: { |
| 115 | + DD_TRACE_AGENT_PORT: agent.port, |
| 116 | + DD_TELEMETRY_HEARTBEAT_INTERVAL: 1, |
| 117 | + DD_API_SECURITY_ENDPOINT_COLLECTION_MESSAGE_LIMIT: '10' |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + await telemetryPromise |
| 122 | + |
| 123 | + const trueCount = isFirstFlags.filter(v => v === true).length |
| 124 | + expect(trueCount).to.equal(1) |
| 125 | + |
| 126 | + // Check that all expected endpoints were found |
| 127 | + expectedEndpoints.forEach(expected => { |
| 128 | + const found = endpointsFound.find(e => |
| 129 | + e.method === expected.method && e.path === expected.path |
| 130 | + ) |
| 131 | + expect(found).to.exist |
| 132 | + expect(found.type).to.equal('REST') |
| 133 | + expect(found.operation_name).to.equal('http.request') |
| 134 | + expect(found.resource_name).to.equal(`${expected.method} ${expected.path}`) |
| 135 | + }) |
| 136 | + |
| 137 | + // check that no additional endpoints were found |
| 138 | + expect(endpointsFound.length).to.equal(expectedEndpoints.length) |
| 139 | + } finally { |
| 140 | + proc?.kill() |
| 141 | + await agent?.stop() |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + it('should send fastify endpoints via telemetry', async () => { |
| 146 | + await runEndpointTest('fastify') |
| 147 | + }) |
| 148 | +}) |
0 commit comments