Skip to content

Commit 18d6673

Browse files
authoredJul 27, 2023
Read Body for Newer Responses in HaveHTTPBodyMatcher (#686)
1 parent 9cbf7b0 commit 18d6673

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed
 

‎matchers/have_http_body_matcher.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111
)
1212

1313
type HaveHTTPBodyMatcher struct {
14-
Expected interface{}
15-
cachedBody []byte
14+
Expected interface{}
15+
cachedResponse interface{}
16+
cachedBody []byte
1617
}
1718

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

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

9293
switch a := actual.(type) {
9394
case *http.Response:
95+
matcher.cachedResponse = a
9496
return body(a)
9597
case *httptest.ResponseRecorder:
98+
matcher.cachedResponse = a
9699
return body(a.Result())
97100
default:
98101
return nil, fmt.Errorf("HaveHTTPBody matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n%s", format.Object(actual, 1))

‎matchers/have_http_body_matcher_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package matchers_test
22

33
import (
44
"bytes"
5+
"io"
56
"net/http"
67
"net/http/httptest"
78
"strings"
@@ -24,6 +25,19 @@ var _ = Describe("HaveHTTPBody", func() {
2425
resp := &http.Response{Body: gutil.NopCloser(strings.NewReader(body))}
2526
Expect(resp).NotTo(HaveHTTPBody("something else"))
2627
})
28+
29+
It("matches the body against later calls", func() {
30+
firstCall := true
31+
getResp := func() *http.Response {
32+
if firstCall {
33+
firstCall = false
34+
return &http.Response{Body: io.NopCloser(strings.NewReader("first_call"))}
35+
} else {
36+
return &http.Response{Body: io.NopCloser(strings.NewReader("later_call"))}
37+
}
38+
}
39+
Eventually(getResp).MustPassRepeatedly(2).Should(HaveHTTPBody([]byte("later_call")))
40+
})
2741
})
2842

2943
When("ACTUAL is *httptest.ResponseRecorder", func() {
@@ -38,6 +52,19 @@ var _ = Describe("HaveHTTPBody", func() {
3852
resp := &httptest.ResponseRecorder{Body: bytes.NewBufferString(body)}
3953
Expect(resp).NotTo(HaveHTTPBody("something else"))
4054
})
55+
56+
It("matches the body against later calls", func() {
57+
firstCall := true
58+
getResp := func() *httptest.ResponseRecorder {
59+
if firstCall {
60+
firstCall = false
61+
return &httptest.ResponseRecorder{Body: bytes.NewBufferString("first_call")}
62+
} else {
63+
return &httptest.ResponseRecorder{Body: bytes.NewBufferString("later_call")}
64+
}
65+
}
66+
Eventually(getResp).MustPassRepeatedly(2).Should(HaveHTTPBody([]byte("later_call")))
67+
})
4168
})
4269

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

0 commit comments

Comments
 (0)
Please sign in to comment.