package api import ( "net/http" "strings" "testing" "unicode/utf8" ) // PATCH 承载 OAuth / 安全设置变更,必须纳入审计;只读方法不留痕。 func TestIsWriteMethod(t *testing.T) { tests := []struct { method string want bool }{ {http.MethodPost, true}, {http.MethodPut, true}, {http.MethodPatch, true}, {http.MethodDelete, true}, {http.MethodGet, false}, {http.MethodHead, false}, } for _, tt := range tests { if got := isWriteMethod(tt.method); got != tt.want { t.Errorf("isWriteMethod(%s) = %v, want %v", tt.method, got, tt.want) } } } func TestTruncateLogField(t *testing.T) { tests := []struct { name string in string max int want string }{ {name: "短串原样", in: "abc", max: 10, want: "abc"}, {name: "ASCII 截断", in: "abcdef", max: 4, want: "abcd"}, {name: "中文边界回退", in: "中文日志", max: 4, want: "中"}, {name: "恰好落在边界", in: "中文", max: 3, want: "中"}, {name: "emoji 拦腰", in: "a😀b", max: 3, want: "a"}, {name: "超长 UA 不产非法序列", in: strings.Repeat("界", 200), max: 255, want: strings.Repeat("界", 85)}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := truncateLogField(tt.in, tt.max) if got != tt.want { t.Errorf("truncateLogField(%q, %d) = %q, want %q", tt.in, tt.max, got, tt.want) } if !utf8.ValidString(got) { t.Errorf("result %q is not valid UTF-8", got) } }) } }