test_URLPattern_matchURLPattern.js (12409B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const { matchURLPattern, parseURLPattern } = ChromeUtils.importESModule( 6 "chrome://remote/content/shared/webdriver/URLPattern.sys.mjs" 7 ); 8 9 // Test several variations which should match a string based http://example.com 10 // pattern. 11 add_task(async function test_matchURLPattern_url_variations() { 12 const pattern = parseURLPattern({ 13 type: "string", 14 pattern: "http://example.com", 15 }); 16 17 const urls = [ 18 "http://example.com", 19 "http://EXAMPLE.com", 20 "http://user:password@example.com", 21 "http://example.com:80", 22 "http://example.com/", 23 "http://example.com/#some-hash", 24 "http:example.com", 25 "http:/example.com", 26 "http://example.com?", 27 "http://example.com/?", 28 ]; 29 for (const url of urls) { 30 ok( 31 matchURLPattern(pattern, url), 32 `url "${url}" should match pattern "http://example.com"` 33 ); 34 } 35 36 // Test URLs close to http://example.com but which should not match. 37 const failingUrls = [ 38 "https://example.com", 39 "http://example.com:88", 40 "http://example.com/a", 41 "http://example.com/?abc", 42 ]; 43 for (const url of failingUrls) { 44 ok( 45 !matchURLPattern(pattern, url), 46 `url "${url}" should not match pattern "http://example.com"` 47 ); 48 } 49 }); 50 51 add_task(async function test_matchURLPattern_stringPatterns() { 52 const tests = [ 53 { 54 pattern: "http://example.com", 55 url: "http://example.com", 56 match: true, 57 }, 58 { 59 pattern: "HTTP://example.com:80", 60 url: "http://example.com", 61 match: true, 62 }, 63 { 64 pattern: "http://example.com:80", 65 url: "http://example.com", 66 match: true, 67 }, 68 { 69 pattern: "http://example.com/path", 70 url: "http://example.com/path", 71 match: true, 72 }, 73 { 74 pattern: "http://example.com/PATH_CASE", 75 url: "http://example.com/path_case", 76 match: false, 77 }, 78 { 79 pattern: "http://example.com/path_single_segment", 80 url: "http://example.com/path_single_segment/", 81 match: false, 82 }, 83 { 84 pattern: "http://example.com/path", 85 url: "http://example.com/path_continued", 86 match: false, 87 }, 88 { 89 pattern: "http://example.com/path_two_segments/", 90 url: "http://example.com/path_two_segments/", 91 match: true, 92 }, 93 { 94 pattern: "http://example.com/path_two_segments/", 95 url: "http://example.com/path_two_segments", 96 match: false, 97 }, 98 { 99 pattern: "http://example.com/emptysearch?", 100 url: "http://example.com/emptysearch?", 101 match: true, 102 }, 103 { 104 pattern: "http://example.com/emptysearch?", 105 url: "http://example.com/emptysearch", 106 match: true, 107 }, 108 { 109 pattern: "http://example.com/emptysearch?", 110 url: "http://example.com/emptysearch??", 111 match: false, 112 }, 113 { 114 pattern: "http://example.com/emptysearch?", 115 url: "http://example.com/emptysearch?a", 116 match: false, 117 }, 118 { 119 pattern: "http://example.com/search?param", 120 url: "http://example.com/search?param", 121 match: true, 122 }, 123 { 124 pattern: "http://example.com/search?param", 125 url: "http://example.com/search?param=value", 126 match: false, 127 }, 128 { 129 pattern: "http://example.com/search?param=value", 130 url: "http://example.com/search?param=value", 131 match: true, 132 }, 133 { 134 pattern: "http://example.com/search?a=b&c=d", 135 url: "http://example.com/search?a=b&c=d", 136 match: true, 137 }, 138 { 139 pattern: "http://example.com/search?a=b&c=d", 140 url: "http://example.com/search?c=d&a=b", 141 match: false, 142 }, 143 { 144 pattern: "http://example.com/search?param", 145 url: "http://example.com/search?param#ref", 146 match: true, 147 }, 148 { 149 pattern: "http://example.com/search?param#ref", 150 url: "http://example.com/search?param#ref", 151 match: true, 152 }, 153 { 154 pattern: "http://example.com/search?param#ref", 155 url: "http://example.com/search?param", 156 match: true, 157 }, 158 { 159 pattern: "http://example.com/search?param", 160 url: "http://example.com/search?parameter", 161 match: false, 162 }, 163 { 164 pattern: "http://example.com/search?parameter", 165 url: "http://example.com/search?param", 166 match: false, 167 }, 168 { 169 pattern: "https://example.com:80", 170 url: "https://example.com", 171 match: false, 172 }, 173 { 174 pattern: "https://example.com:443", 175 url: "https://example.com", 176 match: true, 177 }, 178 { 179 pattern: "ws://example.com", 180 url: "ws://example.com:80", 181 match: true, 182 }, 183 ]; 184 185 runMatchPatternTests(tests, "string"); 186 }); 187 188 add_task(async function test_patternPatterns_no_property() { 189 const tests = [ 190 // Test protocol 191 { 192 pattern: {}, 193 url: "https://example.com", 194 match: true, 195 }, 196 { 197 pattern: {}, 198 url: "https://example.com", 199 match: true, 200 }, 201 { 202 pattern: {}, 203 url: "https://example.com:1234", 204 match: true, 205 }, 206 { 207 pattern: {}, 208 url: "https://example.com/a", 209 match: true, 210 }, 211 { 212 pattern: {}, 213 url: "https://example.com/a?test", 214 match: true, 215 }, 216 ]; 217 218 runMatchPatternTests(tests, "pattern"); 219 }); 220 221 add_task(async function test_patternPatterns_protocol() { 222 const tests = [ 223 // Test protocol 224 { 225 pattern: { 226 protocol: "http", 227 }, 228 url: "http://example.com", 229 match: true, 230 }, 231 { 232 pattern: { 233 protocol: "HTTP", 234 }, 235 url: "http://example.com", 236 match: true, 237 }, 238 { 239 pattern: { 240 protocol: "http", 241 }, 242 url: "http://example.com:80", 243 match: true, 244 }, 245 { 246 pattern: { 247 protocol: "http", 248 }, 249 url: "http://example.com:1234", 250 match: true, 251 }, 252 { 253 pattern: { 254 protocol: "http", 255 port: "80", 256 }, 257 url: "http://example.com:80", 258 match: true, 259 }, 260 { 261 pattern: { 262 protocol: "http", 263 port: "1234", 264 }, 265 url: "http://example.com:1234", 266 match: true, 267 }, 268 { 269 pattern: { 270 protocol: "http", 271 port: "1234", 272 }, 273 url: "http://example.com", 274 match: false, 275 }, 276 { 277 pattern: { 278 protocol: "http", 279 }, 280 url: "https://wrong-scheme.com", 281 match: false, 282 }, 283 { 284 pattern: { 285 protocol: "http", 286 }, 287 url: "http://whatever.com/?search#ref", 288 match: true, 289 }, 290 { 291 pattern: { 292 protocol: "http", 293 }, 294 url: "http://example.com/a", 295 match: true, 296 }, 297 { 298 pattern: { 299 protocol: "http", 300 }, 301 url: "http://whatever.com/path?search#ref", 302 match: true, 303 }, 304 ]; 305 306 runMatchPatternTests(tests, "pattern"); 307 }); 308 309 add_task(async function test_patternPatterns_port() { 310 const tests = [ 311 { 312 pattern: { 313 protocol: "http", 314 port: "80", 315 }, 316 url: "http://abc.com/", 317 match: true, 318 }, 319 { 320 pattern: { 321 port: "1234", 322 }, 323 url: "http://a.com:1234", 324 match: true, 325 }, 326 { 327 pattern: { 328 port: "1234", 329 }, 330 url: "https://a.com:1234", 331 match: true, 332 }, 333 ]; 334 335 runMatchPatternTests(tests, "pattern"); 336 }); 337 338 add_task(async function test_patternPatterns_hostname() { 339 const tests = [ 340 { 341 pattern: { 342 hostname: "example.com", 343 }, 344 url: "http://example.com", 345 match: true, 346 }, 347 { 348 pattern: { 349 hostname: "example.com", 350 }, 351 url: "http://example.com:80", 352 match: true, 353 }, 354 { 355 pattern: { 356 hostname: "example.com", 357 }, 358 url: "https://example.com", 359 match: true, 360 }, 361 { 362 pattern: { 363 hostname: "example.com", 364 }, 365 url: "https://example.com:443", 366 match: true, 367 }, 368 { 369 pattern: { 370 hostname: "example.com", 371 }, 372 url: "ws://example.com", 373 match: true, 374 }, 375 { 376 pattern: { 377 hostname: "example.com", 378 }, 379 url: "ws://example.com:80", 380 match: true, 381 }, 382 { 383 pattern: { 384 hostname: "example.com", 385 }, 386 url: "http://example.com/path", 387 match: true, 388 }, 389 { 390 pattern: { 391 hostname: "example.com", 392 }, 393 url: "http://example.com/?search", 394 match: true, 395 }, 396 { 397 pattern: { 398 hostname: "example\\{.com", 399 }, 400 url: "http://example{.com/", 401 match: true, 402 }, 403 { 404 pattern: { 405 hostname: "example\\{.com", 406 }, 407 url: "http://example\\{.com/", 408 match: false, 409 }, 410 { 411 pattern: { 412 hostname: "127.0.0.1", 413 }, 414 url: "http://127.0.0.1/", 415 match: true, 416 }, 417 { 418 pattern: { 419 hostname: "127.0.0.1", 420 }, 421 url: "http://127.0.0.2/", 422 match: false, 423 }, 424 { 425 pattern: { 426 hostname: "[2001:db8::1]", 427 }, 428 url: "http://[2001:db8::1]/", 429 match: true, 430 }, 431 { 432 pattern: { 433 hostname: "[::AB:1]", 434 }, 435 url: "http://[::ab:1]/", 436 match: true, 437 }, 438 ]; 439 440 runMatchPatternTests(tests, "pattern"); 441 }); 442 443 add_task(async function test_patternPatterns_pathname() { 444 const tests = [ 445 { 446 pattern: { 447 pathname: "/", 448 }, 449 url: "http://example.com", 450 match: true, 451 }, 452 { 453 pattern: { 454 pathname: "/", 455 }, 456 url: "http://example.com/", 457 match: true, 458 }, 459 { 460 pattern: { 461 pathname: "/", 462 }, 463 url: "http://example.com/?", 464 match: true, 465 }, 466 { 467 pattern: { 468 pathname: "path", 469 }, 470 url: "http://example.com/path", 471 match: true, 472 }, 473 { 474 pattern: { 475 pathname: "/path", 476 }, 477 url: "http://example.com/path", 478 match: true, 479 }, 480 { 481 pattern: { 482 pathname: "path", 483 }, 484 url: "http://example.com/path/", 485 match: false, 486 }, 487 { 488 pattern: { 489 pathname: "path", 490 }, 491 url: "http://example.com/path_continued", 492 match: false, 493 }, 494 { 495 pattern: { 496 pathname: "/", 497 }, 498 url: "http://example.com/path", 499 match: false, 500 }, 501 ]; 502 503 runMatchPatternTests(tests, "pattern"); 504 }); 505 506 add_task(async function test_patternPatterns_search() { 507 const tests = [ 508 { 509 pattern: { 510 search: "", 511 }, 512 url: "http://example.com/?", 513 match: true, 514 }, 515 { 516 pattern: { 517 search: "", 518 }, 519 url: "http://example.com/", 520 match: true, 521 }, 522 { 523 pattern: { 524 search: "", 525 }, 526 url: "http://example.com/?#", 527 match: true, 528 }, 529 { 530 pattern: { 531 search: "?", 532 }, 533 url: "http://example.com/?", 534 match: true, 535 }, 536 { 537 pattern: { 538 search: "?a", 539 }, 540 url: "http://example.com/?a", 541 match: true, 542 }, 543 { 544 pattern: { 545 search: "?", 546 }, 547 url: "http://example.com/??", 548 match: false, 549 }, 550 { 551 pattern: { 552 search: "query", 553 }, 554 url: "http://example.com/?query", 555 match: true, 556 }, 557 { 558 pattern: { 559 search: "?query", 560 }, 561 url: "http://example.com/?query", 562 match: true, 563 }, 564 { 565 pattern: { 566 search: "query=value", 567 }, 568 url: "http://example.com/?query=value", 569 match: true, 570 }, 571 { 572 pattern: { 573 search: "query", 574 }, 575 url: "http://example.com/?query=value", 576 match: false, 577 }, 578 { 579 pattern: { 580 search: "query", 581 }, 582 url: "http://example.com/?query#value", 583 match: true, 584 }, 585 ]; 586 587 runMatchPatternTests(tests, "pattern"); 588 }); 589 590 function runMatchPatternTests(tests, type) { 591 for (const test of tests) { 592 let pattern; 593 if (type == "pattern") { 594 pattern = parseURLPattern({ type: "pattern", ...test.pattern }); 595 } else { 596 pattern = parseURLPattern({ type: "string", pattern: test.pattern }); 597 } 598 599 equal( 600 matchURLPattern(pattern, test.url), 601 test.match, 602 `url "${test.url}" ${ 603 test.match ? "should" : "should not" 604 } match pattern ${JSON.stringify(test.pattern)}` 605 ); 606 } 607 }