修复支付网关地址空白字符

This commit is contained in:
yml
2026-06-15 00:53:05 +08:00
parent a275a006eb
commit 4a0d615926
4 changed files with 80 additions and 8 deletions
@@ -374,7 +374,7 @@ func (c *Client) post(ctx context.Context, endpoint string, reqData map[string]a
if err != nil {
return nil, err
}
url := strings.TrimRight(c.cfg.GatewayURL, "/") + endpoint
url := strings.TrimRight(strings.TrimSpace(c.cfg.GatewayURL), "/") + endpoint
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return nil, err
@@ -125,6 +125,14 @@ func TestCreatePaymentSetsCounterParamWhenPayModeConfigured(t *testing.T) {
}
}
func TestCreatePaymentTrimsGatewayURLWhitespace(t *testing.T) {
body := captureCreatePaymentBody(t, "", "\t")
reqData := body["req_data"].(map[string]any)
if reqData["out_order_no"] != "ORDER1" {
t.Fatalf("out_order_no = %#v, want ORDER1", reqData["out_order_no"])
}
}
func TestCreatePaymentUsesShanghaiTimeWhenLocalIsUTC(t *testing.T) {
oldLocal := time.Local
time.Local = time.UTC
@@ -179,7 +187,7 @@ func testKeyPairPEM(t *testing.T, key *rsa.PrivateKey) (string, string) {
return privatePEM, certPEM
}
func captureCreatePaymentBody(t *testing.T, payMode string) map[string]any {
func captureCreatePaymentBody(t *testing.T, payMode string, gatewaySuffix ...string) map[string]any {
t.Helper()
var captured map[string]any
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -199,8 +207,12 @@ func captureCreatePaymentBody(t *testing.T, payMode string) map[string]any {
t.Fatalf("generate key: %v", err)
}
privatePEM, certPEM := testKeyPairPEM(t, key)
gatewayURL := server.URL
if len(gatewaySuffix) > 0 {
gatewayURL += gatewaySuffix[0]
}
client := NewClient(Config{
GatewayURL: server.URL,
GatewayURL: gatewayURL,
AppID: "app-1",
SerialNo: "serial-1",
MerchantID: "merchant-1",
@@ -395,7 +395,7 @@ func notifyParamKeys(params map[string]string) []string {
}
func (c *Client) validate() error {
if c.cfg.GatewayURL == "" || c.cfg.MerchantID == "" || c.cfg.SignKey == "" {
if strings.TrimSpace(c.cfg.GatewayURL) == "" || c.cfg.MerchantID == "" || c.cfg.SignKey == "" {
return ErrConfigIncomplete
}
if c.cfg.SignType != "" && !strings.EqualFold(c.cfg.SignType, "MD5") {
@@ -409,7 +409,7 @@ func (c *Client) post(ctx context.Context, params map[string]string) (map[string
for key, value := range params {
values.Set(key, value)
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.cfg.GatewayURL, strings.NewReader(values.Encode()))
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimSpace(c.cfg.GatewayURL), strings.NewReader(values.Encode()))
if err != nil {
return nil, err
}