@@ -1219,7 +1219,7 @@ describe("follow-redirects", function () {
12191219 } ) ;
12201220
12211221 describe ( "when redirecting to a different host while the host header is set" , function ( ) {
1222- it ( "uses the new host header" , function ( ) {
1222+ it ( "uses the new host header if redirect host is different " , function ( ) {
12231223 app . get ( "/a" , redirectsTo ( 302 , "http://localhost:3600/b" ) ) ;
12241224 app . get ( "/b" , function ( req , res ) {
12251225 res . write ( JSON . stringify ( req . headers ) ) ;
@@ -1242,6 +1242,54 @@ describe("follow-redirects", function () {
12421242 assert . equal ( body . host , "localhost:3600" ) ;
12431243 } ) ;
12441244 } ) ;
1245+
1246+ it ( "uses the location host if redirect host is the same" , function ( ) {
1247+ app . get ( "/a" , redirectsTo ( 302 , "http://localhost:3600/b" ) ) ;
1248+ app . get ( "/b" , function ( req , res ) {
1249+ res . write ( JSON . stringify ( req . headers ) ) ;
1250+ req . pipe ( res ) ; // will invalidate JSON if non-empty
1251+ } ) ;
1252+
1253+ return server . start ( app )
1254+ . then ( asPromise ( function ( resolve , reject ) {
1255+ var opts = url . parse ( "http://127.0.0.1:3600/a" ) ;
1256+ opts . headers = { hOsT : "localhost:3600" } ;
1257+ http . get ( opts , resolve ) . on ( "error" , reject ) ;
1258+ } ) )
1259+ . then ( asPromise ( function ( resolve , reject , res ) {
1260+ assert . deepEqual ( res . statusCode , 200 ) ;
1261+ assert . deepEqual ( res . responseUrl , "http://localhost:3600/b" ) ;
1262+ res . pipe ( concat ( { encoding : "string" } , resolve ) ) . on ( "error" , reject ) ;
1263+ } ) )
1264+ . then ( function ( str ) {
1265+ var body = JSON . parse ( str ) ;
1266+ assert . equal ( body . host , "localhost:3600" ) ;
1267+ } ) ;
1268+ } ) ;
1269+
1270+ it ( "uses the existing host header if redirect host is relative" , function ( ) {
1271+ app . get ( "/a" , redirectsTo ( 302 , "/b" ) ) ;
1272+ app . get ( "/b" , function ( req , res ) {
1273+ res . write ( JSON . stringify ( req . headers ) ) ;
1274+ req . pipe ( res ) ; // will invalidate JSON if non-empty
1275+ } ) ;
1276+
1277+ return server . start ( app )
1278+ . then ( asPromise ( function ( resolve , reject ) {
1279+ var opts = url . parse ( "http://127.0.0.1:3600/a" ) ;
1280+ opts . headers = { hOsT : "localhost:3600" } ;
1281+ http . get ( opts , resolve ) . on ( "error" , reject ) ;
1282+ } ) )
1283+ . then ( asPromise ( function ( resolve , reject , res ) {
1284+ assert . deepEqual ( res . statusCode , 200 ) ;
1285+ assert . deepEqual ( res . responseUrl , "http://localhost:3600/b" ) ;
1286+ res . pipe ( concat ( { encoding : "string" } , resolve ) ) . on ( "error" , reject ) ;
1287+ } ) )
1288+ . then ( function ( str ) {
1289+ var body = JSON . parse ( str ) ;
1290+ assert . equal ( body . host , "localhost:3600" ) ;
1291+ } ) ;
1292+ } ) ;
12451293 } ) ;
12461294
12471295 describe ( "when the client passes an Authorization header" , function ( ) {
@@ -1278,7 +1326,7 @@ describe("follow-redirects", function () {
12781326
12791327 var opts = url . parse ( "http://127.0.0.1:3600/a" ) ;
12801328 opts . headers = {
1281- host : "localhost" ,
1329+ host : "localhost:3600 " ,
12821330 authorization : "bearer my-token-1234" ,
12831331 } ;
12841332
@@ -1296,6 +1344,32 @@ describe("follow-redirects", function () {
12961344 } ) ;
12971345 } ) ;
12981346
1347+ it ( "drops the header when redirected to a different host (same hostname and different port)" , function ( ) {
1348+ app . get ( "/a" , redirectsTo ( 302 , "http://localhost:3600/b" ) ) ;
1349+ app . get ( "/b" , function ( req , res ) {
1350+ res . end ( JSON . stringify ( req . headers ) ) ;
1351+ } ) ;
1352+
1353+ var opts = url . parse ( "http://127.0.0.1:3600/a" ) ;
1354+ opts . headers = {
1355+ host : "localhost" ,
1356+ authorization : "bearer my-token-1234" ,
1357+ } ;
1358+
1359+ return server . start ( app )
1360+ . then ( asPromise ( function ( resolve , reject ) {
1361+ http . get ( opts , resolve ) . on ( "error" , reject ) ;
1362+ } ) )
1363+ . then ( asPromise ( function ( resolve , reject , res ) {
1364+ res . pipe ( concat ( { encoding : "string" } , resolve ) ) . on ( "error" , reject ) ;
1365+ } ) )
1366+ . then ( function ( str ) {
1367+ var body = JSON . parse ( str ) ;
1368+ assert . equal ( body . host , "localhost:3600" ) ;
1369+ assert . equal ( body . authorization , undefined ) ;
1370+ } ) ;
1371+ } ) ;
1372+
12991373 it ( "drops the header when redirected to a different host" , function ( ) {
13001374 app . get ( "/a" , redirectsTo ( 302 , "http://127.0.0.1:3600/b" ) ) ;
13011375 app . get ( "/b" , function ( req , res ) {
0 commit comments