This commit is contained in:
2026-07-21 15:53:29 +08:00
parent 024b6614ba
commit c943f575d5
40 changed files with 5362 additions and 275 deletions

View File

@@ -0,0 +1,57 @@
package fadada
import "testing"
func TestVerifyCallback(t *testing.T) {
secret := "test-secret"
c := NewClient(&Config{
AppID: "80005307",
AppSecret: secret,
ServerURL: "https://uat-api.fadada.com/api/v5/",
})
biz := `{"signTaskId":"st-1","signTaskStatus":"task_finished"}`
timestamp := "1720000000000"
nonce := "nonce-1"
headMap := map[string]string{
"X-FASC-App-Id": "80005307",
"X-FASC-Sign-Type": "HMAC-SHA256",
"X-FASC-Timestamp": timestamp,
"X-FASC-Nonce": nonce,
"X-FASC-Event": "sign-task-finished",
"bizContent": biz,
}
sign := SignByMap(headMap, timestamp, secret)
headers := map[string]string{
"X-FASC-App-Id": "80005307",
"X-FASC-Sign-Type": "HMAC-SHA256",
"X-FASC-Timestamp": timestamp,
"X-FASC-Nonce": nonce,
"X-FASC-Event": "sign-task-finished",
"X-FASC-Sign": sign,
}
if err := c.VerifyCallback(headers, biz); err != nil {
t.Fatalf("verify failed: %v", err)
}
headers["X-FASC-Sign"] = "deadbeef"
if err := c.VerifyCallback(headers, biz); err == nil {
t.Fatal("expected signature failure")
}
}
func TestSignByMapStable(t *testing.T) {
m := map[string]string{
"X-FASC-App-Id": "app",
"X-FASC-Nonce": "n1",
"X-FASC-Sign-Type": "HMAC-SHA256",
"X-FASC-Timestamp": "1720000000000",
"bizContent": `{"a":1}`,
}
s1 := SignByMap(m, "1720000000000", "secret")
s2 := SignByMap(m, "1720000000000", "secret")
if s1 == "" || s1 != s2 {
t.Fatalf("signature unstable: %s vs %s", s1, s2)
}
}