Gate Stripe readiness on real credentials, surface dead-lettered syncs as failed with retry, and add header-safe toast JSON encoding. Switch the test Keycloak realm references to `test` and document the OpenSpec change.
36 lines
1.2 KiB
Go
36 lines
1.2 KiB
Go
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)
|
|
}
|
|
}
|