2022-03-13 13:24:29 +00:00
|
|
|
|
/*
|
2023-02-08 14:35:27 +00:00
|
|
|
|
*FileName: simpleRequest_test.go
|
|
|
|
|
*Author: JJXu
|
|
|
|
|
*CreateTime: 2022/3/3 下午11:34
|
|
|
|
|
*Description:
|
2022-03-13 13:24:29 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2022-12-09 13:43:15 +00:00
|
|
|
|
package excample
|
2022-03-13 13:24:29 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/dorlolo/simpleRequest"
|
2022-03-29 03:37:40 +00:00
|
|
|
|
"net/http"
|
2023-11-16 06:55:08 +00:00
|
|
|
|
"os"
|
2022-03-29 03:37:40 +00:00
|
|
|
|
"strings"
|
2022-03-13 13:24:29 +00:00
|
|
|
|
"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")
|
2022-12-09 13:43:15 +00:00
|
|
|
|
//批量添加,不会覆盖上面user
|
2022-12-09 18:06:12 +00:00
|
|
|
|
pamarsBulid := map[string]any{
|
2022-03-17 08:06:20 +00:00
|
|
|
|
"passwd": "123456",
|
2022-03-17 08:18:15 +00:00
|
|
|
|
"action": "login",
|
2022-03-17 08:06:20 +00:00
|
|
|
|
}
|
2022-03-13 13:24:29 +00:00
|
|
|
|
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中的数据,等后续增加功能
|
2022-12-09 13:43:15 +00:00
|
|
|
|
res, err := r.GET("http://www.webSite.com/end/point")
|
2022-03-13 13:24:29 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Println(res)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-03-24 14:51:14 +00:00
|
|
|
|
|
2023-02-08 14:35:27 +00:00
|
|
|
|
// 测试content-type 为 multipart/form-data格式的数据请求
|
2023-10-30 01:59:34 +00:00
|
|
|
|
func TestAuth_formData(t *testing.T) {
|
2022-03-24 14:51:14 +00:00
|
|
|
|
req := simpleRequest.NewRequest()
|
|
|
|
|
req.Headers().ConentType_formData()
|
|
|
|
|
req.Headers().SetRandomUerAgent()
|
2022-12-09 13:43:15 +00:00
|
|
|
|
req.Body().Set("grant_type", "password").
|
|
|
|
|
Set("client_id", "smz").
|
|
|
|
|
Set("client_secret", "smz").
|
|
|
|
|
Set("scope", "getdata").
|
|
|
|
|
Set("username", "shiming_zyf").
|
|
|
|
|
Set("password", "zyf499bbcb9")
|
2022-03-24 14:51:14 +00:00
|
|
|
|
|
2022-03-24 14:53:45 +00:00
|
|
|
|
var URL = ""
|
2022-03-24 14:51:14 +00:00
|
|
|
|
|
2022-12-09 13:43:15 +00:00
|
|
|
|
data, _ := req.POST(URL)
|
2022-03-24 14:51:14 +00:00
|
|
|
|
t.Log(string(data))
|
|
|
|
|
}
|
2022-03-24 15:18:49 +00:00
|
|
|
|
|
2023-02-08 14:35:27 +00:00
|
|
|
|
// 测试令牌验证
|
2022-03-24 15:18:49 +00:00
|
|
|
|
func TestAuthorization(t *testing.T) {
|
|
|
|
|
req := simpleRequest.NewRequest()
|
|
|
|
|
req.Authorization().Bearer("19f0591e-fab1-4447-90c3-1c60aef78fbd")
|
2022-12-09 13:43:15 +00:00
|
|
|
|
req.Body().
|
|
|
|
|
Set("prjnumber", "3205072020100901A01000").
|
|
|
|
|
Set("date", "20220324")
|
|
|
|
|
data, err := req.PUT("")
|
2022-03-24 15:18:49 +00:00
|
|
|
|
t.Log(string(data))
|
|
|
|
|
t.Log(err)
|
|
|
|
|
|
|
|
|
|
}
|
2022-03-29 03:37:40 +00:00
|
|
|
|
|
|
|
|
|
func TestXml(t *testing.T) {
|
|
|
|
|
idcard := "320324196705101880"
|
2022-12-09 13:43:15 +00:00
|
|
|
|
pass := "96778"
|
2022-03-29 03:37:40 +00:00
|
|
|
|
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()
|
2022-12-09 13:43:15 +00:00
|
|
|
|
req.Headers().
|
|
|
|
|
Set("Content-Type", "application/soap+xml;charset=utf-8").
|
|
|
|
|
SetRandomUerAgent()
|
2022-03-29 03:37:40 +00:00
|
|
|
|
req.Body().SetString(body)
|
2022-12-09 13:43:15 +00:00
|
|
|
|
data, err := req.POST(urlAddr)
|
2022-03-29 03:37:40 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2023-11-16 06:17:11 +00:00
|
|
|
|
|
2023-11-16 06:55:08 +00:00
|
|
|
|
// 阿里云Oss文件上传示例
|
2023-11-16 06:17:11 +00:00
|
|
|
|
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,
|
2023-11-16 06:55:08 +00:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
fileData, err := os.ReadFile("./CHANGELOG.MD")
|
|
|
|
|
req.Body().SetBytes(fileData)
|
|
|
|
|
|
2023-11-16 06:17:11 +00:00
|
|
|
|
body, err := req.PUT(signedUrl)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Error(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(string(body))
|
|
|
|
|
}
|