Skip to content

Commit 3e76ded

Browse files
authored
fix: use baseURL instead of requestURL for /v1 endpoint (#250)
With this, requestURL is unused and therefore removed.
1 parent 288033e commit 3e76ded

File tree

3 files changed

+14
-50
lines changed

3 files changed

+14
-50
lines changed

internal/router/middleware.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func URLMiddleware() gin.HandlerFunc {
2626
}
2727

2828
c.Set("baseURL", url.String())
29-
c.Set("requestURL", url.String()+c.Request.URL.Path)
3029
c.Next()
3130
}
3231
}

internal/router/middleware_test.go

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"testing"
88

99
"github.com/envelope-zero/backend/internal/router"
10-
"github.com/envelope-zero/backend/pkg/test"
1110
"github.com/gin-gonic/gin"
1211
"github.com/stretchr/testify/assert"
1312
)
@@ -19,27 +18,15 @@ func TestURLMiddlewareContextSet(t *testing.T) {
1918
os.Setenv("API_URL", "https://ez.example.com:8081/api")
2019

2120
r.GET("/envelopes", func(ctx *gin.Context) {
22-
urlMiddleware := router.URLMiddleware()
23-
urlMiddleware(c)
24-
25-
c.JSON(http.StatusOK, map[string]string{
26-
"baseURL": c.GetString("baseURL"),
27-
"requestURL": c.GetString("requestURL"),
28-
})
21+
router.URLMiddleware()(c)
22+
c.String(http.StatusOK, c.GetString("baseURL"))
2923
})
3024

31-
urls := struct {
32-
BaseURL string `json:"baseURL"`
33-
RequestURL string `json:"requestURL"`
34-
}{}
35-
3625
// Make and decode repsonse
3726
c.Request, _ = http.NewRequest(http.MethodGet, "https://example.com/envelopes", nil)
3827
r.ServeHTTP(w, c.Request)
39-
test.DecodeResponse(t, w, &urls)
4028

41-
assert.Equal(t, "https://ez.example.com:8081/api", urls.BaseURL)
42-
assert.Equal(t, "https://ez.example.com:8081/api/envelopes", urls.RequestURL)
29+
assert.Equal(t, "https://ez.example.com:8081/api", w.Body.String())
4330

4431
os.Unsetenv("API_URL")
4532
}
@@ -51,27 +38,15 @@ func TestURLMiddlewareNotAnURL(t *testing.T) {
5138
os.Setenv("API_URL", "https://ez.examp\\?le.com:8081/api")
5239

5340
r.GET("/envelopes", func(ctx *gin.Context) {
54-
urlMiddleware := router.URLMiddleware()
55-
urlMiddleware(c)
56-
57-
c.JSON(http.StatusOK, map[string]string{
58-
"baseURL": c.GetString("baseURL"),
59-
"requestURL": c.GetString("requestURL"),
60-
})
41+
router.URLMiddleware()(c)
42+
c.String(http.StatusOK, c.GetString("baseURL"))
6143
})
6244

63-
urls := struct {
64-
BaseURL string `json:"baseURL"`
65-
RequestURL string `json:"requestURL"`
66-
}{}
67-
6845
// Make and decode repsonse
6946
c.Request, _ = http.NewRequest(http.MethodGet, "https://example.com/envelopes", nil)
7047
r.ServeHTTP(w, c.Request)
71-
test.DecodeResponse(t, w, &urls)
7248

73-
assert.Equal(t, "", urls.BaseURL)
74-
assert.Equal(t, "", urls.RequestURL)
49+
assert.Equal(t, "", w.Body.String())
7550

7651
os.Unsetenv("API_URL")
7752
}
@@ -84,22 +59,12 @@ func TestURLMiddlewareEnvNotSet(t *testing.T) {
8459
urlMiddleware := router.URLMiddleware()
8560
urlMiddleware(c)
8661

87-
c.JSON(http.StatusOK, map[string]string{
88-
"baseURL": c.GetString("baseURL"),
89-
"requestURL": c.GetString("requestURL"),
90-
})
62+
c.String(http.StatusOK, c.GetString("baseURL"))
9163
})
9264

93-
urls := struct {
94-
BaseURL string `json:"baseURL"`
95-
RequestURL string `json:"requestURL"`
96-
}{}
97-
9865
// Make and decode repsonse
9966
c.Request, _ = http.NewRequest(http.MethodGet, "https://example.com/envelopes", nil)
10067
r.ServeHTTP(w, c.Request)
101-
test.DecodeResponse(t, w, &urls)
10268

103-
assert.Equal(t, "", urls.BaseURL)
104-
assert.Equal(t, "", urls.RequestURL)
69+
assert.Equal(t, "", w.Body.String())
10570
}

internal/router/router.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,12 @@ type V1Links struct {
210210
func GetV1(c *gin.Context) {
211211
c.JSON(http.StatusOK, V1Response{
212212
Links: V1Links{
213-
Budgets: c.GetString("requestURL") + "/v1/budgets",
214-
Accounts: c.GetString("requestURL") + "/v1/accounts",
215-
Categories: c.GetString("requestURL") + "/v1/categories",
216-
Transactions: c.GetString("requestURL") + "/v1/transactions",
217-
Envelopes: c.GetString("requestURL") + "/v1/envelopes",
218-
Allocations: c.GetString("requestURL") + "/v1/allocations",
213+
Budgets: c.GetString("baseURL") + "/v1/budgets",
214+
Accounts: c.GetString("baseURL") + "/v1/accounts",
215+
Categories: c.GetString("baseURL") + "/v1/categories",
216+
Transactions: c.GetString("baseURL") + "/v1/transactions",
217+
Envelopes: c.GetString("baseURL") + "/v1/envelopes",
218+
Allocations: c.GetString("baseURL") + "/v1/allocations",
219219
},
220220
})
221221
}

0 commit comments

Comments
 (0)