@@ -440,6 +440,89 @@ describe("toResponse", () => {
440440 } ) ;
441441 } ) ;
442442
443+ /**
444+ * @see https://github.com/better-auth/better-call/issues/118
445+ */
446+ describe ( "multiple Set-Cookie preservation" , ( ) => {
447+ it ( "preserves every Set-Cookie when merging init.headers onto an existing Response" , ( ) => {
448+ const existing = new Response ( "ok" ) ;
449+ existing . headers . append ( "set-cookie" , "a=1; Path=/" ) ;
450+ const initHeaders = new Headers ( ) ;
451+ initHeaders . append ( "set-cookie" , "b=2; Path=/" ) ;
452+ initHeaders . append ( "set-cookie" , "c=3; Path=/" ) ;
453+
454+ const response = toResponse ( existing , { headers : initHeaders } ) ;
455+
456+ expect ( response . headers . getSetCookie ( ) ) . toEqual ( [
457+ "a=1; Path=/" ,
458+ "b=2; Path=/" ,
459+ "c=3; Path=/" ,
460+ ] ) ;
461+ } ) ;
462+
463+ it ( "preserves every Set-Cookie from init.headers in JSON-flag responses" , ( ) => {
464+ const initHeaders = new Headers ( ) ;
465+ initHeaders . append ( "set-cookie" , "a=1; Path=/" ) ;
466+ initHeaders . append ( "set-cookie" , "b=2; Path=/" ) ;
467+
468+ const response = toResponse (
469+ { _flag : "json" , body : { ok : true } } ,
470+ { headers : initHeaders } ,
471+ ) ;
472+
473+ expect ( response . headers . getSetCookie ( ) ) . toEqual ( [
474+ "a=1; Path=/" ,
475+ "b=2; Path=/" ,
476+ ] ) ;
477+ } ) ;
478+
479+ it ( "preserves every Set-Cookie from data.headers in JSON-flag responses" , ( ) => {
480+ const dataHeaders = new Headers ( ) ;
481+ dataHeaders . append ( "set-cookie" , "a=1; Path=/" ) ;
482+ dataHeaders . append ( "set-cookie" , "b=2; Path=/" ) ;
483+
484+ const response = toResponse ( {
485+ _flag : "json" ,
486+ body : { ok : true } ,
487+ headers : dataHeaders ,
488+ } ) ;
489+
490+ expect ( response . headers . getSetCookie ( ) ) . toEqual ( [
491+ "a=1; Path=/" ,
492+ "b=2; Path=/" ,
493+ ] ) ;
494+ } ) ;
495+
496+ it ( "preserves every Set-Cookie from routerResponse.headers in JSON-flag responses" , ( ) => {
497+ const routerHeaders = new Headers ( ) ;
498+ routerHeaders . append ( "set-cookie" , "a=1; Path=/" ) ;
499+ routerHeaders . append ( "set-cookie" , "b=2; Path=/" ) ;
500+
501+ const response = toResponse ( {
502+ _flag : "json" ,
503+ body : { ok : true } ,
504+ routerResponse : { headers : routerHeaders } ,
505+ } ) ;
506+
507+ expect ( response . headers . getSetCookie ( ) ) . toEqual ( [
508+ "a=1; Path=/" ,
509+ "b=2; Path=/" ,
510+ ] ) ;
511+ } ) ;
512+
513+ it ( "propagates non-cookie headers from routerResponse.headers in JSON-flag responses" , ( ) => {
514+ const response = toResponse ( {
515+ _flag : "json" ,
516+ body : { ok : true } ,
517+ routerResponse : {
518+ headers : { "x-custom" : "from-router" } ,
519+ } ,
520+ } ) ;
521+
522+ expect ( response . headers . get ( "x-custom" ) ) . toBe ( "from-router" ) ;
523+ } ) ;
524+ } ) ;
525+
443526 describe ( "Request header stripping" , ( ) => {
444527 const REQUEST_ONLY_HEADERS = [
445528 // Request context (RFC 9110 §10.1)
0 commit comments