forked from asmcos/requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_test.go
194 lines (176 loc) · 4.44 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
package examples
import (
ejson "encoding/json"
"strings"
"testing"
"github.com/ahuigo/requests"
)
// Post Params: use <QueryString> with content-type: none
// curl -X POST "https://www.httpbin.org/post?name=ahuigo"
func TestPostParams(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()
resp, err := requests.Post(
ts.URL+"/post",
requests.Params{
"name": "ahuigo",
},
)
if err != nil {
t.Fatal(err)
}
var data = struct {
Args struct {
Name string
}
}{}
_ = resp.Json(&data)
if data.Args.Name != "ahuigo" {
t.Fatal("invalid response body:", resp.Text())
}
}
// Post Datas: use <Form UrlEncoded data> with application/x-www-form-urlencoded
// curl -H 'Content-Type: application/x-www-form-urlencoded' https://www.httpbin.org/post -d 'name=ahuigo'
func TestPostFormUrlEncode(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()
resp, err := requests.Post(
ts.URL+"/post",
requests.Datas{
"name": "ahuigo",
},
)
if err != nil {
t.Fatal(err)
}
var data = struct {
Body string
}{}
resp.Json(&data)
if data.Body != "name=ahuigo" {
t.Fatal("invalid response body:", resp.Text())
}
}
// POST FormData: multipart/form-data; boundary=....
// curl https://www.httpbin.org/post -F 'name=ahuigo'
func TestPostFormData(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()
resp, err := requests.Post(
ts.URL+"/post",
requests.FormData{
"name": "ahuigo",
},
)
if err != nil {
t.Fatal(err)
}
var data = struct {
Body string
}{}
resp.Json(&data)
if !strings.Contains(data.Body, "form-data; name=\"name\"\r\n\r\nahuigo\r\n") {
t.Error("invalid response body:", resp.Text())
t.Error("invalid response body:", data.Body)
}
}
// POST Json: application/json
// curl -H "Content-Type: application/json" https://www.httpbin.org/post -d '{"name":"Alex"}'
func TestPostJson(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()
// You can also use `json := requests.Jsoni(anyTypeData)`
json := requests.Json{
"name": "Alex",
}
resp, err := requests.Post(ts.URL+"/post", json)
if err != nil {
t.Fatal(err)
}
// parse data
var data = struct {
Body string
}{}
resp.Json(&data)
// is expected results
jsonData, _ := ejson.Marshal(json) // if data.Data!= "{\"name\":\"Alex\"}"{
if data.Body != string(jsonData) {
t.Error("invalid response body:", resp.Text())
}
}
// Post Raw Bypes: text/plain
// curl -H "Content-Type: text/plain" https://www.httpbin.org/post -d 'raw data: Hi, Jack!'
func TestRawBytes(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()
println("Test POST: post bytes data")
rawText := "raw data: Hi, Jack!"
resp, err := requests.Post(ts.URL+"/post", []byte(rawText))
if err != nil {
t.Fatal(err)
}
var data = struct {
Body string
}{}
resp.Json(&data)
if data.Body != rawText {
t.Error("invalid response body:", resp.Text())
}
}
// Post Raw String: text/plain
// curl -H "Content-Type: text/plain" http://0:4500/post -d 'raw data: Hi, Jack!'
func TestRawString1(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()
println("Test POST: raw post data ")
rawText := "raw data: Hi, Jack!"
resp, err := requests.Post(ts.URL+"/post", rawText,
requests.Header{"Content-Type": "text/plain"},
)
if err != nil {
t.Fatal(err)
}
var data interface{}
resp.Json(&data)
if data.(map[string]interface{})["body"].(string) != rawText {
t.Error("invalid response body:", resp.Text())
}
}
// Post Raw String2: text/plain
// curl -H "Content-Type: text/plain" https://www.httpbin.org/post -d 'raw data: Hi, Jack!'
func TestRawString2(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()
println("Test POST: raw post data ")
rawText := "raw data: Hi, Jack!"
resp, err := requests.Post(
ts.URL+"/post",
requests.ContentTypePlain,
rawText,
)
if err != nil {
t.Fatal(err)
}
var data interface{}
resp.Json(&data)
if data.(map[string]interface{})["body"].(string) != rawText {
t.Error("invalid response body:", resp.Text())
}
}
// TestPostEncodedString: application/x-www-form-urlencoded
// curl -H 'Content-Type: application/x-www-form-urlencoded' http://0:4500/post -d 'name=Alex&age=29'
func TestPostEncodedString(t *testing.T) {
ts := createHttpbinServer(0)
defer ts.Close()
resp, err := requests.Post(ts.URL+"/post", "name=Alex&age=29")
if err != nil {
t.Fatal(err)
}
var data = struct {
Body string
}{}
resp.Json(&data)
if data.Body != "name=Alex\u0026age=29" {
t.Error("invalid response body:", resp.Text())
}
}