/
have_http_header_with_value_matcher_test.go
161 lines (146 loc) · 5.18 KB
/
have_http_header_with_value_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
package matchers_test
import (
"net/http"
"net/http/httptest"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("HaveHTTPHeader", func() {
It("can match an HTTP header", func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value")
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", "fake value"))
})
It("can mismatch an HTTP header", func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value")
Expect(resp).NotTo(HaveHTTPHeaderWithValue("other-header", "fake value"))
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", "other value"))
})
When("the header is set more than once", func() {
It("matches the first value and not the second", func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value1")
resp.Header.Add("fake-header", "fake value2")
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", "fake value1"))
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", "fake value2"))
})
})
When("ACTUAL is *httptest.ResponseRecorder", func() {
It("can match an HTTP header", func() {
resp := &httptest.ResponseRecorder{}
resp.Header().Add("fake-header", "fake value")
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", "fake value"))
})
It("can mismatch an HTTP header", func() {
resp := &httptest.ResponseRecorder{}
resp.Header().Add("fake-header", "fake value")
Expect(resp).NotTo(HaveHTTPHeaderWithValue("other-header", "fake value"))
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", "other value"))
})
})
When("ACTUAL is neither *http.Response nor *httptest.ResponseRecorder", func() {
It("errors", func() {
failures := InterceptGomegaFailures(func() {
Expect("foo").To(HaveHTTPHeaderWithValue("bar", "baz"))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal("HaveHTTPHeaderWithValue matcher expects *http.Response or *httptest.ResponseRecorder. Got:\n <string>: foo"))
})
})
When("EXPECTED VALUE is a matcher", func() {
It("can match an HTTP header", func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value")
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", ContainSubstring("value")))
})
It("can mismatch an HTTP header", func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value")
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", ContainSubstring("foo")))
})
})
When("EXPECTED VALUE is something else", func() {
It("errors", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{}
Expect(resp).To(HaveHTTPHeaderWithValue("bar", 42))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal("HaveHTTPHeaderWithValue matcher must be passed a string or a GomegaMatcher. Got:\n <int>: 42"))
})
})
Describe("FailureMessage", func() {
When("matching a string", func() {
It("returns message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value")
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", "other value"))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`HTTP header "fake-header":
Expected
<string>: fake value
to equal
<string>: other value`), failures[0])
})
})
When("matching a matcher", func() {
It("returns message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value")
Expect(resp).To(HaveHTTPHeaderWithValue("fake-header", ContainSubstring("other")))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`HTTP header "fake-header":
Expected
<string>: fake value
to contain substring
<string>: other`), failures[0])
})
})
})
Describe("NegatedFailureMessage", func() {
When("matching a string", func() {
It("returns message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value")
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", "fake value"))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`HTTP header "fake-header":
Expected
<string>: fake value
not to equal
<string>: fake value`), failures[0])
})
})
When("matching a matcher", func() {
It("returns message", func() {
failures := InterceptGomegaFailures(func() {
resp := &http.Response{}
resp.Header = make(http.Header)
resp.Header.Add("fake-header", "fake value")
Expect(resp).NotTo(HaveHTTPHeaderWithValue("fake-header", ContainSubstring("value")))
})
Expect(failures).To(HaveLen(1))
Expect(failures[0]).To(Equal(`HTTP header "fake-header":
Expected
<string>: fake value
not to contain substring
<string>: value`), failures[0])
})
})
})
})