safe_test.go (3990B)
1 // Copyright 2017 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package safe 15 16 import ( 17 "html/template" 18 "testing" 19 20 qt "github.com/frankban/quicktest" 21 ) 22 23 type tstNoStringer struct{} 24 25 func TestCSS(t *testing.T) { 26 t.Parallel() 27 c := qt.New(t) 28 29 ns := New() 30 31 for _, test := range []struct { 32 a any 33 expect any 34 }{ 35 {`a[href =~ "//example.com"]#foo`, template.CSS(`a[href =~ "//example.com"]#foo`)}, 36 // errors 37 {tstNoStringer{}, false}, 38 } { 39 40 result, err := ns.CSS(test.a) 41 42 if b, ok := test.expect.(bool); ok && !b { 43 c.Assert(err, qt.Not(qt.IsNil)) 44 continue 45 } 46 47 c.Assert(err, qt.IsNil) 48 c.Assert(result, qt.Equals, test.expect) 49 } 50 } 51 52 func TestHTML(t *testing.T) { 53 t.Parallel() 54 c := qt.New(t) 55 56 ns := New() 57 58 for _, test := range []struct { 59 a any 60 expect any 61 }{ 62 {`Hello, <b>World</b> &tc!`, template.HTML(`Hello, <b>World</b> &tc!`)}, 63 // errors 64 {tstNoStringer{}, false}, 65 } { 66 67 result, err := ns.HTML(test.a) 68 69 if b, ok := test.expect.(bool); ok && !b { 70 c.Assert(err, qt.Not(qt.IsNil)) 71 continue 72 } 73 74 c.Assert(err, qt.IsNil) 75 c.Assert(result, qt.Equals, test.expect) 76 } 77 } 78 79 func TestHTMLAttr(t *testing.T) { 80 t.Parallel() 81 c := qt.New(t) 82 83 ns := New() 84 85 for _, test := range []struct { 86 a any 87 expect any 88 }{ 89 {` dir="ltr"`, template.HTMLAttr(` dir="ltr"`)}, 90 // errors 91 {tstNoStringer{}, false}, 92 } { 93 result, err := ns.HTMLAttr(test.a) 94 95 if b, ok := test.expect.(bool); ok && !b { 96 c.Assert(err, qt.Not(qt.IsNil)) 97 continue 98 } 99 100 c.Assert(err, qt.IsNil) 101 c.Assert(result, qt.Equals, test.expect) 102 } 103 } 104 105 func TestJS(t *testing.T) { 106 t.Parallel() 107 c := qt.New(t) 108 109 ns := New() 110 111 for _, test := range []struct { 112 a any 113 expect any 114 }{ 115 {`c && alert("Hello, World!");`, template.JS(`c && alert("Hello, World!");`)}, 116 // errors 117 {tstNoStringer{}, false}, 118 } { 119 120 result, err := ns.JS(test.a) 121 122 if b, ok := test.expect.(bool); ok && !b { 123 c.Assert(err, qt.Not(qt.IsNil)) 124 continue 125 } 126 127 c.Assert(err, qt.IsNil) 128 c.Assert(result, qt.Equals, test.expect) 129 } 130 } 131 132 func TestJSStr(t *testing.T) { 133 t.Parallel() 134 c := qt.New(t) 135 136 ns := New() 137 138 for _, test := range []struct { 139 a any 140 expect any 141 }{ 142 {`Hello, World & O'Reilly\x21`, template.JSStr(`Hello, World & O'Reilly\x21`)}, 143 // errors 144 {tstNoStringer{}, false}, 145 } { 146 147 result, err := ns.JSStr(test.a) 148 149 if b, ok := test.expect.(bool); ok && !b { 150 c.Assert(err, qt.Not(qt.IsNil)) 151 continue 152 } 153 154 c.Assert(err, qt.IsNil) 155 c.Assert(result, qt.Equals, test.expect) 156 } 157 } 158 159 func TestURL(t *testing.T) { 160 t.Parallel() 161 c := qt.New(t) 162 163 ns := New() 164 165 for _, test := range []struct { 166 a any 167 expect any 168 }{ 169 {`greeting=H%69&addressee=(World)`, template.URL(`greeting=H%69&addressee=(World)`)}, 170 // errors 171 {tstNoStringer{}, false}, 172 } { 173 174 result, err := ns.URL(test.a) 175 176 if b, ok := test.expect.(bool); ok && !b { 177 c.Assert(err, qt.Not(qt.IsNil)) 178 continue 179 } 180 181 c.Assert(err, qt.IsNil) 182 c.Assert(result, qt.Equals, test.expect) 183 } 184 } 185 186 func TestSanitizeURL(t *testing.T) { 187 t.Parallel() 188 c := qt.New(t) 189 190 ns := New() 191 192 for _, test := range []struct { 193 a any 194 expect any 195 }{ 196 {"http://foo/../../bar", "http://foo/bar"}, 197 // errors 198 {tstNoStringer{}, false}, 199 } { 200 201 result, err := ns.SanitizeURL(test.a) 202 203 if b, ok := test.expect.(bool); ok && !b { 204 c.Assert(err, qt.Not(qt.IsNil)) 205 continue 206 } 207 208 c.Assert(err, qt.IsNil) 209 c.Assert(result, qt.Equals, test.expect) 210 } 211 }