Skip to content

Commit

Permalink
fix bug in expect success code
Browse files Browse the repository at this point in the history
would return errors if success code was returned
  • Loading branch information
ansel1 committed Jan 28, 2020
1 parent a810229 commit 91136cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ coverage: build
fmt:
go fmt $(PACKAGES)


lint:
-golangci-lint run
# running golangci-lint run twice, ignoring the first run
# this is to workaround a bug in golangci-lint's type cache which can result
# in false type errors on the first run
# https://github.com/golangci/golangci-lint/issues/885
-golangci-lint run > /dev/null 2>&1
golangci-lint run

vet:
Expand Down
26 changes: 13 additions & 13 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@ type codeChecker struct {
}

func (c *codeChecker) checkCode(resp *http.Response, err error) (*http.Response, error) {
if err != nil || resp == nil {
return resp, err
switch {
case err != nil, resp == nil:
case c.code == expectSuccessCode:
if resp.StatusCode < 200 || resp.StatusCode > 299 {
err = merry.
Errorf("server returned an unsuccessful status code: %d", resp.StatusCode).
WithHTTPCode(resp.StatusCode)
}
case c.code != resp.StatusCode:
err = merry.
Errorf("server returned unexpected status code. expected: %d, received: %d", c.code, resp.StatusCode).
WithHTTPCode(resp.StatusCode)
}
if c.code == expectSuccessCode && (resp.StatusCode < 200 || resp.StatusCode > 299) { // special case, expect a success code
err := merry.Errorf("server returned an unsuccessful status code: %d", resp.StatusCode)
err = err.WithHTTPCode(resp.StatusCode)
return resp, err
}
if c.code != resp.StatusCode {
err := merry.Errorf("server returned unexpected status code. expected: %d, received: %d", c.code, resp.StatusCode)
err = err.WithHTTPCode(resp.StatusCode)
return resp, err
}
return resp, nil
return resp, err
}

func getCodeChecker(req *http.Request) (*http.Request, *codeChecker) {
Expand Down
14 changes: 12 additions & 2 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ func TestExpectCode(t *testing.T) {

func TestExpectSuccessCode(t *testing.T) {

codeToReturn := 407
bodyToReturn := "boom!"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(407)
w.Write([]byte("boom!"))
w.WriteHeader(codeToReturn)
_, _ = w.Write([]byte(bodyToReturn))
}))
defer ts.Close()

Expand All @@ -187,6 +189,14 @@ func TestExpectSuccessCode(t *testing.T) {
require.Error(t, err)
assert.Contains(t, err.Error(), "code: 407")
assert.Equal(t, 407, merry.HTTPCode(err))

// test positive path: if success code is returned, then no error should be returned
successCodes := []int{200, 201, 204, 278}
for _, code := range successCodes {
codeToReturn = code
_, _, err := Receive(Get(ts.URL), ExpectSuccessCode())
require.NoError(t, err, "should not have received an error for code %v", code)
}
}

func ExampleMiddleware() {
Expand Down

0 comments on commit 91136cf

Please sign in to comment.