Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 12 additions & 4 deletions lib/interceptor/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ const DecoratorHandler = require('../handler/decorator-handler')
const { InvalidArgumentError, InformationalError } = require('../core/errors')
const maxInt = Math.pow(2, 31) - 1

function addHostHeader (headers = {}, host) {
if (Array.isArray(headers)) {
const header = ['host', host]
if (Array.isArray(headers[0])) {
return [header, ...headers]
}
return [...header, ...headers]
}
return { host, ...headers }
}

class DNSInstance {
#maxTTL = 0
#maxItems = 0
Expand Down Expand Up @@ -411,10 +422,7 @@ module.exports = interceptorOpts => {
...origDispatchOpts,
servername: origin.hostname, // For SNI on TLS
origin: newOrigin.origin,
headers: {
host: origin.host,
...origDispatchOpts.headers
}
headers: addHostHeader(origDispatchOpts.headers, origin.host)
}

dispatch(
Expand Down
56 changes: 56 additions & 0 deletions test/interceptors/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -1936,3 +1936,59 @@ test('#3951 - Should handle lookup errors correctly', async t => {
origin: 'http://localhost'
}), new Error('lookup error'))
})

test('Various (parameterized) header shapes should work with DNS interceptor', async t => {
const server = createServer({ joinDuplicateHeaders: true })
server.on('request', (req, res) => {
t.equal(req.headers.foo, 'bar')
t.equal(typeof req.headers['0'], 'undefined')
t.match(req.headers.host, /^localhost:\d+$/)
res.end('ok')
})

server.listen(0)
await once(server, 'listening')

const origin = `http://localhost:${server.address().port}`

const { cache: cacheInterceptor, dns: dnsInterceptor } = interceptors

// will expand
const cases = [
{
name: 'array of pairs',
headers: [['foo', 'bar']],
interceptors: [cacheInterceptor(), dnsInterceptor()]
},
{
name: 'record',
headers: { foo: 'bar' },
interceptors: [cacheInterceptor(), dnsInterceptor()]
},
{
name: 'flat array',
headers: ['foo', 'bar'],
// note: cacheInterceptor cannot accept flat arrays as it calls normalizeHeaders
// possibly need to fix
interceptors: [dnsInterceptor()]
},
{
// name: 'record with multi-value',
headers: { foo: ['bar'] },
interceptors: [cacheInterceptor(), dnsInterceptor()]
}
]

t = tspl(t, { plan: cases.length * 4 })

for (const c of cases) {
const agent = new Agent().compose(c.interceptors)
const r = await agent.request({ origin, path: '/', method: 'GET', headers: c.headers })
t.equal(r.statusCode, 200, c.name)
await r.body.text()
await agent.close()
}

server.close()
await once(server, 'close')
})
Loading