diff --git a/packages/datadog-plugin-http/test/client.spec.js b/packages/datadog-plugin-http/test/client.spec.js index 22406986b15..42f4c8436f8 100644 --- a/packages/datadog-plugin-http/test/client.spec.js +++ b/packages/datadog-plugin-http/test/client.spec.js @@ -1,6 +1,5 @@ 'use strict' -const getPort = require('get-port') const agent = require('../../dd-trace/test/plugins/agent') const fs = require('fs') const path = require('path') @@ -28,7 +27,7 @@ describe('Plugin', () => { ['http', 'https', 'node:http', 'node:https'].forEach(pluginToBeLoaded => { const protocol = pluginToBeLoaded.split(':')[1] || pluginToBeLoaded describe(pluginToBeLoaded, () => { - function server (app, port, listener) { + function server (app, listener) { let server if (pluginToBeLoaded === 'https') { process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' @@ -41,7 +40,9 @@ describe('Plugin', () => { } else { server = require('node:http').createServer(app) } - server.listen(port, 'localhost', listener) + server.listen(0, 'localhost', () => { + listener(server.address().port) + }) return server } @@ -71,13 +72,12 @@ describe('Plugin', () => { app.get('/user', (req, res) => { res.status(200).send() }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) - req.end() + appListener = server(app, () => { + const port = appListener.address().port + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) }) + req.end() }) } @@ -99,7 +99,8 @@ describe('Plugin', () => { app.get('/user', (req, res) => { res.status(200).send() }) - getPort().then(port => { + + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0]).to.have.property('service', SERVICE_NAME) @@ -115,13 +116,11 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) - - req.end() + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) }) + + req.end() }) }) @@ -132,22 +131,20 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0].meta).to.have.property('http.status_code', '200') - expect(traces[0][0]).to.not.be.undefined - }) - .then(done) - .catch(done) - - appListener = server(app, port, () => { - const req = http.get(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) + agent + .use(traces => { + expect(traces[0][0].meta).to.have.property('http.status_code', '200') + expect(traces[0][0]).to.not.be.undefined + }) + .then(done) + .catch(done) - req.end() + appListener = server(app, port => { + const req = http.get(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) }) + + req.end() }) }) @@ -156,7 +153,8 @@ describe('Plugin', () => { app.get('/user', (req, res) => { res.status(200).send() }) - getPort().then(port => { + + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0]).to.have.property('service', SERVICE_NAME) @@ -172,27 +170,25 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - appListener.on('connect', (req, clientSocket, head) => { - clientSocket.write('HTTP/1.1 200 Connection Established\r\n' + + appListener.on('connect', (req, clientSocket, head) => { + clientSocket.write('HTTP/1.1 200 Connection Established\r\n' + 'Proxy-agent: Node.js-Proxy\r\n' + '\r\n') - clientSocket.end() - appListener.close() - }) - - const req = http.request({ - protocol: `${protocol}:`, - port, - method: 'CONNECT', - hostname: 'localhost', - path: '/user' - }) + clientSocket.end() + appListener.close() + }) - req.on('connect', (res, socket) => socket.end()) - req.on('error', () => {}) - req.end() + const req = http.request({ + protocol: `${protocol}:`, + port, + method: 'CONNECT', + hostname: 'localhost', + path: '/user' }) + + req.on('connect', (res, socket) => socket.end()) + req.on('error', () => {}) + req.end() }) }) @@ -201,7 +197,8 @@ describe('Plugin', () => { app.get('/user', (req, res) => { res.status(200).send() }) - getPort().then(port => { + + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0]).to.have.property('service', SERVICE_NAME) @@ -216,30 +213,28 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - appListener.on('upgrade', (req, socket, head) => { - socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' + + appListener.on('upgrade', (req, socket, head) => { + socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' + 'Upgrade: WebSocket\r\n' + 'Connection: Upgrade\r\n' + '\r\n') - socket.pipe(socket) - }) - - const req = http.request({ - protocol: `${protocol}:`, - port, - hostname: 'localhost', - path: '/user', - headers: { - Connection: 'Upgrade', - Upgrade: 'websocket' - } - }) + socket.pipe(socket) + }) - req.on('upgrade', (res, socket) => socket.end()) - req.on('error', () => {}) - req.end() + const req = http.request({ + protocol: `${protocol}:`, + port, + hostname: 'localhost', + path: '/user', + headers: { + Connection: 'Upgrade', + Upgrade: 'websocket' + } }) + + req.on('upgrade', (res, socket) => socket.end()) + req.on('error', () => {}) + req.end() }) }) @@ -249,7 +244,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0].meta).to.have.property('http.url', `${protocol}://localhost:${port}/user`) @@ -263,12 +258,9 @@ describe('Plugin', () => { port, path: '/user' } + const req = http.request(uri) - appListener = server(app, port, () => { - const req = http.request(uri) - - req.end() - }) + req.end() }) }) @@ -279,7 +271,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0].meta).to.have.property('http.status_code', '200') @@ -288,13 +280,11 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user?foo=bar`, res => { - res.on('data', () => {}) - }) - - req.end() + const req = http.request(`${protocol}://localhost:${port}/user?foo=bar`, res => { + res.on('data', () => {}) }) + + req.end() }) }) @@ -307,7 +297,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0].meta).to.have.property('http.status_code', '200') @@ -316,11 +306,9 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/another-path`, { path: '/user' }) + const req = http.request(`${protocol}://localhost:${port}/another-path`, { path: '/user' }) - req.end() - }) + req.end() }) }) } @@ -332,7 +320,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0].meta).to.have.property('http.status_code', '200') @@ -347,31 +335,28 @@ describe('Plugin', () => { port, pathname: '/another-path' } + const req = http.request(uri, { path: '/user' }) - appListener = server(app, port, () => { - const req = http.request(uri, { path: '/user' }) - - req.end() - }) + req.end() }) }) - it('should support configuration as an WHATWG URL object', async () => { + it('should support configuration as an WHATWG URL object', done => { const app = express() - const port = await getPort() - const url = new URL(`${protocol}://localhost:${port}/user`) - app.get('/user', (req, res) => res.status(200).send()) + appListener = server(app, port => { + const url = new URL(`${protocol}://localhost:${port}/user`) + + app.get('/user', (req, res) => res.status(200).send()) + + agent.use(traces => { + expect(traces[0][0].meta).to.have.property('http.status_code', '200') + expect(traces[0][0].meta).to.have.property('http.url', `${protocol}://localhost:${port}/user`) + }).then(done, done) - appListener = server(app, port, () => { const req = http.request(url) req.end() }) - - await agent.use(traces => { - expect(traces[0][0].meta).to.have.property('http.status_code', '200') - expect(traces[0][0].meta).to.have.property('http.url', `${protocol}://localhost:${port}/user`) - }) }) it('should use the correct defaults when not specified', done => { @@ -381,7 +366,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0].meta).to.have.property('http.url', `${protocol}://localhost:${port}/`) @@ -389,14 +374,12 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - const req = http.request({ - protocol: `${protocol}:`, - port - }) - - req.end() + const req = http.request({ + protocol: `${protocol}:`, + port }) + + req.end() }) }) @@ -407,19 +390,17 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.not.be.undefined - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0]).to.not.be.undefined + }) + .then(done) + .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`) - req.end() - }) + req.end() }) }) @@ -430,16 +411,14 @@ describe('Plugin', () => { res.status(200).send('OK') }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - setTimeout(() => { - res.on('data', () => done()) - }) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + setTimeout(() => { + res.on('data', () => done()) }) - - req.end() }) + + req.end() }) }) @@ -453,19 +432,17 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0].meta).to.have.property('http.status_code', '200') - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0].meta).to.have.property('http.status_code', '200') + }) + .then(done) + .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`) - req.end() - }) + req.end() }) }) @@ -485,17 +462,15 @@ describe('Plugin', () => { } }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request({ - port, - headers: { - Authorization: 'AWS4-HMAC-SHA256 ...' - } - }) - - req.end() + appListener = server(app, port => { + const req = http.request({ + port, + headers: { + Authorization: 'AWS4-HMAC-SHA256 ...' + } }) + + req.end() }) }) @@ -515,17 +490,15 @@ describe('Plugin', () => { } }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request({ - port, - headers: { - Authorization: ['AWS4-HMAC-SHA256 ...'] - } - }) - - req.end() + appListener = server(app, port => { + const req = http.request({ + port, + headers: { + Authorization: ['AWS4-HMAC-SHA256 ...'] + } }) + + req.end() }) }) @@ -545,17 +518,15 @@ describe('Plugin', () => { } }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request({ - port, - headers: { - 'X-Amz-Signature': 'abc123' - } - }) - - req.end() + appListener = server(app, port => { + const req = http.request({ + port, + headers: { + 'X-Amz-Signature': 'abc123' + } }) + + req.end() }) }) @@ -575,15 +546,13 @@ describe('Plugin', () => { } }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request({ - port, - path: '/?X-Amz-Signature=abc123' - }) - - req.end() + appListener = server(app, port => { + const req = http.request({ + port, + path: '/?X-Amz-Signature=abc123' }) + + req.end() }) }) @@ -593,15 +562,14 @@ describe('Plugin', () => { app.get('/user', (req, res) => { res.status(200).send('OK') }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - expect(tracer.scope().active()).to.be.null - done() - }) - req.end() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + expect(tracer.scope().active()).to.be.null + done() }) + + req.end() }) }) @@ -612,20 +580,18 @@ describe('Plugin', () => { res.status(200).send('OK') }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - const span = tracer.scope().active() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + const span = tracer.scope().active() - res.on('data', () => {}) - res.on('end', () => { - expect(tracer.scope().active()).to.equal(span) - done() - }) + res.on('data', () => {}) + res.on('end', () => { + expect(tracer.scope().active()).to.equal(span) + done() }) - - req.end() }) + + req.end() }) }) @@ -636,42 +602,38 @@ describe('Plugin', () => { res.status(200).send('OK') }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, () => {}) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, () => {}) - req.on('response', () => { - expect(tracer.scope().active()).to.not.be.null - done() - }) - - req.end() + req.on('response', () => { + expect(tracer.scope().active()).to.not.be.null + done() }) + + req.end() }) }) it('should handle connection errors', done => { - getPort().then(port => { - let error - - agent - .use(traces => { - expect(traces[0][0].meta).to.have.property(ERROR_TYPE, error.name) - expect(traces[0][0].meta).to.have.property(ERROR_MESSAGE, error.message || error.code) - expect(traces[0][0].meta).to.have.property(ERROR_STACK, error.stack) - expect(traces[0][0].meta).to.have.property('component', 'http') - }) - .then(done) - .catch(done) - - const req = http.request(`${protocol}://localhost:${port}/user`) - - req.on('error', err => { - error = err + let error + + agent + .use(traces => { + expect(traces[0][0].meta).to.have.property(ERROR_TYPE, error.name) + expect(traces[0][0].meta).to.have.property(ERROR_MESSAGE, error.message || error.code) + expect(traces[0][0].meta).to.have.property(ERROR_STACK, error.stack) + expect(traces[0][0].meta).to.have.property('component', 'http') }) + .then(done) + .catch(done) - req.end() + const req = http.request(`${protocol}://localhost:7357/user`) + + req.on('error', err => { + error = err }) + + req.end() }) it('should not record HTTP 5XX responses as errors by default', done => { @@ -681,21 +643,19 @@ describe('Plugin', () => { res.status(500).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 0) - }) - .then(done) - .catch(done) - - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 0) + }) + .then(done) + .catch(done) - req.end() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) }) + + req.end() }) }) @@ -706,21 +666,19 @@ describe('Plugin', () => { res.status(400).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 1) - }) - .then(done) - .catch(done) - - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 1) + }) + .then(done) + .catch(done) - req.end() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) }) + + req.end() }) }) @@ -729,32 +687,30 @@ describe('Plugin', () => { app.get('/user', (req, res) => {}) - getPort().then(port => { - let error - - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 1) - expect(traces[0][0].meta).to.have.property(ERROR_MESSAGE, error.message) - expect(traces[0][0].meta).to.have.property(ERROR_TYPE, error.name) - expect(traces[0][0].meta).to.have.property(ERROR_STACK, error.stack) - expect(traces[0][0].meta).to.not.have.property('http.status_code') - expect(traces[0][0].meta).to.have.property('component', 'http') - }) - .then(done) - .catch(done) + let error - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 1) + expect(traces[0][0].meta).to.have.property(ERROR_MESSAGE, error.message) + expect(traces[0][0].meta).to.have.property(ERROR_TYPE, error.name) + expect(traces[0][0].meta).to.have.property(ERROR_STACK, error.stack) + expect(traces[0][0].meta).to.not.have.property('http.status_code') + expect(traces[0][0].meta).to.have.property('component', 'http') + }) + .then(done) + .catch(done) - req.on('error', err => { - error = err - }) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) + }) - req.destroy() + req.on('error', err => { + error = err }) + + req.destroy() }) }) @@ -763,24 +719,22 @@ describe('Plugin', () => { app.get('/user', (req, res) => {}) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 0) - expect(traces[0][0].meta).to.not.have.property('http.status_code') - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 0) + expect(traces[0][0].meta).to.not.have.property('http.status_code') + }) + .then(done) + .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) + }) - req.on('abort', () => {}) + req.on('abort', () => {}) - req.abort() - }) + req.abort() }) }) @@ -789,25 +743,23 @@ describe('Plugin', () => { app.get('/user', (req, res) => {}) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 1) - expect(traces[0][0].meta).to.not.have.property('http.status_code') - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 1) + expect(traces[0][0].meta).to.not.have.property('http.status_code') + }) + .then(done) + .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) + }) - req.on('error', () => {}) + req.on('error', () => {}) - req.setTimeout(1) - req.end() - }) + req.setTimeout(1) + req.end() }) }) @@ -822,23 +774,21 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 0) - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 0) + }) + .then(done) + .catch(done) - appListener = server(app, port, async () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) + }) - req.on('error', () => {}) + req.on('error', () => {}) - req.end() - }) + req.end() }) }).timeout(10000) @@ -852,27 +802,25 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 1) - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 1) + }) + .then(done) + .catch(done) - const options = { - agent: new http.Agent({ keepAlive: true, timeout: 5000 }) // custom agent with same default timeout - } + const options = { + agent: new http.Agent({ keepAlive: true, timeout: 5000 }) // custom agent with same default timeout + } - appListener = server(app, port, async () => { - const req = http.request(`${protocol}://localhost:${port}/user`, options, res => { - res.on('data', () => { }) - }) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, options, res => { + res.on('data', () => { }) + }) - req.on('error', () => {}) + req.on('error', () => {}) - req.end() - }) + req.end() }) }).timeout(10000) @@ -886,24 +834,22 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 1) - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 1) + }) + .then(done) + .catch(done) - appListener = server(app, port, async () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) + }) - req.on('error', () => {}) - req.setTimeout(5000) // match default timeout + req.on('error', () => {}) + req.setTimeout(5000) // match default timeout - req.end() - }) + req.end() }) }).timeout(10000) } @@ -924,33 +870,31 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - const spans = traces[0] - expect(spans.length).to.equal(3) + agent + .use(traces => { + const spans = traces[0] + expect(spans.length).to.equal(3) + }) + .then(done) + .catch(done) + + appListener = server(app, port => { + // Activate a new parent span so we capture any double counting that may happen, otherwise double-counts + // would be siblings and our test would only capture 1 as a false positive. + const span = tracer.startSpan('http-test') + tracer.scope().activate(span, () => { + // Test `http(s).request + const req = http.request(`${protocol}://localhost:${port}/user?test=request`, res => { + res.on('data', () => {}) }) - .then(done) - .catch(done) - - appListener = server(app, port, () => { - // Activate a new parent span so we capture any double counting that may happen, otherwise double-counts - // would be siblings and our test would only capture 1 as a false positive. - const span = tracer.startSpan('http-test') - tracer.scope().activate(span, () => { - // Test `http(s).request - const req = http.request(`${protocol}://localhost:${port}/user?test=request`, res => { - res.on('data', () => {}) - }) - req.end() - - // Test `http(s).get` - http.get(`${protocol}://localhost:${port}/user?test=get`, res => { - res.on('data', () => {}) - }) + req.end() - span.finish() + // Test `http(s).get` + http.get(`${protocol}://localhost:${port}/user?test=get`, res => { + res.on('data', () => {}) }) + + span.finish() }) }) }) @@ -962,19 +906,17 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('service', SERVICE_NAME) - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('service', SERVICE_NAME) + }) + .then(done) + .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/abort`) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/abort`) - req.abort() - }) + req.abort() }) }) @@ -985,7 +927,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0].meta).to.have.property('http.status_code', '200') @@ -994,17 +936,15 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - const req = http.request({ - hostname: 'localhost', - port, - path: '/user?foo=bar' - }, res => { - res.on('data', () => {}) - }) - - req.end() + const req = http.request({ + hostname: 'localhost', + port, + path: '/user?foo=bar' + }, res => { + res.on('data', () => {}) }) + + req.end() }) }) @@ -1015,7 +955,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { expect(traces[0][1]).to.have.property('error', 0) @@ -1025,17 +965,15 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - tracer.trace('test.request', (span, finish) => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - res.on('end', () => { - setTimeout(finish) - }) + tracer.trace('test.request', (span, finish) => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) + res.on('end', () => { + setTimeout(finish) }) - - req.end() }) + + req.end() }) }) }) @@ -1048,26 +986,24 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - const timer = setTimeout(done, 100) + const timer = setTimeout(done, 100) - agent - .use(() => { - done(new Error('Noop request was traced.')) - clearTimeout(timer) - }) + agent + .use(() => { + done(new Error('Noop request was traced.')) + clearTimeout(timer) + }) - appListener = server(app, port, () => { - const store = storage.getStore() + appListener = server(app, port => { + const store = storage.getStore() - storage.enterWith({ noop: true }) - const req = http.request(tracer._tracer._url.href) + storage.enterWith({ noop: true }) + const req = http.request(tracer._tracer._url.href) - req.on('error', () => {}) - req.end() + req.on('error', () => {}) + req.end() - storage.enterWith(store) - }) + storage.enterWith(store) }) }) } @@ -1096,23 +1032,21 @@ describe('Plugin', () => { res.end() } - getPort().then(port => { - appListener = server(app, port, () => { - ch.subscribe(sub) + appListener = server(app, port => { + ch.subscribe(sub) - tracer.use('http', false) + tracer.use('http', false) - const req = http.request(`${protocol}://localhost:${port}`, res => { - res.on('error', done) - res.on('data', () => {}) - res.on('end', () => done()) - }) - req.on('error', done) + const req = http.request(`${protocol}://localhost:${port}`, res => { + res.on('error', done) + res.on('data', () => {}) + res.on('end', () => done()) + }) + req.on('error', done) - tracer.use('http', true) + tracer.use('http', true) - req.end() - }) + req.end() }) }) }) @@ -1142,21 +1076,19 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('service', 'custom') - }) - .then(done) - .catch(done) - - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('service', 'custom') + }) + .then(done) + .catch(done) - req.end() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) }) + + req.end() }) }) }) @@ -1192,17 +1124,15 @@ describe('Plugin', () => { } }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request({ - port, - headers: { - Authorization: 'AWS4-HMAC-SHA256 ...' - } - }) - - req.end() + appListener = server(app, port => { + const req = http.request({ + port, + headers: { + Authorization: 'AWS4-HMAC-SHA256 ...' + } }) + + req.end() }) }) }) @@ -1232,21 +1162,19 @@ describe('Plugin', () => { res.status(500).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('error', 1) - }) - .then(done) - .catch(done) - - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('error', 1) + }) + .then(done) + .catch(done) - req.end() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) }) + + req.end() }) }) }) @@ -1276,14 +1204,12 @@ describe('Plugin', () => { app.get('/user', (req, res) => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { serverPort = port - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) - req.end() + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) }) + req.end() }) }, { @@ -1305,7 +1231,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { expect(traces[0][0]).to.have.property('service', `localhost:${port}`) @@ -1313,13 +1239,11 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) - - req.end() + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) }) + + req.end() }) }) }) @@ -1351,7 +1275,7 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { + appListener = server(app, port => { agent .use(traces => { const meta = traces[0][0].meta @@ -1364,15 +1288,13 @@ describe('Plugin', () => { .then(done) .catch(done) - appListener = server(app, port, () => { - const url = `${protocol}://localhost:${port}/user` - const headers = { 'x-baz': 'baz' } - const req = http.request(url, { headers }, res => { - res.on('data', () => {}) - }) - - req.end() + const url = `${protocol}://localhost:${port}/user` + const headers = { 'x-baz': 'baz' } + const req = http.request(url, { headers }, res => { + res.on('data', () => {}) }) + + req.end() }) }) @@ -1383,24 +1305,22 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - const meta = traces[0][0].meta - - expect(meta).to.have.property(`${HTTP_REQUEST_HEADERS}.x-foo`, 'bar') - }) - .then(done) - .catch(done) + agent + .use(traces => { + const meta = traces[0][0].meta - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) + expect(meta).to.have.property(`${HTTP_REQUEST_HEADERS}.x-foo`, 'bar') + }) + .then(done) + .catch(done) - req.setHeader('x-foo', 'bar') - req.end() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) }) + + req.setHeader('x-foo', 'bar') + req.end() }) }) @@ -1411,24 +1331,22 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - const meta = traces[0][0].meta + agent + .use(traces => { + const meta = traces[0][0].meta - expect(meta).to.have.property(`${HTTP_REQUEST_HEADERS}.x-foo`, 'bar1,bar2') - }) - .then(done) - .catch(done) - - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => { }) - }) + expect(meta).to.have.property(`${HTTP_REQUEST_HEADERS}.x-foo`, 'bar1,bar2') + }) + .then(done) + .catch(done) - req.setHeader('x-foo', ['bar1', 'bar2']) - req.end() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => { }) }) + + req.setHeader('x-foo', ['bar1', 'bar2']) + req.end() }) }) }) @@ -1462,23 +1380,21 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - agent - .use(traces => { - expect(traces[0][0]).to.have.property('resource', 'GET /user') - }) - .then(done) - .catch(done) + agent + .use(traces => { + expect(traces[0][0]).to.have.property('resource', 'GET /user') + }) + .then(done) + .catch(done) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) + }) - req._route = '/user' + req._route = '/user' - req.end() - }) + req.end() }) }) }) @@ -1517,15 +1433,13 @@ describe('Plugin', () => { } }) - getPort().then(port => { - appListener = server(app, port, () => { - const req = http.request({ - port, - path: '/users' - }) - - req.end() + appListener = server(app, port => { + const req = http.request({ + port, + path: '/users' }) + + req.end() }) }) }) @@ -1555,23 +1469,21 @@ describe('Plugin', () => { res.status(200).send() }) - getPort().then(port => { - const timer = setTimeout(done, 100) - - agent - .use(() => { - clearTimeout(timer) - done(new Error('Blocklisted requests should not be recorded.')) - }) - .catch(done) + const timer = setTimeout(done, 100) - appListener = server(app, port, () => { - const req = http.request(`${protocol}://localhost:${port}/user`, res => { - res.on('data', () => {}) - }) + agent + .use(() => { + clearTimeout(timer) + done(new Error('Blocklisted requests should not be recorded.')) + }) + .catch(done) - req.end() + appListener = server(app, port => { + const req = http.request(`${protocol}://localhost:${port}/user`, res => { + res.on('data', () => {}) }) + + req.end() }) }) }) diff --git a/packages/datadog-plugin-http/test/server.spec.js b/packages/datadog-plugin-http/test/server.spec.js index ee38d21789e..f3b5f3964ef 100644 --- a/packages/datadog-plugin-http/test/server.spec.js +++ b/packages/datadog-plugin-http/test/server.spec.js @@ -1,6 +1,5 @@ 'use strict' -const getPort = require('get-port') const agent = require('../../dd-trace/test/plugins/agent') const axios = require('axios') const { incomingHttpRequestStart } = require('../../dd-trace/src/appsec/channels') @@ -25,12 +24,6 @@ describe('Plugin', () => { } }) - beforeEach(() => { - return getPort().then(newPort => { - port = newPort - }) - }) - afterEach(() => { appListener && appListener.close() app = null @@ -58,7 +51,10 @@ describe('Plugin', () => { beforeEach(done => { const server = new http.Server(listener) appListener = server - .listen(port, 'localhost', () => done()) + .listen(0, 'localhost', () => { + port = appListener.address().port + done() + }) }) it('should send traces to agent', (done) => { diff --git a/packages/dd-trace/test/plugins/agent.js b/packages/dd-trace/test/plugins/agent.js index 8b64a7b74da..06644401bb4 100644 --- a/packages/dd-trace/test/plugins/agent.js +++ b/packages/dd-trace/test/plugins/agent.js @@ -4,7 +4,6 @@ const http = require('http') const bodyParser = require('body-parser') const msgpack = require('msgpack-lite') const codec = msgpack.createCodec({ int64: true }) -const getPort = require('get-port') const express = require('express') const path = require('path') const ritm = require('../../src/ritm') @@ -270,8 +269,6 @@ module.exports = { res.status(200).send() }) - const port = await getPort() - const server = this.server = http.createServer(agent) const emit = server.emit @@ -283,7 +280,25 @@ module.exports = { server.on('connection', socket => sockets.push(socket)) const promise = new Promise((resolve, reject) => { - listener = server.listen(port, () => resolve()) + listener = server.listen(0, () => { + const port = listener.address().port + + tracer.init(Object.assign({}, { + service: 'test', + env: 'tester', + port, + flushInterval: 0, + plugins: false + }, tracerConfig)) + + tracer.setUrl(`http://127.0.0.1:${port}`) + + for (let i = 0, l = pluginName.length; i < l; i++) { + tracer.use(pluginName[i], config[i]) + } + + resolve() + }) }) pluginName = [].concat(pluginName) @@ -295,20 +310,6 @@ module.exports = { dsmStats = [] }) - tracer.init(Object.assign({}, { - service: 'test', - env: 'tester', - port, - flushInterval: 0, - plugins: false - }, tracerConfig)) - - tracer.setUrl(`http://127.0.0.1:${port}`) - - for (let i = 0, l = pluginName.length; i < l; i++) { - tracer.use(pluginName[i], config[i]) - } - return promise },