切换主体

This commit is contained in:
ziming 2026-03-25 17:10:27 +08:00
parent 9c8f64a35a
commit 5ba27d7f7b
1 changed files with 40 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package request
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"testing"
@ -48,6 +50,7 @@ func Test_RequestHeaders(t *testing.T) {
}
func Test_RequestStatusCode(t *testing.T) {
uri := "http://example.com/api/update"
body := []byte("update data")
@ -64,3 +67,40 @@ func Test_RequestStatusCode(t *testing.T) {
t.Logf("响应体:", string(respBody))
t.Logf("响应头:", respHeader)
}
func Test_WxNotifyRequest(t *testing.T) {
uri := "https://gateway.dev.cdlsxd.cn/voucher/v1/notify/1100040695"
body := []byte(`{"id":"fd06376a-3e1b-5516-81f8-9b69cf1ba416","create_time":"2025-07-28T16:10:15+08:00","resource_type":"encrypt-resource","event_type":"MCHTRANSFER.BILL.FINISHED","summary":"商家转账单据终态通知","resource":{"original_type":"mch_payment","algorithm":"AEAD_AES_256_GCM","ciphertext":"XJBIhrHgbe9NR5q/jLYmZKdT/3xuKm2x7EFu3T52Hj2hjPzarRSA2HCsGTxGojfD+CFyJHIULlL2adqLijAjpi3B6TaYKY4LqhtJ/RYSQtYNxYvBpWX1yLOWe8luJbWxmQvKZxIekFs8lGVgkPBUw0IfEAvJ6jHAGCcgxLIqxgOf6UtGUqxCCNp/V3xy8zCiHB0Mvlw8eXCTuG+ZESJIXvloVGNS79R6iNeqk4kNKRSaV86MNh1KQlmoBxZ4yEshD/vIlMulU3xEc+mM25y8vUS4Ot6pxEpUdUyjwcb9QTwTTnZzm6i+VWYymcItAVBQrvsKBMmqWnPtNXG8++13k3DeO1LyVKURmnWXXT1mImmGx/teN/1xPV5y6nChu/HTbcJGDQy2twuq6TPFbbYlTjZH047z/ZtozJNvGNeh","associated_data":"mch_payment","nonce":"YN3eW5H8mxLs"}}`)
hds := `{"Accept":["*/*"],"Cache-Control":["no-cache"],"Connection":["close"],"Content-Length":["775"],"Content-Type":["application/json"],"Pragma":["no-cache"],"User-Agent":["Mozilla/4.0"],"Wechatpay-Nonce":["dF8R9izUJnPBjVLa2cAcCaa7j6QUgitl"],"Wechatpay-Serial":["PUB_KEY_ID_0116523224422025061800192371001800"],"Wechatpay-Signature":["SoXIiTRTr6jofXXxGlfO+wyf1IzXFXcsfvEU2EggQfRKFu+8h3TT6QMQ8zIf8dpkkTPexB/3igGiATrR3uZY4ZeOpRrhIFHSJj0Ala0Ri2Nt4zk+MuBQnhybSYJ4Cn3/sHC4i2HFoOSil7OqlSr79hjod3h0tjYVQLtZ4+Cjp0IeMNB4p5qmIuERuhtfRqcyqXik9/uYNYxw8/Wkf1mMnTsBxyXK3iHAoinXNrEiqCCrQHCfnORMYosr7l+Ox8v9u1c8FFt+rt09vKssVCqYaZ/XRala3mjslDRiluFKSuqb7/JO3AxQjBK6M0iSZOlnmiXSIAq+UxJg4cem6wHi+g=="],"Wechatpay-Signature-Type":["WECHATPAY2-SHA256-RSA2048"],"Wechatpay-Timestamp":["1753690220"],"X-Forwarded-For":["121.51.58.168"],"X-Forwarded-Proto":["https"],"X-Real-Ip":["121.51.58.168"]}`
var headerMap http.Header
if err := json.Unmarshal([]byte(hds), &headerMap); err != nil {
t.Error(fmt.Sprintf("解析 headers 失败: %v", err))
return
}
hc := &http.Client{
Timeout: 10 * 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, body, WithHttpClient(hc), WithStatusCodeFunc(isSuccess))
if err != nil {
t.Error(err)
return
}
t.Logf("响应体:%s", string(respBody))
t.Logf("响应头:%v", respHeader)
}