Skip to content
Merged
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
4 changes: 4 additions & 0 deletions lib/handler/unwrap-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ module.exports = class UnwrapHandler {
this.#handler.onRequestStart?.(this.#controller, context)
}

onResponseStarted () {
return this.#handler.onResponseStarted?.()
}

onUpgrade (statusCode, rawHeaders, socket) {
this.#handler.onRequestUpgrade?.(this.#controller, statusCode, parseHeaders(rawHeaders), socket)
}
Expand Down
4 changes: 4 additions & 0 deletions lib/handler/wrap-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ module.exports = class WrapHandler {
return this.#handler.onConnect?.(abort, context)
}

onResponseStarted () {
return this.#handler.onResponseStarted?.()
}

onHeaders (statusCode, rawHeaders, resume, statusMessage) {
return this.#handler.onHeaders?.(statusCode, rawHeaders, resume, statusMessage)
}
Expand Down
30 changes: 29 additions & 1 deletion test/fetch/resource-timing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { test } = require('node:test')
const { createServer } = require('node:http')
const { fetch } = require('../..')
const { fetch, Agent } = require('../..')
const { closeServerAsPromise } = require('../utils/node-http')

const {
Expand Down Expand Up @@ -138,3 +138,31 @@ test('redirect timing entries should be included when redirecting', (t, done) =>

t.after(closeServerAsPromise(server))
})

test('responseStart should be greater than 0 with composed interceptor', (t, done) => {
t.plan(4)
const obs = new PerformanceObserver(list => {
const [entry] = list.getEntries()

t.assert.ok(entry.requestStart > 0, `requestStart should be > 0, got ${entry.requestStart}`)
t.assert.ok(entry.responseStart > 0, `responseStart should be > 0, got ${entry.responseStart}`)
t.assert.ok(entry.responseStart >= entry.requestStart, 'responseStart should be >= requestStart')

obs.disconnect()
performance.clearResourceTimings()
done()
})

obs.observe({ entryTypes: ['resource'] })

const dispatcher = new Agent().compose((dispatch) => (opts, handler) => dispatch(opts, handler))

const server = createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.end('ok')
}).listen(0, async () => {
const body = await fetch(`http://localhost:${server.address().port}`, { dispatcher })
t.assert.strictEqual('ok', await body.text())
})

t.after(closeServerAsPromise(server))
})
39 changes: 39 additions & 0 deletions test/node-test/client-dispatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,45 @@ test('dispatches in expected order', async (t) => {
await p.completed
})

test('onResponseStarted is called with interceptor', async (t) => {
const server = http.createServer({ joinDuplicateHeaders: true }, (req, res) => {
res.end('ended')
})
t.after(closeServerAsPromise(server))

const p = tspl(t, { plan: 2 })

server.listen(0, () => {
const pool = new Pool(`http://localhost:${server.address().port}`)
const client = pool.compose((dispatch) => (opts, handler) => dispatch(opts, handler))

t.after(() => { return pool.close() })

let responseStartedCalled = false

client.dispatch({
path: '/',
method: 'GET'
}, {
onConnect () {},
onResponseStarted () {
responseStartedCalled = true
},
onHeaders () {},
onData () {},
onComplete () {
p.strictEqual(responseStartedCalled, true)
p.ok(true)
},
onError (err) {
p.ifError(err)
}
})
})

await p.completed
})

test('dispatches in expected order for http2', async (t) => {
const server = createSecureServer(pem)
server.on('stream', (stream) => {
Expand Down
Loading