-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpleRequest_test.go
160 lines (145 loc) · 3.31 KB
/
simpleRequest_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
// Package simpleRequest -----------------------------
// file : simpleRequest_test.go
// author : JJXu
// contact : [email protected]
// time : 2022/12/9 20:34:52
// -------------------------------------------
package simpleRequest
import (
"encoding/json"
"io"
"net/http"
"testing"
)
type api struct {
Name string `json:"name" form:"name" query:"name"`
}
func httpserver() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
var data api
jsonDecoder := json.NewDecoder(r.Body)
jsonDecoder.Decode(&data)
if data.Name == "JJXu" {
io.WriteString(w, "ok")
} else {
io.WriteString(w, "false")
}
default:
io.WriteString(w, "false")
}
})
//fmt.Println("http服务启动了")
http.ListenAndServe(":8989", nil)
}
func TestPost_withSet(t *testing.T) {
go httpserver()
var r = NewRequest()
r.Headers().ConentType_json()
r.Body().Set("name", "JJXu")
result, err := r.POST("http://localhost:8989/")
if err != nil {
t.Error(err.Error())
} else {
t.Log(string(result))
}
}
func TestPost_withSets(t *testing.T) {
go httpserver()
var r = NewRequest()
r.Headers().ConentType_json()
r.Body().Sets(map[string]any{
"name": "JJXu",
})
result, err := r.POST("http://localhost:8989/")
if err != nil {
t.Error(err.Error())
} else {
t.Log(string(result))
}
}
func TestPost_withSetModel(t *testing.T) {
go httpserver()
var r = NewRequest()
r.Headers().ConentType_json()
var entry = api{
Name: "JJXu",
}
r.Body().SetModel(&entry)
result, err := r.POST("http://localhost:8989/")
if err != nil {
t.Error(err.Error())
} else {
t.Log(string(result))
}
}
// url中的query param字符串参数会被r.QueryParams()中的key值覆盖
func TestQueryUrl2(t *testing.T) {
go httpserver()
var r = NewRequest()
r.Headers().ConentType_formUrlencoded()
r.QueryParams().Set("a", "123")
r.QueryParams().Set("b", "456")
_, err := r.POST("http://localhost:8989?a=1&b=2&c=3")
if err != nil {
t.Error(err.Error())
} else {
if r.Request.URL.RawQuery != "a=123&b=456&c=3" {
t.Errorf("query params want '%s' but get '%s'", "a=123&b=456&c=3", r.Request.URL.RawQuery)
}
}
}
// 请求后,r.Request.Body中的内容仍旧可读
func TestQueryUseStringBody(t *testing.T) {
go httpserver()
var r = NewRequest()
r.Headers().ConentType_json()
bodyData := "{'a'=123,'b'=56}"
r.Body().SetString(bodyData)
_, err := r.POST("http://localhost:8989")
if err != nil {
t.Error(err)
return
}
body, err := io.ReadAll(r.Request.Body)
if err != nil {
t.Error(err)
return
}
if string(body) != bodyData {
t.Errorf("request body want '%s' but get '%s'", bodyData, body)
}
}
func TestEmptyBody(t *testing.T) {
go httpserver()
var r = NewRequest()
r.Headers().ConentType_json()
_, err := r.POST("http://localhost:8989")
if err != nil {
t.Error(err)
return
}
body, err := io.ReadAll(r.Request.Body)
if err != nil {
t.Error(err)
return
}
if string(body) != "{}" {
t.Errorf("request body want '%s' but get '%s'", "{}", body)
}
r.Headers().ConentType_formUrlencoded()
_, err = r.POST("http://localhost:8989")
if err != nil {
t.Error(err)
return
}
body, err = io.ReadAll(r.Request.Body)
if err != nil {
t.Error(err)
return
}
if string(body) != "" {
t.Errorf("request body want '%s' but get '%s'", "", body)
}
}