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" bodyBytes := []byte(`{"id":"0e92e862-66b0-55a3-b1da-0e54a820b923","create_time":"2026-03-27T14:20:12+08:00","resource_type":"encrypt-resource","event_type":"COUPON.USE","summary":"代金券核销通知","resource":{"original_type":"coupon","algorithm":"AEAD_AES_256_GCM","ciphertext":"LFFqFfGIu1uHYbKwqxND29QS7sH54U1k9uj+W5ecKf0ZzBT8YzikcSqa6W0iIcpnlQE0AJkDxs/nfJQ/pcRJajwy+4JzE8fzrMmhn1vgvUiXt+EyvJ8mPJwfVMtg8YtMuQBUUPxIyc5ACZVawGDVlwG9nxFYCGF0jZAtzKdwZw2MMmGFTCJri6kCE60hic5Yn3b9iPaCwmXdFlVKA6NozMEp2uNoQYYzy1+OEAfyXHyspz0IQnhUFcVuxfnGbK4zJQYUZ8yGhg3YfNFTtDW0vRTBwkNeR8BWy/LXSKMZJ7sC+SaX7eASd0QHXA/UPWpItCQGRDeZHUXWEQw6yB3GpHfKRTlO5H7VINLVe9pj+XWnXMzDEqDShpUnCP3cgf7TBYykopXPKXfI/bekTxy6ZWbikQH1Ts9lc/AMqd3NK7H5AmQPNld6DkBkt1jgI/DP5tUJeyQyYX39fX0pJCFpy94AQhXyPXLKT2VQHmnX3qnaQyztgY7GfvavtmgTNFunKcnqXtgU65wpg156iTGhHyoG01msdoUMjynY7pbdUHM1O/ljsizysqiGBvYaDfhx4Mcf8Fd3kT5bbuelM2DLg7nycz9ZjPrsKWz47eNG2U3hixk6yVxrBpAsO4QM7/ngB1+5Z7Pv06nSoP7jnFxIMv9iwPn5Y+O5tb1JfO/2su7UTtDcY25iJBqiKJvwgtzy9IB1hR781PPNzHVDT9ORUfJVVlrkGgaOn7aMDyzv3DYYUkK0fTENAReCa7D/wrajM6U6Gu8Z1/TfKkJ9BdwbEqI=","associated_data":"coupon","nonce":"b2AW4NUIk2AX"}}`) header := `{"Accept":["*/*"],"Cache-Control":["no-cache"],"Connection":["close"],"Content-Length":["1109"],"Content-Type":["application/json"],"Pragma":["no-cache"],"User-Agent":["Mozilla/4.0"],"Wechatpay-Nonce":["OaVm4rYgmQnOzu9dra2OM9J560D5uuLh"],"Wechatpay-Serial":["PUB_KEY_ID_0111000406952026032500382251001000"],"Wechatpay-Signature":["QUnj2WwPfh3nAycuwQ/7zFVjhSHObnFfmfLnPx/4UXjgFlkY1REyCHrBHrJ1lDpUOodiY5qsGg/7CQ6aMlXZe24xsLOBjwOgZr0JGmsRisrwnQL+jy6ar8pAf1fABv+e/lCLO+UZtLXrv1GCQix51GLerkELEVTQkn6qzT4aVyL1Ukte6hk+r3HlWOQC6OM3cpxyr8XhVilGWaElRCgPwHI2XYLGjH6QWtcRnCSMESLFGOwI7t9GbxgGLptEelfmVpBHdRFTDWa8rWJI7b9ArI//vMawkJUE7tW5nkqF5sWMydKUQ5ZDLf5oI447zic/LOljZ4SA7I/aTN4MlpulDA=="],"Wechatpay-Signature-Type":["WECHATPAY2-SHA256-RSA2048"],"Wechatpay-Timestamp":["1774592412"],"X-Forwarded-For":["121.51.58.169"],"X-Real-Ip":["121.51.58.169"]}` var headerMap http.Header if err := json.Unmarshal([]byte(header), &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)) if err != nil { t.Error(err) return } t.Logf("响应体:%s", string(respBody)) t.Logf("响应头:%v", respHeader) }