Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use resolved ports in dns interceptor #3786

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
73 changes: 73 additions & 0 deletions test/interceptors/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,79 @@ test('Should prefer affinity (dual stack - 6)', async t => {
t.equal(lookupCounter, 1)
})

test('Should use resolved ports', async t => {
luddd3 marked this conversation as resolved.
Show resolved Hide resolved
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([
dispatch => {
return (opts, handler) => {
return dispatch(opts, handler)
}
luddd3 marked this conversation as resolved.
Show resolved Hide resolved
},
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 handle max cached items', async t => {
t = tspl(t, { plan: 9 })

Expand Down
Loading