// Copyright (c) 2025 Probo Inc . // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. // // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR // PERFORMANCE OF THIS SOFTWARE. package validator import ( "strings" "testing" ) func TestNoHTML(t *testing.T) { t.Run("valid text without HTML", func(t *testing.T) { str := "This is a normal text" err := NoHTML()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid text with special characters", func(t *testing.T) { str := "Price: $10.99 - 20% off!" err := NoHTML()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid UTF-8 text", func(t *testing.T) { str := "José García 张伟" err := NoHTML()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("valid text with emojis", func(t *testing.T) { str := "Hello World 🌍" err := NoHTML()(&str) if err != nil { t.Errorf("expected no error, got: %v", err) } }) t.Run("invalid - script tag XSS", func(t *testing.T) { str := "" err := NoHTML()(&str) if err == nil { t.Fatal("expected validation error for script tag") } if !strings.Contains(err.Message, "HTML tags") { t.Errorf("unexpected error message: %s", err.Message) } }) t.Run("invalid - simple bold tag", func(t *testing.T) { str := "Hello World" err := NoHTML()(&str) if err == nil { t.Fatal("expected validation error for bold tag") } if !strings.Contains(err.Message, "HTML tags") { t.Errorf("unexpected error message: %s", err.Message) } }) t.Run("invalid - div tag", func(t *testing.T) { str := "
Content
" err := NoHTML()(&str) if err == nil { t.Error("expected validation error for div tag") } }) t.Run("invalid - self-closing tag", func(t *testing.T) { str := "Line break
here" err := NoHTML()(&str) if err == nil { t.Error("expected validation error for self-closing tag") } }) t.Run("invalid - img tag", func(t *testing.T) { str := `` err := NoHTML()(&str) if err == nil { t.Error("expected validation error for img tag") } }) t.Run("invalid - anchor tag", func(t *testing.T) { str := `Click` err := NoHTML()(&str) if err == nil { t.Error("expected validation error for anchor tag") } }) t.Run("invalid - less than symbol", func(t *testing.T) { str := "5 < 10" err := NoHTML()(&str) if err == nil { t.Fatal("expected validation error for angle bracket") } if !strings.Contains(err.Message, "angle brackets") { t.Errorf("unexpected error message: %s", err.Message) } }) t.Run("invalid - greater than symbol", func(t *testing.T) { str := "10 > 5" err := NoHTML()(&str) if err == nil { t.Error("expected validation error for angle bracket") } }) t.Run("invalid - both angle brackets", func(t *testing.T) { str := "5 < x > 10" err := NoHTML()(&str) if err == nil { t.Error("expected validation error for angle brackets") } }) t.Run("invalid - malformed tag", func(t *testing.T) { str := "text