@@ -162,6 +162,53 @@ describe("toResponse", () => {
162162 } ) ;
163163 } ) ;
164164
165+ describe ( "Set-Cookie header preservation" , ( ) => {
166+ it ( "should preserve multiple Set-Cookie when merging onto existing Response" , ( ) => {
167+ const res = new Response ( "ok" ) ;
168+ const initHeaders = new Headers ( ) ;
169+ initHeaders . append ( "set-cookie" , "session=abc123; Path=/" ) ;
170+ initHeaders . append ( "set-cookie" , "session_data=xyz; Path=/" ) ;
171+ const response = toResponse ( res , { headers : initHeaders } ) ;
172+ const setCookies = response . headers . getSetCookie ?.( ) ?? [ ] ;
173+ expect ( setCookies ) . toHaveLength ( 2 ) ;
174+ expect ( setCookies [ 0 ] ) . toContain ( "session=abc123" ) ;
175+ expect ( setCookies [ 1 ] ) . toContain ( "session_data=xyz" ) ;
176+ } ) ;
177+
178+ it ( "should preserve multiple Set-Cookie in _flag=json with init.headers" , ( ) => {
179+ const flaggedData = {
180+ _flag : "json" ,
181+ body : { ok : true } ,
182+ status : 200 ,
183+ } ;
184+ const initHeaders = new Headers ( ) ;
185+ initHeaders . append ( "set-cookie" , "session_token=token1; Path=/; HttpOnly" ) ;
186+ initHeaders . append ( "set-cookie" , "session_data=cache; Path=/" ) ;
187+ const response = toResponse ( flaggedData , { headers : initHeaders } ) ;
188+ const setCookies = response . headers . getSetCookie ?.( ) ?? [ ] ;
189+ expect ( setCookies ) . toHaveLength ( 2 ) ;
190+ expect ( setCookies [ 0 ] ) . toContain ( "session_token=token1" ) ;
191+ expect ( setCookies [ 1 ] ) . toContain ( "session_data=cache" ) ;
192+ } ) ;
193+
194+ it ( "should preserve multiple Set-Cookie from routerResponse.headers in _flag=json" , ( ) => {
195+ const routerHeaders = new Headers ( ) ;
196+ routerHeaders . append ( "set-cookie" , "a=1; Path=/" ) ;
197+ routerHeaders . append ( "set-cookie" , "b=2; Path=/" ) ;
198+ const flaggedData = {
199+ _flag : "json" ,
200+ body : { ok : true } ,
201+ routerResponse : { headers : routerHeaders } ,
202+ status : 200 ,
203+ } ;
204+ const response = toResponse ( flaggedData ) ;
205+ const setCookies = response . headers . getSetCookie ?.( ) ?? [ ] ;
206+ expect ( setCookies ) . toHaveLength ( 2 ) ;
207+ expect ( setCookies [ 0 ] ) . toContain ( "a=1" ) ;
208+ expect ( setCookies [ 1 ] ) . toContain ( "b=2" ) ;
209+ } ) ;
210+ } ) ;
211+
165212 describe ( "BigInt handling" , ( ) => {
166213 it ( "should handle simple bigint values" , async ( ) => {
167214 const data = { id : BigInt ( 9007199254740991 ) } ;
0 commit comments