58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package domain
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNormalizeSessionID(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
raw string
|
|
want string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "trims surrounding Unicode whitespace",
|
|
raw: "\u2003 session-123 \u2003",
|
|
want: "session-123",
|
|
},
|
|
{
|
|
name: "blank input is omitted",
|
|
raw: " \t\u2003 ",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "maximum Unicode length is accepted",
|
|
raw: strings.Repeat("界", SessionIDMaxLength),
|
|
want: strings.Repeat("界", SessionIDMaxLength),
|
|
},
|
|
{
|
|
name: "one Unicode code point over maximum is rejected",
|
|
raw: strings.Repeat("界", SessionIDMaxLength+1),
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := NormalizeSessionID(tt.raw)
|
|
if tt.wantErr {
|
|
if err == nil {
|
|
t.Fatal("expected normalization error")
|
|
}
|
|
if !strings.Contains(err.Error(), "exceeds maximum") {
|
|
t.Fatalf("expected useful length diagnostic, got %v", err)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("normalize session id: %v", err)
|
|
}
|
|
if got != tt.want {
|
|
t.Fatalf("normalized session id = %q, want %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|