Skip to content

Commit 420674c

Browse files
committed
Try to fix issue with IP address look-up
1 parent 689ef69 commit 420674c

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

.golangci.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
# inspired by: https://github.com/golangci/golangci-lint/blob/master/.golangci.yml
22

33
run:
4-
go: "1.18"
4+
go: "1.19"
55

66
linters-settings:
77
# dupl:
88
# threshold: 100
99
funlen:
1010
lines: 100
1111
statements: 50
12-
golint:
13-
min-confidence: 0
1412
lll:
1513
line-length: 125
1614
goconst:
1715
min-len: 2
18-
min-occurences: 2
16+
min-occurrences: 3
1917
misspell:
2018
locale: US
2119

@@ -26,14 +24,12 @@ linters:
2624
# - bidichk
2725
# - bodyclose
2826
# - contextcheck
29-
- deadcode
3027
- depguard
3128
- dogsled
3229
# - dupl
3330
# - durationcheck
3431
- errcheck
3532
# - errname
36-
- exportloopref
3733
- funlen
3834
- gochecknoinits
3935
- goconst
@@ -60,15 +56,13 @@ linters:
6056
# - predeclared
6157
# - revive
6258
- staticcheck
63-
- structcheck
6459
- stylecheck
6560
# - tagliatelle
6661
# - thelper
6762
- typecheck
6863
- unconvert
6964
- unparam
7065
- unused
71-
- varcheck
7266
# - wastedassign
7367
- whitespace
7468

geoblock_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,28 @@ func TestMultipleIpAddressesReverse(t *testing.T) {
207207
assertStatusCode(t, recorder.Result(), http.StatusForbidden)
208208
}
209209

210+
func createMockAPIServer(t *testing.T, expectedRequestIP string, response []byte) *httptest.Server {
211+
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
212+
t.Logf("Intercepted request: %s %s", req.Method, req.URL.String())
213+
t.Logf("Headers: %v", req.Header)
214+
215+
if !strings.Contains(req.URL.String(), expectedRequestIP) {
216+
t.Errorf("Unexpected IP: %s", req.URL.String()[1:])
217+
}
218+
rw.WriteHeader(http.StatusOK)
219+
_, _ = rw.Write(response)
220+
}))
221+
}
222+
210223
func TestMultipleIpAddressesProxy(t *testing.T) {
224+
mockServer := createMockAPIServer(t, caExampleIP, []byte(`CA`))
225+
defer mockServer.Close()
226+
211227
cfg := createTesterConfig()
212228

213229
cfg.Countries = append(cfg.Countries, "CA")
214230
cfg.XForwardedForReverseProxy = true
231+
cfg.API = mockServer.URL + "/{ip}"
215232

216233
ctx := context.Background()
217234
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
@@ -236,10 +253,14 @@ func TestMultipleIpAddressesProxy(t *testing.T) {
236253
}
237254

238255
func TestMultipleIpAddressesProxyReverse(t *testing.T) {
256+
mockServer := createMockAPIServer(t, chExampleIP, []byte(`CH`))
257+
defer mockServer.Close()
258+
239259
cfg := createTesterConfig()
240260

241261
cfg.Countries = append(cfg.Countries, "CA")
242262
cfg.XForwardedForReverseProxy = true
263+
cfg.API = mockServer.URL + "/{ip}"
243264

244265
ctx := context.Background()
245266
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})

0 commit comments

Comments
 (0)