Skip to content

Commit

Permalink
feat: forward onRequestSent to handler (nodejs#2375)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored and crysmags committed Feb 27, 2024
1 parent 04fbc08 commit 6123572
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ class Request {
if (channels.bodySent.hasSubscribers) {
channels.bodySent.publish({ request: this })
}

if (this[kHandler].onRequestSent) {
try {
this[kHandler].onRequestSent()
} catch (err) {
this.onError(err)
}
}
}

onConnect (abort) {
Expand Down
13 changes: 11 additions & 2 deletions test/client-dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,8 @@ test('dispatch onBodySent not a function', (t) => {
})

test('dispatch onBodySent buffer', (t) => {
t.plan(3)

const server = http.createServer((req, res) => {
res.end('ad')
})
Expand All @@ -688,20 +690,24 @@ test('dispatch onBodySent buffer', (t) => {
onBodySent (chunk) {
t.equal(chunk.toString(), body)
},
onRequestSent () {
t.pass()
},
onError (err) {
throw err
},
onConnect () {},
onHeaders () {},
onData () {},
onComplete () {
t.end()
t.pass()
}
})
})
})

test('dispatch onBodySent stream', (t) => {
t.plan(8)
const server = http.createServer((req, res) => {
res.end('ad')
})
Expand All @@ -723,6 +729,9 @@ test('dispatch onBodySent stream', (t) => {
t.equal(chunks[currentChunk++], chunk)
sentBytes += Buffer.byteLength(chunk)
},
onRequestSent () {
t.pass()
},
onError (err) {
throw err
},
Expand All @@ -732,7 +741,7 @@ test('dispatch onBodySent stream', (t) => {
onComplete () {
t.equal(currentChunk, chunks.length)
t.equal(sentBytes, toSendBytes)
t.end()
t.pass()
}
})
})
Expand Down

0 comments on commit 6123572

Please sign in to comment.