github.com/SDLMoe/hugo@v0.47.1/tpl/safe/safe_test.go (about) 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 "fmt" 18 "html/template" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 ) 24 25 type tstNoStringer struct{} 26 27 func TestCSS(t *testing.T) { 28 t.Parallel() 29 30 ns := New() 31 32 for i, test := range []struct { 33 a interface{} 34 expect interface{} 35 }{ 36 {`a[href =~ "//example.com"]#foo`, template.CSS(`a[href =~ "//example.com"]#foo`)}, 37 // errors 38 {tstNoStringer{}, false}, 39 } { 40 errMsg := fmt.Sprintf("[%d] %v", i, test) 41 42 result, err := ns.CSS(test.a) 43 44 if b, ok := test.expect.(bool); ok && !b { 45 require.Error(t, err, errMsg) 46 continue 47 } 48 49 require.NoError(t, err, errMsg) 50 assert.Equal(t, test.expect, result, errMsg) 51 } 52 } 53 54 func TestHTML(t *testing.T) { 55 t.Parallel() 56 57 ns := New() 58 59 for i, test := range []struct { 60 a interface{} 61 expect interface{} 62 }{ 63 {`Hello, <b>World</b> &tc!`, template.HTML(`Hello, <b>World</b> &tc!`)}, 64 // errors 65 {tstNoStringer{}, false}, 66 } { 67 errMsg := fmt.Sprintf("[%d] %v", i, test) 68 69 result, err := ns.HTML(test.a) 70 71 if b, ok := test.expect.(bool); ok && !b { 72 require.Error(t, err, errMsg) 73 continue 74 } 75 76 require.NoError(t, err, errMsg) 77 assert.Equal(t, test.expect, result, errMsg) 78 } 79 } 80 81 func TestHTMLAttr(t *testing.T) { 82 t.Parallel() 83 84 ns := New() 85 86 for i, test := range []struct { 87 a interface{} 88 expect interface{} 89 }{ 90 {` dir="ltr"`, template.HTMLAttr(` dir="ltr"`)}, 91 // errors 92 {tstNoStringer{}, false}, 93 } { 94 errMsg := fmt.Sprintf("[%d] %v", i, test) 95 96 result, err := ns.HTMLAttr(test.a) 97 98 if b, ok := test.expect.(bool); ok && !b { 99 require.Error(t, err, errMsg) 100 continue 101 } 102 103 require.NoError(t, err, errMsg) 104 assert.Equal(t, test.expect, result, errMsg) 105 } 106 } 107 108 func TestJS(t *testing.T) { 109 t.Parallel() 110 111 ns := New() 112 113 for i, test := range []struct { 114 a interface{} 115 expect interface{} 116 }{ 117 {`c && alert("Hello, World!");`, template.JS(`c && alert("Hello, World!");`)}, 118 // errors 119 {tstNoStringer{}, false}, 120 } { 121 errMsg := fmt.Sprintf("[%d] %v", i, test) 122 123 result, err := ns.JS(test.a) 124 125 if b, ok := test.expect.(bool); ok && !b { 126 require.Error(t, err, errMsg) 127 continue 128 } 129 130 require.NoError(t, err, errMsg) 131 assert.Equal(t, test.expect, result, errMsg) 132 } 133 } 134 135 func TestJSStr(t *testing.T) { 136 t.Parallel() 137 138 ns := New() 139 140 for i, test := range []struct { 141 a interface{} 142 expect interface{} 143 }{ 144 {`Hello, World & O'Reilly\x21`, template.JSStr(`Hello, World & O'Reilly\x21`)}, 145 // errors 146 {tstNoStringer{}, false}, 147 } { 148 errMsg := fmt.Sprintf("[%d] %v", i, test) 149 150 result, err := ns.JSStr(test.a) 151 152 if b, ok := test.expect.(bool); ok && !b { 153 require.Error(t, err, errMsg) 154 continue 155 } 156 157 require.NoError(t, err, errMsg) 158 assert.Equal(t, test.expect, result, errMsg) 159 } 160 } 161 162 func TestURL(t *testing.T) { 163 t.Parallel() 164 165 ns := New() 166 167 for i, test := range []struct { 168 a interface{} 169 expect interface{} 170 }{ 171 {`greeting=H%69&addressee=(World)`, template.URL(`greeting=H%69&addressee=(World)`)}, 172 // errors 173 {tstNoStringer{}, false}, 174 } { 175 errMsg := fmt.Sprintf("[%d] %v", i, test) 176 177 result, err := ns.URL(test.a) 178 179 if b, ok := test.expect.(bool); ok && !b { 180 require.Error(t, err, errMsg) 181 continue 182 } 183 184 require.NoError(t, err, errMsg) 185 assert.Equal(t, test.expect, result, errMsg) 186 } 187 } 188 189 func TestSanitizeURL(t *testing.T) { 190 t.Parallel() 191 192 ns := New() 193 194 for i, test := range []struct { 195 a interface{} 196 expect interface{} 197 }{ 198 {"http://foo/../../bar", "http://foo/bar"}, 199 // errors 200 {tstNoStringer{}, false}, 201 } { 202 errMsg := fmt.Sprintf("[%d] %v", i, test) 203 204 result, err := ns.SanitizeURL(test.a) 205 206 if b, ok := test.expect.(bool); ok && !b { 207 require.Error(t, err, errMsg) 208 continue 209 } 210 211 require.NoError(t, err, errMsg) 212 assert.Equal(t, test.expect, result, errMsg) 213 } 214 }