-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocation_test.go
More file actions
180 lines (149 loc) · 5.3 KB
/
location_test.go
File metadata and controls
180 lines (149 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// SPDX-License-Identifier: EUPL-1.2
package api_test
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-contrib/location/v2"
"github.com/gin-gonic/gin"
api "dappco.re/go/core/api"
)
// ── Helpers ─────────────────────────────────────────────────────────────
// locationTestGroup exposes a route that returns the detected location.
type locationTestGroup struct{}
func (l *locationTestGroup) Name() string { return "loc" }
func (l *locationTestGroup) BasePath() string { return "/loc" }
func (l *locationTestGroup) RegisterRoutes(rg *gin.RouterGroup) {
rg.GET("/info", func(c *gin.Context) {
url := location.Get(c)
c.JSON(http.StatusOK, api.OK(map[string]string{
"scheme": url.Scheme,
"host": url.Host,
}))
})
}
// locationResponse is the typed response envelope for location info tests.
type locationResponse struct {
Success bool `json:"success"`
Data map[string]string `json:"data"`
}
// ── WithLocation ────────────────────────────────────────────────────────
func TestWithLocation_Good_DetectsForwardedHost(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithLocation())
e.Register(&locationTestGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/loc/info", nil)
req.Header.Set("X-Forwarded-Host", "api.example.com")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
var resp locationResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if resp.Data["host"] != "api.example.com" {
t.Fatalf("expected host=%q, got %q", "api.example.com", resp.Data["host"])
}
}
func TestWithLocation_Good_DetectsForwardedProto(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithLocation())
e.Register(&locationTestGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/loc/info", nil)
req.Header.Set("X-Forwarded-Proto", "https")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
var resp locationResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if resp.Data["scheme"] != "https" {
t.Fatalf("expected scheme=%q, got %q", "https", resp.Data["scheme"])
}
}
func TestWithLocation_Good_FallsBackToRequestHost(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithLocation())
e.Register(&locationTestGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/loc/info", nil)
// No X-Forwarded-* headers — middleware should fall back to defaults.
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
var resp locationResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
// Without forwarded headers the middleware falls back to its default
// scheme ("http"). The host will be either the request Host header
// value or the configured default; either way it must not be empty.
if resp.Data["scheme"] != "http" {
t.Fatalf("expected fallback scheme=%q, got %q", "http", resp.Data["scheme"])
}
if resp.Data["host"] == "" {
t.Fatal("expected a non-empty host in fallback mode")
}
}
func TestWithLocation_Good_CombinesWithOtherMiddleware(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(
api.WithLocation(),
api.WithRequestID(),
)
e.Register(&locationTestGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/loc/info", nil)
req.Header.Set("X-Forwarded-Host", "proxy.example.com")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
// Location middleware should populate the detected host.
var resp locationResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if resp.Data["host"] != "proxy.example.com" {
t.Fatalf("expected host=%q, got %q", "proxy.example.com", resp.Data["host"])
}
// RequestID middleware should also have run.
if w.Header().Get("X-Request-ID") == "" {
t.Fatal("expected X-Request-ID header from WithRequestID")
}
}
func TestWithLocation_Good_BothHeadersCombined(t *testing.T) {
gin.SetMode(gin.TestMode)
e, _ := api.New(api.WithLocation())
e.Register(&locationTestGroup{})
h := e.Handler()
w := httptest.NewRecorder()
req, _ := http.NewRequest(http.MethodGet, "/loc/info", nil)
req.Header.Set("X-Forwarded-Proto", "https")
req.Header.Set("X-Forwarded-Host", "secure.example.com")
h.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
var resp locationResponse
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if resp.Data["scheme"] != "https" {
t.Fatalf("expected scheme=%q, got %q", "https", resp.Data["scheme"])
}
if resp.Data["host"] != "secure.example.com" {
t.Fatalf("expected host=%q, got %q", "secure.example.com", resp.Data["host"])
}
}