@@ -153,10 +153,33 @@ func TestMultipleAllowedCountry(t *testing.T) {
153153 assertStatusCode (t , recorder .Result (), http .StatusOK )
154154}
155155
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+
156175func TestMultipleIpAddresses (t * testing.T ) {
176+ mockServer := createMockAPIServer (t , map [string ][]byte {caExampleIP : []byte (`CA` ), chExampleIP : []byte (`CH` )})
177+ defer mockServer .Close ()
178+
157179 cfg := createTesterConfig ()
158180
159181 cfg .Countries = append (cfg .Countries , "CH" )
182+ cfg .API = mockServer .URL + "/{ip}"
160183
161184 ctx := context .Background ()
162185 next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -181,9 +204,13 @@ func TestMultipleIpAddresses(t *testing.T) {
181204}
182205
183206func TestMultipleIpAddressesReverse (t * testing.T ) {
207+ mockServer := createMockAPIServer (t , map [string ][]byte {caExampleIP : []byte (`CA` ), chExampleIP : []byte (`CH` )})
208+ defer mockServer .Close ()
209+
184210 cfg := createTesterConfig ()
185211
186212 cfg .Countries = append (cfg .Countries , "CH" )
213+ cfg .API = mockServer .URL + "/{ip}"
187214
188215 ctx := context .Background ()
189216 next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -208,10 +235,14 @@ func TestMultipleIpAddressesReverse(t *testing.T) {
208235}
209236
210237func TestMultipleIpAddressesProxy (t * testing.T ) {
238+ mockServer := createMockAPIServer (t , map [string ][]byte {caExampleIP : []byte (`CA` )})
239+ defer mockServer .Close ()
240+
211241 cfg := createTesterConfig ()
212242
213243 cfg .Countries = append (cfg .Countries , "CA" )
214244 cfg .XForwardedForReverseProxy = true
245+ cfg .API = mockServer .URL + "/{ip}"
215246
216247 ctx := context .Background ()
217248 next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -228,18 +259,24 @@ func TestMultipleIpAddressesProxy(t *testing.T) {
228259 t .Fatal (err )
229260 }
230261
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 )
232265
233266 handler .ServeHTTP (recorder , req )
234267
235268 assertStatusCode (t , recorder .Result (), http .StatusOK )
236269}
237270
238271func TestMultipleIpAddressesProxyReverse (t * testing.T ) {
272+ mockServer := createMockAPIServer (t , map [string ][]byte {chExampleIP : []byte (`CH` )})
273+ defer mockServer .Close ()
274+
239275 cfg := createTesterConfig ()
240276
241277 cfg .Countries = append (cfg .Countries , "CA" )
242278 cfg .XForwardedForReverseProxy = true
279+ cfg .API = mockServer .URL + "/{ip}"
243280
244281 ctx := context .Background ()
245282 next := http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {})
@@ -256,7 +293,9 @@ func TestMultipleIpAddressesProxyReverse(t *testing.T) {
256293 t .Fatal (err )
257294 }
258295
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 )
260299
261300 handler .ServeHTTP (recorder , req )
262301
0 commit comments