-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2386 from threefoldtech/development_geoip_docs_tests
Development geoip docs tests
- Loading branch information
Showing
6 changed files
with
105 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Geoip Module | ||
|
||
The `geoip` module provides a simple way to determine the geographical location of the system executing the code. | ||
This includes details such as longitude, latitude, country, city, and continent. | ||
|
||
## Features | ||
|
||
- **Failover Mechanism:** The module attempts to fetch location data from multiple services to ensure high availability. If one URL fails, it logs the error and retries with the next URL. | ||
- **Structured Location Data:** Returns structured data in a `Location` struct for easy integration. | ||
|
||
```go | ||
type Location struct { | ||
Longitude float64 `json:"longitude"` | ||
Latitude float64 `json:"latitude"` | ||
Continent string `json:"continent"` | ||
Country string `json:"country_name"` | ||
CountryCode string `json:"country_code"` | ||
City string `json:"city_name"` | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
The module provides a single exported function: `Fetch`. | ||
|
||
The `Fetch` function retrieves the geographical location of the system calling the function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package geoip | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetLocation(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
require := require.New(t) | ||
|
||
t.Run("test valid response", func(t *testing.T) { | ||
l := Location{ | ||
Continent: "Africa", | ||
Country: "Egypt", | ||
City: "Cairo", | ||
} | ||
|
||
for _, url := range geoipURLs { | ||
httpmock.RegisterResponder("GET", url, | ||
func(req *http.Request) (*http.Response, error) { | ||
return httpmock.NewJsonResponse(200, l) | ||
}, | ||
) | ||
|
||
resp, err := getLocation(url) | ||
require.NoError(err) | ||
require.Equal(resp, l) | ||
} | ||
}) | ||
|
||
l := Location{ | ||
Continent: "Unknown", | ||
Country: "Unknown", | ||
City: "Unknown", | ||
} | ||
|
||
t.Run("test 404 status code", func(t *testing.T) { | ||
for _, url := range geoipURLs { | ||
httpmock.RegisterResponder("GET", url, | ||
func(req *http.Request) (*http.Response, error) { | ||
return httpmock.NewJsonResponse(404, l) | ||
}, | ||
) | ||
|
||
resp, err := getLocation(url) | ||
require.Error(err) | ||
require.Equal(resp, l) | ||
} | ||
}) | ||
|
||
t.Run("test invalid response data", func(t *testing.T) { | ||
for _, url := range geoipURLs { | ||
httpmock.RegisterResponder("GET", url, | ||
func(req *http.Request) (*http.Response, error) { | ||
resp, err := httpmock.NewJsonResponse(200, "Cairo") | ||
return resp, err | ||
}, | ||
) | ||
resp, err := getLocation(url) | ||
require.Error(err) | ||
require.Equal(resp, l) | ||
} | ||
}) | ||
} |