gohorsejobs/backend/internal/utils/sanitizer_test.go
Tiago Yamamoto 7934afcf0d docs: complete project documentation overhaul
- Add comprehensive root README with badges, architecture diagram, and setup guide
- Update backend README with security middlewares and endpoint documentation
- Update frontend README with design system and page structure
- Update seeder-api README with generated data and credentials
- Add internal module READMEs (middleware, handlers, components)
- Document Clean Architecture layers and request flow
- Add environment variables reference table
2025-12-09 19:36:36 -03:00

101 lines
2.3 KiB
Go

package utils
import (
"testing"
)
func TestSanitizeString(t *testing.T) {
s := DefaultSanitizer()
tests := []struct {
name string
input string
expected string
}{
{"simple text", "hello world", "hello world"},
{"with whitespace", " hello ", "hello"},
{"with html", "<script>alert('xss')</script>", "&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;"},
{"empty string", "", ""},
{"special chars", "café & thé", "café &amp; thé"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := s.SanitizeString(tt.input)
if result != tt.expected {
t.Errorf("SanitizeString(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestSanitizeSlug(t *testing.T) {
s := DefaultSanitizer()
tests := []struct {
name string
input string
expected string
}{
{"simple text", "Hello World", "hello-world"},
{"special chars", "Café & Thé!", "caf-th"},
{"multiple spaces", "hello world", "hello-world"},
{"already slug", "hello-world", "hello-world"},
{"numbers", "test 123", "test-123"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := s.SanitizeSlug(tt.input)
if result != tt.expected {
t.Errorf("SanitizeSlug(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestSanitizeName(t *testing.T) {
s := DefaultSanitizer()
s.MaxNameLength = 10
tests := []struct {
name string
input string
expected string
}{
{"short name", "John", "John"},
{"max length", "1234567890", "1234567890"},
{"over limit", "12345678901", "1234567890"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := s.SanitizeName(tt.input)
if result != tt.expected {
t.Errorf("SanitizeName(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestStripHTML(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{"simple html", "<p>hello</p>", "hello"},
{"script tag", "<script>alert('xss')</script>", "alert('xss')"},
{"nested tags", "<div><span>text</span></div>", "text"},
{"no html", "plain text", "plain text"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := StripHTML(tt.input)
if result != tt.expected {
t.Errorf("StripHTML(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}