You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
simpleRequest/excample/simpleRequest_test.go

153 lines
3.9 KiB
Go

3 years ago
/*
2 years ago
*FileName: simpleRequest_test.go
*Author: JJXu
*CreateTime: 2022/3/3 11:34
*Description:
3 years ago
*/
package excample
3 years ago
import (
"fmt"
"github.com/dorlolo/simpleRequest"
"net/http"
"strings"
3 years ago
"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{
3 years ago
"passwd": "123456",
3 years ago
"action": "login",
3 years ago
}
3 years ago
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")
3 years ago
if err != nil {
t.Error(err)
} else {
fmt.Println(res)
}
}
3 years ago
2 years ago
// 测试content-type 为 multipart/form-data格式的数据请求
11 months ago
func TestAuth_formData(t *testing.T) {
3 years ago
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")
3 years ago
var URL = ""
3 years ago
data, _ := req.POST(URL)
3 years ago
t.Log(string(data))
}
2 years ago
// 测试令牌验证
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)
}
}
10 months ago
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,
}).
Omit("Content-Type") //oss默认不支持传Content-Type需要忽略掉。或者可以在oss控制台上放行这个请求头
req.Body().SetFromDataFile("file", "./CHANGELOG.MD")
body, err := req.PUT(signedUrl)
if err != nil {
t.Error(err)
return
}
fmt.Println(string(body))
}