Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1060)

Unified Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc

Issue 11569007: Refactoring how conditions without URL attributes are handled. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactor of GetMatches, IsFulfilled etc. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc
diff --git a/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc b/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc
index df0771dc38d8a0cb0b384b02e403dcab69a8fc60..1a6d0f9e9d8f3b6fab07a1fc1f75d9a2fc9b4239 100644
--- a/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc
+++ b/chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc
@@ -85,6 +85,128 @@ TEST(WebRequestConditionTest, CreateCondition) {
WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST)));
}
+// The IsFulfilledIndependentlyOfURL method tests that a condition has no
+// UrlFilter attributes, and all its (non-UrlFilter) attributes are satisfied.
+// We test here that:
+// 1. A non-empty condition without UrlFilter attributes is fulfilled iff its
+// attributes are fulfilled.
+// 2. An empty condition (in particular, without UrlFilter attributes) is
+// always fulfilled.
+// 3. A condition with a UrlFilter and a non-UrlFilter attribute is never
+// fulfilled.
+// 4. A condition with only a UrlFilter attribute is never fulfilled.
+TEST(WebRequestConditionTest, IsFulfilledIndependentlyOfURL) {
+ // Necessary for TestURLRequest.
+ MessageLoop message_loop(MessageLoop::TYPE_IO);
+ URLMatcher matcher;
+ std::string error;
+
+ // The empty condition.
+ error.clear();
+ scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create(
+ matcher.condition_factory(),
+ *base::test::ParseJson(
+ "{ \n"
+ " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
+ "}"),
+ &error);
+ EXPECT_EQ("", error);
+ ASSERT_TRUE(condition_empty.get());
+
+ // A condition without a UrlFilter attribute, which is always true.
+ error.clear();
+ scoped_ptr<WebRequestCondition> condition_no_url_true =
+ WebRequestCondition::Create(
+ matcher.condition_factory(),
+ *base::test::ParseJson(
+ "{ \n"
+ " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
+ // There is no "first party for cookies URL in the requests below,
+ // therefore all requests are considered first party for cookies.
+ " \"thirdPartyForCookies\": false, \n"
+ "}"),
+ &error);
+ EXPECT_EQ("", error);
+ ASSERT_TRUE(condition_no_url_true.get());
+
+ // A condition without a UrlFilter attribute, which is always false.
+ error.clear();
+ scoped_ptr<WebRequestCondition> condition_no_url_false =
+ WebRequestCondition::Create(
+ matcher.condition_factory(),
+ *base::test::ParseJson(
+ "{ \n"
+ " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
+ " \"thirdPartyForCookies\": true, \n"
+ "}"),
+ &error);
+ EXPECT_EQ("", error);
+ ASSERT_TRUE(condition_no_url_false.get());
+
+ // MATCHING_URL_FILTER must always match kUrl.
+ static const char kUrl[] = "https://www.example.com";
+#define MATCHING_URL_FILTER \
Jeffrey Yasskin 2012/12/18 21:56:02 It'll work to define this as a std::string, and co
vabr (Chromium) 2012/12/19 08:38:19 Agreed, only this part of the test is no longer ne
+ " \"url\": { \n" \
+ " \"hostSuffix\": \"example.com\", \n" \
+ " \"hostPrefix\": \"www\", \n" \
+ " \"schemes\": [\"https\"], \n" \
+ " }, \n"
+
+ // A condition with only a UrlFilter attribute.
+ error.clear();
+ scoped_ptr<WebRequestCondition> condition_url = WebRequestCondition::Create(
+ matcher.condition_factory(),
+ *base::test::ParseJson(
+ "{ \n"
+ " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
+ MATCHING_URL_FILTER
+ "}"),
+ &error);
+ EXPECT_EQ("", error);
+ ASSERT_TRUE(condition_url.get());
+
+ // A condition with a UrlFilter attribute and an always true non-UrlFilter
+ // attribute.
+ error.clear();
+ scoped_ptr<WebRequestCondition> condition_both = WebRequestCondition::Create(
+ matcher.condition_factory(),
+ *base::test::ParseJson(
+ "{ \n"
+ " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
+ MATCHING_URL_FILTER
+ " \"thirdPartyForCookies\": false, \n"
+ "}"),
+ &error);
+ EXPECT_EQ("", error);
+ ASSERT_TRUE(condition_both.get());
+#undef MATCHING_URL_FILTER
+
+ net::TestURLRequestContext context;
+ net::TestURLRequest https_request(GURL(kUrl), NULL, &context);
+
+ // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its
+ // attributes are fulfilled.
+ EXPECT_FALSE(condition_no_url_false->IsFulfilledIndependentlyOfURL(
+ WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
+
+ EXPECT_TRUE(condition_no_url_true->IsFulfilledIndependentlyOfURL(
+ WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
+
+ // 2. An empty condition (in particular, without UrlFilter attributes) is
+ // always fulfilled.
+ EXPECT_TRUE(condition_empty->IsFulfilledIndependentlyOfURL(
+ WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
+
+ // 3. A condition with a UrlFilter and a non-UrlFilter attribute is never
+ // fulfilled.
+ EXPECT_FALSE(condition_both->IsFulfilledIndependentlyOfURL(
+ WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
+
+ // 4. A condition with only a UrlFilter attribute is never fulfilled.
+ EXPECT_FALSE(condition_url->IsFulfilledIndependentlyOfURL(
+ WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
+}
+
TEST(WebRequestConditionTest, CreateConditionSet) {
// Necessary for TestURLRequest.
MessageLoop message_loop(MessageLoop::TYPE_IO);
@@ -132,7 +254,6 @@ TEST(WebRequestConditionTest, CreateConditionSet) {
matcher.AddConditionSets(url_matcher_condition_set);
std::set<URLMatcherConditionSet::ID> url_match_ids;
- int number_matches = 0;
// Test that the result is correct and matches http://www.example.com and
// https://www.example.com
@@ -140,39 +261,24 @@ TEST(WebRequestConditionTest, CreateConditionSet) {
net::TestURLRequestContext context;
net::TestURLRequest http_request(http_url, NULL, &context);
url_match_ids = matcher.MatchURL(http_url);
- for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin();
- i != url_match_ids.end(); ++i) {
- if (result->IsFulfilled(
- *i, WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)))
- ++number_matches;
- }
- EXPECT_EQ(1, number_matches);
+ EXPECT_TRUE(result->IsFulfilled(
+ url_match_ids,
+ WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)));
GURL https_url("https://www.example.com");
url_match_ids = matcher.MatchURL(https_url);
net::TestURLRequest https_request(https_url, NULL, &context);
- number_matches = 0;
- for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin();
- i != url_match_ids.end(); ++i) {
- if (result->IsFulfilled(
- *i, WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)))
- ++number_matches;
- }
- EXPECT_EQ(1, number_matches);
+ EXPECT_TRUE(result->IsFulfilled(
+ url_match_ids,
+ WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
// Check that both, hostPrefix and hostSuffix are evaluated.
GURL https_foo_url("https://foo.example.com");
url_match_ids = matcher.MatchURL(https_foo_url);
net::TestURLRequest https_foo_request(https_foo_url, NULL, &context);
- number_matches = 0;
- for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin();
- i != url_match_ids.end(); ++i) {
- if (result->IsFulfilled(
- *i, WebRequestRule::RequestData(
- &https_foo_request, ON_BEFORE_REQUEST)))
- ++number_matches;
- }
- EXPECT_EQ(0, number_matches);
+ EXPECT_FALSE(result->IsFulfilled(
+ url_match_ids,
+ WebRequestRule::RequestData(&https_foo_request, ON_BEFORE_REQUEST)));
}
TEST(WebRequestConditionTest, TestPortFilter) {

Powered by Google App Engine
This is Rietveld 408576698