Skip to content

Commit

Permalink
builder uses path-prefix when available, tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
aryan-p03 committed Jan 15, 2025
1 parent 3904b08 commit 5e882ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions links/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,29 @@ type Builder struct {
}

func FromHeadersOrDefault(h *http.Header, r *http.Request, defaultURL *url.URL) *Builder {
path := h.Get("X-Forwarded-Path-Prefix")

host := h.Get("X-Forwarded-Host")
if host == "" {
if r.Host != "" {
defaultURL.Host = r.Host
}
defaultURL = defaultURL.JoinPath(path)
return &Builder{
URL: defaultURL,
}
}

scheme := h.Get("X-Forwarded-Proto")
if scheme == "" {
scheme = "http"
scheme = "https"
}

port := h.Get("X-Forwarded-Port")
if port != "" {
host += ":" + port
}

path := h.Get("X-Forwarded-Path-Prefix")

url := &url.URL{
Scheme: scheme,
Host: host,
Expand Down
16 changes: 8 additions & 8 deletions links/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
"forwardedhost",
"",
"",
"http://forwardedhost/",
"https://forwardedhost/",
},
// With only forwarded port
{
Expand All @@ -71,7 +71,7 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
"",
"",
"/prefix",
"http://localhost:8080/",
"http://localhost:8080/prefix",
},
// Without all headers except forwarded proto
{
Expand All @@ -80,7 +80,7 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
"forwardedhost",
"9090",
"/prefix",
"http://forwardedhost:9090/prefix",
"https://forwardedhost:9090/prefix",
},
// Without all headers except forwarded host
{
Expand All @@ -89,7 +89,7 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
"",
"9090",
"/prefix",
"http://localhost:8080/",
"http://localhost:8080/prefix",
},
// Without all headers except forwarded port
{
Expand Down Expand Up @@ -125,7 +125,7 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
"forwardedhost",
"9090",
"",
"http://forwardedhost:9090/",
"https://forwardedhost:9090/",
},
// With only forwarded prefix and host
{
Expand All @@ -134,7 +134,7 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
"forwardedhost",
"",
"/prefix",
"http://forwardedhost/prefix",
"https://forwardedhost/prefix",
},
// With only forwarded proto and port
{
Expand All @@ -152,7 +152,7 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
"",
"",
"/prefix",
"http://localhost:8080/",
"http://localhost:8080/prefix",
},
// With only forwarded port and prefix
{
Expand All @@ -161,7 +161,7 @@ func Test_FromHeadersOrDefault_With_Forwarded_Headers(t *testing.T) {
"",
"9090",
"/prefix",
"http://localhost:8080/",
"http://localhost:8080/prefix",
},
}

Expand Down

0 comments on commit 5e882ea

Please sign in to comment.