Add production hardening: monitoring, backups, caching, and high-traffic resilience
- Recovery middleware (no more crash-on-panic), request metrics/monitoring store + admin API, in-memory TTL caching for read-heavy option/question/ category lookups, gzip + rate-limiting middleware, sanitized chat message encoding, and pg_dump-based database backups with an admin API. - Fix N+1 queries in the discover feed, candidate detail, and match list endpoints by batching selection/photo/user/last-message lookups into single queries instead of one-per-item loops. - Tune DB connection pooling and add HTTP server timeouts + graceful shutdown so the API degrades gracefully instead of falling over under load. - Fix a null (vs empty array) JSON response for candidates/matches without photos or interests, which crashed the web app's swipe card.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSanitizeMessageText_TrimsWhitespace(t *testing.T) {
|
||||
got, err := SanitizeMessageText(" سلام دوست من ")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "سلام دوست من" {
|
||||
t.Fatalf("expected trimmed text, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_EmptyAfterTrim(t *testing.T) {
|
||||
_, err := SanitizeMessageText(" ")
|
||||
if !errors.Is(err, ErrMessageEmpty) {
|
||||
t.Fatalf("expected ErrMessageEmpty, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_StripsControlChars(t *testing.T) {
|
||||
got, err := SanitizeMessageText("سلام\x00\x07دنیا")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "سلامدنیا" {
|
||||
t.Fatalf("expected control chars stripped, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_KeepsNewlinesAndTabs(t *testing.T) {
|
||||
got, err := SanitizeMessageText("خط اول\nخط دوم\tبا تب")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got != "خط اول\nخط دوم\tبا تب" {
|
||||
t.Fatalf("expected newline/tab preserved, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_InvalidUTF8(t *testing.T) {
|
||||
invalid := string([]byte{0xff, 0xfe, 0xfd})
|
||||
_, err := SanitizeMessageText(invalid)
|
||||
if !errors.Is(err, ErrInvalidMessageEncoding) {
|
||||
t.Fatalf("expected ErrInvalidMessageEncoding, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_TooLong(t *testing.T) {
|
||||
long := strings.Repeat("a", MaxMessageLength+1)
|
||||
_, err := SanitizeMessageText(long)
|
||||
if !errors.Is(err, ErrMessageTooLong) {
|
||||
t.Fatalf("expected ErrMessageTooLong, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSanitizeMessageText_ExactlyMaxLength(t *testing.T) {
|
||||
exact := strings.Repeat("a", MaxMessageLength)
|
||||
got, err := SanitizeMessageText(exact)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error at exact max length: %v", err)
|
||||
}
|
||||
if len(got) != MaxMessageLength {
|
||||
t.Fatalf("expected length %d, got %d", MaxMessageLength, len(got))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user