-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimpleRequest_test.go
156 lines (133 loc) · 3.84 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
/*
*FileName: simpleRequest_test.go
*Author: JJXu
*CreateTime: 2022/3/3 下午11:34
*Description:
*/
package excample
import (
"fmt"
"github.com/dorlolo/simpleRequest"
"net/http"
"os"
"strings"
"testing"
"time"
)
func TestRequest(t *testing.T) {
var r = simpleRequest.NewRequest()
//---设置请求头
r.Headers().Set("token", "d+jfdji*D%1=")
//串联使用示例:设置Conent-Type为applicaiton/json 并且 随机user-agent
r.Headers().ConentType_json().SetRandomUerAgent()
//设置params
r.QueryParams().Set("user", "dorlolo")
//批量添加,不会覆盖上面user
pamarsBulid := map[string]any{
"passwd": "123456",
"action": "login",
}
r.QueryParams().Sets(pamarsBulid)
//--添加body
r.Body().Set("beginDate", "2022-03-01").Set("endDate", "2022-03-03")
//--其它请求参数
r.TimeOut(time.Second * 30) //请求超时,默认7秒
r.SkipCertVerify() //跳过证书验证
//--发送请求,这里返回的直接是body中的数据,等后续增加功能
res, err := r.GET("http://www.webSite.com/end/point")
if err != nil {
t.Error(err)
} else {
fmt.Println(res)
}
}
// 测试content-type 为 multipart/form-data格式的数据请求
func TestAuth_formData(t *testing.T) {
req := simpleRequest.NewRequest()
req.Headers().ConentType_formData()
req.Headers().SetRandomUerAgent()
req.Body().Set("grant_type", "password").
Set("client_id", "smz").
Set("client_secret", "smz").
Set("scope", "getdata").
Set("username", "shiming_zyf").
Set("password", "zyf499bbcb9")
var URL = ""
data, _ := req.POST(URL)
t.Log(string(data))
}
// 测试令牌验证
func TestAuthorization(t *testing.T) {
req := simpleRequest.NewRequest()
req.Authorization().Bearer("19f0591e-fab1-4447-90c3-1c60aef78fbd")
req.Body().
Set("prjnumber", "3205072020100901A01000").
Set("date", "20220324")
data, err := req.PUT("")
t.Log(string(data))
t.Log(err)
}
func TestXml(t *testing.T) {
idcard := "320324196705101880"
pass := "96778"
urlAddr := "http://218.4.84.171:5445/AppWebService/GHBackBone_SAMWS.asmx?Content-Type=application/soap+xml;charset=utf-8"
body := fmt.Sprintf(`<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetWorkerSAM xmlns="http://tempuri.org/">
<zjhm>%v</zjhm>
<pass>%v</pass>
</GetWorkerSAM>
</soap12:Body>
</soap12:Envelope>`, idcard, pass)
req := simpleRequest.NewRequest()
req.Headers().
Set("Content-Type", "application/soap+xml;charset=utf-8").
SetRandomUerAgent()
req.Body().SetString(body)
data, err := req.POST(urlAddr)
t.Log(string(data))
t.Log(err)
return
}
func TestIsJsonType(t *testing.T) {
var headers = http.Header{}
headers.Set("Content-Type", "application/json")
headers.Add("Content-Type", "charset=UTF-8")
RES := simpleRequest.IsJSONType(headers.Get("Content-Type"))
t.Log(RES)
}
func TestIsXmlType(t *testing.T) {
var headers = http.Header{}
headers.Add("Content-Type", "application/soap+xml;charset=utf-8")
RES := simpleRequest.IsXMLType(headers.Get("Content-Type"))
t.Log(RES)
}
func TestTextPlain(t *testing.T) {
var headers = http.Header{}
headers.Add("Content-Type", "text/plain;charset=utf-8")
res := strings.Contains(headers.Get("Content-Type"), "text")
if res {
t.Log(res)
} else {
t.Log(false)
}
}
// 阿里云Oss文件上传示例
func TestUploadFileToOss(t *testing.T) {
var signedUrl = "" //STS授权url
var xOssCallback = "" //回调信息
var req = simpleRequest.NewRequest()
req.Headers().
Sets(map[string]string{
"X-Oss-Callback": xOssCallback,
})
fileData, err := os.ReadFile("./CHANGELOG.MD")
req.Body().SetBytes(fileData)
body, err := req.PUT(signedUrl)
if err != nil {
t.Error(err)
return
}
fmt.Println(string(body))
}