-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoozzle_test.go
311 lines (246 loc) · 7.38 KB
/
goozzle_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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package goozzle
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func Test_Headers(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodDelete {
t.Errorf(`Method should be %s, %s given`, http.MethodDelete, r.Method)
}
customHeader := r.Header.Get("X-Request-Header")
userAgent := r.Header.Get("User-Agent")
referer := r.Header.Get("Referer")
if customHeader != "Value" {
t.Errorf(`Custom header "X-Request-Header" should be "%s", "%s" given`, "Value", customHeader)
}
if userAgent != "Test" {
t.Errorf(`User agent should be "%s", "%s" given`, "Test", userAgent)
}
if referer != "http://foo.bar/fizz?buz=baz" {
t.Errorf(`Referer should be "%s", "%s" given`, "http://foo.bar/fizz?buz=baz", referer)
}
w.Header().Set("X-Response-Header", "Bite me")
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Error(err)
}
res, err := Delete(u).Header("X-Request-Header", "Value").
UserAgent("Test").
Referer("http://foo.bar/fizz?buz=baz").
Do()
if err != nil {
t.Error(err)
}
responseHeader := res.Header("X-Response-Header")
if responseHeader != "Bite me" {
t.Errorf(`Response header should be "%s", "%s" given`, "Bite me", responseHeader)
}
}
func Test_Cookies(t *testing.T) {
requestCookie := &http.Cookie{
Name: "RequestCookie",
Value: "Some value",
}
responseCookie := &http.Cookie{
Name: "ResponseCookie",
Value: "Another value",
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf(`Method should be %s, %s given`, http.MethodGet, r.Method)
}
cookies := r.Cookies()
if len(cookies) != 1 {
t.Errorf("Request should contain 1 cookie, %d given", len(cookies))
}
if cookies[0].Name != requestCookie.Name {
t.Errorf(`Request cookie name should be "%s", "%s" given`, requestCookie.Name, cookies[0].Name)
}
if cookies[0].Value != requestCookie.Value {
t.Errorf(`Request cookie value should be "%s", "%s" given`, requestCookie.Value, cookies[0].Value)
}
http.SetCookie(w, responseCookie)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Error(err)
}
res, err := Get(u).Cookie(requestCookie).Do()
if err != nil {
t.Error(err)
}
cookies := res.Cookies()
if len(cookies) != 1 {
t.Errorf("Response should contain 1 cookie, %d given", len(cookies))
}
if cookies[0].Name != responseCookie.Name {
t.Errorf(`Response cookie name should be "%s", "%s" given`, responseCookie.Name, cookies[0].Name)
}
if res.Cookie(responseCookie.Name) != responseCookie.Value {
t.Errorf(`Response cookie value should be "%s", "%s" given`, responseCookie.Value, res.Cookie(responseCookie.Name))
}
}
func Test_Body(t *testing.T) {
requestBody := "Ping"
responseBody := "Pong"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPut {
t.Errorf(`Method should be %s, %s given`, http.MethodPut, r.Method)
}
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if string(reqBody) != requestBody {
t.Errorf(`Request body should be "%s", "%s" given`, requestBody, string(reqBody))
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(responseBody))
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Error(err)
}
res, err := Put(u).Body([]byte(requestBody))
if err != nil {
t.Error(err)
}
if res.String() != responseBody {
t.Errorf(`Response body should be "%s", "%s" given`, responseBody, res.String())
}
if res.Status() != http.StatusOK {
t.Errorf(`Response status should be "%d", "%d" given`, http.StatusOK, res.Status())
}
}
type testStruct struct {
Foo string `json:"foo"`
Fizz int `json:"bar"`
}
func Test_JSON(t *testing.T) {
testStructValue := testStruct{
Foo: "bar",
Fizz: 42,
}
testJSON, err := json.Marshal(&testStructValue)
if err != nil {
t.Error(err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf(`Method should be %s, %s given`, http.MethodPost, r.Method)
}
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
t.Errorf(`Content type should be "%s", "%s" given`, "application/json", contentType)
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if string(body) != string(testJSON) {
t.Errorf(`Body should be "%s", "%s" given`, string(testJSON), string(body))
}
w.WriteHeader(http.StatusBadRequest)
w.Write(testJSON)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Error(err)
}
res, err := Post(u).JSON(&testStructValue)
if err != nil {
t.Error(err)
}
if res.Status() != http.StatusBadRequest {
t.Errorf(`Response status should be "%d", "%d" given`, http.StatusBadRequest, res.Status())
}
var resValue testStruct
err = res.JSON(&resValue)
if err != nil {
t.Error(err)
}
if resValue.Foo != testStructValue.Foo {
t.Errorf(`Should be "%s"", "%s" given`, testStructValue.Foo, resValue.Foo)
}
if resValue.Fizz != testStructValue.Fizz {
t.Errorf(`Should be %d, %d given`, testStructValue.Fizz, resValue.Fizz)
}
}
func Test_Form(t *testing.T) {
form := url.Values{}
form.Add("Foo", "Bar")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf(`Method should be %s, %s given`, http.MethodPost, r.Method)
}
contentType := r.Header.Get("Content-Type")
if contentType != "application/x-www-form-urlencoded" {
t.Errorf(`Content type should be "%s", "%s" given`, "application/x-www-form-urlencoded", contentType)
}
val := r.FormValue("Foo")
if val != "Bar" {
t.Errorf(`Form value of "Foo" should be "%s", "%s" given`, "Bar", val)
}
w.WriteHeader(http.StatusBadRequest)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Error(err)
}
res, err := Post(u).Form(form)
if err != nil {
t.Error(err)
}
if res.Status() != http.StatusBadRequest {
t.Errorf(`Response status should be "%d", "%d" given`, http.StatusBadRequest, res.Status())
}
}
func Test_Debug(t *testing.T) {
userAgent := UserAgentChrome
reqBody := "Foobar"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Error(err)
}
res, err := Delete(u).UserAgent(userAgent).Debug(func(res *Response) {
req := res.Request()
if req.Method() != http.MethodDelete {
t.Errorf(`Request method should be %s, %s given`, http.MethodDelete, req.Method())
}
if u.String() != req.URL().String() {
t.Errorf(`Request url should be "%s", "%s" given`, u.String(), req.URL().String())
}
headers := req.Headers()
if len(headers) != 1 {
t.Errorf(`Request should have %d headers, %d given`, 1, len(headers))
}
if req.String() != reqBody {
t.Errorf(`Request body should be "%s", "%s" given`, reqBody, req.String())
}
headers = res.Headers()
if len(headers) != 2 {
t.Errorf(`Response should have %d headers, %d given`, 2, len(headers))
}
}).Body([]byte(reqBody))
if err != nil {
t.Error(err)
}
if res.Status() != http.StatusOK {
t.Errorf(`Response status should be "%d", "%d" given`, http.StatusBadRequest, res.Status())
}
}