Skip to content

Commit

Permalink
fix requests to agent via IPv6
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter committed Apr 14, 2023
1 parent 0da8386 commit 9e756be
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/dd-trace/src/exporters/common/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ function request (data, options, callback) {
}

if (options.url) {
const url = typeof options.url === 'object' ? options.url : fromUrlString(options.url)
const url = typeof options.url === 'object' ? urlToOptions(options.url) : fromUrlString(options.url)
if (url.protocol === 'unix:') {
options.socketPath = url.pathname
} else {
if (!options.path) options.path = url.path
options.protocol = url.protocol
options.hostname = url.hostname
options.hostname = url.hostname // for IPv6 this should be '::1' and not '[::1]'
options.port = url.port
}
}
Expand Down
38 changes: 38 additions & 0 deletions packages/dd-trace/test/exporters/common/request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,42 @@ describe('request', function () {
done(err)
})
})

describe('when intercepting http', () => {
const sandbox = sinon.createSandbox()

beforeEach(() => {
sandbox.spy(http, 'request')
})

afterEach(() => {
sandbox.reset()
})

it('should properly set request host with IPv6', (done) => {
nock('http://[1337::cafe]:123', {
reqheaders: {
'content-type': 'application/octet-stream',
'content-length': '13'
}
})
.put('/path')
.reply(200, 'OK')

request(
Buffer.from(JSON.stringify({ foo: 'bar' })), {
url: new URL('http://[1337::cafe]:123/path'),
method: 'PUT',
headers: {
'Content-Type': 'application/octet-stream'
}
},
(err, res) => {
const options = http.request.getCall(0).args[0]
expect(options.hostname).to.equal('1337::cafe') // no brackets
expect(res).to.equal('OK')
done(err)
})
})
})
})

0 comments on commit 9e756be

Please sign in to comment.