Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed Apr 8, 2022
1 parent a8c4d61 commit 2d92062
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# checkip

Checkip is a CLI tool and library that provides generic and security information about an IP address in a quick way. It uses various [checks](https://pkg.go.dev/github.com/jreisinger/checkip/check) to do so.
Checkip is a CLI tool and [library](https://pkg.go.dev/github.com/jreisinger/checkip/check) that provides generic and security information about an IP address in a quick way.

```
$ checkip 218.92.0.158
Expand Down
8 changes: 4 additions & 4 deletions check/abuseipdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ func TestAbuseIPDB(t *testing.T) {
t.Run("given valid response then result and no error is returned", func(t *testing.T) {
handlerFn := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
rw.Write(LoadResponse(t, "abuseipdb_response.json"))
rw.Write(loadResponse(t, "abuseipdb_response.json"))
})
setAbuseIPDBConfig(t)
testUrl := SetMockHttpClient(t, handlerFn)
testUrl := setMockHttpClient(t, handlerFn)
setAbuseIPDBUrl(t, testUrl)

result, err := AbuseIPDB(net.ParseIP("118.25.6.39"))
Expand All @@ -34,7 +34,7 @@ func TestAbuseIPDB(t *testing.T) {
rw.WriteHeader(http.StatusInternalServerError)
})
setAbuseIPDBConfig(t)
testUrl := SetMockHttpClient(t, handlerFn)
testUrl := setMockHttpClient(t, handlerFn)
setAbuseIPDBUrl(t, testUrl)

_, err := AbuseIPDB(net.ParseIP("118.25.6.39"))
Expand All @@ -53,7 +53,7 @@ func setAbuseIPDBUrl(t *testing.T, testUrl string) {
}

func setAbuseIPDBConfig(t *testing.T) {
SetMockConfig(t, func(key string) (string, error) {
setMockConfig(t, func(key string) (string, error) {
if key == "ABUSEIPDB_API_KEY" {
return "123-secret-789", nil
}
Expand Down
10 changes: 0 additions & 10 deletions check/http_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package check

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -56,11 +54,3 @@ func TestHttpClient_GetJson(t *testing.T) {
require.Error(t, err)
})
}

// --- helper functions ---

func loadResponse(t *testing.T, name string) []byte {
b, err := ioutil.ReadFile(filepath.Join("testdata", name))
require.NoError(t, err)
return b
}
6 changes: 3 additions & 3 deletions check/otx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func TestOTX(t *testing.T) {
t.Run("given valid response then result and no error is returned", func(t *testing.T) {
handlerFn := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
rw.Write(LoadResponse(t, "otx_response.json"))
rw.Write(loadResponse(t, "otx_response.json"))
})

testUrl := SetMockHttpClient(t, handlerFn)
testUrl := setMockHttpClient(t, handlerFn)
setOTXUrl(t, testUrl)

result, err := OTX(net.ParseIP("118.25.6.39"))
Expand All @@ -33,7 +33,7 @@ func TestOTX(t *testing.T) {
rw.WriteHeader(http.StatusInternalServerError)
})

testUrl := SetMockHttpClient(t, handlerFn)
testUrl := setMockHttpClient(t, handlerFn)
setOTXUrl(t, testUrl)

_, err := OTX(net.ParseIP("118.25.6.39"))
Expand Down
28 changes: 14 additions & 14 deletions check/utils_test.go → check/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ import (
"github.com/stretchr/testify/require"
)

// SetMockConfig helper to replace GetConfigValue function
func SetMockConfig(t *testing.T, fn func(key string) (string, error)) {
defaultConfig := getConfigValue
getConfigValue = fn
t.Cleanup(func() {
getConfigValue = defaultConfig
})
// loadResponse loads named file form testdata directory
func loadResponse(t *testing.T, name string) []byte {
b, err := ioutil.ReadFile(filepath.Join("testdata", name))
require.NoError(t, err)
return b
}

// SetSetMockHttpClient sets DefaultHttpClient to httptest handler and returns test url
func SetMockHttpClient(t *testing.T, handlerFn http.HandlerFunc) string {
// setMockHttpClient sets DefaultHttpClient to httptest handler and returns test url
func setMockHttpClient(t *testing.T, handlerFn http.HandlerFunc) string {
server := httptest.NewServer(handlerFn)
dHC := defaultHttpClient
defaultHttpClient = newHttpClient(server.Client())
Expand All @@ -31,9 +29,11 @@ func SetMockHttpClient(t *testing.T, handlerFn http.HandlerFunc) string {
return server.URL
}

// LoadResponse loads named file form testdata directory
func LoadResponse(t *testing.T, name string) []byte {
b, err := ioutil.ReadFile(filepath.Join("testdata", name))
require.NoError(t, err)
return b
// setMockConfig helper to replace GetConfigValue function
func setMockConfig(t *testing.T, fn func(key string) (string, error)) {
defaultConfig := getConfigValue
getConfigValue = fn
t.Cleanup(func() {
getConfigValue = defaultConfig
})
}
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package cli contains functions for checking IP addresses from command-line.
// Package cli contains functions for running checkip.Checks from command-line.
package cli

import (
Expand Down

0 comments on commit 2d92062

Please sign in to comment.