Skip to content

Commit

Permalink
switch on filename, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Arrowood committed Oct 4, 2023
1 parent 2a76f99 commit 84ecb2a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
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', 'node')
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')
})
2 changes: 1 addition & 1 deletion test/mock-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2449,7 +2449,7 @@ test('MockAgent - using fetch yields a headers object in the reply callback', {
accept: '*/*',
'accept-language': '*',
'sec-fetch-mode': 'cors',
'user-agent': 'node',
'user-agent': 'undici',
'accept-encoding': 'gzip, deflate'
})

Expand Down
2 changes: 1 addition & 1 deletion test/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', { ski
accept: '*/*',
'accept-language': '*',
'sec-fetch-mode': 'cors',
'user-agent': 'node',
'user-agent': 'undici',
'accept-encoding': 'gzip, deflate'
}

Expand Down

0 comments on commit 84ecb2a

Please sign in to comment.