Skip to content

Commit

Permalink
feat: formatter for HTTP responses (#461)
Browse files Browse the repository at this point in the history
Previously when the HaveHTTPStatus() matcher failed, the whole HTTP
response object was printed out, and any message was rendered in
bytes rather than as a string.

This introduces a formatter for HTTP responses, so that the key data is
presented in a helpful format.
  • Loading branch information
blgm committed Aug 19, 2021
1 parent 4165d33 commit e5b3157
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 6 deletions.
38 changes: 36 additions & 2 deletions matchers/have_http_status_matcher.go
Expand Up @@ -2,8 +2,11 @@ package matchers

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"strings"

"github.com/onsi/gomega/format"
)
Expand Down Expand Up @@ -34,9 +37,40 @@ func (matcher *HaveHTTPStatusMatcher) Match(actual interface{}) (success bool, e
}

func (matcher *HaveHTTPStatusMatcher) FailureMessage(actual interface{}) (message string) {
return format.Message(actual, "to have HTTP status", matcher.Expected)
return fmt.Sprintf("Expected\n%s\n%s\n%s", formatHttpResponse(actual), "to have HTTP status", format.Object(matcher.Expected, 1))
}

func (matcher *HaveHTTPStatusMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to have HTTP status", matcher.Expected)
return fmt.Sprintf("Expected\n%s\n%s\n%s", formatHttpResponse(actual), "not to have HTTP status", format.Object(matcher.Expected, 1))
}

func formatHttpResponse(input interface{}) string {
var resp *http.Response
switch r := input.(type) {
case *http.Response:
resp = r
case *httptest.ResponseRecorder:
resp = r.Result()
default:
return "cannot format invalid HTTP response"
}

body := "<nil>"
if resp.Body != nil {
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
data = []byte("<error reading body>")
}
body = format.Object(string(data), 0)
}

var s strings.Builder
s.WriteString(fmt.Sprintf("%s<%s>: {\n", format.Indent, reflect.TypeOf(input)))
s.WriteString(fmt.Sprintf("%s%sStatus: %s\n", format.Indent, format.Indent, format.Object(resp.Status, 0)))
s.WriteString(fmt.Sprintf("%s%sStatusCode: %s\n", format.Indent, format.Indent, format.Object(resp.StatusCode, 0)))
s.WriteString(fmt.Sprintf("%s%sBody: %s\n", format.Indent, format.Indent, body))
s.WriteString(fmt.Sprintf("%s}", format.Indent))

return s.String()
}
39 changes: 35 additions & 4 deletions matchers/have_http_status_matcher_test.go
@@ -1,8 +1,10 @@
package matchers_test

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -17,13 +19,15 @@ var _ = Describe("HaveHTTPStatus", func() {
Expect(resp).NotTo(HaveHTTPStatus(http.StatusNotFound))
})
})

When("EXPECTED is string", func() {
It("matches the Status", func() {
resp := &http.Response{Status: "200 OK"}
Expect(resp).To(HaveHTTPStatus("200 OK"))
Expect(resp).NotTo(HaveHTTPStatus("404 Not Found"))
})
})

When("EXPECTED is anything else", func() {
It("does not match", func() {
failures := InterceptGomegaFailures(func() {
Expand All @@ -43,13 +47,15 @@ var _ = Describe("HaveHTTPStatus", func() {
Expect(resp).NotTo(HaveHTTPStatus(http.StatusNotFound))
})
})

When("EXPECTED is string", func() {
It("matches the Status", func() {
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
Expect(resp).To(HaveHTTPStatus("200 OK"))
Expect(resp).NotTo(HaveHTTPStatus("404 Not Found"))
})
})

When("EXPECTED is anything else", func() {
It("does not match", func() {
failures := InterceptGomegaFailures(func() {
Expand All @@ -73,19 +79,44 @@ var _ = Describe("HaveHTTPStatus", func() {
Describe("FailureMessage", func() {
It("returns message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{StatusCode: http.StatusBadGateway}
resp := &http.Response{
StatusCode: http.StatusBadGateway,
Status: "502 Bad Gateway",
Body: ioutil.NopCloser(strings.NewReader("did not like it")),
}
Expect(resp).To(HaveHTTPStatus(http.StatusOK))
})
Expect(failures).To(ConsistOf(MatchRegexp("Expected(.|\n)*StatusCode: 502(.|\n)*to have HTTP status\n <int>: 200")))
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`Expected
<*http.Response>: {
Status: <string>: "502 Bad Gateway"
StatusCode: <int>: 502
Body: <string>: "did not like it"
}
to have HTTP status
<int>: 200`), failures[0])
})
})

Describe("NegatedFailureMessage", func() {
It("returns message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{StatusCode: http.StatusOK}
resp := &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: ioutil.NopCloser(strings.NewReader("got it!")),
}
Expect(resp).NotTo(HaveHTTPStatus(http.StatusOK))
})
Expect(failures).To(ConsistOf(MatchRegexp("Expected(.|\n)*StatusCode: 200(.|\n)*not to have HTTP status\n <int>: 200")))
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`Expected
<*http.Response>: {
Status: <string>: "200 OK"
StatusCode: <int>: 200
Body: <string>: "got it!"
}
not to have HTTP status
<int>: 200`), failures[0])
})
})
})

0 comments on commit e5b3157

Please sign in to comment.