Files
Home-Vue-Go/internal/api/validation_test.go
T

43 lines
1.2 KiB
Go

package api
import "testing"
func TestURLValidation(t *testing.T) {
for _, value := range []string{"https://example.com", "http://localhost:8080"} {
if !validHTTPURL(value) {
t.Errorf("expected valid site URL: %s", value)
}
}
for _, value := range []string{"javascript:alert(1)", "//example.com", "example.com"} {
if validHTTPURL(value) {
t.Errorf("expected invalid site URL: %s", value)
}
}
for _, value := range []string{"mailto:a@example.com", "tel:+8613800000000", "https://example.com"} {
if !validContactURL(value) {
t.Errorf("expected valid contact URL: %s", value)
}
}
for _, value := range []string{"/uploads/qr.png", "https://example.com/qr.png"} {
if !validQRCode(value) {
t.Errorf("expected valid QR path: %s", value)
}
}
for _, value := range []string{"/uploads/../secret.png", "data:image/png;base64,abc"} {
if validQRCode(value) {
t.Errorf("expected invalid QR path: %s", value)
}
}
}
func TestPercentageCountsSumsToOneHundred(t *testing.T) {
values := percentageCounts(map[string]int{"direct": 1, "search": 1, "other": 1}, 3)
total := 0
for _, value := range values {
total += value["value"].(int)
}
if total != 100 {
t.Fatalf("percentage total = %d, want 100", total)
}
}