@@ -3,55 +3,118 @@ var httpProxy = require('../lib/http-proxy/passes/web-outgoing'),
3
3
4
4
describe ( 'lib/http-proxy/passes/web-outgoing.js' , function ( ) {
5
5
describe ( '#setRedirectHostRewrite' , function ( ) {
6
- context ( 'rewrites location host to option' , function ( ) {
7
- beforeEach ( function ( ) {
8
- this . proxyRes = {
9
- statusCode : 301 ,
10
- headers : {
11
- location : "http://f.com/"
12
- }
13
- } ;
6
+ beforeEach ( function ( ) {
7
+ this . req = {
8
+ headers : {
9
+ host : "x2.com"
10
+ }
11
+ } ;
12
+ this . proxyRes = {
13
+ statusCode : 301 ,
14
+ headers : {
15
+ location : "http://f.com/"
16
+ }
17
+ } ;
18
+ } ) ;
14
19
20
+ context ( 'rewrites location host with hostRewrite' , function ( ) {
21
+ beforeEach ( function ( ) {
15
22
this . options = {
16
23
hostRewrite : "x.com"
17
24
} ;
18
25
} ) ;
26
+ [ 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
27
+ it ( 'on ' + code , function ( ) {
28
+ this . proxyRes . statusCode = code ;
29
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
30
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'http://' + this . options . hostRewrite + '/' ) ;
31
+ } ) ;
32
+ } ) ;
19
33
20
- it ( 'on 301 ' , function ( ) {
21
- this . proxyRes . statusCode = 301 ;
22
- httpProxy . setRedirectHostRewrite ( { } , { } , this . proxyRes , this . options ) ;
23
- expect ( this . proxyRes . headers . location ) . to . eql ( 'http://' + this . options . hostRewrite + ' /') ;
34
+ it ( 'not on 200 ' , function ( ) {
35
+ this . proxyRes . statusCode = 200 ;
36
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
37
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'http://f.com /' ) ;
24
38
} ) ;
25
39
26
- it ( 'on 302 ' , function ( ) {
27
- this . proxyRes . statusCode = 302 ;
28
- httpProxy . setRedirectHostRewrite ( { } , { } , this . proxyRes , this . options ) ;
29
- expect ( this . proxyRes . headers . location ) . to . eql ( 'http://' + this . options . hostRewrite + ' /') ;
40
+ it ( 'not when hostRewrite is unset ' , function ( ) {
41
+ delete this . options . hostRewrite ;
42
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
43
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'http://f.com /' ) ;
30
44
} ) ;
31
45
32
- it ( 'on 307 ' , function ( ) {
33
- this . proxyRes . statusCode = 307 ;
34
- httpProxy . setRedirectHostRewrite ( { } , { } , this . proxyRes , this . options ) ;
46
+ it ( 'takes precedence over autoRewrite ' , function ( ) {
47
+ this . options . autoRewrite = true ;
48
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
35
49
expect ( this . proxyRes . headers . location ) . to . eql ( 'http://' + this . options . hostRewrite + '/' ) ;
36
50
} ) ;
51
+ } ) ;
37
52
38
- it ( 'on 308' , function ( ) {
39
- this . proxyRes . statusCode = 308 ;
40
- httpProxy . setRedirectHostRewrite ( { } , { } , this . proxyRes , this . options ) ;
41
- expect ( this . proxyRes . headers . location ) . to . eql ( 'http://' + this . options . hostRewrite + '/' ) ;
53
+ context ( 'rewrites location host with autoRewrite' , function ( ) {
54
+ beforeEach ( function ( ) {
55
+ this . options = {
56
+ autoRewrite : true ,
57
+ } ;
58
+ } ) ;
59
+ [ 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
60
+ it ( 'on ' + code , function ( ) {
61
+ this . proxyRes . statusCode = code ;
62
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
63
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'http://' + this . req . headers . host + '/' ) ;
64
+ } ) ;
42
65
} ) ;
43
66
44
67
it ( 'not on 200' , function ( ) {
45
68
this . proxyRes . statusCode = 200 ;
46
- httpProxy . setRedirectHostRewrite ( { } , { } , this . proxyRes , this . options ) ;
69
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
47
70
expect ( this . proxyRes . headers . location ) . to . eql ( 'http://f.com/' ) ;
48
71
} ) ;
49
72
50
- it ( 'not when hostRewrite is unset' , function ( ) {
51
- httpProxy . setRedirectHostRewrite ( { } , { } , this . proxyRes , { } ) ;
73
+ it ( 'not when autoRewrite is unset' , function ( ) {
74
+ delete this . options . autoRewrite ;
75
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
52
76
expect ( this . proxyRes . headers . location ) . to . eql ( 'http://f.com/' ) ;
53
77
} ) ;
54
78
} ) ;
79
+
80
+ context ( 'rewrites location protocol with protocolRewrite' , function ( ) {
81
+ beforeEach ( function ( ) {
82
+ this . options = {
83
+ protocolRewrite : 'https' ,
84
+ } ;
85
+ } ) ;
86
+ [ 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
87
+ it ( 'on ' + code , function ( ) {
88
+ this . proxyRes . statusCode = code ;
89
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
90
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'https://f.com/' ) ;
91
+ } ) ;
92
+ } ) ;
93
+
94
+ it ( 'not on 200' , function ( ) {
95
+ this . proxyRes . statusCode = 200 ;
96
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
97
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'http://f.com/' ) ;
98
+ } ) ;
99
+
100
+ it ( 'not when protocolRewrite is unset' , function ( ) {
101
+ delete this . options . protocolRewrite ;
102
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
103
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'http://f.com/' ) ;
104
+ } ) ;
105
+
106
+ it ( 'works together with hostRewrite' , function ( ) {
107
+ this . options . hostRewrite = 'x.com'
108
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
109
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'https://x.com/' ) ;
110
+ } ) ;
111
+
112
+ it ( 'works together with autoRewrite' , function ( ) {
113
+ this . options . autoRewrite = true
114
+ httpProxy . setRedirectHostRewrite ( this . req , { } , this . proxyRes , this . options ) ;
115
+ expect ( this . proxyRes . headers . location ) . to . eql ( 'https://x2.com/' ) ;
116
+ } ) ;
117
+ } ) ;
55
118
} ) ;
56
119
57
120
describe ( '#setConnection' , function ( ) {
@@ -64,7 +127,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
64
127
}
65
128
} , { } , proxyRes ) ;
66
129
67
- expect ( proxyRes . headers . connection ) . to . eql ( 'close' ) ;
130
+ expect ( proxyRes . headers . connection ) . to . eql ( 'close' ) ;
68
131
} ) ;
69
132
70
133
it ( 'set the right connection with 1.0 - req.connection' , function ( ) {
@@ -76,7 +139,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
76
139
}
77
140
} , { } , proxyRes ) ;
78
141
79
- expect ( proxyRes . headers . connection ) . to . eql ( 'hey' ) ;
142
+ expect ( proxyRes . headers . connection ) . to . eql ( 'hey' ) ;
80
143
} ) ;
81
144
82
145
it ( 'set the right connection - req.connection' , function ( ) {
@@ -88,7 +151,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
88
151
}
89
152
} , { } , proxyRes ) ;
90
153
91
- expect ( proxyRes . headers . connection ) . to . eql ( 'hola' ) ;
154
+ expect ( proxyRes . headers . connection ) . to . eql ( 'hola' ) ;
92
155
} ) ;
93
156
94
157
it ( 'set the right connection - `keep-alive`' , function ( ) {
@@ -100,7 +163,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
100
163
}
101
164
} , { } , proxyRes ) ;
102
165
103
- expect ( proxyRes . headers . connection ) . to . eql ( 'keep-alive' ) ;
166
+ expect ( proxyRes . headers . connection ) . to . eql ( 'keep-alive' ) ;
104
167
} ) ;
105
168
106
169
} ) ;
@@ -153,4 +216,4 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
153
216
} ) ;
154
217
155
218
} ) ;
156
-
219
+
0 commit comments