addon-sdk/source/test/test-match-pattern.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial