-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrequest_body_test.go
150 lines (118 loc) · 3.23 KB
/
request_body_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
package cast
import (
"bytes"
"encoding/json"
"encoding/xml"
"strings"
"testing"
"github.com/google/go-querystring/query"
)
func TestReqJsonBody_ContentType(t *testing.T) {
reqJSONBody := requestJSONBody{}
assert(t, reqJSONBody.ContentType() == "application/json; charset=utf-8", "unexpected json ContentType()")
}
func TestReqJsonBody_Body(t *testing.T) {
type payload struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
var p payload
p.Code = 0
p.Msg = "3323"
b, err := json.Marshal(&p)
if err != nil {
t.Fatal(err)
}
req := requestJSONBody{
payload: b,
}
body, err := req.Body()
if err != nil {
t.Fatal(err)
}
t.Log(string(body))
}
func TestReqFormUrlEncodedBody_ContentType(t *testing.T) {
formBody := requestFormURLEncodedBody{}
assert(t, formBody.ContentType() == "application/x-www-form-urlencoded", "unexpected form urlencoded ContentType()")
}
func TestReqFormUrlEncodedBody_Body(t *testing.T) {
type payload struct {
Code int `url:"code"`
Msg string `url:"msg"`
}
var p payload
p.Code = 0
p.Msg = "ok"
req := requestFormURLEncodedBody{
payload: p,
}
body, err := req.Body()
if err != nil {
t.Fatal(err)
}
values, err := query.Values(&p)
if err != nil {
t.Fatal(err)
}
if values.Encode() != string(body) {
t.Fatal("unexpected return")
}
}
func TestReqXmlBody_Body(t *testing.T) {
type Address struct {
City, State string
}
type Person struct {
XMLName xml.Name `xml:"person"`
ID int `xml:"id,attr"`
FirstName string `xml:"name>first"`
LastName string `xml:"name>last"`
Age int `xml:"age"`
Height float32 `xml:"height,omitempty"`
Married bool
Address
Comment string `xml:",comment"`
}
v := &Person{ID: 13, FirstName: "John", LastName: "Doe", Age: 42}
v.Comment = " Need more details. "
v.Address = Address{"Hanga Roa", "Easter Island"}
xmlBody := requestXMLBody{
payload: v,
}
body, err := xmlBody.Body()
ok(t, err)
var buffer bytes.Buffer
err = xml.NewEncoder(&buffer).Encode(v)
ok(t, err)
t.Log(buffer.String())
assert(t, string(body) == buffer.String(), "unexpected Body()")
}
func TestReqXmlBody_ContentType(t *testing.T) {
reqXMLBody := requestXMLBody{}
assert(t, reqXMLBody.ContentType() == "application/xml; charset=utf-8", "unexpected xml ContentType()")
}
func TestReqPlainBody_Body(t *testing.T) {
plainBody := requestPlainBody{
payload: "xssfddfdfdfdfdsfds",
}
body, err := plainBody.Body()
ok(t, err)
assert(t, string(body) == "xssfddfdfdfdfdsfds", "unexpected Body()")
}
func TestReqPlainBody_ContentType(t *testing.T) {
plainBody := requestPlainBody{}
assert(t, plainBody.ContentType() == "text/plain; charset=utf-8", "unexpected xml ContentType()")
}
func TestReqMultipartFormDataBody(t *testing.T) {
field1 := FormData{FieldName: "text", Value: "input"}
field2 := FormData{FieldName: "media", FileName: "test.txt", Reader: strings.NewReader("txt body")}
multipartBody := requestMultipartFormDataBody{
formData: []*FormData{&field1, &field2},
}
body, err := multipartBody.Body()
ok(t, err)
t.Log(string(body))
t.Log(multipartBody.ContentType())
assert(t, strings.HasPrefix(multipartBody.ContentType(), "multipart/form-data; boundary="), "unexpected multipart ContentType()")
}