-
Notifications
You must be signed in to change notification settings - Fork 6
/
post_test.go
202 lines (164 loc) · 5.61 KB
/
post_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
package fetch
import (
"fmt"
"os"
"testing"
)
func Test_Post(t *testing.T) {
response, _ := Post("https://httpbin.zcorky.com/post", &Config{
Body: map[string]interface{}{
"foo": "bar",
"foo2": "bar2",
"number": 1,
"boolean": true,
"array": []string{
"foo3",
"bar3",
},
"nest": map[string]string{
"foo4": "bar4",
},
},
})
if response.Status != 200 {
t.Error("Expected status code 200, got", response.Status)
}
if response.Headers.Get("content-type") != "application/json; charset=utf-8" {
t.Error("Expected content-type application/json; charset=utf-8, got", response.Headers.Get("content-type"))
}
if response.Headers.Get("server") != "openresty" {
t.Error("Expected server openresty, got", response.Headers.Get("server"))
}
if response.Get("url").String() != "/post" {
t.Error("Expected url /post, got", response.Get("url").String())
}
if response.Get("method").String() != "POST" {
t.Error("Expected method POST, got", response.Get("method").String())
}
if response.Get("headers.host").String() != "httpbin.zcorky.com" {
t.Error("Expected Host httpbin.zcorky.com, got", response.Get("headers.Host").String())
}
if response.Get("headers.accept-encoding").String() != "gzip" {
t.Error("Expected accept-encoding gzip, got", response.Get("headers.accept-encoding").String())
}
if response.Get("headers.user-agent").String() != DefaultUserAgent() {
t.Error(fmt.Sprintf("Expected user-agent %s, got", DefaultUserAgent()), response.Get("headers.user-agent").String())
}
if response.Get("headers.connection").String() != "close" {
t.Error("Expected connection close, got", response.Get("headers.connection").String())
}
if response.Get("origin").String() != "https://httpbin.zcorky.com" {
t.Error("Expected origin https://httpbin.zcorky.com, got", response.Get("origin").String())
}
if response.Get("body.foo").String() != "bar" {
t.Error("Expected body.foo bar, got", response.Get("body.foo").String())
}
if response.Get("body.foo2").String() != "bar2" {
t.Error("Expected body.foo2 bar2, got", response.Get("body.foo2").String())
}
if response.Get("body.number").Int() != 1 {
t.Error("Expected body.number 1, got", response.Get("body.number").String())
}
if response.Get("body.boolean").Bool() != true {
t.Error("Expected body.boolean true, got", response.Get("body.boolean").String())
}
if response.Get("body.array.0").String() != "foo3" {
t.Error("Expected body.array foo3, got", response.Get("body.array").String())
}
if response.Get("body.array.1").String() != "bar3" {
t.Error("Expected body.array bar3, got", response.Get("body.array").String())
}
if response.Get("body.nest.foo4").String() != "bar4" {
t.Error("Expected body.nest.foo4 bar4, got", response.Get("body.nest.foo4").String())
}
}
func Test_Post_With_Header(t *testing.T) {
response, _ := Post("https://httpbin.zcorky.com/post", &Config{
Headers: map[string]string{
"X-CUSTOM-VAR": "custom-value",
"x-custom-var-2": "custom-value-2",
},
})
if response.Get("headers.x-custom-var").String() != "custom-value" {
t.Error("Expected x-custom-var custom-value, got", response.Get("headers.x-custom-var").String())
}
if response.Get("headers.x-custom-var-2").String() != "custom-value-2" {
t.Error("Expected x-custom-var-2 custom-value, got", response.Get("headers.x-custom-var").String())
}
}
func Test_Post_With_Query(t *testing.T) {
response, _ := Post("https://httpbin.zcorky.com/post", &Config{
Query: map[string]string{
"foo": "bar",
"foo2": "bar2",
},
})
if response.Get("query.foo").String() != "bar" {
t.Error("Expected foo bar, got", response.Get("query.foo").String())
}
if response.Get("query.foo2").String() != "bar2" {
t.Error("Expected foo2 bar2, got", response.Get("query.foo2").String())
}
}
func Test_Post_With_UrlFormEncoded(t *testing.T) {
response, _ := Post("https://httpbin.zcorky.com/post", &Config{
Headers: map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
},
Body: map[string]string{
"foo": "bar",
"foo2": "bar2",
},
})
// fmt.Println("response:", response.String())
if response.Get("body.foo").String() != "bar" {
t.Error("Expected foo bar, got", response.Get("body.foo").String())
}
if response.Get("body.foo2").String() != "bar2" {
t.Error("Expected foo2 bar2, got", response.Get("body.foo2").String())
}
}
func Test_Post_With_FormData(t *testing.T) {
response, err := Post("https://httpbin.zcorky.com/post", &Config{
Headers: map[string]string{
"Content-Type": "multipart/form-data",
},
Body: map[string]string{
"foo": "bar",
"foo2": "bar2",
},
})
if err != nil {
t.Error(err)
}
// fmt.Println("response:", response.String())
if response.Get("body.foo").String() != "bar" {
t.Error("Expected foo bar, got", response.Get("body.foo").String())
}
if response.Get("body.foo2").String() != "bar2" {
t.Error("Expected foo2 bar2, got", response.Get("body.foo2").String())
}
}
func Test_Post_With_FormData_Upload_File(t *testing.T) {
file, _ := os.Open("go.mod")
response, err := Post("https://httpbin.zcorky.com/upload", &Config{
Headers: map[string]string{
"Content-Type": "multipart/form-data",
},
Body: map[string]interface{}{
"foo": "bar",
"thefilename": file,
},
})
if err != nil {
t.Error(err)
return
}
// fmt.Println("response:", response.String())
if response.Get("body.foo").String() != "bar" {
t.Error("Expected foo bar, got", response.Get("body.foo").String())
}
if response.Get("files.thefilename.name").String() != "go.mod" {
t.Error("Expected thefilename go.mod, got", response.Get("files.thefilename.name").String())
}
}