121 lines
3.4 KiB
Go
121 lines
3.4 KiB
Go
package yuyuecha
|
||
|
||
import (
|
||
"context"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
func TestBuildOpenAPIPath(t *testing.T) {
|
||
tests := []struct {
|
||
in string
|
||
want string
|
||
}{
|
||
{"a01/person-company-relations", "/openapi/v1/a01/person-company-relations"},
|
||
{"/a01/person-company-relations", "/openapi/v1/a01/person-company-relations"},
|
||
{"openapi/v1/a01/person-company-relations", "/openapi/v1/a01/person-company-relations"},
|
||
}
|
||
for _, tt := range tests {
|
||
if got := buildOpenAPIPath(tt.in); got != tt.want {
|
||
t.Fatalf("buildOpenAPIPath(%q)=%q, want %q", tt.in, got, tt.want)
|
||
}
|
||
}
|
||
}
|
||
|
||
func TestHashIDCard(t *testing.T) {
|
||
got := HashIDCard("450322197603171539")
|
||
if len(got) != 32 {
|
||
t.Fatalf("md5 digest length=%d, want 32", len(got))
|
||
}
|
||
if got != strings.ToLower(got) {
|
||
t.Fatalf("digest should be lowercase: %s", got)
|
||
}
|
||
if pass := HashIDCard(got); pass != got {
|
||
t.Fatalf("already-hashed pass-through: got %q want %q", pass, got)
|
||
}
|
||
if pass := HashIDCard(strings.ToUpper(got)); pass != got {
|
||
t.Fatalf("uppercase digest normalize: got %q want %q", pass, got)
|
||
}
|
||
}
|
||
|
||
func TestGenerateCurlCommandHeaderOrder(t *testing.T) {
|
||
cmd := generateCurlCommand("POST", "https://example.com/openapi/v1/a01/x", map[string]string{
|
||
"signature": "sig",
|
||
"Content-Type": "application/json",
|
||
"nonce": "n",
|
||
"clientId": "cid",
|
||
"timestamp": "1",
|
||
}, `{"name":"a"}`)
|
||
wantOrder := []string{
|
||
"-H 'Content-Type: application/json'",
|
||
"-H 'clientId: cid'",
|
||
"-H 'timestamp: 1'",
|
||
"-H 'nonce: n'",
|
||
"-H 'signature: sig'",
|
||
}
|
||
pos := 0
|
||
for _, part := range wantOrder {
|
||
idx := strings.Index(cmd[pos:], part)
|
||
if idx < 0 {
|
||
t.Fatalf("missing ordered header %q in:\n%s", part, cmd)
|
||
}
|
||
pos += idx + len(part)
|
||
}
|
||
}
|
||
|
||
func sandboxService() *YuyuechaService {
|
||
return NewYuyuechaService(ServiceConfig{
|
||
BaseURL: "https://sandbox-api.yuyuecha.com",
|
||
ClientID: "sandbox-haiyu-20260724",
|
||
ClientSecret: "HuidklNz6RnSfCjn6MIcUW_Rj8trLljdN4v6FUs3NsY",
|
||
Timeout: defaultRequestTimeout,
|
||
}, nil)
|
||
}
|
||
|
||
func TestSandboxPersonCompanyRelations(t *testing.T) {
|
||
svc := sandboxService()
|
||
|
||
result, err := svc.CallAPI(context.Background(), "a01/person-company-relations", map[string]interface{}{
|
||
"name": "骆炳荣",
|
||
"idCard": HashIDCard("450322197603171539"),
|
||
})
|
||
if err != nil {
|
||
t.Fatalf("CallAPI failed: %v", err)
|
||
}
|
||
if result == nil || len(result.Data) == 0 {
|
||
t.Fatal("expected non-empty data")
|
||
}
|
||
// 沙箱不计费
|
||
if result.Billable {
|
||
t.Fatalf("sandbox should not be billable, got chargeFen=%d", result.ChargeFen)
|
||
}
|
||
t.Logf("requestId=%s sandbox=%v billable=%v data=%s", result.RequestID, result.Sandbox, result.Billable, string(result.Data))
|
||
}
|
||
|
||
// TestSandboxGetAccount 查询账户汇总:GET,请求体为空
|
||
func TestSandboxGetAccount(t *testing.T) {
|
||
svc := sandboxService()
|
||
|
||
info, err := svc.GetAccount(context.Background())
|
||
if err != nil {
|
||
t.Fatalf("GetAccount failed: %v", err)
|
||
}
|
||
if info == nil {
|
||
t.Fatal("expected account info")
|
||
}
|
||
if info.ClientID != "sandbox-haiyu-20260724" {
|
||
t.Fatalf("clientId=%q", info.ClientID)
|
||
}
|
||
if info.Mode != "sandbox" {
|
||
t.Fatalf("mode=%q", info.Mode)
|
||
}
|
||
if !info.Enabled {
|
||
t.Fatal("expected enabled=true")
|
||
}
|
||
if info.TotalLimit <= 0 {
|
||
t.Fatalf("totalLimit=%d", info.TotalLimit)
|
||
}
|
||
t.Logf("account: clientId=%s mode=%s totalLimit=%d trialRemaining=%d trialEndsAt=%s totalCalls=%d",
|
||
info.ClientID, info.Mode, info.TotalLimit, info.TrialRemaining, info.TrialEndsAt, info.TotalCalls)
|
||
}
|