Skip to content

Commit

Permalink
Read Body for Newer Responses in HaveHTTPBodyMatcher (#686)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Cooley committed Jul 27, 2023
1 parent 9cbf7b0 commit 18d6673
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
9 changes: 6 additions & 3 deletions matchers/have_http_body_matcher.go
Expand Up @@ -11,8 +11,9 @@ import (
)

type HaveHTTPBodyMatcher struct {
Expected interface{}
cachedBody []byte
Expected interface{}
cachedResponse interface{}
cachedBody []byte
}

func (matcher *HaveHTTPBodyMatcher) Match(actual interface{}) (bool, error) {
Expand Down Expand Up @@ -73,7 +74,7 @@ func (matcher *HaveHTTPBodyMatcher) NegatedFailureMessage(actual interface{}) (m
// the Reader is closed and it is not readable again in FailureMessage()
// or NegatedFailureMessage()
func (matcher *HaveHTTPBodyMatcher) body(actual interface{}) ([]byte, error) {
if matcher.cachedBody != nil {
if matcher.cachedResponse == actual && matcher.cachedBody != nil {
return matcher.cachedBody, nil
}

Expand All @@ -91,8 +92,10 @@ func (matcher *HaveHTTPBodyMatcher) body(actual interface{}) ([]byte, error) {

switch a := actual.(type) {
case *http.Response:
matcher.cachedResponse = a
return body(a)
case *httptest.ResponseRecorder:
matcher.cachedResponse = a
return body(a.Result())
default:
return nil, fmt.Errorf("HaveHTTPBody matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n%s", format.Object(actual, 1))
Expand Down
27 changes: 27 additions & 0 deletions matchers/have_http_body_matcher_test.go
Expand Up @@ -2,6 +2,7 @@ package matchers_test

import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -24,6 +25,19 @@ var _ = Describe("HaveHTTPBody", func() {
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
Expect(resp).NotTo(HaveHTTPBody("something else"))
})

It("matches the body against later calls", func() {
firstCall := true
getResp := func() *http.Response {
if firstCall {
firstCall = false
return &http.Response{Body: io.NopCloser(strings.NewReader("first_call"))}
} else {
return &http.Response{Body: io.NopCloser(strings.NewReader("later_call"))}
}
}
Eventually(getResp).MustPassRepeatedly(2).Should(HaveHTTPBody([]byte("later_call")))
})
})

When("ACTUAL is *httptest.ResponseRecorder", func() {
Expand All @@ -38,6 +52,19 @@ var _ = Describe("HaveHTTPBody", func() {
resp := &httptest.ResponseRecorder{Body: bytes.NewBufferString(body)}
Expect(resp).NotTo(HaveHTTPBody("something else"))
})

It("matches the body against later calls", func() {
firstCall := true
getResp := func() *httptest.ResponseRecorder {
if firstCall {
firstCall = false
return &httptest.ResponseRecorder{Body: bytes.NewBufferString("first_call")}
} else {
return &httptest.ResponseRecorder{Body: bytes.NewBufferString("later_call")}
}
}
Eventually(getResp).MustPassRepeatedly(2).Should(HaveHTTPBody([]byte("later_call")))
})
})

When("ACTUAL is neither *http.Response nor *httptest.ResponseRecorder", func() {
Expand Down

0 comments on commit 18d6673

Please sign in to comment.