|
|
|
@ -7792,6 +7792,129 @@ func TestUniqueValidation(t *testing.T) { |
|
|
|
|
PanicMatches(t, func() { validate.Var(1.0, "unique") }, "Bad field type float64") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestHTMLValidation(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
param string |
|
|
|
|
expected bool |
|
|
|
|
}{ |
|
|
|
|
{"<html>", true}, |
|
|
|
|
{"<script>", true}, |
|
|
|
|
{"<stillworks>", true}, |
|
|
|
|
{"</html", false}, |
|
|
|
|
{"</script>", true}, |
|
|
|
|
{"<//script>", false}, |
|
|
|
|
{"<123nonsense>", false}, |
|
|
|
|
{"test", false}, |
|
|
|
|
{"&example", false}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
validate := New() |
|
|
|
|
|
|
|
|
|
for i, test := range tests { |
|
|
|
|
|
|
|
|
|
errs := validate.Var(test.param, "html") |
|
|
|
|
|
|
|
|
|
if test.expected { |
|
|
|
|
if !IsEqual(errs, nil) { |
|
|
|
|
t.Fatalf("Index: %d html failed Error: %v", i, errs) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if IsEqual(errs, nil) { |
|
|
|
|
t.Fatalf("Index: %d html failed Error: %v", i, errs) |
|
|
|
|
} else { |
|
|
|
|
val := getError(errs, "", "") |
|
|
|
|
if val.Tag() != "html" { |
|
|
|
|
t.Fatalf("Index: %d html failed Error: %v", i, errs) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestHTMLEncodedValidation(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
param string |
|
|
|
|
expected bool |
|
|
|
|
}{ |
|
|
|
|
{"<", true}, |
|
|
|
|
{"¯", true}, |
|
|
|
|
{"�", true}, |
|
|
|
|
{"ð", true}, |
|
|
|
|
{"<", true}, |
|
|
|
|
{"¯", true}, |
|
|
|
|
{"�", true}, |
|
|
|
|
{"ð", true}, |
|
|
|
|
{"&#ab", true}, |
|
|
|
|
{"<", true}, |
|
|
|
|
{">", true}, |
|
|
|
|
{""", true}, |
|
|
|
|
{"&", true}, |
|
|
|
|
{"#x0a", false}, |
|
|
|
|
{"&x00", false}, |
|
|
|
|
{"z", false}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
validate := New() |
|
|
|
|
|
|
|
|
|
for i, test := range tests { |
|
|
|
|
|
|
|
|
|
errs := validate.Var(test.param, "html_encoded") |
|
|
|
|
|
|
|
|
|
if test.expected { |
|
|
|
|
if !IsEqual(errs, nil) { |
|
|
|
|
t.Fatalf("Index: %d html_encoded failed Error: %v", i, errs) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if IsEqual(errs, nil) { |
|
|
|
|
t.Fatalf("Index: %d html_enocded failed Error: %v", i, errs) |
|
|
|
|
} else { |
|
|
|
|
val := getError(errs, "", "") |
|
|
|
|
if val.Tag() != "html_encoded" { |
|
|
|
|
t.Fatalf("Index: %d html_encoded failed Error: %v", i, errs) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestURLEncodedValidation(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
param string |
|
|
|
|
expected bool |
|
|
|
|
}{ |
|
|
|
|
{"%20", true}, |
|
|
|
|
{"%af", true}, |
|
|
|
|
{"%ff", true}, |
|
|
|
|
{"<%az", false}, |
|
|
|
|
{"%test%", false}, |
|
|
|
|
{"a%b", false}, |
|
|
|
|
{"1%2", false}, |
|
|
|
|
{"%%a%%", false}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
validate := New() |
|
|
|
|
|
|
|
|
|
for i, test := range tests { |
|
|
|
|
|
|
|
|
|
errs := validate.Var(test.param, "url_encoded") |
|
|
|
|
|
|
|
|
|
if test.expected { |
|
|
|
|
if !IsEqual(errs, nil) { |
|
|
|
|
t.Fatalf("Index: %d url_encoded failed Error: %v", i, errs) |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
if IsEqual(errs, nil) { |
|
|
|
|
t.Fatalf("Index: %d url_enocded failed Error: %v", i, errs) |
|
|
|
|
} else { |
|
|
|
|
val := getError(errs, "", "") |
|
|
|
|
if val.Tag() != "url_encoded" { |
|
|
|
|
t.Fatalf("Index: %d url_encoded failed Error: %v", i, errs) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestKeys(t *testing.T) { |
|
|
|
|
|
|
|
|
|
type Test struct { |
|
|
|
|