Skip to content

Commit 15012c0

Browse files
committed
test: add test to check patterns matching order
1 parent 4ddf6ea commit 15012c0

1 file changed

Lines changed: 54 additions & 10 deletions

File tree

webextensions/test/test-is-redirect-url.js

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ describe('isRedirectURL', () => {
2323
const config = {...baseConfig, ...additionals};
2424
config.URLPatterns = [...config.URLPatterns, ...URLPatterns];
2525
config.HostNamePatterns = [...config.HostNamePatterns, ...HostNamePatterns];
26-
if(config.UseRegex) {
27-
config.URLPatternsMatchers = {};
28-
config.HostNamePatternsMatchers = {};
29-
if (config.UseRegex) {
30-
redirector._generateMatcher(config.URLPatterns, config.URLPatternsMatchers);
31-
redirector._generateMatcher(config.HostNamePatterns, config.HostNamePatternsMatchers);
32-
}
33-
}
3426
return config;
3527
}
3628
describe('Empty redirect pattern', () => {
@@ -70,15 +62,67 @@ describe('isRedirectURL', () => {
7062
it(`Match redirect pattern with regex: partial match`, () => {
7163
const url = 'http://www.example.com/';
7264
const conf = config([], [['www\.example\.com', 'firefox']], { UseRegex: 1 })
73-
console.log(conf);
7465
assert.equal(redirector.isRedirectURL(conf, url), true);
7566
});
7667
it(`Match redirect pattern with regex: exact match`, () => {
7768
const url = 'http://www.example.com/';
7869
const conf = config([], [['^www\.example\.com$', 'firefox']], { UseRegex: 1 })
79-
console.log(conf);
8070
assert.equal(redirector.isRedirectURL(conf, url), true);
8171
});
72+
it(`Match first pattern: redirection (matches firefox)`, () => {
73+
const url = 'http://www.example.com/';
74+
const conf = config(
75+
[],
76+
[
77+
['not-match', browser],
78+
['www\.example\.com', 'firefox'],
79+
['www\.example\.com', browser]
80+
])
81+
assert.equal(redirector.isRedirectURL(conf, url), true);
82+
});
83+
it(`Match first pattern: non redirection (matches myself)`, () => {
84+
const url = 'http://www.example.com/';
85+
const conf = config(
86+
[],
87+
[
88+
['not-match', browser],
89+
['www\.example\.com', browser],
90+
['www\.example\.com', 'firefox']
91+
])
92+
assert.equal(redirector.isRedirectURL(conf, url), false);
93+
});
94+
it(`Match first pattern with regex: redirection (matches firefox)`, () => {
95+
// On 2.2.0 or before, patterns were grouped by browser, so they were matched in
96+
// the order in which the browsers appeared, rather than in top-down order of the patterns.
97+
// On the versions later than 2.2.0, the patterns were matched in the order in top-down order.
98+
const url = 'http://www.example.com/';
99+
const conf = config(
100+
[],
101+
[
102+
// We need this to make the "browser" the first appeared browser.
103+
['not-match', browser],
104+
['www\.example\.com', 'firefox'],
105+
['www\.example\.com', browser]
106+
],
107+
{ UseRegex: 1 })
108+
assert.equal(redirector.isRedirectURL(conf, url), true);
109+
});
110+
it(`Match first pattern with regex: non redirection (matches myself)`, () => {
111+
// On 2.2.0 or before, patterns were grouped by browser, so they were matched in
112+
// the order in which the browsers appeared, rather than in top-down order of the patterns.
113+
// On the versions later than 2.2.0, the patterns were matched in the order in top-down order.
114+
const url = 'http://www.example.com/';
115+
const conf = config(
116+
[],
117+
[
118+
// We need this to make the "browser" the first appeared browser.
119+
['not-match', browser],
120+
['www\.example\.com', browser],
121+
['www\.example\.com', 'firefox']
122+
],
123+
{ UseRegex: 1 })
124+
assert.equal(redirector.isRedirectURL(conf, url), false);
125+
});
82126
it(`Unmatch redirect pattern without wildcard`, () => {
83127
const url = 'http://www.google.com/';
84128
const conf = config([], [['www.example.com', 'firefox']])

0 commit comments

Comments
 (0)