56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
type failingWriter struct {
|
|
header http.Header
|
|
}
|
|
|
|
func (f *failingWriter) Header() http.Header {
|
|
if f.header == nil {
|
|
f.header = http.Header{}
|
|
}
|
|
return f.header
|
|
}
|
|
|
|
func (f *failingWriter) WriteHeader(status int) {}
|
|
|
|
func (f *failingWriter) Write([]byte) (int, error) {
|
|
return 0, errors.New("boom")
|
|
}
|
|
|
|
func TestWriteJSONWriteError(t *testing.T) {
|
|
w := &failingWriter{}
|
|
writeJSON(w, map[string]any{"ok": true})
|
|
if got := w.Header().Get("Content-Type"); got != "text/plain; charset=utf-8" {
|
|
t.Fatalf("expected fallback error content type, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestEnvDefaultsAndContains(t *testing.T) {
|
|
if got := env("PEGASUS_TEST_MISSING", "fallback"); got != "fallback" {
|
|
t.Fatalf("unexpected env fallback %q", got)
|
|
}
|
|
if !contains([]string{"a", "b"}, "a") {
|
|
t.Fatalf("expected contains to find value")
|
|
}
|
|
if contains([]string{"a", "b"}, "z") {
|
|
t.Fatalf("expected contains to return false")
|
|
}
|
|
}
|
|
|
|
func TestSanitizeSegmentTrimsAndClamps(t *testing.T) {
|
|
if got := sanitizeSegment(" /A bad/segment "); got != "A_bad" {
|
|
t.Fatalf("unexpected sanitized segment %q", got)
|
|
}
|
|
|
|
long := "abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789"
|
|
if got := sanitizeSegment(long); len(got) != 64 {
|
|
t.Fatalf("expected 64-char clamp, got %d", len(got))
|
|
}
|
|
}
|