@@ -5,156 +5,169 @@ import {
5
5
writeStatusCode ,
6
6
writeHeaders ,
7
7
} from "../../dist/lib/http-proxy/passes/web-outgoing" ;
8
+ import * as url from "url" ;
8
9
9
10
const state : any = { headers : { } } ;
10
11
11
- describe ( "#setRedirectHostRewrite" , ( ) => {
12
- beforeEach ( ( ) => {
13
- state . req = {
14
- headers : {
15
- host : "ext-auto.com" ,
16
- } ,
17
- } ;
18
- state . proxyRes = {
19
- statusCode : 301 ,
20
- headers : {
21
- location : "http://backend.com/" ,
22
- } ,
23
- } ;
24
- state . options = {
25
- target : "http://backend.com" ,
26
- } ;
27
- } ) ;
28
-
29
- describe ( "rewrites location host with hostRewrite" , ( ) => {
12
+ // NOTE: here url.parse("http://backend.com") uses the deprecated url.parse
13
+ // function, and we're testing that we still support it.
14
+ for ( const target of [ "http://backend.com" , url . parse ( "http://backend.com" ) ] ) {
15
+ describe ( "#setRedirectHostRewrite" , ( ) => {
30
16
beforeEach ( ( ) => {
31
- state . options . hostRewrite = "ext-manual.com" ;
17
+ state . req = {
18
+ headers : {
19
+ host : "ext-auto.com" ,
20
+ } ,
21
+ } ;
22
+ state . proxyRes = {
23
+ statusCode : 301 ,
24
+ headers : {
25
+ location : "http://backend.com/" ,
26
+ } ,
27
+ } ;
28
+ state . options = {
29
+ target,
30
+ } ;
32
31
} ) ;
33
- [ 201 , 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
34
- it ( "on " + code , ( ) => {
35
- state . proxyRes . statusCode = code ;
32
+
33
+ describe ( "rewrites location host with hostRewrite" , ( ) => {
34
+ beforeEach ( ( ) => {
35
+ state . options . hostRewrite = "ext-manual.com" ;
36
+ } ) ;
37
+ [ 201 , 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
38
+ it ( "on " + code , ( ) => {
39
+ state . proxyRes . statusCode = code ;
40
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
41
+ expect ( state . proxyRes . headers . location ) . toEqual (
42
+ "http://ext-manual.com/" ,
43
+ ) ;
44
+ } ) ;
45
+ } ) ;
46
+
47
+ it ( "not on 200" , ( ) => {
48
+ state . proxyRes . statusCode = 200 ;
49
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
50
+ expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
51
+ } ) ;
52
+
53
+ it ( "not when hostRewrite is unset" , ( ) => {
54
+ delete state . options . hostRewrite ;
55
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
56
+ expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
57
+ } ) ;
58
+
59
+ it ( "takes precedence over autoRewrite" , ( ) => {
60
+ state . options . autoRewrite = true ;
36
61
setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
37
62
expect ( state . proxyRes . headers . location ) . toEqual (
38
63
"http://ext-manual.com/" ,
39
64
) ;
40
65
} ) ;
41
- } ) ;
42
66
43
- it ( "not on 200" , ( ) => {
44
- state . proxyRes . statusCode = 200 ;
45
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
46
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
47
- } ) ;
48
-
49
- it ( "not when hostRewrite is unset" , ( ) => {
50
- delete state . options . hostRewrite ;
51
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
52
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
53
- } ) ;
54
-
55
- it ( "takes precedence over autoRewrite" , ( ) => {
56
- state . options . autoRewrite = true ;
57
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
58
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://ext-manual.com/" ) ;
59
- } ) ;
67
+ it ( "not when the redirected location does not match target host" , ( ) => {
68
+ state . proxyRes . statusCode = 302 ;
69
+ state . proxyRes . headers . location = "http://some-other/" ;
70
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
71
+ expect ( state . proxyRes . headers . location ) . toEqual ( "http://some-other/" ) ;
72
+ } ) ;
60
73
61
- it ( "not when the redirected location does not match target host" , ( ) => {
62
- state . proxyRes . statusCode = 302 ;
63
- state . proxyRes . headers . location = "http://some-other/" ;
64
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
65
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://some-other/" ) ;
74
+ it ( "not when the redirected location does not match target port" , ( ) => {
75
+ state . proxyRes . statusCode = 302 ;
76
+ state . proxyRes . headers . location = "http://backend.com:8080/" ;
77
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
78
+ expect ( state . proxyRes . headers . location ) . toEqual (
79
+ "http://backend.com:8080/" ,
80
+ ) ;
81
+ } ) ;
66
82
} ) ;
67
83
68
- it ( "not when the redirected location does not match target port" , ( ) => {
69
- state . proxyRes . statusCode = 302 ;
70
- state . proxyRes . headers . location = "http://backend.com:8080/" ;
71
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
72
- expect ( state . proxyRes . headers . location ) . toEqual (
73
- "http://backend.com:8080/" ,
74
- ) ;
75
- } ) ;
76
- } ) ;
84
+ describe ( "rewrites location host with autoRewrite" , ( ) => {
85
+ beforeEach ( ( ) => {
86
+ state . options . autoRewrite = true ;
87
+ } ) ;
88
+ [ 201 , 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
89
+ it ( "on " + code , ( ) => {
90
+ state . proxyRes . statusCode = code ;
91
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
92
+ expect ( state . proxyRes . headers . location ) . toEqual (
93
+ "http://ext-auto.com/" ,
94
+ ) ;
95
+ } ) ;
96
+ } ) ;
77
97
78
- describe ( "rewrites location host with autoRewrite" , ( ) => {
79
- beforeEach ( ( ) => {
80
- state . options . autoRewrite = true ;
81
- } ) ;
82
- [ 201 , 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
83
- it ( "on " + code , ( ) => {
84
- state . proxyRes . statusCode = code ;
98
+ it ( "not on 200" , ( ) => {
99
+ state . proxyRes . statusCode = 200 ;
85
100
setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
86
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://ext-auto .com/" ) ;
101
+ expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend .com/" ) ;
87
102
} ) ;
88
- } ) ;
89
103
90
- it ( "not on 200 " , ( ) => {
91
- state . proxyRes . statusCode = 200 ;
92
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
93
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
94
- } ) ;
104
+ it ( "not when autoRewrite is unset " , ( ) => {
105
+ delete state . options . autoRewrite ;
106
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
107
+ expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
108
+ } ) ;
95
109
96
- it ( "not when autoRewrite is unset" , ( ) => {
97
- delete state . options . autoRewrite ;
98
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
99
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
100
- } ) ;
110
+ it ( "not when the redirected location does not match target host" , ( ) => {
111
+ state . proxyRes . statusCode = 302 ;
112
+ state . proxyRes . headers . location = "http://some-other/" ;
113
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
114
+ expect ( state . proxyRes . headers . location ) . toEqual ( "http://some-other/" ) ;
115
+ } ) ;
101
116
102
- it ( "not when the redirected location does not match target host" , ( ) => {
103
- state . proxyRes . statusCode = 302 ;
104
- state . proxyRes . headers . location = "http://some-other/" ;
105
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
106
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://some-other/" ) ;
117
+ it ( "not when the redirected location does not match target port" , ( ) => {
118
+ state . proxyRes . statusCode = 302 ;
119
+ state . proxyRes . headers . location = "http://backend.com:8080/" ;
120
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
121
+ expect ( state . proxyRes . headers . location ) . toEqual (
122
+ "http://backend.com:8080/" ,
123
+ ) ;
124
+ } ) ;
107
125
} ) ;
108
126
109
- it ( "not when the redirected location does not match target port" , ( ) => {
110
- state . proxyRes . statusCode = 302 ;
111
- state . proxyRes . headers . location = "http://backend.com:8080/" ;
112
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
113
- expect ( state . proxyRes . headers . location ) . toEqual (
114
- "http://backend.com:8080/" ,
115
- ) ;
116
- } ) ;
117
- } ) ;
127
+ describe ( "rewrites location protocol with protocolRewrite" , ( ) => {
128
+ beforeEach ( ( ) => {
129
+ state . options . protocolRewrite = "https" ;
130
+ } ) ;
131
+ [ 201 , 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
132
+ it ( "on " + code , ( ) => {
133
+ state . proxyRes . statusCode = code ;
134
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
135
+ expect ( state . proxyRes . headers . location ) . toEqual (
136
+ "https://backend.com/" ,
137
+ ) ;
138
+ } ) ;
139
+ } ) ;
118
140
119
- describe ( "rewrites location protocol with protocolRewrite" , ( ) => {
120
- beforeEach ( ( ) => {
121
- state . options . protocolRewrite = "https" ;
122
- } ) ;
123
- [ 201 , 301 , 302 , 307 , 308 ] . forEach ( function ( code ) {
124
- it ( "on " + code , ( ) => {
125
- state . proxyRes . statusCode = code ;
141
+ it ( "not on 200" , ( ) => {
142
+ state . proxyRes . statusCode = 200 ;
126
143
setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
127
- expect ( state . proxyRes . headers . location ) . toEqual ( "https ://backend.com/" ) ;
144
+ expect ( state . proxyRes . headers . location ) . toEqual ( "http ://backend.com/" ) ;
128
145
} ) ;
129
- } ) ;
130
-
131
- it ( "not on 200" , ( ) => {
132
- state . proxyRes . statusCode = 200 ;
133
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
134
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
135
- } ) ;
136
146
137
- it ( "not when protocolRewrite is unset" , ( ) => {
138
- delete state . options . protocolRewrite ;
139
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
140
- expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
141
- } ) ;
147
+ it ( "not when protocolRewrite is unset" , ( ) => {
148
+ delete state . options . protocolRewrite ;
149
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
150
+ expect ( state . proxyRes . headers . location ) . toEqual ( "http://backend.com/" ) ;
151
+ } ) ;
142
152
143
- it ( "works together with hostRewrite" , ( ) => {
144
- state . options . hostRewrite = "ext-manual.com" ;
145
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
146
- expect ( state . proxyRes . headers . location ) . toEqual (
147
- "https://ext-manual.com/" ,
148
- ) ;
149
- } ) ;
153
+ it ( "works together with hostRewrite" , ( ) => {
154
+ state . options . hostRewrite = "ext-manual.com" ;
155
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
156
+ expect ( state . proxyRes . headers . location ) . toEqual (
157
+ "https://ext-manual.com/" ,
158
+ ) ;
159
+ } ) ;
150
160
151
- it ( "works together with autoRewrite" , ( ) => {
152
- state . options . autoRewrite = true ;
153
- setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
154
- expect ( state . proxyRes . headers . location ) . toEqual ( "https://ext-auto.com/" ) ;
161
+ it ( "works together with autoRewrite" , ( ) => {
162
+ state . options . autoRewrite = true ;
163
+ setRedirectHostRewrite ( state . req , { } , state . proxyRes , state . options ) ;
164
+ expect ( state . proxyRes . headers . location ) . toEqual (
165
+ "https://ext-auto.com/" ,
166
+ ) ;
167
+ } ) ;
155
168
} ) ;
156
169
} ) ;
157
- } ) ;
170
+ }
158
171
159
172
describe ( "#setConnection" , ( ) => {
160
173
it ( "set the right connection with 1.0 - `close`" , ( ) => {
0 commit comments