@@ -6,23 +6,23 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
66 beforeEach ( function ( ) {
77 this . req = {
88 headers : {
9- host : " ext-auto.com"
9+ host : ' ext-auto.com'
1010 }
1111 } ;
1212 this . proxyRes = {
1313 statusCode : 301 ,
1414 headers : {
15- location : " http://backend.com/"
15+ location : ' http://backend.com/'
1616 }
1717 } ;
1818 this . options = {
19- target : " http://backend.com"
19+ target : ' http://backend.com'
2020 } ;
2121 } ) ;
2222
2323 context ( 'rewrites location host with hostRewrite' , function ( ) {
2424 beforeEach ( function ( ) {
25- this . options . hostRewrite = " ext-manual.com" ;
25+ this . options . hostRewrite = ' ext-manual.com' ;
2626 } ) ;
2727 [ 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
2828 it ( 'on ' + code , function ( ) {
@@ -52,14 +52,14 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
5252
5353 it ( 'not when the redirected location does not match target host' , function ( ) {
5454 this . proxyRes . statusCode = 302 ;
55- this . proxyRes . headers . location = " http://some-other/" ;
55+ this . proxyRes . headers . location = ' http://some-other/' ;
5656 httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
5757 expect ( this . proxyRes . headers . location ) . to . eql ( 'http://some-other/' ) ;
5858 } ) ;
5959
6060 it ( 'not when the redirected location does not match target port' , function ( ) {
6161 this . proxyRes . statusCode = 302 ;
62- this . proxyRes . headers . location = " http://backend.com:8080/" ;
62+ this . proxyRes . headers . location = ' http://backend.com:8080/' ;
6363 httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
6464 expect ( this . proxyRes . headers . location ) . to . eql ( 'http://backend.com:8080/' ) ;
6565 } ) ;
@@ -91,14 +91,14 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
9191
9292 it ( 'not when the redirected location does not match target host' , function ( ) {
9393 this . proxyRes . statusCode = 302 ;
94- this . proxyRes . headers . location = " http://some-other/" ;
94+ this . proxyRes . headers . location = ' http://some-other/' ;
9595 httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
9696 expect ( this . proxyRes . headers . location ) . to . eql ( 'http://some-other/' ) ;
9797 } ) ;
9898
9999 it ( 'not when the redirected location does not match target port' , function ( ) {
100100 this . proxyRes . statusCode = 302 ;
101- this . proxyRes . headers . location = " http://backend.com:8080/" ;
101+ this . proxyRes . headers . location = ' http://backend.com:8080/' ;
102102 httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
103103 expect ( this . proxyRes . headers . location ) . to . eql ( 'http://backend.com:8080/' ) ;
104104 } ) ;
@@ -129,13 +129,13 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
129129 } ) ;
130130
131131 it ( 'works together with hostRewrite' , function ( ) {
132- this . options . hostRewrite = 'ext-manual.com'
132+ this . options . hostRewrite = 'ext-manual.com' ;
133133 httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
134134 expect ( this . proxyRes . headers . location ) . to . eql ( 'https://ext-manual.com/' ) ;
135135 } ) ;
136136
137137 it ( 'works together with autoRewrite' , function ( ) {
138- this . options . autoRewrite = true
138+ this . options . autoRewrite = true ;
139139 httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
140140 expect ( this . proxyRes . headers . location ) . to . eql ( 'https://ext-auto.com/' ) ;
141141 } ) ;
@@ -199,31 +199,89 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
199199 writeHead : function ( n ) {
200200 expect ( n ) . to . eql ( 200 ) ;
201201 }
202- }
202+ } ;
203203
204204 httpProxy . writeStatusCode ( { } , res , { statusCode : 200 } ) ;
205205 } ) ;
206206 } ) ;
207207
208208 describe ( '#writeHeaders' , function ( ) {
209- var proxyRes = {
210- headers : {
211- hey : 'hello' ,
212- how : 'are you?'
213- }
214- } ;
209+ beforeEach ( function ( ) {
210+ this . proxyRes = {
211+ headers : {
212+ hey : 'hello' ,
213+ how : 'are you?' ,
214+ 'set-cookie' : 'hello; domain=my.domain; path=/'
215+ }
216+ } ;
217+ this . res = {
218+ setHeader : function ( k , v ) {
219+ this . headers [ k ] = v ;
220+ } ,
221+ headers : { }
222+ } ;
223+ } ) ;
215224
216- var res = {
217- setHeader : function ( k , v ) {
218- this . headers [ k ] = v ;
219- } ,
220- headers : { }
221- } ;
225+ it ( 'writes headers' , function ( ) {
226+ var options = { } ;
222227
223- httpProxy . writeHeaders ( { } , res , proxyRes ) ;
228+ httpProxy . writeHeaders ( { } , this . res , this . proxyRes , options ) ;
229+
230+ expect ( this . res . headers . hey ) . to . eql ( 'hello' ) ;
231+ expect ( this . res . headers . how ) . to . eql ( 'are you?' ) ;
232+ } ) ;
224233
225- expect ( res . headers . hey ) . to . eql ( 'hello' ) ;
226- expect ( res . headers . how ) . to . eql ( 'are you?' ) ;
234+ it ( 'does not rewrite domain' , function ( ) {
235+ var options = { } ;
236+
237+ httpProxy . writeHeaders ( { } , this . res , this . proxyRes , options ) ;
238+
239+ expect ( this . res . headers [ 'set-cookie' ] ) . to . eql ( 'hello; domain=my.domain; path=/' ) ;
240+ } ) ;
241+
242+ it ( 'rewrites domain' , function ( ) {
243+ var options = {
244+ cookieDomainRewrite : 'my.new.domain'
245+ } ;
246+
247+ httpProxy . writeHeaders ( { } , this . res , this . proxyRes , options ) ;
248+
249+ expect ( this . res . headers [ 'set-cookie' ] ) . to . eql ( 'hello; domain=my.new.domain; path=/' ) ;
250+ } ) ;
251+
252+ it ( 'removes domain' , function ( ) {
253+ var options = {
254+ cookieDomainRewrite : ''
255+ } ;
256+
257+ httpProxy . writeHeaders ( { } , this . res , this . proxyRes , options ) ;
258+
259+ expect ( this . res . headers [ 'set-cookie' ] ) . to . eql ( 'hello; path=/' ) ;
260+ } ) ;
261+
262+ it ( 'rewrites headers with advanced configuration' , function ( ) {
263+ var options = {
264+ cookieDomainRewrite : {
265+ '*' : '' ,
266+ 'my.old.domain' : 'my.new.domain' ,
267+ 'my.special.domain' : 'my.special.domain'
268+ }
269+ } ;
270+ this . proxyRes . headers [ 'set-cookie' ] = [
271+ 'hello-on-my.domain; domain=my.domain; path=/' ,
272+ 'hello-on-my.old.domain; domain=my.old.domain; path=/' ,
273+ 'hello-on-my.special.domain; domain=my.special.domain; path=/'
274+ ] ;
275+
276+ httpProxy . writeHeaders ( { } , this . res , this . proxyRes , options ) ;
277+
278+ expect ( this . res . headers [ 'set-cookie' ] )
279+ . to . contain ( 'hello-on-my.domain; path=/' ) ;
280+ expect ( this . res . headers [ 'set-cookie' ] )
281+ . to . contain ( 'hello-on-my.old.domain; domain=my.new.domain; path=/' ) ;
282+ expect ( this . res . headers [ 'set-cookie' ] )
283+ . to . contain ( 'hello-on-my.special.domain; domain=my.special.domain; path=/' ) ;
284+ } ) ;
227285 } ) ;
228286
229287
0 commit comments