/
have_http_body_matcher_test.go
187 lines (169 loc) · 5.79 KB
/
have_http_body_matcher_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package matchers_test
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("HaveHTTPBody", func() {
When("ACTUAL is *http.Response", func() {
It("matches the body", func() {
const body = "this is the body"
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(body))}
Expect(resp).To(HaveHTTPBody(body))
})
It("mismatches the body", func() {
const body = "this is the body"
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(body))}
Expect(resp).NotTo(HaveHTTPBody("something else"))
})
})
When("ACTUAL is *httptest.ResponseRecorder", func() {
It("matches the body", func() {
const body = "this is the body"
resp := &httptest.ResponseRecorder{Body: bytes.NewBufferString(body)}
Expect(resp).To(HaveHTTPBody(body))
})
It("mismatches the body", func() {
const body = "this is the body"
resp := &httptest.ResponseRecorder{Body: bytes.NewBufferString(body)}
Expect(resp).NotTo(HaveHTTPBody("something else"))
})
})
When("ACTUAL is neither *http.Response nor *httptest.ResponseRecorder", func() {
It("errors", func() {
failures := InterceptGomegaFailures(func() {
Expect("foo").To(HaveHTTPBody("bar"))
})
Expect(failures).To(ConsistOf("HaveHTTPBody matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n <string>: foo"))
})
})
When("EXPECTED is []byte", func() {
It("matches the body", func() {
const body = "this is the body"
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(body))}
Expect(resp).To(HaveHTTPBody([]byte(body)))
})
It("mismatches the body", func() {
const body = "this is the body"
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(body))}
Expect(resp).NotTo(HaveHTTPBody([]byte("something else")))
})
})
When("EXPECTED is a submatcher", func() {
It("matches the body", func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
Expect(resp).To(HaveHTTPBody(MatchJSON(`{ "some": "json" }`)))
})
It("mismatches the body", func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
Expect(resp).NotTo(HaveHTTPBody(MatchJSON(`{ "something": "different" }`)))
})
})
When("EXPECTED is something else", func() {
It("errors", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader("body"))}
Expect(resp).To(HaveHTTPBody(map[int]bool{}))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal("HaveHTTPBody matcher expects string, []byte, or GomegaMatcher. Got:\n <map[int]bool | len:0>: {}"))
})
})
Describe("FailureMessage", func() {
Context("EXPECTED is string", func() {
It("returns a match failure message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader("this is the body"))}
Expect(resp).To(HaveHTTPBody("this is a different body"))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`Expected
<string>: this is the body
to equal
<string>: this is a different body`), failures[0])
})
})
Context("EXPECTED is []byte", func() {
It("returns a match failure message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader("this is the body"))}
Expect(resp).To(HaveHTTPBody([]byte("this is a different body")))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(MatchRegexp(`^Expected
<\[\]uint8 \| len:\d+, cap:\d+>: this is the body
to equal
<\[\]uint8 ]| len:\d+, cap:\d+>: this is a different body$`))
})
})
Context("EXPECTED is submatcher", func() {
It("returns a match failure message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(`{"some":"json"}`))}
Expect(resp).To(HaveHTTPBody(MatchJSON(`{"other":"stuff"}`)))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`Expected
<string>: {
"some": "json"
}
to match JSON of
<string>: {
"other": "stuff"
}`))
})
})
})
Describe("NegatedFailureMessage", func() {
Context("EXPECTED is string", func() {
It("returns a negated failure message", func() {
const body = "this is the body"
failures := InterceptGomegaFailures(func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(body))}
Expect(resp).NotTo(HaveHTTPBody(body))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`Expected
<string>: this is the body
not to equal
<string>: this is the body`))
})
})
Context("EXPECTED is []byte", func() {
It("returns a match failure message", func() {
const body = "this is the body"
failures := InterceptGomegaFailures(func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(body))}
Expect(resp).NotTo(HaveHTTPBody([]byte(body)))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(MatchRegexp(`^Expected
<\[\]uint8 \| len:\d+, cap:\d+>: this is the body
not to equal
<\[\]uint8 \| len:\d+, cap:\d+>: this is the body$`))
})
})
Context("EXPECTED is submatcher", func() {
It("returns a match failure message", func() {
const body = `{"some":"json"}`
failures := InterceptGomegaFailures(func() {
resp := &http.Response{Body: ioutil.NopCloser(strings.NewReader(body))}
Expect(resp).NotTo(HaveHTTPBody(MatchJSON(body)))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`Expected
<string>: {
"some": "json"
}
not to match JSON of
<string>: {
"some": "json"
}`))
})
})
})
})