/
have_http_status_matcher_test.go
205 lines (189 loc) · 6.71 KB
/
have_http_status_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package matchers_test
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = FDescribe("HaveHTTPStatus", func() {
When("EXPECTED is single integer", func() {
It("matches the StatusCode", func() {
resp := &http.Response{StatusCode: http.StatusOK}
Expect(resp).To(HaveHTTPStatus(http.StatusOK))
Expect(resp).NotTo(HaveHTTPStatus(http.StatusNotFound))
})
})
When("EXPECTED is single 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 empty", func() {
It("errors", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{StatusCode: http.StatusOK}
Expect(resp).To(HaveHTTPStatus())
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal("HaveHTTPStatus matcher must be passed an int or a string. Got nothing"))
})
})
When("EXPECTED is not a string or integer", func() {
It("errors", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{StatusCode: http.StatusOK}
Expect(resp).To(HaveHTTPStatus(true))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal("HaveHTTPStatus matcher must be passed int or string types. Got:\n <bool>: true"))
})
})
When("EXPECTED is a list of strings and integers", func() {
It("matches the StatusCode and Status", func() {
resp := &http.Response{
Status: "200 OK",
StatusCode: http.StatusOK,
}
Expect(resp).To(HaveHTTPStatus(http.StatusOK, http.StatusNoContent, http.StatusNotFound))
Expect(resp).To(HaveHTTPStatus("204 Feeling Fine", "200 OK", "404 Not Found"))
Expect(resp).To(HaveHTTPStatus("204 Feeling Fine", http.StatusOK, "404 Not Found"))
Expect(resp).To(HaveHTTPStatus(http.StatusNoContent, "200 OK", http.StatusNotFound))
Expect(resp).NotTo(HaveHTTPStatus(http.StatusNotFound, http.StatusNoContent, http.StatusGone))
Expect(resp).NotTo(HaveHTTPStatus("204 Feeling Fine", "201 Sleeping", "404 Not Found"))
Expect(resp).NotTo(HaveHTTPStatus(http.StatusNotFound, "404 Not Found", http.StatusGone))
})
})
When("EXPECTED is a list containing non-string or integer types", func() {
It("errors", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{StatusCode: http.StatusOK}
Expect(resp).To(HaveHTTPStatus(http.StatusGone, "204 No Content", true, http.StatusNotFound))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal("HaveHTTPStatus matcher must be passed int or string types. Got:\n <bool>: true"))
})
})
When("ACTUAL is *httptest.ResponseRecorder", func() {
When("EXPECTED is integer", func() {
It("matches the StatusCode", func() {
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
Expect(resp).To(HaveHTTPStatus(http.StatusOK))
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() {
resp := &httptest.ResponseRecorder{Code: http.StatusOK}
Expect(resp).NotTo(HaveHTTPStatus(nil))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal("HaveHTTPStatus matcher must be passed int or string types. Got:\n <nil>: nil"))
})
})
})
When("ACTUAL is neither *http.Response nor *httptest.ResponseRecorder", func() {
It("errors", func() {
failures := InterceptGomegaFailures(func() {
Expect("foo").To(HaveHTTPStatus(http.StatusOK))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal("HaveHTTPStatus matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n <string>: foo"))
})
})
Describe("FailureMessage", func() {
It("returns a message for a single expected value", func() {
failures := InterceptGomegaFailures(func() {
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(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])
})
It("returns a message for a multiple expected values", func() {
failures := InterceptGomegaFailures(func() {
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, http.StatusNotFound, "204 No content"))
})
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
<int>: 404
<string>: 204 No content`), failures[0])
})
})
Describe("NegatedFailureMessage", func() {
It("returns a message for a single expected value", func() {
failures := InterceptGomegaFailures(func() {
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(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])
})
It("returns a message for a multiple expected values", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{
StatusCode: http.StatusOK,
Status: "200 OK",
Body: ioutil.NopCloser(strings.NewReader("got it!")),
}
Expect(resp).NotTo(HaveHTTPStatus(http.StatusOK, "204 No content", http.StatusGone))
})
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
<string>: 204 No content
<int>: 410`), failures[0])
})
})
})