Skip to content

Commit

Permalink
feat: use resolved ports in dns interceptor (#3786)
Browse files Browse the repository at this point in the history
Co-authored-by: Carlos Fuentes <me@metcoder.dev>
  • Loading branch information
luddd3 and metcoder95 authored Nov 6, 2024
1 parent 20b8026 commit 45c3259
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/interceptor/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,20 @@ class DNSInstance {
newOpts.affinity
)

let port
if (typeof ip.port === 'number') {
port = `:${ip.port}`
} else if (origin.port !== '') {
port = `:${origin.port}`
} else {
port = ''
}

cb(
null,
`${origin.protocol}//${
ip.family === 6 ? `[${ip.address}]` : ip.address
}${origin.port === '' ? '' : `:${origin.port}`}`
}${port}`
)
})
} else {
Expand All @@ -85,11 +94,20 @@ class DNSInstance {
return
}

let port
if (typeof ip.port === 'number') {
port = `:${ip.port}`
} else if (origin.port !== '') {
port = `:${origin.port}`
} else {
port = ''
}

cb(
null,
`${origin.protocol}//${
ip.family === 6 ? `[${ip.address}]` : ip.address
}${origin.port === '' ? '' : `:${origin.port}`}`
}${port}`
)
}
}
Expand Down
136 changes: 136 additions & 0 deletions test/interceptors/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,142 @@ test('Should prefer affinity (dual stack - 6)', async t => {
t.equal(lookupCounter, 1)
})

test('Should use resolved ports (4)', async t => {
t = tspl(t, { plan: 5 })

let lookupCounter = 0
const server1 = createServer()
const server2 = createServer()
const requestOptions = {
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
}

server1.on('request', (req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('hello world!')
})

server1.listen(0)

server2.on('request', (req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('hello world! (x2)')
})
server2.listen(0)

await Promise.all([once(server1, 'listening'), once(server2, 'listening')])

const client = new Agent().compose([
dns({
lookup (origin, opts, cb) {
lookupCounter++
cb(null, [
{ address: '127.0.0.1', family: 4, port: server1.address().port },
{ address: '127.0.0.1', family: 4, port: server2.address().port }
])
}
})
])

after(async () => {
await client.close()
server1.close()
server2.close()

await Promise.all([once(server1, 'close'), once(server2, 'close')])
})

const response = await client.request({
...requestOptions,
origin: 'http://localhost'
})

t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

const response2 = await client.request({
...requestOptions,
origin: 'http://localhost'
})

t.equal(response2.statusCode, 200)
t.equal(await response2.body.text(), 'hello world! (x2)')

t.equal(lookupCounter, 1)
})

test('Should use resolved ports (6)', async t => {
t = tspl(t, { plan: 5 })

let lookupCounter = 0
const server1 = createServer()
const server2 = createServer()
const requestOptions = {
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
}
}

server1.on('request', (req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('hello world!')
})

server1.listen(0, '::1')

server2.on('request', (req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' })
res.end('hello world! (x2)')
})
server2.listen(0, '::1')

await Promise.all([once(server1, 'listening'), once(server2, 'listening')])

const client = new Agent().compose([
dns({
lookup (origin, opts, cb) {
lookupCounter++
cb(null, [
{ address: '::1', family: 6, port: server1.address().port },
{ address: '::1', family: 6, port: server2.address().port }
])
}
})
])

after(async () => {
await client.close()
server1.close()
server2.close()

await Promise.all([once(server1, 'close'), once(server2, 'close')])
})

const response = await client.request({
...requestOptions,
origin: 'http://localhost'
})

t.equal(response.statusCode, 200)
t.equal(await response.body.text(), 'hello world!')

const response2 = await client.request({
...requestOptions,
origin: 'http://localhost'
})

t.equal(response2.statusCode, 200)
t.equal(await response2.body.text(), 'hello world! (x2)')

t.equal(lookupCounter, 1)
})

test('Should handle max cached items', async t => {
t = tspl(t, { plan: 9 })

Expand Down

0 comments on commit 45c3259

Please sign in to comment.