Skip to content

Commit

Permalink
internal requests respond with defaultURL
Browse files Browse the repository at this point in the history
  • Loading branch information
aryan-p03 committed Jan 24, 2025
1 parent a84f410 commit c69b9d5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
7 changes: 5 additions & 2 deletions links/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ func FromHeadersOrDefault(h *http.Header, r *http.Request, defaultURL *url.URL)
}
}
if !strings.HasPrefix(host, "api") {
log.Info(r.Context(), "X-Forwarded-Host is not an external host, using incoming request host", log.Data{
log.Info(r.Context(), "X-Forwarded-Host is not an external host, returning defaultURL", log.Data{
"r.Host": r.Host,
"r.remoteAddr": r.RemoteAddr,
})
host = r.Host
defaultURL = defaultURL.JoinPath(path)
return &Builder{
URL: defaultURL,
}
}

scheme := h.Get("X-Forwarded-Proto")
Expand Down
26 changes: 26 additions & 0 deletions links/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,29 @@ func TestBuilder_BuildLink(t *testing.T) {
})

}

func Test_FromHeadersOrDefault_NonApiHost(t *testing.T) {
Convey("Given a non-api forwarded host", t, func() {
defaultURL, err := url.Parse("http://localhost:8080/")
So(err, ShouldBeNil)

h := &http.Header{}
h.Add("X-Forwarded-Host", "internalhost")
h.Add("X-Forwarded-Path-Prefix", "/prefix")

r := &http.Request{
URL: &url.URL{},
Host: "differenthost:8080",
}

Convey("When the builder is created", func() {
builder := FromHeadersOrDefault(h, r, defaultURL)

Convey("Then the builder URL should be the default URL with the path prefix", func() {
So(builder, ShouldNotBeNil)
So(builder.URL, ShouldNotBeNil)
So(builder.URL.String(), ShouldEqual, "http://localhost:8080/prefix")
})
})
})
}

0 comments on commit c69b9d5

Please sign in to comment.