Skip to content

Commit

Permalink
fix: don't override user-agent in fetch if specified (#35547)
Browse files Browse the repository at this point in the history
* fix: don't override user-agent in fetch if specified

* Add test for fetch user-agent in middleware

Co-authored-by: JJ Kasper <jj@jjsweb.site>
  • Loading branch information
SethFalco and ijjk committed Mar 28, 2022
1 parent abfbf41 commit ad7cb3b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/next/server/web/sandbox/context.ts
Expand Up @@ -154,7 +154,10 @@ async function createModuleContext(options: {
const prevs = init.headers.get(`x-middleware-subrequest`)?.split(':') || []
const value = prevs.concat(options.module).join(':')
init.headers.set('x-middleware-subrequest', value)
init.headers.set(`user-agent`, `Next.js Middleware`)

if (!init.headers.has('user-agent')) {
init.headers.set(`user-agent`, `Next.js Middleware`)
}

if (typeof input === 'object' && 'url' in input) {
return fetch(input.url, {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/middleware/core/pages/api/headers.js
@@ -0,0 +1,3 @@
export default function handler(req, res) {
res.json({ url: req.url, headers: req.headers })
}
46 changes: 46 additions & 0 deletions test/integration/middleware/core/pages/interface/_middleware.js
Expand Up @@ -31,6 +31,52 @@ export async function middleware(request) {
}
}

if (url.pathname.includes('/fetchUserAgentDefault')) {
try {
const apiRoute = new URL(url)
apiRoute.pathname = '/api/headers'
const res = await fetch(apiRoute)
return new Response(await res.text(), {
status: 200,
headers: {
'content-type': 'application/json',
},
})
} catch (err) {
return new Response(JSON.stringify({ error: err.message }), {
status: 500,
headers: {
'content-type': 'application/json',
},
})
}
}

if (url.pathname.includes('/fetchUserAgentCustom')) {
try {
const apiRoute = new URL(url)
apiRoute.pathname = '/api/headers'
const res = await fetch(apiRoute, {
headers: {
'user-agent': 'custom-agent',
},
})
return new Response(await res.text(), {
status: 200,
headers: {
'content-type': 'application/json',
},
})
} catch (err) {
return new Response(JSON.stringify({ error: err.message }), {
status: 500,
headers: {
'content-type': 'application/json',
},
})
}
}

if (url.pathname.endsWith('/webcrypto')) {
const response = {}
try {
Expand Down
14 changes: 14 additions & 0 deletions test/integration/middleware/core/test/index.test.js
Expand Up @@ -139,6 +139,20 @@ describe('Middleware base tests', () => {
})

function urlTests(_log, locale = '') {
it('should set fetch user agent correctly', async () => {
const res = await fetchViaHTTP(
context.appPort,
`${locale}/interface/fetchUserAgentDefault`
)
expect((await res.json()).headers['user-agent']).toBe('Next.js Middleware')

const res2 = await fetchViaHTTP(
context.appPort,
`${locale}/interface/fetchUserAgentCustom`
)
expect((await res2.json()).headers['user-agent']).toBe('custom-agent')
})

it('rewrites by default to a target location', async () => {
const res = await fetchViaHTTP(context.appPort, `${locale}/urls`)
const html = await res.text()
Expand Down

0 comments on commit ad7cb3b

Please sign in to comment.