107 lines
4.4 KiB
Go
107 lines
4.4 KiB
Go
package request
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func Test_Get(t *testing.T) {
|
|
uri := "https://gateway.dev.cdlsxd.cn/adminyx/admin/v1/key_batch/list"
|
|
|
|
uv := url.Values{}
|
|
uv.Set("page", "1")
|
|
uv.Set("limit", "2")
|
|
|
|
h := http.Header{
|
|
"Content-Type": []string{"application/x-www-form-urlencoded"},
|
|
}
|
|
respHeader, respBody, err := Get(context.Background(), uri+"?"+uv.Encode(), WithHeaders(h))
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
t.Logf("响应体:%s", string(respBody))
|
|
t.Logf("响应头:%+v", respHeader)
|
|
}
|
|
|
|
func Test_RequestHeaders(t *testing.T) {
|
|
uri := "http://example.com/api"
|
|
body := []byte("request body")
|
|
|
|
h := http.Header{
|
|
"Content-Type": []string{"application/json"},
|
|
"Authorization": []string{"Bearer token"},
|
|
}
|
|
|
|
respHeader, respBody, err := Post(context.Background(), uri, body, WithTimeout(10*time.Second), WithHeaders(h))
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
t.Logf("响应体:%s", string(respBody))
|
|
t.Logf("响应头:%+v", respHeader)
|
|
}
|
|
|
|
func Test_RequestStatusCode(t *testing.T) {
|
|
|
|
uri := "http://example.com/api/update"
|
|
body := []byte("update data")
|
|
|
|
isSuccess := func(code int) bool {
|
|
return code == http.StatusOK || code == http.StatusCreated
|
|
}
|
|
|
|
respHeader, respBody, err := Put(context.Background(), uri, body, WithStatusCodeFunc(isSuccess))
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
t.Logf("响应体:%s", string(respBody))
|
|
t.Logf("响应头:%+v", respHeader)
|
|
}
|
|
|
|
func Test_WxNotifyRequest(t *testing.T) {
|
|
|
|
uri := "https://gateway.dev.cdlsxd.cn/voucher/v1/notify/1100040695"
|
|
//uri := "https://voucher.86698.cn/voucher/v1/notify/1100040695"
|
|
|
|
headerBytes := []byte(`{"Accept":["*/*"],"Cache-Control":["no-cache"],"Connection":["close"],"Content-Length":["1113"],"Content-Type":["application/json"],"Pragma":["no-cache"],"User-Agent":["Mozilla/4.0"],"Wechatpay-Nonce":["TD238pwsm3x0rYo57A8Z27Qa3AFDmToI"],"Wechatpay-Serial":["PUB_KEY_ID_0111000406952026032500382251001000"],"Wechatpay-Signature":["a64Z4Lngoz61OhhpG82g/rv8tYV0KmnE7g5KA4hvj7bY6XKSdQaifajXnLQHxA70owmd1kGA6wY5ZxDCvxnNHRjiNP4f68Ii7swL0TCfcU3aLpkLlmPM4wtzSh4YK1cIg4z/mqpxLDkxUky2vp4BvmGx6oKjK5Yv/emFerS2rWWdM2tTdi5Rv1rAGz/osqmrW8d+ZsLlGKHZ6wh7GUmNqt/qF+/kx+GB1jpzCgn8aUQqNvfnbtFcs9SKMQqm+DmBUO85IHL2WCQsuQ6cWpTrRCg5TGj0M0b8TEVHLbX0eBxtUyyGtdwuQbVbEzqCbwLs234YLG1Koq8lxZx6Nib1OQ=="],"Wechatpay-Signature-Type":["WECHATPAY2-SHA256-RSA2048"],"Wechatpay-Timestamp":["1774864935"],"X-Forwarded-For":["121.51.58.174, 172.29.42.89"]}`)
|
|
bodyBytes := []byte(`{"id":"ab4dbb6c-d9e9-5d2f-9f22-9697dfe2a58e","create_time":"2026-03-30T18:02:15+08:00","resource_type":"encrypt-resource","event_type":"COUPON.USE","summary":"代金券核销通知","resource":{"original_type":"coupon","algorithm":"AEAD_AES_256_GCM","ciphertext":"lsBBeh/DxRiecPLgtFyZov6tsCA38vWQ4QZxZouyp48mr3/kP3jSu/l2Sl1Rr59a4F0iIFSxuc7p6+/ovX11Ol7i+gbJmNlc+F3Yfo/bb8zyt/ubys4Ep0Or3YEvZr+h4G4k8rPn0iTYlzuY2hBcfLAazt/qTSz87aNmpBz3XN1CJL+5C0LffWdd5j1zjxfY2AYiUwtSuzo2cRYrxberxyeL9SeHJnqc+8o0njB6fyOJ4q5D9SyPqSNe228tW2xw0/d9QNWtzIlwQhyCwuhdXh0GBSWO7jdEmD3tQCIOk3OgoooywBZFtfwUvMiy2/tohEKd9JIQi3OVV2ctUx+cBdJ7RIH2r8PFZO55PqIND9itDCraSbfHQpqlYfVLZMossa92U54khv9FOzy/RQJPut/YWFkC4PsXgtfPpXQI0l6M+if1AVX+h4XHKk+wCd3fQ37tAwpxNF9kCuLgCIzgmnWjxj8296z6lQkKFomDn6xLdv6ECtnNYGhItQqU6v122/klkeysURSn1qFSbkujFvByCEKSmasTHe1yLorvfAYl+YJVnvLKsBxjjlCiEdCjspTFlDG3m+N/DxsYpYegYxvzfrM2MhgjJwPBbuxxgz4KMtYyiOrHvMDg8qCA41fRkxO4JkSA0JP22Q7v6zXOcHgf/w5aNo6xyICUrg2AueY0UV2uDA4yF6hYTho+CfGLjRfdFCuDReBPlA2TcJKSEtAWJW25nFrqKY/C87xAiD99JPf+tg7h2HcDCzasnV8h/AGA3PSQf2ZX4TsuyRdyPQ+uQWSW","associated_data":"coupon","nonce":"c6H0W8tKIiJP"}}`)
|
|
|
|
var headerMap http.Header
|
|
if err := json.Unmarshal(headerBytes, &headerMap); err != nil {
|
|
t.Error(fmt.Sprintf("解析 headers 失败: %v", err))
|
|
return
|
|
}
|
|
|
|
hc := &http.Client{
|
|
Timeout: 5 * time.Second,
|
|
Transport: &http.Transport{
|
|
MaxIdleConns: 1, // 最大空闲连接数
|
|
MaxIdleConnsPerHost: 1, // 每个主机的最大空闲连接数
|
|
IdleConnTimeout: 10 * time.Second, // 空闲连接超时时间
|
|
},
|
|
}
|
|
|
|
isSuccess := func(code int) bool {
|
|
return code == http.StatusOK || code == http.StatusCreated
|
|
}
|
|
|
|
respHeader, respBody, err := Post(context.Background(), uri, bodyBytes, WithHttpClient(hc), WithStatusCodeFunc(isSuccess), WithHeaders(headerMap))
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
t.Logf("响应体:%s", string(respBody))
|
|
t.Logf("响应头:%v", respHeader)
|
|
}
|