Skip to content

Commit

Permalink
Filter out undefined headers (#40)
Browse files Browse the repository at this point in the history
* Filter out undefined headers

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Dec 4, 2024
1 parent 86654a3 commit 88dee75
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,18 @@ function wire ({ server: newServer, port, ...undiciOpts }) {
if (msg.type === 'request') {
const { id, opts } = msg

const headers = {}

for (const [key, value] of Object.entries(opts.headers)) {
if (value !== undefined && value !== null) {
headers[key] = value
}
}

const injectOpts = {
method: opts.method,
url: opts.path,
headers: opts.headers,
headers,
query: opts.query,
body: opts.body instanceof Uint8Array ? Buffer.from(opts.body) : opts.body,
}
Expand Down
20 changes: 20 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,23 @@ test('aborting a request', async (t) => {
signal: abortController.signal,
}))
})

test('empty header', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'empty-headers.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

const { statusCode, body } = await request('http://myserver.local', {
dispatcher: agent,
headers: { foo: undefined }
})

strictEqual(statusCode, 200)
deepStrictEqual(await body.text(), 'hello world')
})
11 changes: 11 additions & 0 deletions test/fixtures/empty-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'

const { parentPort } = require('node:worker_threads')
const { wire } = require('../../')

const app = (req, res) => {
// res.setHeader('foo', undefined)
res.end('hello world')
}

wire({ server: app, port: parentPort })

0 comments on commit 88dee75

Please sign in to comment.