Skip to content

Commit 3904b08

Browse files
committed
internal requests now use the incoming host to build links, tests added
1 parent 6c6b694 commit 3904b08

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

links/links.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ type Builder struct {
1212
URL *url.URL
1313
}
1414

15-
func FromHeadersOrDefault(h *http.Header, defaultURL *url.URL) *Builder {
15+
func FromHeadersOrDefault(h *http.Header, r *http.Request, defaultURL *url.URL) *Builder {
1616
host := h.Get("X-Forwarded-Host")
1717
if host == "" {
18+
if r.Host != "" {
19+
defaultURL.Host = r.Host
20+
}
1821
return &Builder{
1922
URL: defaultURL,
2023
}

links/links_test.go

+57-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
. "github.com/smartystreets/goconvey/convey"
99
)
1010

11-
func Test_FromHeadersOrDefault(t *testing.T) {
11+
func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
1212

1313
Convey("Given a list of test cases", t, func() {
1414
tests := []struct {
@@ -184,7 +184,7 @@ func Test_FromHeadersOrDefault(t *testing.T) {
184184
}
185185

186186
du.JoinPath()
187-
builder := FromHeadersOrDefault(&h, du)
187+
builder := FromHeadersOrDefault(&h, &http.Request{}, du)
188188
So(builder, ShouldNotBeNil)
189189
So(builder.URL, ShouldNotBeNil)
190190
So(builder.URL.String(), ShouldEqual, tt.want)
@@ -195,6 +195,61 @@ func Test_FromHeadersOrDefault(t *testing.T) {
195195

196196
}
197197

198+
func Test_FromHeadersOrDefault_Without_Forwarded_Headers(t *testing.T) {
199+
200+
Convey("Given a list of test cases", t, func() {
201+
tests := []struct {
202+
incomingRequestHost string
203+
defaultURL string
204+
want string
205+
}{
206+
// Without incoming request host
207+
{
208+
"",
209+
"http://localhost:8080/",
210+
"http://localhost:8080/",
211+
},
212+
// With incoming request host and no port
213+
{
214+
"localhost",
215+
"http://localhost:8080/",
216+
"http://localhost/",
217+
},
218+
// With incoming request host and different port
219+
{
220+
"localhost:6789",
221+
"http://localhost:8080/",
222+
"http://localhost:6789/",
223+
},
224+
{
225+
"10.30.100.123:4567",
226+
"http://localhost:8080/",
227+
"http://10.30.100.123:4567/",
228+
},
229+
// With incoming request host and default URL with path
230+
{
231+
"localhost",
232+
"http://localhost:8080/some/path",
233+
"http://localhost/some/path",
234+
},
235+
}
236+
237+
for _, tt := range tests {
238+
du, err := url.Parse(tt.defaultURL)
239+
So(err, ShouldBeNil)
240+
241+
incomingRequest := &http.Request{Host: tt.incomingRequestHost}
242+
243+
builder := FromHeadersOrDefault(&http.Header{}, incomingRequest, du)
244+
So(builder, ShouldNotBeNil)
245+
So(builder.URL, ShouldNotBeNil)
246+
So(builder.URL.String(), ShouldEqual, tt.want)
247+
248+
}
249+
250+
})
251+
}
252+
198253
func TestBuilder_BuildLink(t *testing.T) {
199254

200255
Convey("Given a list of test cases", t, func() {

0 commit comments

Comments
 (0)