michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: const { MatchPattern } = require("sdk/util/match-pattern"); michael@0: michael@0: exports.testMatchPatternTestTrue = function(assert) { michael@0: function ok(pattern, url) { michael@0: let mp = new MatchPattern(pattern); michael@0: assert.ok(mp.test(url), pattern + " should match " + url); michael@0: } michael@0: michael@0: ok("*", "http://example.com"); michael@0: ok("*", "https://example.com"); michael@0: ok("*", "ftp://example.com"); michael@0: michael@0: ok("*.example.com", "http://example.com"); michael@0: ok("*.example.com", "http://hamburger.example.com"); michael@0: ok("*.example.com", "http://hotdog.hamburger.example.com"); michael@0: michael@0: ok("http://example.com*", "http://example.com"); michael@0: ok("http://example.com*", "http://example.com/"); michael@0: ok("http://example.com/*", "http://example.com/"); michael@0: ok("http://example.com/*", "http://example.com/potato-salad"); michael@0: ok("http://example.com/pickles/*", "http://example.com/pickles/"); michael@0: ok("http://example.com/pickles/*", "http://example.com/pickles/lemonade"); michael@0: michael@0: ok("http://example.com", "http://example.com"); michael@0: ok("http://example.com/ice-cream", "http://example.com/ice-cream"); michael@0: michael@0: ok(/.*zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); michael@0: ok(/https:.*zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); michael@0: ok('*.sample.com', 'http://ex.sample.com/foo.html'); michael@0: ok('*.amp.le.com', 'http://ex.amp.le.com'); michael@0: michael@0: ok('data:*', 'data:text/html;charset=utf-8,'); michael@0: }; michael@0: michael@0: exports.testMatchPatternTestFalse = function(assert) { michael@0: function ok(pattern, url) { michael@0: let mp = new MatchPattern(pattern); michael@0: assert.ok(!mp.test(url), pattern + " should not match " + url); michael@0: } michael@0: michael@0: ok("*", null); michael@0: ok("*", ""); michael@0: ok("*", "bogus"); michael@0: ok("*", "chrome://browser/content/browser.xul"); michael@0: ok("*", "nttp://example.com"); michael@0: michael@0: ok("*.example.com", null); michael@0: ok("*.example.com", ""); michael@0: ok("*.example.com", "bogus"); michael@0: ok("*.example.com", "http://example.net"); michael@0: ok("*.example.com", "http://foo.com"); michael@0: ok("*.example.com", "http://example.com.foo"); michael@0: ok("*.example2.com", "http://example.com"); michael@0: michael@0: ok("http://example.com/*", null); michael@0: ok("http://example.com/*", ""); michael@0: ok("http://example.com/*", "bogus"); michael@0: ok("http://example.com/*", "http://example.com"); michael@0: ok("http://example.com/*", "http://foo.com/"); michael@0: michael@0: ok("http://example.com", null); michael@0: ok("http://example.com", ""); michael@0: ok("http://example.com", "bogus"); michael@0: ok("http://example.com", "http://example.com/"); michael@0: michael@0: ok(/zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); michael@0: ok(/.*zilla/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); michael@0: ok(/.*Zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=655464"); // bug 655464 michael@0: ok(/https:.*zilla/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); michael@0: michael@0: // bug 856913 michael@0: ok('*.ign.com', 'http://www.design.com'); michael@0: ok('*.ign.com', 'http://design.com'); michael@0: ok('*.zilla.com', 'http://bugzilla.mozilla.com'); michael@0: ok('*.zilla.com', 'http://mo-zilla.com'); michael@0: ok('*.amp.le.com', 'http://amp-le.com'); michael@0: ok('*.amp.le.com', 'http://examp.le.com'); michael@0: }; michael@0: michael@0: exports.testMatchPatternErrors = function(assert) { michael@0: assert.throws( michael@0: function() new MatchPattern("*.google.com/*"), michael@0: /There can be at most one/, michael@0: "MatchPattern throws when supplied multiple '*'" michael@0: ); michael@0: michael@0: assert.throws( michael@0: function() new MatchPattern("google.com"), michael@0: /expected to be either an exact URL/, michael@0: "MatchPattern throws when the wildcard doesn't use '*' and doesn't " + michael@0: "look like a URL" michael@0: ); michael@0: michael@0: assert.throws( michael@0: function() new MatchPattern("http://google*.com"), michael@0: /expected to be the first or the last/, michael@0: "MatchPattern throws when a '*' is in the middle of the wildcard" michael@0: ); michael@0: michael@0: assert.throws( michael@0: function() new MatchPattern(/ /g), michael@0: /^A RegExp match pattern cannot be set to `global` \(i\.e\. \/\/g\)\.$/, michael@0: "MatchPattern throws on a RegExp set to `global` (i.e. //g)." michael@0: ); michael@0: michael@0: assert.throws( michael@0: function() new MatchPattern(/ /i), michael@0: /^A RegExp match pattern cannot be set to `ignoreCase` \(i\.e\. \/\/i\)\.$/, michael@0: "MatchPattern throws on a RegExp set to `ignoreCase` (i.e. //i)." michael@0: ); michael@0: michael@0: assert.throws( michael@0: function() new MatchPattern( / /m ), michael@0: /^A RegExp match pattern cannot be set to `multiline` \(i\.e\. \/\/m\)\.$/, michael@0: "MatchPattern throws on a RegExp set to `multiline` (i.e. //m)." michael@0: ); michael@0: }; michael@0: michael@0: exports.testMatchPatternInternals = function(assert) { michael@0: assert.equal( michael@0: new MatchPattern("http://google.com/test").exactURL, michael@0: "http://google.com/test" michael@0: ); michael@0: michael@0: assert.equal( michael@0: new MatchPattern("http://google.com/test/*").urlPrefix, michael@0: "http://google.com/test/" michael@0: ); michael@0: michael@0: assert.equal( michael@0: new MatchPattern("*.example.com").domain, michael@0: "example.com" michael@0: ); michael@0: }; michael@0: michael@0: require('sdk/test').run(exports);