1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-match-pattern.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,141 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 +"use strict"; 1.8 + 1.9 +const { MatchPattern } = require("sdk/util/match-pattern"); 1.10 + 1.11 +exports.testMatchPatternTestTrue = function(assert) { 1.12 + function ok(pattern, url) { 1.13 + let mp = new MatchPattern(pattern); 1.14 + assert.ok(mp.test(url), pattern + " should match " + url); 1.15 + } 1.16 + 1.17 + ok("*", "http://example.com"); 1.18 + ok("*", "https://example.com"); 1.19 + ok("*", "ftp://example.com"); 1.20 + 1.21 + ok("*.example.com", "http://example.com"); 1.22 + ok("*.example.com", "http://hamburger.example.com"); 1.23 + ok("*.example.com", "http://hotdog.hamburger.example.com"); 1.24 + 1.25 + ok("http://example.com*", "http://example.com"); 1.26 + ok("http://example.com*", "http://example.com/"); 1.27 + ok("http://example.com/*", "http://example.com/"); 1.28 + ok("http://example.com/*", "http://example.com/potato-salad"); 1.29 + ok("http://example.com/pickles/*", "http://example.com/pickles/"); 1.30 + ok("http://example.com/pickles/*", "http://example.com/pickles/lemonade"); 1.31 + 1.32 + ok("http://example.com", "http://example.com"); 1.33 + ok("http://example.com/ice-cream", "http://example.com/ice-cream"); 1.34 + 1.35 + ok(/.*zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); 1.36 + ok(/https:.*zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); 1.37 + ok('*.sample.com', 'http://ex.sample.com/foo.html'); 1.38 + ok('*.amp.le.com', 'http://ex.amp.le.com'); 1.39 + 1.40 + ok('data:*', 'data:text/html;charset=utf-8,'); 1.41 +}; 1.42 + 1.43 +exports.testMatchPatternTestFalse = function(assert) { 1.44 + function ok(pattern, url) { 1.45 + let mp = new MatchPattern(pattern); 1.46 + assert.ok(!mp.test(url), pattern + " should not match " + url); 1.47 + } 1.48 + 1.49 + ok("*", null); 1.50 + ok("*", ""); 1.51 + ok("*", "bogus"); 1.52 + ok("*", "chrome://browser/content/browser.xul"); 1.53 + ok("*", "nttp://example.com"); 1.54 + 1.55 + ok("*.example.com", null); 1.56 + ok("*.example.com", ""); 1.57 + ok("*.example.com", "bogus"); 1.58 + ok("*.example.com", "http://example.net"); 1.59 + ok("*.example.com", "http://foo.com"); 1.60 + ok("*.example.com", "http://example.com.foo"); 1.61 + ok("*.example2.com", "http://example.com"); 1.62 + 1.63 + ok("http://example.com/*", null); 1.64 + ok("http://example.com/*", ""); 1.65 + ok("http://example.com/*", "bogus"); 1.66 + ok("http://example.com/*", "http://example.com"); 1.67 + ok("http://example.com/*", "http://foo.com/"); 1.68 + 1.69 + ok("http://example.com", null); 1.70 + ok("http://example.com", ""); 1.71 + ok("http://example.com", "bogus"); 1.72 + ok("http://example.com", "http://example.com/"); 1.73 + 1.74 + ok(/zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); 1.75 + ok(/.*zilla/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); 1.76 + ok(/.*Zilla.*/, "https://bugzilla.redhat.com/show_bug.cgi?id=655464"); // bug 655464 1.77 + ok(/https:.*zilla/, "https://bugzilla.redhat.com/show_bug.cgi?id=569753"); 1.78 + 1.79 + // bug 856913 1.80 + ok('*.ign.com', 'http://www.design.com'); 1.81 + ok('*.ign.com', 'http://design.com'); 1.82 + ok('*.zilla.com', 'http://bugzilla.mozilla.com'); 1.83 + ok('*.zilla.com', 'http://mo-zilla.com'); 1.84 + ok('*.amp.le.com', 'http://amp-le.com'); 1.85 + ok('*.amp.le.com', 'http://examp.le.com'); 1.86 +}; 1.87 + 1.88 +exports.testMatchPatternErrors = function(assert) { 1.89 + assert.throws( 1.90 + function() new MatchPattern("*.google.com/*"), 1.91 + /There can be at most one/, 1.92 + "MatchPattern throws when supplied multiple '*'" 1.93 + ); 1.94 + 1.95 + assert.throws( 1.96 + function() new MatchPattern("google.com"), 1.97 + /expected to be either an exact URL/, 1.98 + "MatchPattern throws when the wildcard doesn't use '*' and doesn't " + 1.99 + "look like a URL" 1.100 + ); 1.101 + 1.102 + assert.throws( 1.103 + function() new MatchPattern("http://google*.com"), 1.104 + /expected to be the first or the last/, 1.105 + "MatchPattern throws when a '*' is in the middle of the wildcard" 1.106 + ); 1.107 + 1.108 + assert.throws( 1.109 + function() new MatchPattern(/ /g), 1.110 + /^A RegExp match pattern cannot be set to `global` \(i\.e\. \/\/g\)\.$/, 1.111 + "MatchPattern throws on a RegExp set to `global` (i.e. //g)." 1.112 + ); 1.113 + 1.114 + assert.throws( 1.115 + function() new MatchPattern(/ /i), 1.116 + /^A RegExp match pattern cannot be set to `ignoreCase` \(i\.e\. \/\/i\)\.$/, 1.117 + "MatchPattern throws on a RegExp set to `ignoreCase` (i.e. //i)." 1.118 + ); 1.119 + 1.120 + assert.throws( 1.121 + function() new MatchPattern( / /m ), 1.122 + /^A RegExp match pattern cannot be set to `multiline` \(i\.e\. \/\/m\)\.$/, 1.123 + "MatchPattern throws on a RegExp set to `multiline` (i.e. //m)." 1.124 + ); 1.125 +}; 1.126 + 1.127 +exports.testMatchPatternInternals = function(assert) { 1.128 + assert.equal( 1.129 + new MatchPattern("http://google.com/test").exactURL, 1.130 + "http://google.com/test" 1.131 + ); 1.132 + 1.133 + assert.equal( 1.134 + new MatchPattern("http://google.com/test/*").urlPrefix, 1.135 + "http://google.com/test/" 1.136 + ); 1.137 + 1.138 + assert.equal( 1.139 + new MatchPattern("*.example.com").domain, 1.140 + "example.com" 1.141 + ); 1.142 +}; 1.143 + 1.144 +require('sdk/test').run(exports);