test-match-pattern.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const { MatchPattern } = require("sdk/util/match-pattern");
  6. exports.testMatchPatternTestTrue = function(assert) {
  7. function ok(pattern, url) {
  8. let mp = new MatchPattern(pattern);
  9. assert.ok(mp.test(url), pattern + " should match " + url);
  10. }
  11. ok("*", "http://example.com");
  12. ok("*", "https://example.com");
  13. ok("*", "ftp://example.com");
  14. ok("*.example.com", "http://example.com");
  15. ok("*.example.com", "http://hamburger.example.com");
  16. ok("*.example.com", "http://hotdog.hamburger.example.com");
  17. ok("http://example.com*", "http://example.com");
  18. ok("http://example.com*", "http://example.com/");
  19. ok("http://example.com/*", "http://example.com/");
  20. ok("http://example.com/*", "http://example.com/potato-salad");
  21. ok("http://example.com/pickles/*", "http://example.com/pickles/");
  22. ok("http://example.com/pickles/*", "http://example.com/pickles/lemonade");
  23. ok("http://example.com", "http://example.com");
  24. ok("http://example.com/ice-cream", "http://example.com/ice-cream");
  25. ok(/.*zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
  26. ok(/https:.*zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
  27. ok('*.sample.com', 'http://ex.sample.com/foo.html');
  28. ok('*.amp.le.com', 'http://ex.amp.le.com');
  29. ok('data:*', 'data:text/html;charset=utf-8,');
  30. };
  31. exports.testMatchPatternTestFalse = function(assert) {
  32. function ok(pattern, url) {
  33. let mp = new MatchPattern(pattern);
  34. assert.ok(!mp.test(url), pattern + " should not match " + url);
  35. }
  36. ok("*", null);
  37. ok("*", "");
  38. ok("*", "bogus");
  39. ok("*", "chrome://browser/content/browser.xul");
  40. ok("*", "nttp://example.com");
  41. ok("*.example.com", null);
  42. ok("*.example.com", "");
  43. ok("*.example.com", "bogus");
  44. ok("*.example.com", "http://example.net");
  45. ok("*.example.com", "http://foo.com");
  46. ok("*.example.com", "http://example.com.foo");
  47. ok("*.example2.com", "http://example.com");
  48. ok("http://example.com/*", null);
  49. ok("http://example.com/*", "");
  50. ok("http://example.com/*", "bogus");
  51. ok("http://example.com/*", "http://example.com");
  52. ok("http://example.com/*", "http://foo.com/");
  53. ok("http://example.com", null);
  54. ok("http://example.com", "");
  55. ok("http://example.com", "bogus");
  56. ok("http://example.com", "http://example.com/");
  57. ok(/zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
  58. ok(/.*zilla/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
  59. ok(/.*Zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=655464"); // bug 655464
  60. ok(/https:.*zilla/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753");
  61. // bug 856913
  62. ok('*.ign.com', 'http://www.design.com');
  63. ok('*.ign.com', 'http://design.com');
  64. ok('*.zilla.com', 'http://bugzilla.mozilla.com');
  65. ok('*.zilla.com', 'http://mo-zilla.com');
  66. ok('*.amp.le.com', 'http://amp-le.com');
  67. ok('*.amp.le.com', 'http://examp.le.com');
  68. };
  69. exports.testMatchPatternErrors = function(assert) {
  70. assert.throws(
  71. function() new MatchPattern("*.google.com/*"),
  72. /There can be at most one/,
  73. "MatchPattern throws when supplied multiple '*'"
  74. );
  75. assert.throws(
  76. function() new MatchPattern("google.com"),
  77. /expected to be either an exact URL/,
  78. "MatchPattern throws when the wildcard doesn't use '*' and doesn't " +
  79. "look like a URL"
  80. );
  81. assert.throws(
  82. function() new MatchPattern("http://google*.com"),
  83. /expected to be the first or the last/,
  84. "MatchPattern throws when a '*' is in the middle of the wildcard"
  85. );
  86. assert.throws(
  87. function() new MatchPattern(/ /g),
  88. /^A RegExp match pattern cannot be set to `global` \(i\.e\. \/\/g\)\.$/,
  89. "MatchPattern throws on a RegExp set to `global` (i.e. //g)."
  90. );
  91. assert.throws(
  92. function() new MatchPattern(/ /i),
  93. /^A RegExp match pattern cannot be set to `ignoreCase` \(i\.e\. \/\/i\)\.$/,
  94. "MatchPattern throws on a RegExp set to `ignoreCase` (i.e. //i)."
  95. );
  96. assert.throws(
  97. function() new MatchPattern( / /m ),
  98. /^A RegExp match pattern cannot be set to `multiline` \(i\.e\. \/\/m\)\.$/,
  99. "MatchPattern throws on a RegExp set to `multiline` (i.e. //m)."
  100. );
  101. };
  102. exports.testMatchPatternInternals = function(assert) {
  103. assert.equal(
  104. new MatchPattern("http://google.com/test").exactURL,
  105. "http://google.com/test"
  106. );
  107. assert.equal(
  108. new MatchPattern("http://google.com/test/*").urlPrefix,
  109. "http://google.com/test/"
  110. );
  111. assert.equal(
  112. new MatchPattern("*.example.com").domain,
  113. "example.com"
  114. );
  115. };
  116. require('sdk/test').run(exports);