Skip to content

Commit

Permalink
fix: undefined function called
Browse files Browse the repository at this point in the history
  • Loading branch information
luddd3 committed Dec 17, 2024
1 parent 2c36442 commit 8576bf6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/interceptor/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ class DNSInstance {
this.#records.set(origin.hostname, records)
}

deleteRecords (origin) {
this.#records.delete(origin.hostname)
}

getHandler (meta, opts) {
return new DNSDispatchHandler(this, meta, opts)
}
Expand Down Expand Up @@ -261,7 +265,7 @@ class DNSDispatchHandler extends DecoratorHandler {
break
}
case 'ENOTFOUND':
this.#state.deleteRecord(this.#origin)
this.#state.deleteRecords(this.#origin)
// eslint-disable-next-line no-fallthrough
default:
super.onResponseError(controller, err)
Expand Down
62 changes: 62 additions & 0 deletions test/interceptors/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,68 @@ test('Should handle max cached items', async t => {
t.equal(await response3.body.text(), 'hello world! (x2)')
})

test('Should handle ENOTFOUND response error', async t => {
t = tspl(t, { plan: 4 })
let lookupCounter = 0
const hostname = 'something-which-should-result-in-enotfound.undici'

const requestOptions = {
method: 'GET',
path: '/',
origin: 'http://localhost'
}

const client = new Agent().compose([
dispatch => {
return (opts, handler) => {
opts.origin = new URL(`http://${hostname}`)
return dispatch(opts, handler)
}
},
dns({
lookup (origin, opts, cb) {
lookupCounter++
if (lookupCounter === 1) {
cb(null, [
{
address: '127.0.0.1',
family: 4
}
])
} else {
// Causes InformationalError
cb(null, [])
}
}
})
])

after(async () => {
await client.close()
})

let error1
try {
await client.request(requestOptions)
} catch (err) {
error1 = err
}
t.equal(error1.code, 'ENOTFOUND')
t.equal(error1.hostname, hostname)

// Test that the records in the dns interceptor were deleted after the
// previous request
let error2
try {
await client.request(requestOptions)
} catch (err) {
error2 = err
}
t.equal(error2.name, 'InformationalError')

t.equal(lookupCounter, 2)
})

test('#3937 - Handle host correctly', async t => {
t = tspl(t, { plan: 10 })

Expand Down

0 comments on commit 8576bf6

Please sign in to comment.