-
Notifications
You must be signed in to change notification settings - Fork 0
/
goutil_test.go
39 lines (30 loc) · 1.39 KB
/
goutil_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package goutil
import (
"errors"
"testing"
"github.com/AspieSoft/go-regex-re2/v2"
)
func Test(t *testing.T){
// this test function is for debugging
}
func TestBasic(t *testing.T){
if val := toString[string](0); val != "0" {
t.Error("[", val, "]\n", errors.New("toString Method Failed"))
}
if val := toNumber[int]("1"); val != 1 {
t.Error("[", val, "]\n", errors.New("toInt Method Failed"))
}
if val := ToType[float64]("5.0"); val != 5.0 {
t.Error("[", val, "]\n", errors.New("ToType[float64] Method Failed"))
}
if args := MapArgs([]string{"arg1", "--key=value", "--bool", "-flags"}); args["0"] != "arg1" || args["bool"] != "true" || args["key"] != "value" || args["f"] != "true" || args["l"] != "true" || args["s"] != "true" {
t.Error(args, "\n", errors.New("MapArgs Produced The Wrong Output"))
}
}
func TestHtmlEscape(t *testing.T) {
html := regex.JoinBytes([]byte("<a href=\""), HTML.EscapeArgs([]byte(`test 1\\" attr="hack" null="`), '"'), '"', []byte("js=\""), HTML.EscapeArgs([]byte("this.media='all' `line \\n break`"), '"'), []byte("\">"), HTML.Escape([]byte(`<html>element & & &amp; &bad; test</html>`)), []byte("</a>"))
html = regex.Comp(`href="(?:\\[\"]|.)*?"`).RepStrLit(html, []byte{})
if regex.Comp(`(hack|<html>|&bad;|&amp;|\\\\n|\\')`).Match(html) {
t.Error(errors.New("'EscapeHTML' and/or 'EscapeHTMLArgs' method failed to prevent a test html hack properly"))
}
}