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/test/simpleRequest_test.go

81 lines
1.9 KiB
Go

3 years ago
/*
* @FileName: simpleRequest_test.go
* @Author: JuneXu
* @CreateTime: 2022/3/3 11:34
* @Description:
*/
package test
import (
"fmt"
"github.com/dorlolo/simpleRequest"
"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
3 years ago
pamarsBulid := map[string]interface{}{
3 years ago
"passwd": "123456",
3 years ago
"action": "login",
3 years ago
}
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
//测试content-type 为 multipart/form-data格式的数据请求
func TestAuth_fotmData(t *testing.T) {
3 years ago
req := simpleRequest.NewRequest()
req.Headers().ConentType_formData()
req.Headers().SetRandomUerAgent()
req.Body().Set("grant_type", "password")
req.Body().Set("client_id", "smz")
req.Body().Set("client_secret", "smz")
req.Body().Set("scope", "getdata")
req.Body().Set("username", "shiming_zyf")
req.Body().Set("password", "zyf499bbcb9")
var URL = ""
3 years ago
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")
req.Body().Set("date", "20220324")
data, err := req.Post("")
t.Log(string(data))
t.Log(err)
}