Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Body for Newer Responses in HaveHTTPBodyMatcher #686

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions matchers/have_http_body_matcher.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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