Skip to content

Commit

Permalink
change default header to node (#2310)
Browse files Browse the repository at this point in the history
* change default header to `node`

* switch on filename, add tests
  • Loading branch information
Ethan-Arrowood committed Oct 5, 2023
1 parent 0fde27d commit fcc1e39
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ async function httpNetworkOrCacheFetch (
// user agents should append `User-Agent`/default `User-Agent` value to
// httpRequest’s header list.
if (!httpRequest.headersList.contains('user-agent')) {
httpRequest.headersList.append('user-agent', 'undici')
httpRequest.headersList.append('user-agent', __filename.endsWith('index.js') ? 'undici' : 'node')
}

// 15. If httpRequest’s cache mode is "default" and httpRequest’s header
Expand Down
25 changes: 25 additions & 0 deletions test/fetch/user-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const { test } = require('tap')
const events = require('events')
const http = require('http')
const undici = require('../../')
const nodeBuild = require('../../undici-fetch.js')

test('user-agent defaults correctly', async (t) => {
const server = http.createServer((req, res) => {
res.end(JSON.stringify({ userAgentHeader: req.headers['user-agent'] }))
})
t.teardown(server.close.bind(server))

server.listen(0)
await events.once(server, 'listening')
const url = `http://localhost:${server.address().port}`
const [nodeBuildJSON, undiciJSON] = await Promise.all([
nodeBuild.fetch(url).then((body) => body.json()),
undici.fetch(url).then((body) => body.json())
])

t.same(nodeBuildJSON.userAgentHeader, 'node')
t.same(undiciJSON.userAgentHeader, 'undici')
})

0 comments on commit fcc1e39

Please sign in to comment.