@@ -153,10 +153,33 @@ func TestMultipleAllowedCountry(t *testing.T) {
153
153
assertStatusCode (t , recorder .Result (), http .StatusOK )
154
154
}
155
155
156
+ func createMockAPIServer (t * testing.T , ipResponseMap map [string ][]byte ) * httptest.Server {
157
+ return httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
158
+ t .Logf ("Intercepted request: %s %s" , req .Method , req .URL .String ())
159
+ t .Logf ("Headers: %v" , req .Header )
160
+
161
+ requestedIP := req .URL .String ()[1 :]
162
+
163
+ if response , exists := ipResponseMap [requestedIP ]; exists {
164
+ t .Logf ("Matched IP: %s" , requestedIP )
165
+ rw .WriteHeader (http .StatusOK )
166
+ _ , _ = rw .Write (response )
167
+ } else {
168
+ t .Errorf ("Unexpected IP: %s" , requestedIP )
169
+ rw .WriteHeader (http .StatusNotFound )
170
+ _ , _ = rw .Write ([]byte (`{"error": "IP not found"}` ))
171
+ }
172
+ }))
173
+ }
174
+
156
175
func TestMultipleIpAddresses (t * testing.T ) {
176
+ mockServer := createMockAPIServer (t , map [string ][]byte {caExampleIP : []byte (`CA` ), chExampleIP : []byte (`CH` )})
177
+ defer mockServer .Close ()
178
+
157
179
cfg := createTesterConfig ()
158
180
159
181
cfg .Countries = append (cfg .Countries , "CH" )
182
+ cfg .API = mockServer .URL + "/{ip}"
160
183
161
184
ctx := context .Background ()
162
185
next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -181,9 +204,13 @@ func TestMultipleIpAddresses(t *testing.T) {
181
204
}
182
205
183
206
func TestMultipleIpAddressesReverse (t * testing.T ) {
207
+ mockServer := createMockAPIServer (t , map [string ][]byte {caExampleIP : []byte (`CA` ), chExampleIP : []byte (`CH` )})
208
+ defer mockServer .Close ()
209
+
184
210
cfg := createTesterConfig ()
185
211
186
212
cfg .Countries = append (cfg .Countries , "CH" )
213
+ cfg .API = mockServer .URL + "/{ip}"
187
214
188
215
ctx := context .Background ()
189
216
next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -208,10 +235,14 @@ func TestMultipleIpAddressesReverse(t *testing.T) {
208
235
}
209
236
210
237
func TestMultipleIpAddressesProxy (t * testing.T ) {
238
+ mockServer := createMockAPIServer (t , map [string ][]byte {caExampleIP : []byte (`CA` )})
239
+ defer mockServer .Close ()
240
+
211
241
cfg := createTesterConfig ()
212
242
213
243
cfg .Countries = append (cfg .Countries , "CA" )
214
244
cfg .XForwardedForReverseProxy = true
245
+ cfg .API = mockServer .URL + "/{ip}"
215
246
216
247
ctx := context .Background ()
217
248
next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -228,18 +259,24 @@ func TestMultipleIpAddressesProxy(t *testing.T) {
228
259
t .Fatal (err )
229
260
}
230
261
231
- req .Header .Add (xForwardedFor , strings .Join ([]string {caExampleIP , chExampleIP }, "," ))
262
+ forwardedForContent := strings .Join ([]string {caExampleIP , chExampleIP }, "," )
263
+ req .Header .Add (xForwardedFor , forwardedForContent )
264
+ t .Logf ("X-Forwarded-For header: %s" , forwardedForContent )
232
265
233
266
handler .ServeHTTP (recorder , req )
234
267
235
268
assertStatusCode (t , recorder .Result (), http .StatusOK )
236
269
}
237
270
238
271
func TestMultipleIpAddressesProxyReverse (t * testing.T ) {
272
+ mockServer := createMockAPIServer (t , map [string ][]byte {chExampleIP : []byte (`CH` )})
273
+ defer mockServer .Close ()
274
+
239
275
cfg := createTesterConfig ()
240
276
241
277
cfg .Countries = append (cfg .Countries , "CA" )
242
278
cfg .XForwardedForReverseProxy = true
279
+ cfg .API = mockServer .URL + "/{ip}"
243
280
244
281
ctx := context .Background ()
245
282
next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -256,7 +293,9 @@ func TestMultipleIpAddressesProxyReverse(t *testing.T) {
256
293
t .Fatal (err )
257
294
}
258
295
259
- req .Header .Add (xForwardedFor , strings .Join ([]string {chExampleIP , caExampleIP }, "," ))
296
+ forwardedForContent := strings .Join ([]string {chExampleIP , caExampleIP }, "," )
297
+ req .Header .Add (xForwardedFor , forwardedForContent )
298
+ t .Logf ("X-Forwarded-For header: %s" , forwardedForContent )
260
299
261
300
handler .ServeHTTP (recorder , req )
262
301
0 commit comments