Files
member-console/internal/server/toast_encoding_test.go
T
cgalo5758 88db730fcc Add dual licensing and SPDX headers
Introduce a commercial license option alongside AGPL-3.0-only, require a
CLA for contributors, and document the terms in COMMERCIAL.md and
NOTICE. Add a script to stamp SPDX headers on Go files and apply it
across the tree.
2026-09-06 02:29:42 -05:00

39 lines
1.3 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
package server
import (
"encoding/json"
"testing"
)
// TestASCIITriggerJSONEscapesNonASCII guards the HX-Trigger header encoding:
// HTTP header values are ISO-8859-1, so a raw multi-byte UTF-8 rune (e.g. the
// em-dash in a toast message) would be mangled by the browser. The payload must
// be pure ASCII with \uXXXX escapes, and JSON.parse must restore the original.
func TestASCIITriggerJSONEscapesNonASCII(t *testing.T) {
const msg = "Sync enqueued — soon" // contains an em-dash (U+2014)
out, err := asciiTriggerJSON(map[string]string{"showSuccessToast": msg})
if err != nil {
t.Fatalf("asciiTriggerJSON: %v", err)
}
// Header-safe: the payload must be pure ASCII (no raw multi-byte UTF-8).
for i := 0; i < len(out); i++ {
if out[i] >= 0x80 {
t.Fatalf("non-ASCII byte 0x%x at index %d — unsafe for an HTTP header: %s", out[i], i, out)
}
}
// Reversible: a JSON parse (as htmx does client-side) restores the em-dash.
var got map[string]string
if err := json.Unmarshal([]byte(out), &got); err != nil {
t.Fatalf("payload is not valid JSON: %v (%s)", err, out)
}
if got["showSuccessToast"] != msg {
t.Errorf("round-trip mismatch: got %q, want %q", got["showSuccessToast"], msg)
}
}