Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Mar 12, 2024
1 parent a3a140a commit 4e646d1
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 2 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/codecov.yaml
Original file line number Diff line number Diff line change
@@ -0,0 1,18 @@
name: Test and coverage

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 'stable'
- name: Run coverage
run: go test -v -cover ./... -coverprofile coverage.out -coverpkg ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/ip2location/ip2location-io-go)](https://goreportcard.com/report/github.com/ip2location/ip2location-io-go)
[![codecov](https://codecov.io/gh/ip2location/ip2location-io-go/graph/badge.svg?token=WE2JBOC3D0)](https://codecov.io/gh/ip2location/ip2location-io-go)

IP2Location.io Go SDK
=====================
Expand Down
99 changes: 97 additions & 2 deletions ip2locationio/domainwhois_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 28,8 @@ func TestInvalidKeyWhois(t *testing.T) {
}
}

// TestURLWhois calls LookUp with a bad URL.
func TestURLWhois(t *testing.T) {
// TestBadURLWhois calls LookUp with a bad URL.
func TestBadURLWhois(t *testing.T) {
dummy := "DUMMY"
config, err := OpenConfiguration(dummy)

Expand All @@ -52,6 52,54 @@ func TestURLWhois(t *testing.T) {
}
}

// TestNonJSONWhois calls LookUp with non-JSON result URL.
func TestNonJSONWhois(t *testing.T) {
dummy := "DUMMY"
config, err := OpenConfiguration(dummy)

if err != nil {
t.Fatalf(`OpenConfiguration(dummy) = % v, %v, error`, config, err)
}

whois, err := OpenDomainWhois(config)
whois.baseUrl = "ip2location.io"

if err != nil {
t.Fatalf(`OpenDomainWhois(config) = % v, %v, error`, whois, err)
}

domain := "locaproxy.com"
res, err := whois.LookUp(domain)

if err == nil {
t.Fatalf(`whois.LookUp(domain) = % v, error`, res)
}
}

// TestJSONWhois calls LookUp with JSON result URL.
func TestJSONWhois(t *testing.T) {
dummy := "DUMMY"
config, err := OpenConfiguration(dummy)

if err != nil {
t.Fatalf(`OpenConfiguration(dummy) = % v, %v, error`, config, err)
}

whois, err := OpenDomainWhois(config)
whois.baseUrl = "ip2location.io/get-ip.json"

if err != nil {
t.Fatalf(`OpenDomainWhois(config) = % v, %v, error`, whois, err)
}

domain := "locaproxy.com"
res, err := whois.LookUp(domain)

if err != nil {
t.Fatalf(`whois.LookUp(domain) = % v, error`, res)
}
}

// TestGetPunycode calls GetPunycode
// to see if it returns the correct Punycode.
func TestGetPunycode(t *testing.T) {
Expand Down Expand Up @@ -124,6 172,30 @@ func TestGetDomainName(t *testing.T) {
}
}

// TestGetBadDomainName calls GetDomainName with bad domain
// to see if it throws error.
func TestGetBadDomainName(t *testing.T) {
config, err := OpenConfiguration("")

if err != nil {
t.Fatalf(`OpenConfiguration(dummy) = % v, %v, error`, config, err)
}

whois, err := OpenDomainWhois(config)

if err != nil {
t.Fatalf(`OpenDomainWhois(config) = % v, %v, error`, whois, err)
}

url := "abcde"
// expected := "example.com"
res, err := whois.GetDomainName(url)

if err == nil {
t.Fatalf(`whois.GetDomainName(url) = % v, error`, res)
}
}

// TestGetDomainExtension calls GetDomainExtension
// to see if it returns the correct domain extension.
func TestGetDomainExtension(t *testing.T) {
Expand All @@ -147,3 219,26 @@ func TestGetDomainExtension(t *testing.T) {
t.Fatalf(`whois.GetDomainExtension(str) = % v, error`, res)
}
}

// TestGetBadDomainExtension calls GetDomainExtension with bad domain
// to see if it throws error.
func TestGetBadDomainExtension(t *testing.T) {
config, err := OpenConfiguration("")

if err != nil {
t.Fatalf(`OpenConfiguration(dummy) = % v, %v, error`, config, err)
}

whois, err := OpenDomainWhois(config)

if err != nil {
t.Fatalf(`OpenDomainWhois(config) = % v, %v, error`, whois, err)
}

str := "example"
res, err := whois.GetDomainExtension(str)

if err == nil {
t.Fatalf(`whois.GetDomainExtension(str) = % v, error`, res)
}
}
50 changes: 50 additions & 0 deletions ip2locationio/ipgeolocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 103,53 @@ func TestURLIPGeolocation(t *testing.T) {
t.Fatalf(`ipl.LookUp(ip, lang) = % v, %v, error`, res, err)
}
}

// TestNonJSONIPGeolocation calls LookUp with a non-JSON result URL.
func TestNonJSONIPGeolocation(t *testing.T) {
dummy := "" // blank key
config, err := OpenConfiguration(dummy)

if err != nil {
t.Fatalf(`OpenConfiguration(dummy) = % v, %v, error`, config, err)
}

ipl, err := OpenIPGeolocation(config)

ipl.baseUrl = "ip2location.io"
if err != nil {
t.Fatalf(`OpenIPGeolocation(config) = % v, %v, error`, ipl, err)
}

ip := "8.8.8.8"
lang := ""
res, err := ipl.LookUp(ip, lang)

if err == nil {
t.Fatalf(`ipl.LookUp(ip, lang) = % v, %v, error`, res, err)
}
}

// TestHTTPResponseIPGeolocation calls LookUp with a 404 URL.
func TestHTTPResponseIPGeolocation(t *testing.T) {
dummy := "" // blank key
config, err := OpenConfiguration(dummy)

if err != nil {
t.Fatalf(`OpenConfiguration(dummy) = % v, %v, error`, config, err)
}

ipl, err := OpenIPGeolocation(config)

ipl.baseUrl = "ip2location.io/notfound"
if err != nil {
t.Fatalf(`OpenIPGeolocation(config) = % v, %v, error`, ipl, err)
}

ip := "8.8.8.8"
lang := ""
res, err := ipl.LookUp(ip, lang)

if err == nil {
t.Fatalf(`ipl.LookUp(ip, lang) = % v, error`, res)
}
}

0 comments on commit 4e646d1

Please sign in to comment.