@@ -8,14 +8,13 @@ import (
8
8
. "github.com/smartystreets/goconvey/convey"
9
9
)
10
10
11
- func Test_FromHeadersOrDefault_With_Forwarded_Headers (t * testing.T ) {
11
+ func Test_FromHeadersOrDefault (t * testing.T ) {
12
12
13
13
Convey ("Given a list of test cases" , t , func () {
14
14
tests := []struct {
15
15
defaultURL string
16
16
fwdProto string
17
17
fwdHost string
18
- fwdPort string
19
18
fwdPathPrefix string
20
19
want string
21
20
}{
@@ -25,143 +24,95 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
25
24
"" ,
26
25
"" ,
27
26
"" ,
28
- "" ,
29
27
"http://localhost:8080/" ,
30
28
},
31
29
// With all forwarded headers
32
30
{
33
31
"http://localhost:8080/" ,
34
32
"https" ,
35
- "forwardedhost" ,
36
- "9090" ,
33
+ "api.external.host" ,
37
34
"/prefix" ,
38
- "https://forwardedhost:9090 /prefix" ,
35
+ "https://api.external.host /prefix" ,
39
36
},
40
37
// With only forwarded proto
41
38
{
42
39
"http://localhost:8080/" ,
43
40
"https" ,
44
41
"" ,
45
42
"" ,
46
- "" ,
47
43
"http://localhost:8080/" ,
48
44
},
49
45
// With only forwarded host
50
46
{
51
47
"http://localhost:8080/" ,
52
48
"" ,
53
- "forwardedhost" ,
54
- "" ,
55
- "" ,
56
- "https://forwardedhost/" ,
57
- },
58
- // With only forwarded port
59
- {
60
- "http://localhost:8080/" ,
61
- "" ,
49
+ "api.external.host" ,
62
50
"" ,
63
- "9090" ,
64
- "" ,
65
- "http://localhost:8080/" ,
51
+ "https://api.external.host/" ,
66
52
},
67
53
// With only forwarded path prefix
68
54
{
69
55
"http://localhost:8080/" ,
70
56
"" ,
71
57
"" ,
72
- "" ,
73
58
"/prefix" ,
74
59
"http://localhost:8080/prefix" ,
75
60
},
76
61
// Without all headers except forwarded proto
77
62
{
78
63
"http://localhost:8080/" ,
79
64
"" ,
80
- "forwardedhost" ,
81
- "9090" ,
65
+ "api.external.host" ,
82
66
"/prefix" ,
83
- "https://forwardedhost:9090 /prefix" ,
67
+ "https://api.external.host /prefix" ,
84
68
},
85
69
// Without all headers except forwarded host
86
70
{
87
71
"http://localhost:8080/" ,
88
72
"https" ,
89
73
"" ,
90
- "9090" ,
91
74
"/prefix" ,
92
75
"http://localhost:8080/prefix" ,
93
76
},
94
- // Without all headers except forwarded port
95
- {
96
- "http://localhost:8080/" ,
97
- "https" ,
98
- "forwardedhost" ,
99
- "" ,
100
- "/prefix" ,
101
- "https://forwardedhost/prefix" ,
102
- },
103
77
// Without all headers except forwarded path prefix
104
78
{
105
79
"http://localhost:8080/" ,
106
80
"https" ,
107
- "forwardedhost" ,
108
- "9090" ,
81
+ "api.external.host" ,
109
82
"" ,
110
- "https://forwardedhost:9090 /" ,
83
+ "https://api.external.host /" ,
111
84
},
112
85
// With only forwarded proto and host
113
86
{
114
87
"http://localhost:8080/" ,
115
88
"https" ,
116
- "forwardedhost " ,
89
+ "api.external.host " ,
117
90
"" ,
118
- "" ,
119
- "https://forwardedhost/" ,
120
- },
121
- // With only forwarded port and host
122
- {
123
- "http://localhost:8080/" ,
124
- "" ,
125
- "forwardedhost" ,
126
- "9090" ,
127
- "" ,
128
- "https://forwardedhost:9090/" ,
91
+ "https://api.external.host/" ,
129
92
},
130
93
// With only forwarded prefix and host
131
94
{
132
95
"http://localhost:8080/" ,
133
96
"" ,
134
- "forwardedhost" ,
135
- "" ,
97
+ "api.external.host" ,
136
98
"/prefix" ,
137
- "https://forwardedhost/prefix" ,
138
- },
139
- // With only forwarded proto and port
140
- {
141
- "http://localhost:8080/" ,
142
- "https" ,
143
- "" ,
144
- "9090" ,
145
- "" ,
146
- "http://localhost:8080/" ,
99
+ "https://api.external.host/prefix" ,
147
100
},
148
101
// With only forwarded proto and prefix
149
102
{
150
103
"http://localhost:8080/" ,
151
104
"https" ,
152
105
"" ,
153
- "" ,
154
106
"/prefix" ,
155
107
"http://localhost:8080/prefix" ,
156
108
},
157
- // With only forwarded port and prefix
109
+ // With non-external forwarded host
158
110
{
159
111
"http://localhost:8080/" ,
160
112
"" ,
113
+ "internalhost" ,
161
114
"" ,
162
- "9090" ,
163
- "/prefix" ,
164
- "http://localhost:8080/prefix" ,
115
+ "http://localhost:8080/" ,
165
116
},
166
117
}
167
118
@@ -176,16 +127,14 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
176
127
if tt .fwdHost != "" {
177
128
h .Add ("X-Forwarded-Host" , tt .fwdHost )
178
129
}
179
- if tt .fwdPort != "" {
180
- h .Add ("X-Forwarded-Port" , tt .fwdPort )
181
- }
182
130
if tt .fwdPathPrefix != "" {
183
131
h .Add ("X-Forwarded-Path-Prefix" , tt .fwdPathPrefix )
184
132
}
185
133
186
134
du .JoinPath ()
187
135
r := & http.Request {
188
- URL : & url.URL {Scheme : "http" , Host : "example.com" },
136
+ URL : & url.URL {},
137
+ Host : "localhost:8080" ,
189
138
}
190
139
builder := FromHeadersOrDefault (& h , r , du )
191
140
So (builder , ShouldNotBeNil )
@@ -196,64 +145,48 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
196
145
197
146
})
198
147
199
- }
200
-
201
- func Test_FromHeadersOrDefault_Without_Forwarded_Headers (t * testing.T ) {
202
-
203
- Convey ("Given a list of test cases" , t , func () {
204
- tests := []struct {
205
- incomingRequestHost string
206
- defaultURL string
207
- want string
208
- }{
209
- // Without incoming request host
210
- {
211
- "" ,
212
- "http://localhost:8080/" ,
213
- "http://localhost:8080/" ,
214
- },
215
- // With incoming request host and no port
216
- {
217
- "localhost" ,
218
- "http://localhost:8080/" ,
219
- "http://localhost/" ,
220
- },
221
- // With incoming request host and different port
222
- {
223
- "localhost:6789" ,
224
- "http://localhost:8080/" ,
225
- "http://localhost:6789/" ,
226
- },
227
- {
228
- "10.30.100.123:4567" ,
229
- "http://localhost:8080/" ,
230
- "http://10.30.100.123:4567/" ,
231
- },
232
- // With incoming request host and default URL with path
233
- {
234
- "localhost" ,
235
- "http://localhost:8080/some/path" ,
236
- "http://localhost/some/path" ,
237
- },
148
+ Convey ("Given an empty incoming request host" , t , func () {
149
+ r := & http.Request {
150
+ URL : & url.URL {},
151
+ Host : "" ,
238
152
}
239
153
240
- for _ , tt := range tests {
241
- du , err := url .Parse (tt .defaultURL )
154
+ Convey ("When the builder is created with forwarded headers" , func () {
155
+ h := & http.Header {}
156
+ h .Add ("X-Forwarded-Proto" , "https" )
157
+ h .Add ("X-Forwarded-Host" , "api.newhost" )
158
+ h .Add ("X-Forwarded-Path-Prefix" , "v1" )
159
+
160
+ defaultURL , err := url .Parse ("http://localhost:8080/" )
242
161
So (err , ShouldBeNil )
243
162
244
- incomingRequest := & http.Request {
245
- Host : tt .incomingRequestHost ,
246
- URL : & url.URL {Scheme : "http" , Host : "example.com" },
247
- }
163
+ builder := FromHeadersOrDefault (h , r , defaultURL )
248
164
249
- builder := FromHeadersOrDefault (& http.Header {}, incomingRequest , du )
250
165
So (builder , ShouldNotBeNil )
251
166
So (builder .URL , ShouldNotBeNil )
252
- So (builder .URL .String (), ShouldEqual , tt .want )
253
167
254
- }
168
+ Convey ("Then the builder URL should be the default URL with the path prefix" , func () {
169
+ So (builder .URL .String (), ShouldEqual , "http://localhost:8080/v1" )
170
+ })
171
+ })
172
+
173
+ Convey ("When the builder is created without forwarded headers" , func () {
174
+ h := & http.Header {}
175
+
176
+ defaultURL , err := url .Parse ("http://localhost:8080/" )
177
+ So (err , ShouldBeNil )
178
+
179
+ builder := FromHeadersOrDefault (h , r , defaultURL )
255
180
181
+ So (builder , ShouldNotBeNil )
182
+ So (builder .URL , ShouldNotBeNil )
183
+
184
+ Convey ("Then the builder URL should be the default URL" , func () {
185
+ So (builder .URL .String (), ShouldEqual , "http://localhost:8080/" )
186
+ })
187
+ })
256
188
})
189
+
257
190
}
258
191
259
192
func TestBuilder_BuildLink (t * testing.T ) {
@@ -349,7 +282,7 @@ func TestBuilder_BuildLink(t *testing.T) {
349
282
})
350
283
351
284
Convey ("When an invalid old URL is provided" , t , func () {
352
- builder := & Builder {URL : & url.URL {Scheme : "http" , Host : "localhost:8080" }}
285
+ builder := & Builder {URL : & url.URL {}}
353
286
invalidURL := ":invalid/url"
354
287
newurl , err := builder .BuildLink (invalidURL )
355
288
So (err , ShouldNotBeNil )
0 commit comments