File tree Expand file tree Collapse file tree 4 files changed +67
-1
lines changed
packages/next/server/web/sandbox
test/integration/middleware/core Expand file tree Collapse file tree 4 files changed +67
-1
lines changed Original file line number Diff line number Diff line change @@ -154,7 +154,10 @@ async function createModuleContext(options: {
154154 const prevs = init . headers . get ( `x-middleware-subrequest` ) ?. split ( ':' ) || [ ]
155155 const value = prevs . concat ( options . module ) . join ( ':' )
156156 init . headers . set ( 'x-middleware-subrequest' , value )
157- init . headers . set ( `user-agent` , `Next.js Middleware` )
157+
158+ if ( ! init . headers . has ( 'user-agent' ) ) {
159+ init . headers . set ( `user-agent` , `Next.js Middleware` )
160+ }
158161
159162 if ( typeof input === 'object' && 'url' in input ) {
160163 return fetch ( input . url , {
Original file line number Diff line number Diff line change 1+ export default function handler ( req , res ) {
2+ res . json ( { url : req . url , headers : req . headers } )
3+ }
Original file line number Diff line number Diff line change @@ -31,6 +31,52 @@ export async function middleware(request) {
3131 }
3232 }
3333
34+ if ( url . pathname . includes ( '/fetchUserAgentDefault' ) ) {
35+ try {
36+ const apiRoute = new URL ( url )
37+ apiRoute . pathname = '/api/headers'
38+ const res = await fetch ( apiRoute )
39+ return new Response ( await res . text ( ) , {
40+ status : 200 ,
41+ headers : {
42+ 'content-type' : 'application/json' ,
43+ } ,
44+ } )
45+ } catch ( err ) {
46+ return new Response ( JSON . stringify ( { error : err . message } ) , {
47+ status : 500 ,
48+ headers : {
49+ 'content-type' : 'application/json' ,
50+ } ,
51+ } )
52+ }
53+ }
54+
55+ if ( url . pathname . includes ( '/fetchUserAgentCustom' ) ) {
56+ try {
57+ const apiRoute = new URL ( url )
58+ apiRoute . pathname = '/api/headers'
59+ const res = await fetch ( apiRoute , {
60+ headers : {
61+ 'user-agent' : 'custom-agent' ,
62+ } ,
63+ } )
64+ return new Response ( await res . text ( ) , {
65+ status : 200 ,
66+ headers : {
67+ 'content-type' : 'application/json' ,
68+ } ,
69+ } )
70+ } catch ( err ) {
71+ return new Response ( JSON . stringify ( { error : err . message } ) , {
72+ status : 500 ,
73+ headers : {
74+ 'content-type' : 'application/json' ,
75+ } ,
76+ } )
77+ }
78+ }
79+
3480 if ( url . pathname . endsWith ( '/webcrypto' ) ) {
3581 const response = { }
3682 try {
Original file line number Diff line number Diff line change @@ -139,6 +139,20 @@ describe('Middleware base tests', () => {
139139} )
140140
141141function urlTests ( _log , locale = '' ) {
142+ it ( 'should set fetch user agent correctly' , async ( ) => {
143+ const res = await fetchViaHTTP (
144+ context . appPort ,
145+ `${ locale } /interface/fetchUserAgentDefault`
146+ )
147+ expect ( ( await res . json ( ) ) . headers [ 'user-agent' ] ) . toBe ( 'Next.js Middleware' )
148+
149+ const res2 = await fetchViaHTTP (
150+ context . appPort ,
151+ `${ locale } /interface/fetchUserAgentCustom`
152+ )
153+ expect ( ( await res2 . json ( ) ) . headers [ 'user-agent' ] ) . toBe ( 'custom-agent' )
154+ } )
155+
142156 it ( 'rewrites by default to a target location' , async ( ) => {
143157 const res = await fetchViaHTTP ( context . appPort , `${ locale } /urls` )
144158 const html = await res . text ( )
You can’t perform that action at this time.
0 commit comments