Skip to content

Commit 44441fc

Browse files
committed
update geoip tests
1 parent 19e5376 commit 44441fc

File tree

4 files changed

+40
-51
lines changed

4 files changed

+40
-51
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ require (
3939
github.com/joncrlsn/dque v0.0.0-20200702023911-3e80e3146ce5
4040
github.com/lestrrat-go/jwx v1.1.7
4141
github.com/machinebox/graphql v0.2.2
42-
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a
4342
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417
4443
github.com/patrickmn/go-cache v2.1.0+incompatible
4544
github.com/pkg/errors v0.9.1

pkg/geoip/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Geoip Module
22

3-
The `geoip` module provides a simple way to determine the geographical location of an IPv4 or IPv6 address.
3+
The `geoip` module provides a simple way to determine the geographical location of the system executing the code.
44
This includes details such as longitude, latitude, country, city, and continent.
55

66
## Features

pkg/geoip/geoip.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ var (
2929

3030
// Fetch retrieves the location of the system calling this function
3131
func Fetch() (Location, error) {
32-
3332
for _, url := range geoipURLs {
3433
l, err := getLocation(url)
3534
if err != nil {

pkg/geoip/geoip_test.go

Lines changed: 39 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,66 @@ import (
55
"testing"
66

77
"github.com/jarcoal/httpmock"
8-
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
99
)
1010

11-
func Test_getLocation(t *testing.T) {
11+
func TestGetLocation(t *testing.T) {
1212
httpmock.Activate()
1313
defer httpmock.DeactivateAndReset()
1414

15-
t.Run("running correct response", func(t *testing.T) {
16-
for i := 0; i < len(geoipURLs); i++ {
17-
httpmock.RegisterResponder("GET", geoipURLs[i],
15+
require := require.New(t)
16+
17+
t.Run("test valid response", func(t *testing.T) {
18+
l := Location{
19+
Continent: "Africa",
20+
Country: "Egypt",
21+
City: "Cairo",
22+
}
23+
24+
for _, url := range geoipURLs {
25+
httpmock.RegisterResponder("GET", url,
1826
func(req *http.Request) (*http.Response, error) {
19-
l := Location{
20-
Continent: "Africa",
21-
Country: "Egypt",
22-
City: "Cairo",
23-
}
24-
resp, err := httpmock.NewJsonResponse(200, l)
25-
return resp, err
27+
return httpmock.NewJsonResponse(200, l)
2628
},
2729
)
28-
value, err := getLocation(geoipURLs[i])
29-
assert.Equal(t, nil, err)
30-
assert.Equal(t, "Egypt", value.Country)
31-
assert.Equal(t, "Africa", value.Continent)
32-
assert.Equal(t, "Cairo", value.City)
33-
if err != nil {
34-
t.Errorf("got %v", err)
35-
}
30+
31+
resp, err := getLocation(url)
32+
require.NoError(err)
33+
require.Equal(resp, l)
3634
}
3735
})
3836

39-
t.Run("asserting wrong response code", func(t *testing.T) {
40-
for i := 0; i < len(geoipURLs); i++ {
41-
httpmock.RegisterResponder("GET", geoipURLs[i],
37+
l := Location{
38+
Continent: "Unknown",
39+
Country: "Unknown",
40+
City: "Unknown",
41+
}
42+
43+
t.Run("test 404 status code", func(t *testing.T) {
44+
for _, url := range geoipURLs {
45+
httpmock.RegisterResponder("GET", url,
4246
func(req *http.Request) (*http.Response, error) {
43-
l := Location{
44-
Continent: "Africa",
45-
Country: "Egypt",
46-
City: "Cairo",
47-
}
48-
resp, err := httpmock.NewJsonResponse(404, l)
49-
return resp, err
47+
return httpmock.NewJsonResponse(404, l)
5048
},
5149
)
52-
value, err := getLocation(geoipURLs[i])
53-
assert.NotEqual(t, err, nil)
54-
assert.Equal(t, "Unknown", value.Country)
55-
assert.Equal(t, "Unknown", value.Continent)
56-
assert.Equal(t, "Unknown", value.City)
50+
51+
resp, err := getLocation(url)
52+
require.Error(err)
53+
require.Equal(resp, l)
5754
}
5855
})
5956

60-
t.Run("asserting sending wrong response data", func(t *testing.T) {
61-
for i := 0; i < len(geoipURLs); i++ {
62-
httpmock.RegisterResponder("GET", geoipURLs[i],
57+
t.Run("test invalid response data", func(t *testing.T) {
58+
for _, url := range geoipURLs {
59+
httpmock.RegisterResponder("GET", url,
6360
func(req *http.Request) (*http.Response, error) {
64-
l := Location{
65-
City: "Cairo",
66-
}
67-
resp, err := httpmock.NewJsonResponse(200, l.City)
61+
resp, err := httpmock.NewJsonResponse(200, "Cairo")
6862
return resp, err
6963
},
7064
)
71-
value, err := getLocation(geoipURLs[i])
72-
assert.NotEqual(t, err, nil)
73-
assert.Equal(t, "Unknown", value.Country)
74-
assert.Equal(t, "Unknown", value.Continent)
75-
assert.Equal(t, "Unknown", value.City)
65+
resp, err := getLocation(url)
66+
require.Error(err)
67+
require.Equal(resp, l)
7668
}
7769
})
78-
7970
}

0 commit comments

Comments
 (0)