transfer_middleware/until/request/test/wxbiz_test.go

84 lines
2.5 KiB
Go

package test
import (
"fmt"
"testing"
"trasfer_middleware/until/request"
)
// 发送GET 请求
func TestGet(t *testing.T) {
r := request.Request{
Method: "GET",
Url: "https://httpbin.org/get?name=张三&age=12",
Headers: map[string]string{"Cookie": "abc", "Token": "123"}}
resp, _ := r.Send()
fmt.Printf("状态码: %d\n", resp.StatusCode)
fmt.Printf("原因: %s\n", resp.Reason)
fmt.Printf("响应时间: %f秒\n", resp.Elapsed)
fmt.Printf("响应文本: %s\n", resp.Text)
}
// 发送GET 带单独Query参数请求
func TestWAccessToken(t *testing.T) {
appid := "wx381e6a04ab7f69b6"
secret := "16f6152c216c07fdff4c0ecaf1a173ed"
r := request.Request{
Method: "GET",
Url: "https://api.weixin.qq.com/cgi-bin/token",
Params: map[string]string{"grant_type": "client_credential", "appid": appid, "secret": secret},
}
resp, err := r.Send()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("状态码: %d\n", resp.StatusCode)
fmt.Printf("原因: %s\n", resp.Reason)
fmt.Printf("响应时间: %f秒\n", resp.Elapsed)
fmt.Printf("响应文本: %s\n", resp.Text)
}
func TestDtalkBlackBard(t *testing.T) {
r := request.Request{
Method: "POST",
Url: "https://httpbin.org/post",
Data: map[string]string{"name": "张三", "age": "12"},
Headers: map[string]string{"Cookie": "abc", "Token": "123"}}
resp, _ := r.Send()
fmt.Printf("状态码: %d\n", resp.StatusCode)
fmt.Printf("原因: %s\n", resp.Reason)
fmt.Printf("响应时间: %f秒\n", resp.Elapsed)
fmt.Printf("响应文本: %s\n", resp.Text)
}
// 发送POST 表单请求
func TestPostForm(t *testing.T) {
r := request.Request{
Method: "POST",
Url: "https://httpbin.org/post",
Data: map[string]string{"name": "张三", "age": "12"},
Headers: map[string]string{"Cookie": "abc", "Token": "123"}}
resp, _ := r.Send()
fmt.Printf("状态码: %d\n", resp.StatusCode)
fmt.Printf("原因: %s\n", resp.Reason)
fmt.Printf("响应时间: %f秒\n", resp.Elapsed)
fmt.Printf("响应文本: %s\n", resp.Text)
}
// 发送POST JSON请求
func TestPostJson(t *testing.T) {
r := request.Request{
Method: "POST",
Url: "https://httpbin.org/post",
Json: map[string]string{"name": "张三", "age": "12"},
Headers: map[string]string{"Cookie": "abc", "Token": "123"}}
resp, _ := r.Send()
fmt.Printf("状态码: %d\n", resp.StatusCode)
fmt.Printf("原因: %s\n", resp.Reason)
fmt.Printf("响应时间: %f秒\n", resp.Elapsed)
fmt.Printf("响应文本: %s\n", resp.Text)
}