65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package errcode
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRegistryUniqueAndPublic(t *testing.T) {
|
|
all := All()
|
|
if len(all) < 50 {
|
|
t.Fatalf("catalog too small: %d", len(all))
|
|
}
|
|
seen := map[string]struct{}{}
|
|
for _, d := range all {
|
|
if _, ok := seen[d.ID]; ok {
|
|
t.Fatalf("duplicate %s", d.ID)
|
|
}
|
|
seen[d.ID] = struct{}{}
|
|
if d.Message == "" || d.Guide == "" || d.Class == "" {
|
|
t.Fatalf("incomplete %+v", d)
|
|
}
|
|
if Sanitize(d.Message) != d.Message || Sanitize(d.Guide) != d.Guide {
|
|
t.Fatalf("public text redacted: %+v", d)
|
|
}
|
|
if strings.Contains(d.Guide, "/home/") || strings.Contains(strings.ToLower(d.Message), "password") {
|
|
t.Fatalf("leak %+v", d)
|
|
}
|
|
}
|
|
if _, ok := Lookup("INTERNAL"); !ok {
|
|
t.Fatal("missing INTERNAL")
|
|
}
|
|
if got := FromStatus(404).ID; got != "RESOURCE_NOT_FOUND" {
|
|
t.Fatalf("404 -> %s", got)
|
|
}
|
|
if got := FromLegacy(400, `{"error":"ERR_INVALID_PARAM","message":"无效的 JSON 请求体"}`).ID; got != "REQUEST_JSON_INVALID" {
|
|
t.Fatalf("json legacy -> %s", got)
|
|
}
|
|
if got := FromLegacy(400, "invalid request body: boom").ID; got != "REQUEST_BODY_INVALID" {
|
|
t.Fatalf("prefix -> %s", got)
|
|
}
|
|
}
|
|
|
|
func TestFromErrorSentinelAndTyped(t *testing.T) {
|
|
sentinel := errors.New("errcode test sentinel")
|
|
Bind(sentinel, "REQUEST_INVALID")
|
|
def, ok := FromError(sentinel)
|
|
if !ok || def.ID != "REQUEST_INVALID" {
|
|
t.Fatalf("sentinel %+v ok=%v", def, ok)
|
|
}
|
|
wrapped := New("DOC_BUSY", errors.New("编辑中"))
|
|
def, ok = FromError(wrapped)
|
|
if !ok || def.ID != "DOC_BUSY" {
|
|
t.Fatalf("typed %+v", def)
|
|
}
|
|
if Sanitize("pq: relation users password /home/x") != "" {
|
|
t.Fatal("expected redact")
|
|
}
|
|
}
|
|
|
|
func TestReportWithoutAuditor(t *testing.T) {
|
|
SetAuditor(nil)
|
|
Report(t.Context(), Event{Code: "INTERNAL", RequestID: "req_test", Upstream: "pq: secret"})
|
|
}
|