forked from Eun/go-hit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expect_specific_header_test.go
204 lines (177 loc) · 4.3 KB
/
expect_specific_header_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
package hit_test
import (
"net/http"
"net/http/httptest"
"testing"
. "github.com/Eun/go-hit"
)
func TestExpectHeadersSpecificHeader_Len(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Header", "Hello")
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Expect().Header("X-Header").Len(5),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Header").Len(0),
),
PtrStr(`"Hello" should have 0 item(s), but has 5`),
)
}
func TestExpectHeadersSpecificHeader_Empty(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Header1", "Hello")
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Expect().Header("X-Header2").Empty(),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Header1").Empty(),
),
PtrStr(`"Hello" should be empty, but has 5 item(s)`),
)
}
func TestExpectSpecificHeader_Equal(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Header", "Hello")
writer.Header().Set("X-Int", "3")
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Expect().Header("X-Header").Equal("Hello"),
Expect().Header("X-Int").Equal(3),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Header").Equal("Bye"),
),
PtrStr("Not equal"), PtrStr(`expected: "Bye"`), nil, nil, nil, nil, nil,
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Int").Equal(1),
),
PtrStr("Not equal"), PtrStr("expected: 1"), nil, nil, nil, nil, nil,
)
}
func TestExpectSpecificHeader_NotEqual(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Header", "Hello")
writer.Header().Set("X-Int", "3")
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Expect().Header("X-Header").NotEqual("Bye"),
Expect().Header("X-Int").NotEqual(1),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Header").NotEqual("Hello"),
),
PtrStr(`should not be "Hello"`),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Int").NotEqual(3),
),
PtrStr(`should not be 3`),
)
}
func TestExpectSpecificHeader_Contains(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Header", "Hello")
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Expect().Header("X-Header").Contains("Hello"),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Header").Contains("Bye"),
),
PtrStr(`"Hello" does not contain "Bye"`),
)
}
func TestExpectSpecificHeader_NotContains(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Header", "Hello")
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Expect().Header("X-Header").NotContains("Bye"),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Header").NotContains("H"),
),
PtrStr(`"Hello" should not contain "H"`),
)
}
func TestExpectSpecificHeader_OneOf(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Header", "Hello")
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Expect().Header("X-Header").OneOf("Hello", "World"),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Header").OneOf("Universe"),
),
PtrStr("[]interface {}{"), PtrStr(`"Universe",`), PtrStr(`} does not contain "Hello"`),
)
}
func TestExpectSpecificHeader_NotOneOf(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("X-Header", "Hello")
})
s := httptest.NewServer(mux)
defer s.Close()
Test(t,
Post(s.URL),
Expect().Header("X-Header").NotOneOf("Universe"),
)
ExpectError(t,
Do(
Post(s.URL),
Expect().Header("X-Header").NotOneOf("Hello", "World"),
),
nil, nil, nil, PtrStr(`} should not contain "Hello"`),
)
}