github.com/krum110487/go-htaccess@v0.0.0-20240316004156-60641c8e7598/tests/parser/testCaseParser_test.go (about) 1 package parser 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 ) 8 9 func init() { 10 11 } 12 13 func testMatch(t *testing.T, tests [][]string) { 14 failed := false 15 for i, test := range tests { 16 name := fmt.Sprintf("Test %d", i) 17 src := test[0] 18 res := fmt.Sprintf(`%s`, test[1]) 19 20 hta, err := TestParser.ParseString(name, src) 21 if err != nil { 22 panic(err) 23 } 24 25 data, _ := json.Marshal(hta) 26 jsonStr := string(data) 27 if jsonStr != res { 28 failed = true 29 t.Logf("Test %d failed to match:\n\tSource: %s\n\tParsedStr: %s\n\tExpected: %s", 30 i+1, src, jsonStr, res) 31 } 32 } 33 34 if failed { 35 t.FailNow() 36 } 37 } 38 39 func TestParserTestName(t *testing.T) { 40 tests := [][]string{ 41 {`#test-name test1 42 test 1 data here! 43 # test-name test2`, 44 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"test1","Setup":null,"Request":null,"Response":null,"Cleanup":null},{"Name":"test2","Setup":null,"Request":null,"Response":null,"Cleanup":null}],"Cleanup":null}`}, 45 } 46 47 testMatch(t, tests) 48 } 49 50 func TestParserOneTest(t *testing.T) { 51 tests := [][]string{ 52 {`# test-name SpecificTestNameHere 53 # setup-makeFile ${TEST_PATH}/pass.html 54 # request-url ${TEST_URL}/Some/Madeup/Path?Query=yes 55 # request-url 56 # other comment to be ignored! 57 RewriteCond $1 ^/Some/Madeup/Path$ 58 RewriteRule ^/Some/Madeup/Path$ /pass.html`, 59 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"SpecificTestNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path?Query=yes","MultiLineValue":""},{"Operation":"request-url","Parameter":"","MultiLineValue":""}],"Response":null,"Cleanup":null}],"Cleanup":null}`}, 60 } 61 62 testMatch(t, tests) 63 } 64 65 func TestParserTwoTests(t *testing.T) { 66 tests := [][]string{ 67 {`# test-name Test1 68 # setup-makeFile ${TEST_PATH}/pass.html 69 # request-url ${TEST_URL}/Some/Madeup/Path?Query=yes 70 # other comment to be ignored! 71 RewriteCond $1 ^/Some/Madeup/Path$ 72 RewriteRule ^/Some/Madeup/Path$ /pass.html 73 74 # test-name Test2 75 # setup-makeFile ${TEST_PATH}/pass2.html 76 # request-url ${TEST_URL}/Some/Madeup/Path2?Query=yes 77 # other comment to be ignored! 78 RewriteCond $1 ^/Some/Madeup/Path2$ 79 RewriteRule ^/Some/Madeup/Path2$ /pass2.html`, 80 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"Test1","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path?Query=yes","MultiLineValue":""}],"Response":null,"Cleanup":null},{"Name":"Test2","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass2.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path2?Query=yes","MultiLineValue":""}],"Response":null,"Cleanup":null}],"Cleanup":null}`}, 81 } 82 83 testMatch(t, tests) 84 } 85 86 func TestParserMultiLine(t *testing.T) { 87 tests := [][]string{ 88 {`# test-name TEST-MultiLine 89 # response-status 200 90 # response-header HeaderValue==MustMatchThis 91 # response-cookie CookieValue==MustMatchThis 92 # response-body pass 93 ## { 94 ## "canAlso": "#look ##like ;this ==if =you want", 95 ## "but for": "rewrites, I only care that 'pass' is returned" 96 ## } 97 RewriteCond $1 ^/Some/Madeup/Path$ 98 RewriteRule ^/Some/Madeup/Path$ /pass.html`, 99 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"TEST-MultiLine","Setup":null,"Request":null,"Response":[{"Operation":"response-status","Parameter":"200","MultiLineValue":""},{"Operation":"response-header","Parameter":"HeaderValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-cookie","Parameter":"CookieValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-body","Parameter":"pass","MultiLineValue":"{\n\t\"canAlso\": \"#look ##like ;this ==if =you want\",\n\"but for\": \"rewrites, I only care that 'pass' is returned\"\n}"}],"Cleanup":null}],"Cleanup":null}`}, 100 } 101 102 testMatch(t, tests) 103 } 104 105 func TestParserMultipleParams(t *testing.T) { 106 tests := [][]string{ 107 {`# test-name SpecificTestNameHere 108 # setup-makeFile ${TEST_PATH}/pass.html 109 # request-url ${TEST_URL}/Some/Madeup/Path?Query=yes 110 # request-header Header1 = HeaderVal1; Header2 = HeaderVal2 111 RewriteCond $1 ^/Some/Madeup/Path$ 112 RewriteRule ^/Some/Madeup/Path$ /pass.html`, 113 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"SpecificTestNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path?Query=yes","MultiLineValue":""},{"Operation":"request-header","Parameter":"Header1 = HeaderVal1; Header2 = HeaderVal2","MultiLineValue":""}],"Response":null,"Cleanup":null}],"Cleanup":null}`}, 114 } 115 116 testMatch(t, tests) 117 } 118 119 func TestIntermittenComments(t *testing.T) { 120 tests := [][]string{ 121 {`# comment at the top of the file that doesn't do shit 122 # test-name SpecificTestNameHere 123 # another comment to ignore 124 # setup-makeFile ${TEST_PATH}/pass.html 125 # another comment to ignore 126 # request-url ${TEST_URL}/Some/Madeup/Path?Query=yes 127 # another comment to ignore 128 # request-header Header1 = HeaderVal1; Header2 = HeaderVal2 129 # other comment to be ignored! 130 RewriteCond $1 ^/Some/Madeup/Path$ 131 RewriteRule ^/Some/Madeup/Path$ /pass.html 132 # another comment to ignore`, 133 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"SpecificTestNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path?Query=yes","MultiLineValue":""},{"Operation":"request-header","Parameter":"Header1 = HeaderVal1; Header2 = HeaderVal2","MultiLineValue":""}],"Response":null,"Cleanup":null}],"Cleanup":null}`}, 134 } 135 136 testMatch(t, tests) 137 } 138 139 func TestMalformedMultiLine1(t *testing.T) { 140 tests := [][]string{ 141 {`# test-name TEST-MultiLine 142 # response-status 200 143 # response-header HeaderValue==MustMatchThis 144 # response-cookie CookieValue==MustMatchThis 145 # response-body pass 146 ## { 147 148 ## "canAlso": "#look ##like ;this ==if =you want", 149 ## "but for": "rewrites, I only care that 'pass' is returned" 150 ## } 151 RewriteCond $1 ^/Some/Madeup/Path$ 152 RewriteRule ^/Some/Madeup/Path$ /pass.html`, 153 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"TEST-MultiLine","Setup":null,"Request":null,"Response":[{"Operation":"response-status","Parameter":"200","MultiLineValue":""},{"Operation":"response-header","Parameter":"HeaderValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-cookie","Parameter":"CookieValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-body","Parameter":"pass","MultiLineValue":"{\n\t\"canAlso\": \"#look ##like ;this ==if =you want\",\n\"but for\": \"rewrites, I only care that 'pass' is returned\"\n}"}],"Cleanup":null}],"Cleanup":null}`}, 154 } 155 156 testMatch(t, tests) 157 } 158 159 func TestMalformedMultiLine2(t *testing.T) { 160 tests := [][]string{ 161 {`# test-name TEST-MultiLine 162 # response-status 200 163 # response-header HeaderValue==MustMatchThis 164 # response-cookie CookieValue==MustMatchThis 165 # response-body pass 166 ## { 167 ## "canAlso": "#look ##like ;this ==if =you want", 168 ## "but for": "rewrites, I only care that 'pass' is returned" 169 ## } 170 RewriteCond $1 ^/Some/Madeup/Path$ 171 RewriteRule ^/Some/Madeup/Path$ /pass.html`, 172 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"TEST-MultiLine","Setup":null,"Request":null,"Response":[{"Operation":"response-status","Parameter":"200","MultiLineValue":""},{"Operation":"response-header","Parameter":"HeaderValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-cookie","Parameter":"CookieValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-body","Parameter":"pass","MultiLineValue":"{\n\t\"canAlso\": \"#look ##like ;this ==if =you want\",\n\"but for\": \"rewrites, I only care that 'pass' is returned\"\n}"}],"Cleanup":null}],"Cleanup":null}`}, 173 } 174 175 testMatch(t, tests) 176 } 177 178 func TestMalformedMultiLine3(t *testing.T) { 179 tests := [][]string{ 180 {`# test-name TEST-MultiLine 181 # response-status 200 182 # response-header HeaderValue==MustMatchThis 183 # response-cookie CookieValue==MustMatchThis 184 # response-body 185 ## { 186 ## "canAlso": "#look ##like ;this ==if =you want", 187 ## "but for": "rewrites, I only care that 'pass' is returned" 188 ## } 189 RewriteCond $1 ^/Some/Madeup/Path$ 190 RewriteRule ^/Some/Madeup/Path$ /pass.html`, 191 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"TEST-MultiLine","Setup":null,"Request":null,"Response":[{"Operation":"response-status","Parameter":"200","MultiLineValue":""},{"Operation":"response-header","Parameter":"HeaderValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-cookie","Parameter":"CookieValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-body","Parameter":"","MultiLineValue":"{\n\t\"canAlso\": \"#look ##like ;this ==if =you want\",\n\"but for\": \"rewrites, I only care that 'pass' is returned\"\n}"}],"Cleanup":null}],"Cleanup":null}`}, 192 } 193 194 testMatch(t, tests) 195 } 196 197 func TestMalformedMultiLine4(t *testing.T) { 198 tests := [][]string{ 199 {`# test-name TEST-MultiLine 200 # response-status 200 201 # response-header HeaderValue==MustMatchThis 202 # response-cookie CookieValue==MustMatchThis 203 # response-body 204 ## 205 RewriteCond $1 ^/Some/Madeup/Path$ 206 RewriteRule ^/Some/Madeup/Path$ /pass.html`, 207 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":null,"Setup":null,"TestList":[{"Name":"TEST-MultiLine","Setup":null,"Request":null,"Response":[{"Operation":"response-status","Parameter":"200","MultiLineValue":""},{"Operation":"response-header","Parameter":"HeaderValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-cookie","Parameter":"CookieValue==MustMatchThis","MultiLineValue":""},{"Operation":"response-body","Parameter":"","MultiLineValue":""}],"Cleanup":null}],"Cleanup":null}`}, 208 } 209 210 testMatch(t, tests) 211 } 212 213 func TestParserCollection(t *testing.T) { 214 tests := [][]string{ 215 {`# collection-name CollectionNameHere 216 # setup-makeFile ${TEST_PATH}/pass.html 217 218 # test-name SpecificTestNameHere 219 # setup-makeFile ${TEST_PATH}/pass.html 220 # request-url ${TEST_URL}/Some/Madeup/Path?Query=yes 221 # request-url 222 # other comment to be ignored! 223 RewriteCond $1 ^/Some/Madeup/Path$ 224 RewriteRule ^/Some/Madeup/Path$ /pass.html`, 225 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":"CollectionNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"TestList":[{"Name":"SpecificTestNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path?Query=yes","MultiLineValue":""},{"Operation":"request-url","Parameter":"","MultiLineValue":""}],"Response":null,"Cleanup":null}],"Cleanup":null}`}, 226 } 227 228 testMatch(t, tests) 229 } 230 231 func TestParserCollectionCleanup(t *testing.T) { 232 tests := [][]string{ 233 {`# collection-name CollectionNameHere 234 # setup-makeFile ${TEST_PATH}/pass.html 235 236 # test-name SpecificTestNameHere 237 # setup-makeFile ${TEST_PATH}/pass.html 238 # request-url ${TEST_URL}/Some/Madeup/Path?Query=yes 239 # request-url 240 # other comment to be ignored! 241 RewriteCond $1 ^/Some/Madeup/Path$ 242 RewriteRule ^/Some/Madeup/Path$ /pass.html 243 244 # collection-cleanup 245 # cleanup-delFile ${TEST_PATH}/pass.html`, 246 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":"CollectionNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"TestList":[{"Name":"SpecificTestNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path?Query=yes","MultiLineValue":""},{"Operation":"request-url","Parameter":"","MultiLineValue":""}],"Response":null,"Cleanup":null}],"Cleanup":[{"Operation":"cleanup-delFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}]}`}, 247 } 248 249 testMatch(t, tests) 250 } 251 252 func TestParserTestCleanup(t *testing.T) { 253 tests := [][]string{ 254 {`# collection-name CollectionNameHere 255 # setup-makeFile ${TEST_PATH}/pass.html 256 257 # test-name SpecificTestNameHere 258 # setup-makeFile ${TEST_PATH}/pass.html 259 # request-url ${TEST_URL}/Some/Madeup/Path?Query=yes 260 # request-url 261 # other comment to be ignored! 262 RewriteCond $1 ^/Some/Madeup/Path$ 263 RewriteRule ^/Some/Madeup/Path$ /pass.html 264 265 # comment at the end 266 # cleanup-delFile ${TEST_PATH}/pass.html 267 # comment at the end again`, 268 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":"CollectionNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"TestList":[{"Name":"SpecificTestNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path?Query=yes","MultiLineValue":""},{"Operation":"request-url","Parameter":"","MultiLineValue":""}],"Response":null,"Cleanup":[{"Operation":"cleanup-delFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}]}],"Cleanup":null}`}, 269 } 270 271 testMatch(t, tests) 272 } 273 274 func TestParserCollectionCleanupCommentAfter(t *testing.T) { 275 tests := [][]string{ 276 {`# collection-name CollectionNameHere 277 # setup-makeFile ${TEST_PATH}/pass.html 278 279 # test-name SpecificTestNameHere 280 # setup-makeFile ${TEST_PATH}/pass.html 281 # request-url ${TEST_URL}/Some/Madeup/Path?Query=yes 282 # request-url 283 # other comment to be ignored! 284 RewriteCond $1 ^/Some/Madeup/Path$ 285 RewriteRule ^/Some/Madeup/Path$ /pass.html 286 287 # collection-cleanup 288 # cleanup-delFile ${TEST_PATH}/pass.html 289 290 291 #EXAMPLE 292 #https://httpd.apache.org/docs/trunk/rewrite/intro.html 293 `, 294 `{"FolderPath":"","TEST_URL":"","TEST_PATH":"","Name":"CollectionNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"TestList":[{"Name":"SpecificTestNameHere","Setup":[{"Operation":"setup-makeFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}],"Request":[{"Operation":"request-url","Parameter":"${TEST_URL}/Some/Madeup/Path?Query=yes","MultiLineValue":""},{"Operation":"request-url","Parameter":"","MultiLineValue":""}],"Response":null,"Cleanup":null}],"Cleanup":[{"Operation":"cleanup-delFile","Parameter":"${TEST_PATH}/pass.html","MultiLineValue":""}]}`}, 295 } 296 297 testMatch(t, tests) 298 }