Skip to content

Commit

Permalink
fix(#3736): back-port 183f8e9 to v6.x (#3855)
Browse files Browse the repository at this point in the history
* fix(#3736): back-port 183f8e9 to v6.x

* Backport #3769: fix http2 test
  • Loading branch information
ggoodman authored Nov 21, 2024
1 parent 61ec353 commit 353ab63
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class RequestHandler extends AsyncResource {
this.removeAbortListener = util.addAbortListener(this.signal, () => {
this.reason = this.signal.reason ?? new RequestAbortedError()
if (this.res) {
util.destroy(this.res, this.reason)
util.destroy(this.res.on('error', util.nop), this.reason)
} else if (this.abort) {
this.abort(this.reason)
}
Expand Down
36 changes: 36 additions & 0 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -1252,3 +1252,39 @@ test('request post body DataView', async (t) => {

await t.completed
})

test('#3736 - Aborted Response (without consuming body)', async (t) => {
const plan = tspl(t, { plan: 1 })

const controller = new AbortController()
const server = createServer((req, res) => {
setTimeout(() => {
res.writeHead(200, 'ok', {
'content-type': 'text/plain'
})
res.write('hello from server')
res.end()
}, 100)
})

server.listen(0)

await EE.once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)

after(server.close.bind(server))
after(client.destroy.bind(client))

const { signal } = controller
const promise = client.request({
path: '/',
method: 'GET',
signal
})

controller.abort()

await plan.rejects(promise, { message: 'This operation was aborted' })

await plan.completed
})
24 changes: 8 additions & 16 deletions test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,23 +334,15 @@ test(

after(() => server.close())
after(() => client.close())
t = tspl(t, { plan: 2 })
t = tspl(t, { plan: 1 })

try {
await client.request({
path: '/',
method: 'GET',
headers: {
'x-my-header': 'foo'
}
})
} catch (error) {
t.strictEqual(
error.message,
'Client network socket disconnected before secure TLS connection was established'
)
t.strictEqual(error.code, 'ECONNRESET')
}
await t.rejects(client.request({
path: '/',
method: 'GET',
headers: {
'x-my-header': 'foo'
}
}))
}
)

Expand Down

0 comments on commit 353ab63

Please sign in to comment.