45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package signature_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/local/http-client-app/backend/internal/signature"
|
|
)
|
|
|
|
func TestCalculateHMACSHA256Hex(t *testing.T) {
|
|
result, err := signature.Calculate(signature.Request{
|
|
Algorithm: "hmac-sha256",
|
|
Data: "hello",
|
|
Secret: "secret",
|
|
Encoding: "hex",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got %v", err)
|
|
}
|
|
|
|
if result.Value != "88aab3ede8d3adf94d26ab90d3bafd4a2083070c3bcce9c014ee04a443847c0b" {
|
|
t.Fatalf("unexpected signature: %s", result.Value)
|
|
}
|
|
}
|
|
|
|
func TestCalculateSHA256Base64(t *testing.T) {
|
|
result, err := signature.Calculate(signature.Request{
|
|
Algorithm: "sha256",
|
|
Data: "hello",
|
|
Encoding: "base64",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got %v", err)
|
|
}
|
|
|
|
if result.Value != "LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ=" {
|
|
t.Fatalf("unexpected SHA-256 base64: %s", result.Value)
|
|
}
|
|
}
|
|
|
|
func TestCalculateRejectsUnsupportedAlgorithm(t *testing.T) {
|
|
if _, err := signature.Calculate(signature.Request{Algorithm: "md5", Data: "hello"}); err == nil {
|
|
t.Fatalf("expected unsupported algorithm error")
|
|
}
|
|
}
|