File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed
Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -728,8 +728,23 @@ OutgoingMessage.prototype.setHeaders = function setHeaders(headers) {
728728 throw new ERR_INVALID_ARG_TYPE ( 'headers' , [ 'Headers' , 'Map' ] , headers ) ;
729729 }
730730
731- for ( const key of headers . keys ( ) ) {
732- this . setHeader ( key , headers . get ( key ) ) ;
731+ // Headers object joins multiple cookies with a comma when using
732+ // the getter to retrieve the value,
733+ // unless iterating over the headers directly.
734+ // We also cannot safely split by comma.
735+ // To avoid setHeader owerwriting the previous value we push
736+ // set-cookie values in array and set them all at once.
737+ const cookies = [ ] ;
738+
739+ for ( const { 0 : key , 1 : value } of headers ) {
740+ if ( key === 'set-cookie' ) {
741+ cookies . push ( value ) ;
742+ continue ;
743+ }
744+ this . setHeader ( key , value ) ;
745+ }
746+ if ( cookies . length ) {
747+ this . setHeader ( 'set-cookie' , cookies ) ;
733748 }
734749
735750 return this ;
Original file line number Diff line number Diff line change @@ -129,3 +129,25 @@ const assert = require('assert');
129129 } ) ;
130130 } ) ) ;
131131}
132+
133+ {
134+ const server = http . createServer ( { requireHostHeader : false } , common . mustCall ( ( req , res ) => {
135+ const headers = new Headers ( ) ;
136+ headers . append ( 'Set-Cookie' , 'a=b' ) ;
137+ headers . append ( 'Set-Cookie' , 'c=d' ) ;
138+ res . setHeaders ( headers ) ;
139+ res . end ( ) ;
140+ } ) ) ;
141+
142+ server . listen ( 0 , common . mustCall ( ( ) => {
143+ http . get ( { port : server . address ( ) . port } , ( res ) => {
144+ assert ( Array . isArray ( res . headers [ 'set-cookie' ] ) ) ;
145+ assert . strictEqual ( res . headers [ 'set-cookie' ] . length , 2 ) ;
146+ assert . strictEqual ( res . headers [ 'set-cookie' ] [ 0 ] , 'a=b' ) ;
147+ assert . strictEqual ( res . headers [ 'set-cookie' ] [ 1 ] , 'c=d' ) ;
148+ res . resume ( ) . on ( 'end' , common . mustCall ( ( ) => {
149+ server . close ( ) ;
150+ } ) ) ;
151+ } ) ;
152+ } ) ) ;
153+ }
You can’t perform that action at this time.
0 commit comments