Index: test/matcher.js |
=================================================================== |
--- a/test/matcher.js |
+++ b/test/matcher.js |
@@ -89,16 +89,38 @@ |
// For next run: add whitelisting filters for filters that aren't already |
filters = filters.map(text => text.substr(0, 2) == "@@" ? text : "@@" + text); |
if (expected && expected.substr(0, 2) != "@@") |
expected = "@@" + expected; |
} |
} |
+function checkSearch(test, filters, location, contentType, docDomain, |
+ thirdParty, sitekey, specificOnly, filterType, |
+ expected) |
+{ |
+ let matcher = new CombinedMatcher(); |
+ for (let filter of filters) |
+ matcher.add(Filter.fromText(filter)); |
+ |
+ let result = matcher.search(location, RegExpFilter.typeMap[contentType], |
+ docDomain, thirdParty, sitekey, specificOnly, |
+ filterType); |
+ for (let key in result) |
+ result[key] = result[key].map(filter => filter.text); |
+ |
+ test.deepEqual(result, expected, "search(" + location + ", " + |
+ contentType + ", " + docDomain + ", " + |
+ (thirdParty ? "third-party" : "first-party") + ", " + |
+ (sitekey || "no-sitekey") + ", " + |
+ (specificOnly ? "specificOnly" : "not-specificOnly") + ", " + |
+ filterType + ") with:\n" + filters.join("\n")); |
+} |
+ |
function cacheCheck(test, matcher, location, contentType, docDomain, thirdParty, expected) |
{ |
let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], docDomain, thirdParty); |
if (result) |
result = result.text; |
test.equal(result, expected, "match(" + location + ", " + contentType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with static filters"); |
} |
@@ -221,16 +243,51 @@ |
checkMatch(test, ["||foo.com^$popup", "@@||foo.com^$popup"], "http://foo.com/bar", "POPUP", "foo.com", false, null, false, "@@||foo.com^$popup", "||foo.com^$popup"); |
checkMatch(test, ["||foo.com^$csp=script-src 'none'"], "http://foo.com/bar", "CSP", "foo.com", false, null, false, "||foo.com^$csp=script-src 'none'"); |
checkMatch(test, ["@@||foo.com^$csp"], "http://foo.com/bar", "CSP", "foo.com", false, null, false, null, "@@||foo.com^$csp"); |
checkMatch(test, ["||foo.com^$csp=script-src 'none'", "@@||foo.com^$csp"], "http://foo.com/bar", "CSP", "foo.com", false, null, false, "@@||foo.com^$csp", "||foo.com^$csp=script-src 'none'"); |
test.done(); |
}; |
+exports.testFilterSearch = function(test) |
+{ |
+ // Start with three filters: foo, bar, and @@foo |
+ let filters = ["foo", "bar", "@@foo"]; |
+ |
+ checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com", |
+ false, null, false, "all", |
+ {blocking: ["foo"], whitelist: ["@@foo"]}); |
+ |
+ // Blocking only. |
+ checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com", |
+ false, null, false, "blocking", {blocking: ["foo"]}); |
+ |
+ // Whitelist only. |
+ checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com", |
+ false, null, false, "whitelist", {whitelist: ["@@foo"]}); |
+ |
+ // Different URLs. |
+ checkSearch(test, filters, "http://example.com/bar", "IMAGE", "example.com", |
+ false, null, false, "all", {blocking: ["bar"], whitelist: []}); |
+ checkSearch(test, filters, "http://example.com/foo/bar", "IMAGE", |
+ "example.com", false, null, false, "all", |
+ {blocking: ["foo", "bar"], whitelist: ["@@foo"]}); |
+ |
+ // Non-matching content type. |
+ checkSearch(test, filters, "http://example.com/foo", "CSP", "example.com", |
+ false, null, false, "all", {blocking: [], whitelist: []}); |
+ |
+ // Non-matching specificity. |
+ checkSearch(test, filters, "http://example.com/foo", "IMAGE", "example.com", |
+ false, null, true, "all", {blocking: [], whitelist: ["@@foo"]}); |
+ |
+ test.done(); |
+}; |
+ |
exports.testResultCacheChecks = function(test) |
{ |
let matcher = new CombinedMatcher(); |
matcher.add(Filter.fromText("abc$image")); |
matcher.add(Filter.fromText("abc$script")); |
matcher.add(Filter.fromText("abc$~image,~script,~media,~ping")); |
matcher.add(Filter.fromText("cba$third-party")); |
matcher.add(Filter.fromText("cba$~third-party,~script")); |