Skip to content

Commit

Permalink
Merge pull request #3 from theckman/improve_coverage
Browse files Browse the repository at this point in the history
Increase the test coverage of package

Signed-off-by: Tim Heckman <t@heckman.io>
  • Loading branch information
theckman committed Dec 17, 2017
2 parents c9279a9 + 29c15a1 commit 4fbceb1
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
language: go
go:
- tip
- 1.10beta1
- 1.9.2
sudo: false
notifications:
Expand Down
89 changes: 87 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func testHTTPServer(addr string) (net.Listener, *http.Server, error) {

// 200 response code
mux.HandleFunc("/76.14.47.42", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("api-key") != "testAPIkey" {
w.WriteHeader(http.StatusUnauthorized)
io.WriteString(w, "API key does not exist.")
return
}
io.WriteString(w, testJSONValid)
})

Expand All @@ -40,6 +45,17 @@ func testHTTPServer(addr string) (net.Listener, *http.Server, error) {
io.WriteString(w, "bacon does not appear to be an IPv4 or IPv6 address")
})

// 401 response code
mux.HandleFunc("/8.8.4.4", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
io.WriteString(w, "API key does not exist.")
})

mux.HandleFunc("/8.4.0.3", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized) // TODO(theckman) use StatusForbidden soon
io.WriteString(w, "unexpected HTTP status code")
})

// 429 response code
mux.HandleFunc("/8.8.8.8", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusTooManyRequests)
Expand Down Expand Up @@ -68,6 +84,43 @@ func testHTTPServer(addr string) (net.Listener, *http.Server, error) {
return l, server, nil
}

func TestNewClient(t *testing.T) {
tests := []struct {
name string
i string
e string
k string
}{
{"no_api_key", "", "https://api.ipdata.co/", ""},
{"with_api_key", "testAPIkey", "https://api.ipdata.co/", "testAPIkey"},
}

var c Client

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c = NewClient(tt.i)

cc, ok := c.(client)
if !ok {
t.Fatal("expected type assert for c.(client) to succeed")
}

if cc.e != tt.e {
t.Fatalf("cc.e = %q,want %q", cc.e, tt.e)
}

if cc.k != tt.k {
t.Fatalf("cc.k = %q,want %q", cc.k, tt.k)
}

if cc.c == nil {
t.Fatal("cc.c should not be nil")
}
})
}
}

func Test_client_Lookup(t *testing.T) {
ln, srvr, err := testHTTPServer("")
if err != nil {
Expand All @@ -90,6 +143,7 @@ func Test_client_Lookup(t *testing.T) {
c := client{
c: newHTTPClient(),
e: "http://" + ln.Addr().String() + "/",
k: "testAPIkey",
}

tests := []struct {
Expand Down Expand Up @@ -246,6 +300,7 @@ func Test_client_LookupRaw(t *testing.T) {
c := client{
c: newHTTPClient(),
e: "http://" + ln.Addr().String() + "/",
k: "testAPIkey",
}

tests := []struct {
Expand Down Expand Up @@ -402,30 +457,60 @@ func Test_client_Request(t *testing.T) {
c := client{
c: newHTTPClient(),
e: "http://" + ln.Addr().String() + "/",
k: "testAPIkey",
}

tests := []struct {
c client
name string
i string
o string
e string
}{
{
c: c,
name: "invalid_request",
i: "%ƒail",
e: "error building request to look up %ƒail",
},
{
c: c,
name: "private_ipv4",
i: "192.168.0.1",
e: "192.168.0.1 is a private IP address",
},
{
c: c,
name: "invalid_ip",
i: "bacon",
e: "bacon does not appear to be an IPv4 or IPv6 address",
},
{
c: c,
name: "rate_limited",
i: "8.8.8.8",
e: "You have exceeded your free tier limit of 1500 requests. Register for a paid plan at https://ipdata.co to make more requests.",
e: "(ratelimited)",
},
{
c: c,
name: "unexpected_error",
i: "8.4.0.3",
e: "unexpected http status: 401 Unauthorized",
},
// TODO(theckman): enable these tests
// {
// c: client{c: newHTTPClient(), e: "http://127.0.0.1:8404/", k: "testAPIkey"},
// name: "tcp_conn_err",
// i: "76.14.47.42",
// e: `http request to "http://127.0.0.1:8404/76.14.47.42" failed`,
// },
// {
// name: "invalid_api-key",
// i: "8.8.4.4",
// e: "(authentication failure)",
// },
{
c: c,
name: "valid_address",
i: "76.14.47.42",
o: testJSONValid,
Expand All @@ -439,7 +524,7 @@ func Test_client_Request(t *testing.T) {
var resp *http.Response
var err error

resp, err = c.Request(tt.i)
resp, err = tt.c.Request(tt.i)

if len(tt.e) > 0 {
if err == nil {
Expand Down
68 changes: 68 additions & 0 deletions ipdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,18 @@ func Test_ripToIP(t *testing.T) {
name string
i RawIP
o IP
e string
}{
{
name: "invalid_flag",
i: RawIP{Flag: `http://%ƒail`},
e: "failed to parse flag",
},
{
name: "invalid_timezone",
i: RawIP{TimeZone: `http://%ƒail`},
e: "failed to parse timezone",
},
{
name: "test_valid_RawIP",
i: RawIP{
Expand Down Expand Up @@ -178,6 +189,20 @@ func Test_ripToIP(t *testing.T) {

t.Run(tt.name, func(t *testing.T) {
ip, err := ripToIP(tt.i)

if len(tt.e) > 0 {
if err == nil {
t.Fatal("error expected but was nil")
}

if !strings.Contains(err.Error(), tt.e) {
t.Fatalf("error message %q not found in error: %s", tt.e, err)
}

return

}

if err != nil {
t.Fatalf("ripToIP(%+v) returned an unexpected error: %s", tt.i, err)
}
Expand Down Expand Up @@ -258,7 +283,13 @@ func TestDecodeRawIP(t *testing.T) {
name string
i string
o RawIP
e string
}{
{
name: "test_invalid_json",
i: "garbage",
e: "failed to parse JSON:",
},
{
name: "test_valid_json",
i: testJSONValid,
Expand Down Expand Up @@ -289,6 +320,19 @@ func TestDecodeRawIP(t *testing.T) {

t.Run(tt.name, func(t *testing.T) {
ip, err := DecodeRawIP(strings.NewReader(tt.i))

if len(tt.e) > 0 {
if err == nil {
t.Fatal("error expected but was nil")
}

if !strings.Contains(err.Error(), tt.e) {
t.Fatalf("error message %q not found in error: %s", tt.e, err)
}

return
}

if err != nil {
t.Fatalf("DecodeIP(%+v) returned an unexpected error: %s", tt.i, err)
}
Expand Down Expand Up @@ -379,7 +423,18 @@ func TestDecodeIP(t *testing.T) {
name string
i string
o IP
e string
}{
{
name: "invalid_json",
i: "garbage",
e: "failed to parse JSON:",
},
{
name: "invalid_field",
i: `{"flag":"http://%ƒail"}`,
e: "failed to parse flag",
},
{
name: "test_valid_json",
i: testJSONValid,
Expand Down Expand Up @@ -410,6 +465,19 @@ func TestDecodeIP(t *testing.T) {

t.Run(tt.name, func(t *testing.T) {
ip, err := DecodeIP(strings.NewReader(tt.i))

if len(tt.e) > 0 {
if err == nil {
t.Fatal("error expected but was nil")
}

if !strings.Contains(err.Error(), tt.e) {
t.Fatalf("error message %q not found in error: %s", tt.e, err)
}

return
}

if err != nil {
t.Fatalf("DecodeIP(%+v) returned an unexpected error: %s", tt.i, err)
}
Expand Down

0 comments on commit 4fbceb1

Please sign in to comment.