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