Skip to content

Commit

Permalink
Merge pull request #4 from theckman/enable_tests
Browse files Browse the repository at this point in the history
Fix bugs uncovered with the increase in test coverage

Signed-off-by: Tim Heckman <t@heckman.io>
  • Loading branch information
theckman committed Dec 17, 2017
2 parents 4fbceb1 + 4d334a0 commit 07700a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,25 @@ func (c client) Request(ip string) (*http.Response, error) {
// action request
resp, err := c.c.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "http request to %q failed", resp.Request.URL.String())
return nil, errors.Wrapf(err, "http request to %q failed", req.URL.String())
}

switch resp.StatusCode {
case 200:
case http.StatusOK: // 200
// we can try and parse
return resp, nil
case 400, 403, 429:
case http.StatusBadRequest, http.StatusUnauthorized, http.StatusTooManyRequests: // 400, 401, 429
// provide response body as error to consumer
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "failed to read body from response with status code %q: %s", resp.Status, err)
}

if resp.StatusCode == 403 {
if resp.StatusCode == http.StatusUnauthorized {
return nil, errors.Errorf("request for %q failed (authentication failure): %s", ip, string(body))
}

if resp.StatusCode == 429 {
if resp.StatusCode == http.StatusTooManyRequests {
rerr := rateErr{r: true, m: string(body)}
return nil, errors.Wrapf(rerr, "request for %q failed (ratelimited)")
}
Expand Down
28 changes: 14 additions & 14 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func testHTTPServer(addr string) (net.Listener, *http.Server, error) {
})

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

Expand Down Expand Up @@ -495,26 +495,26 @@ func Test_client_Request(t *testing.T) {
c: c,
name: "unexpected_error",
i: "8.4.0.3",
e: "unexpected http status: 401 Unauthorized",
e: "unexpected http status: 403 Forbidden",
},
{
c: c,
name: "invalid_api-key",
i: "8.8.4.4",
e: "(authentication failure)",
},
// 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,
},
{
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`,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 07700a4

Please sign in to comment.