Skip to content

Commit f624884

Browse files
committed
removed logging and refactored code
1 parent f19da28 commit f624884

File tree

2 files changed

+56
-100
lines changed

2 files changed

+56
-100
lines changed

links/links.go

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,35 @@ type Builder struct {
1212
URL *url.URL
1313
}
1414

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

1918
host := h.Get("X-Forwarded-Host")
20-
if host == "" || r.Host == "" {
21-
defaultURL = defaultURL.JoinPath(path)
19+
if host == "" || !strings.HasPrefix(host, "api.") {
2220
return &Builder{
23-
URL: defaultURL,
24-
}
25-
}
26-
if !strings.HasPrefix(host, "api") {
27-
defaultURL = defaultURL.JoinPath(path)
28-
return &Builder{
29-
URL: defaultURL,
21+
URL: defaultURL.JoinPath(path),
3022
}
3123
}
3224

3325
scheme := h.Get("X-Forwarded-Proto")
3426
if scheme == "" {
35-
if !strings.HasPrefix(host, "api") {
36-
scheme = "http"
37-
} else {
38-
scheme = "https"
39-
}
40-
}
41-
42-
url := &url.URL{
43-
Scheme: scheme,
44-
Host: host,
45-
Path: "/",
27+
scheme = "https"
4628
}
4729

4830
return &Builder{
49-
URL: url.JoinPath(path),
31+
URL: &url.URL{
32+
Scheme: scheme,
33+
Host: host,
34+
Path: path,
35+
},
5036
}
5137
}
5238

5339
func (b *Builder) BuildURL(oldURL *url.URL) *url.URL {
5440
newPath := oldURL.Path
55-
newPath = strings.ReplaceAll(newPath, "/v1", "")
41+
for strings.HasPrefix(newPath, "/v1") {
42+
newPath = strings.TrimPrefix(newPath, "/v1")
43+
}
5644

5745
apiURL := b.URL.JoinPath(newPath)
5846
apiURL.RawQuery = oldURL.RawQuery
@@ -64,6 +52,6 @@ func (b *Builder) BuildLink(link string) (string, error) {
6452
if err != nil {
6553
return "", errors.Wrap(err, "unable to parse link to URL")
6654
}
67-
newURL := b.BuildURL(oldURL)
68-
return newURL.String(), nil
55+
56+
return b.BuildURL(oldURL).String(), nil
6957
}

links/links_test.go

Lines changed: 42 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -20,99 +20,99 @@ func Test_FromHeadersOrDefault(t *testing.T) {
2020
}{
2121
// Without any forwarded headers
2222
{
23-
"http://localhost:8080/",
23+
"http://localhost:8080",
2424
"",
2525
"",
2626
"",
27-
"http://localhost:8080/",
27+
"http://localhost:8080",
2828
},
2929
// With all forwarded headers
3030
{
31-
"http://localhost:8080/",
31+
"http://localhost:8080",
3232
"https",
3333
"api.external.host",
34-
"/prefix",
34+
"prefix",
3535
"https://api.external.host/prefix",
3636
},
3737
// With only forwarded proto
3838
{
39-
"http://localhost:8080/",
39+
"http://localhost:8080",
4040
"https",
4141
"",
4242
"",
43-
"http://localhost:8080/",
43+
"http://localhost:8080",
4444
},
4545
// With only forwarded host
4646
{
47-
"http://localhost:8080/",
47+
"http://localhost:8080",
4848
"",
4949
"api.external.host",
5050
"",
51-
"https://api.external.host/",
51+
"https://api.external.host",
5252
},
5353
// With only forwarded path prefix
5454
{
55-
"http://localhost:8080/",
55+
"http://localhost:8080",
5656
"",
5757
"",
58-
"/prefix",
58+
"prefix",
5959
"http://localhost:8080/prefix",
6060
},
6161
// Without all headers except forwarded proto
6262
{
63-
"http://localhost:8080/",
63+
"http://localhost:8080",
6464
"",
6565
"api.external.host",
66-
"/prefix",
66+
"prefix",
6767
"https://api.external.host/prefix",
6868
},
6969
// Without all headers except forwarded host
7070
{
71-
"http://localhost:8080/",
71+
"http://localhost:8080",
7272
"https",
7373
"",
74-
"/prefix",
74+
"prefix",
7575
"http://localhost:8080/prefix",
7676
},
7777
// Without all headers except forwarded path prefix
7878
{
79-
"http://localhost:8080/",
79+
"http://localhost:8080",
8080
"https",
8181
"api.external.host",
8282
"",
83-
"https://api.external.host/",
83+
"https://api.external.host",
8484
},
8585
// With only forwarded proto and host
8686
{
87-
"http://localhost:8080/",
87+
"http://localhost:8080",
8888
"https",
8989
"api.external.host",
9090
"",
91-
"https://api.external.host/",
91+
"https://api.external.host",
9292
},
9393
// With only forwarded prefix and host
9494
{
95-
"http://localhost:8080/",
95+
"http://localhost:8080",
9696
"",
9797
"api.external.host",
98-
"/prefix",
98+
"prefix",
9999
"https://api.external.host/prefix",
100100
},
101101
// With only forwarded proto and prefix
102102
{
103-
"http://localhost:8080/",
103+
"http://localhost:8080",
104104
"https",
105105
"",
106-
"/prefix",
106+
"prefix",
107107
"http://localhost:8080/prefix",
108108
},
109109
// With non-external forwarded host
110110
{
111-
"http://localhost:8080/",
111+
"http://localhost:8080",
112112
"",
113113
"internalhost",
114114
"",
115-
"http://localhost:8080/",
115+
"http://localhost:8080",
116116
},
117117
}
118118

@@ -132,41 +132,15 @@ func Test_FromHeadersOrDefault(t *testing.T) {
132132
}
133133

134134
du.JoinPath()
135-
r := &http.Request{
136-
URL: &url.URL{},
137-
Host: "localhost:8080",
138-
Header: h,
139-
}
140-
builder := FromHeadersOrDefault(r, du)
135+
136+
builder := FromHeadersOrDefault(&h, du)
141137
So(builder, ShouldNotBeNil)
142138
So(builder.URL, ShouldNotBeNil)
143139
So(builder.URL.String(), ShouldEqual, tt.want)
144140

145141
}
146142

147143
})
148-
149-
Convey("Given an empty incoming request host", t, func() {
150-
r := &http.Request{
151-
URL: &url.URL{},
152-
Host: "",
153-
}
154-
155-
Convey("When the builder is created without forwarded headers", func() {
156-
defaultURL, err := url.Parse("http://localhost:8080/")
157-
So(err, ShouldBeNil)
158-
159-
builder := FromHeadersOrDefault(r, defaultURL)
160-
161-
So(builder, ShouldNotBeNil)
162-
So(builder.URL, ShouldNotBeNil)
163-
164-
Convey("Then the builder URL should be the default URL", func() {
165-
So(builder.URL.String(), ShouldEqual, "http://localhost:8080/")
166-
})
167-
})
168-
})
169-
170144
}
171145

172146
func TestBuilder_BuildLink(t *testing.T) {
@@ -179,49 +153,49 @@ func TestBuilder_BuildLink(t *testing.T) {
179153
}{
180154
// Empty old link
181155
{
182-
"http://localhost:8080/",
156+
"http://localhost:8080",
183157
"",
184-
"http://localhost:8080/",
158+
"http://localhost:8080",
185159
},
186160
// Old link with no path
187161
{
188-
"http://localhost:8080/",
189-
"http://localhost:8080/",
190-
"http://localhost:8080/",
162+
"http://localhost:8080",
163+
"http://localhost:8080",
164+
"http://localhost:8080",
191165
},
192166
// Old link with different base url
193167
{
194-
"http://localhost:8080/",
195-
"https://oldHost:1000/",
196-
"http://localhost:8080/",
168+
"http://localhost:8080",
169+
"https://oldHost:1000",
170+
"http://localhost:8080",
197171
},
198172
// Old link with path
199173
{
200-
"http://localhost:8080/",
174+
"http://localhost:8080",
201175
"http://localhost:8080/some/path",
202176
"http://localhost:8080/some/path",
203177
},
204178
// Old link with path and different base url
205179
{
206-
"http://localhost:8080/",
180+
"http://localhost:8080",
207181
"http://oldHost:1000/some/path",
208182
"http://localhost:8080/some/path",
209183
},
210184
// Old link without base url
211185
{
212-
"http://localhost:8080/",
186+
"http://localhost:8080",
213187
"/some/path",
214188
"http://localhost:8080/some/path",
215189
},
216190
// Old link with query params
217191
{
218-
"http://localhost:8080/",
192+
"http://localhost:8080",
219193
"http://localhost:8080/some/path?param1=value1&param2=value2",
220194
"http://localhost:8080/some/path?param1=value1&param2=value2",
221195
},
222196
// Old external link to new internal url
223197
{
224-
"http://localhost:8080/",
198+
"http://localhost:8080",
225199
"https://some.api.host/v1/some/path",
226200
"http://localhost:8080/some/path",
227201
},
@@ -280,21 +254,15 @@ func TestBuilder_BuildLink(t *testing.T) {
280254

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

286260
h := http.Header{}
287261
h.Add("X-Forwarded-Host", "internalhost")
288-
h.Add("X-Forwarded-Path-Prefix", "/prefix")
289-
290-
r := &http.Request{
291-
URL: &url.URL{},
292-
Host: "differenthost:8080",
293-
Header: h,
294-
}
262+
h.Add("X-Forwarded-Path-Prefix", "prefix")
295263

296264
Convey("When the builder is created", func() {
297-
builder := FromHeadersOrDefault(r, defaultURL)
265+
builder := FromHeadersOrDefault(&h, defaultURL)
298266

299267
Convey("Then the builder URL should be the default URL with the path prefix", func() {
300268
So(builder, ShouldNotBeNil)

0 commit comments

Comments
 (0)