Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h" |
| 6 | 6 |
| 7 #include <string> | |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 10 #include "base/basictypes.h" | |
| 9 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 10 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 11 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 12 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 13 #include "base/test/values_test_util.h" | 15 #include "base/test/values_test_util.h" |
| 14 #include "base/values.h" | 16 #include "base/values.h" |
| 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" | 17 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h" |
| 16 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" | 18 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" |
| 17 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 19 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
| 18 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 176 | 178 |
| 177 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | 179 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
| 178 rule->id.reset(new std::string(kRuleId4)); | 180 rule->id.reset(new std::string(kRuleId4)); |
| 179 rule->priority.reset(new int(200)); | 181 rule->priority.reset(new int(200)); |
| 180 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 182 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
| 181 rule->conditions.push_back( | 183 rule->conditions.push_back( |
| 182 linked_ptr<base::Value>(condition_dict.DeepCopy())); | 184 linked_ptr<base::Value>(condition_dict.DeepCopy())); |
| 183 return rule; | 185 return rule; |
| 184 } | 186 } |
| 185 | 187 |
| 186 // Create a condition with the attributes specified. An example value of a | 188 // Create a condition with the attributes specified. An example value of |
| 187 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". | 189 // |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". |
| 188 linked_ptr<base::Value> CreateCondition( | 190 linked_ptr<base::Value> CreateCondition(const std::string& attributes) { |
| 189 const std::vector<const char *>& attributes) { | |
| 190 // Starting boilerplate. | |
| 191 std::string json_description = | 191 std::string json_description = |
| 192 "{ \n" | 192 "{ \n" |
| 193 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; | 193 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; |
| 194 for (size_t i = 0; i < attributes.size(); ++i) { | 194 json_description += attributes; |
| 195 json_description += attributes[i]; | |
| 196 } | |
| 197 // Ending boilerplate. | |
| 198 json_description += "}"; | 195 json_description += "}"; |
| 199 | 196 |
| 200 return linked_ptr<base::Value>( | 197 return linked_ptr<base::Value>( |
| 201 base::test::ParseJson(json_description).release()); | 198 base::test::ParseJson(json_description).release()); |
| 202 } | 199 } |
| 203 | 200 |
| 204 // Create a rule with the ID |rule_id| and with a single condition and a | 201 // Create a rule with the ID |rule_id| and with conditions created from the |
| 205 // cancelling action. The condition contains a non-matching non-URL attribute, | 202 // |attributes| specified (one entry one condition). An example value of a |
| 206 // and optionally, according to |with_url_attribute| also a URL attribute | 203 // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". |
| 207 // matching any (!) URL. | 204 linked_ptr<RulesRegistry::Rule> CreateCancellingRule( |
| 208 linked_ptr<RulesRegistry::Rule> CreateNonMatchingRule( | 205 const char* rule_id, |
| 209 bool with_url_attribute, | 206 const std::vector<const std::string*>& attributes) { |
| 210 const char* rule_id) { | |
| 211 std::vector<const char*> attributes; | |
| 212 if (with_url_attribute) | |
| 213 attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); | |
| 214 | |
| 215 // The following attribute never matches in this unit test. | |
| 216 attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); | |
| 217 | |
| 218 DictionaryValue action_dict; | 207 DictionaryValue action_dict; |
| 219 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 208 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
| 220 | 209 |
| 221 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | |
| 222 rule->id.reset(new std::string(rule_id)); | |
| 223 rule->priority.reset(new int(1)); | |
| 224 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | |
| 225 rule->conditions.push_back(CreateCondition(attributes)); | |
| 226 return rule; | |
| 227 } | |
| 228 | |
| 229 // Create a rule with the ID |rule_id| and with two conditions and a | |
| 230 // cancelling action. One condition contains a non-matching non-URL attribute, | |
| 231 // and the other one a URL attribute matching any URL. | |
| 232 linked_ptr<RulesRegistry::Rule> CreateRuleWithTwoConditions( | |
| 233 const char* rule_id) { | |
| 234 std::vector<const char*> url_attributes; | |
| 235 url_attributes.push_back("\"url\": { \"pathContains\": \"\" }, \n"); | |
| 236 | |
| 237 // The following attribute never matches in this unit test. | |
| 238 std::vector<const char*> non_matching_attributes; | |
| 239 non_matching_attributes.push_back("\"resourceType\": [\"stylesheet\"], \n"); | |
| 240 | |
| 241 DictionaryValue action_dict; | |
| 242 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | |
| 243 | |
| 244 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); | 210 linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); |
| 245 rule->id.reset(new std::string(rule_id)); | 211 rule->id.reset(new std::string(rule_id)); |
| 246 rule->priority.reset(new int(1)); | 212 rule->priority.reset(new int(1)); |
| 247 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); | 213 rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); |
| 248 rule->conditions.push_back(CreateCondition(url_attributes)); | 214 for (std::vector<const std::string*>::const_iterator it = |
| 249 rule->conditions.push_back(CreateCondition(non_matching_attributes)); | 215 attributes.begin(); |
| 216 it != attributes.end(); ++it) | |
| 217 rule->conditions.push_back(CreateCondition(**it)); | |
| 250 return rule; | 218 return rule; |
| 251 } | 219 } |
| 252 | 220 |
| 253 protected: | 221 protected: |
| 254 MessageLoop message_loop; | 222 MessageLoop message_loop; |
| 255 content::TestBrowserThread ui; | 223 content::TestBrowserThread ui; |
| 256 content::TestBrowserThread io; | 224 content::TestBrowserThread io; |
| 257 }; | 225 }; |
| 258 | 226 |
| 259 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 227 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
| 260 scoped_refptr<TestWebRequestRulesRegistry> registry( | 228 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 261 new TestWebRequestRulesRegistry()); | 229 new TestWebRequestRulesRegistry()); |
| 262 std::string error; | 230 std::string error; |
| 263 | 231 |
| 264 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 232 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 265 rules.push_back(CreateRule1()); | 233 rules.push_back(CreateRule1()); |
| 266 rules.push_back(CreateRule2()); | 234 rules.push_back(CreateRule2()); |
| 267 | 235 |
| 268 error = registry->AddRules(kExtensionId, rules); | 236 error = registry->AddRules(kExtensionId, rules); |
| 269 EXPECT_EQ("", error); | 237 EXPECT_EQ("", error); |
| 270 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 238 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 271 | 239 |
| 272 std::set<const WebRequestRule*> matches; | 240 std::set<const WebRequestRule*> matches; |
| 273 | 241 |
| 274 GURL http_url("http://www.example.com"); | 242 GURL http_url("http://www.example.com"); |
| 275 net::TestURLRequestContext context; | 243 net::TestURLRequestContext context; |
| 276 net::TestURLRequest http_request(http_url, NULL, &context); | 244 net::TestURLRequest http_request(http_url, NULL, &context); |
| 277 matches = registry->GetMatches( | 245 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
| 278 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); | 246 matches = registry->GetMatches(request_data); |
| 279 EXPECT_EQ(2u, matches.size()); | 247 EXPECT_EQ(2u, matches.size()); |
| 280 | 248 |
| 281 std::set<WebRequestRule::GlobalRuleId> matches_ids; | 249 std::set<WebRequestRule::GlobalRuleId> matches_ids; |
| 282 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); | 250 for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); |
| 283 it != matches.end(); ++it) | 251 it != matches.end(); ++it) |
| 284 matches_ids.insert((*it)->id()); | 252 matches_ids.insert((*it)->id()); |
| 285 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); | 253 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); |
| 286 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); | 254 EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); |
| 287 | 255 |
| 288 GURL foobar_url("http://www.foobar.com"); | 256 GURL foobar_url("http://www.foobar.com"); |
| 289 net::TestURLRequest foobar_request(foobar_url, NULL, &context); | 257 net::TestURLRequest foobar_request(foobar_url, NULL, &context); |
| 290 matches = registry->GetMatches( | 258 request_data.request = &foobar_request; |
| 291 DeclarativeWebRequestData(&foobar_request, ON_BEFORE_REQUEST)); | 259 matches = registry->GetMatches(request_data); |
| 292 EXPECT_EQ(1u, matches.size()); | 260 EXPECT_EQ(1u, matches.size()); |
| 293 WebRequestRule::GlobalRuleId expected_pair = | 261 WebRequestRule::GlobalRuleId expected_pair = |
| 294 std::make_pair(kExtensionId, kRuleId2); | 262 std::make_pair(kExtensionId, kRuleId2); |
| 295 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 263 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 296 } | 264 } |
| 297 | 265 |
| 298 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 266 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
| 299 scoped_refptr<TestWebRequestRulesRegistry> registry( | 267 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 300 new TestWebRequestRulesRegistry()); | 268 new TestWebRequestRulesRegistry()); |
| 301 std::string error; | 269 std::string error; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 EXPECT_EQ("", error); | 374 EXPECT_EQ("", error); |
| 407 | 375 |
| 408 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); | 376 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); |
| 409 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); | 377 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); |
| 410 error = registry->AddRules(kExtensionId2, rules_to_add_2); | 378 error = registry->AddRules(kExtensionId2, rules_to_add_2); |
| 411 EXPECT_EQ("", error); | 379 EXPECT_EQ("", error); |
| 412 | 380 |
| 413 GURL url("http://www.google.com"); | 381 GURL url("http://www.google.com"); |
| 414 net::TestURLRequestContext context; | 382 net::TestURLRequestContext context; |
| 415 net::TestURLRequest request(url, NULL, &context); | 383 net::TestURLRequest request(url, NULL, &context); |
| 384 WebRequestData request_data(&request, ON_BEFORE_REQUEST); | |
| 416 std::list<LinkedPtrEventResponseDelta> deltas = | 385 std::list<LinkedPtrEventResponseDelta> deltas = |
| 417 registry->CreateDeltas( | 386 registry->CreateDeltas(NULL, request_data, false); |
| 418 NULL, | |
| 419 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), | |
| 420 false); | |
| 421 | 387 |
| 422 // The second extension is installed later and will win for this reason | 388 // The second extension is installed later and will win for this reason |
| 423 // in conflict resolution. | 389 // in conflict resolution. |
| 424 ASSERT_EQ(2u, deltas.size()); | 390 ASSERT_EQ(2u, deltas.size()); |
| 425 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); | 391 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); |
| 426 | 392 |
| 427 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); | 393 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); |
| 428 LinkedPtrEventResponseDelta winner = *i++; | 394 LinkedPtrEventResponseDelta winner = *i++; |
| 429 LinkedPtrEventResponseDelta loser = *i; | 395 LinkedPtrEventResponseDelta loser = *i; |
| 430 | 396 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 456 EXPECT_EQ("", error); | 422 EXPECT_EQ("", error); |
| 457 | 423 |
| 458 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); | 424 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); |
| 459 rules_to_add_3[0] = CreateIgnoreRule(); | 425 rules_to_add_3[0] = CreateIgnoreRule(); |
| 460 error = registry->AddRules(kExtensionId, rules_to_add_3); | 426 error = registry->AddRules(kExtensionId, rules_to_add_3); |
| 461 EXPECT_EQ("", error); | 427 EXPECT_EQ("", error); |
| 462 | 428 |
| 463 GURL url("http://www.google.com/index.html"); | 429 GURL url("http://www.google.com/index.html"); |
| 464 net::TestURLRequestContext context; | 430 net::TestURLRequestContext context; |
| 465 net::TestURLRequest request(url, NULL, &context); | 431 net::TestURLRequest request(url, NULL, &context); |
| 432 WebRequestData request_data(&request, ON_BEFORE_REQUEST); | |
| 466 std::list<LinkedPtrEventResponseDelta> deltas = | 433 std::list<LinkedPtrEventResponseDelta> deltas = |
| 467 registry->CreateDeltas( | 434 registry->CreateDeltas(NULL, request_data, false); |
| 468 NULL, | |
| 469 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST), | |
| 470 false); | |
| 471 | 435 |
| 472 // The redirect by the first extension is ignored due to the ignore rule. | 436 // The redirect by the first extension is ignored due to the ignore rule. |
| 473 ASSERT_EQ(1u, deltas.size()); | 437 ASSERT_EQ(1u, deltas.size()); |
| 474 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 438 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
| 475 | 439 |
| 476 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 440 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
| 477 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 441 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
| 478 effective_rule->extension_install_time); | 442 effective_rule->extension_install_time); |
| 479 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 443 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
| 480 } | 444 } |
| 481 | 445 |
| 482 // Test that rules failing IsFulfilled on their conditions are never returned by | 446 // Test that rules failing IsFulfilled on their conditions are never returned by |
| 483 // GetMatches. | 447 // GetMatches. |
| 484 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { | 448 TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { |
| 485 scoped_refptr<TestWebRequestRulesRegistry> registry( | 449 scoped_refptr<TestWebRequestRulesRegistry> registry( |
| 486 new TestWebRequestRulesRegistry()); | 450 new TestWebRequestRulesRegistry()); |
| 451 const std::string kMatchingUrlAttribute( | |
| 452 "\"url\": { \"pathContains\": \"\" }, \n"); | |
| 453 const std::string kNonMatchingNonUrlAttribute( | |
| 454 "\"resourceType\": [\"stylesheet\"], \n"); | |
| 455 const std::string kBothAttributes(kMatchingUrlAttribute + | |
| 456 kNonMatchingNonUrlAttribute); | |
| 487 std::string error; | 457 std::string error; |
| 458 std::vector<const std::string*> attributes; | |
| 459 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | |
| 488 | 460 |
| 489 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 461 // Rules 1 and 2 have one condition, neither of them should fire. |
| 490 // Both rules have one condition, neither of them should fire. | 462 attributes.push_back(&kNonMatchingNonUrlAttribute); |
| 491 // This rule's condition has only one, non-matching and non-URL attribute. | 463 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); |
| 492 rules.push_back(CreateNonMatchingRule(false, kRuleId1)); | |
| 493 // This rule's condition has two attributes: a matching URL, and a | |
| 494 // non-matching non-URL attribute. | |
| 495 rules.push_back(CreateNonMatchingRule(true, kRuleId2)); | |
| 496 | 464 |
| 497 // The 3rd rule has two conditions, one with a matching URL attribute, and one | 465 attributes.clear(); |
| 466 attributes.push_back(&kBothAttributes); | |
| 467 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); | |
| 468 | |
| 469 // Rule 3 has two conditions, one with a matching URL attribute, and one | |
| 498 // with a non-matching non-URL attribute. | 470 // with a non-matching non-URL attribute. |
| 499 rules.push_back(CreateRuleWithTwoConditions(kRuleId3)); | 471 attributes.clear(); |
| 472 attributes.push_back(&kMatchingUrlAttribute); | |
| 473 attributes.push_back(&kNonMatchingNonUrlAttribute); | |
| 474 rules.push_back(CreateCancellingRule(kRuleId3, attributes)); | |
| 500 | 475 |
| 501 error = registry->AddRules(kExtensionId, rules); | 476 error = registry->AddRules(kExtensionId, rules); |
| 502 EXPECT_EQ("", error); | 477 EXPECT_EQ("", error); |
| 503 EXPECT_EQ(1, registry->num_clear_cache_calls()); | 478 EXPECT_EQ(1, registry->num_clear_cache_calls()); |
| 504 | 479 |
| 505 std::set<const WebRequestRule*> matches; | 480 std::set<const WebRequestRule*> matches; |
| 506 | 481 |
| 507 GURL http_url("http://www.example.com"); | 482 GURL http_url("http://www.example.com"); |
| 508 net::TestURLRequestContext context; | 483 net::TestURLRequestContext context; |
| 509 net::TestURLRequest http_request(http_url, NULL, &context); | 484 net::TestURLRequest http_request(http_url, NULL, &context); |
| 510 matches = registry->GetMatches( | 485 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
| 511 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST)); | 486 matches = registry->GetMatches(request_data); |
| 512 EXPECT_EQ(1u, matches.size()); | 487 EXPECT_EQ(1u, matches.size()); |
| 513 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, | 488 WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, |
| 514 kRuleId3); | 489 kRuleId3); |
| 515 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | 490 EXPECT_EQ(expected_pair, (*matches.begin())->id()); |
| 516 } | 491 } |
| 517 | 492 |
| 493 // Test that the url and firstPartyForCookiesUrl attributes are evaluated | |
| 494 // against corresponding URLs. Tested on requests where these URLs actually | |
| 495 // differ. | |
| 496 TEST_F(WebRequestRulesRegistryTest, GetMatchesDifferentUrls) { | |
| 497 scoped_refptr<TestWebRequestRulesRegistry> registry( | |
| 498 new TestWebRequestRulesRegistry()); | |
| 499 const std::string kUrlAttribute( | |
| 500 "\"url\": { \"hostContains\": \"url\" }, \n"); | |
| 501 const std::string kFirstPartyUrlAttribute( | |
| 502 "\"firstPartyForCookiesUrl\": { \"hostContains\": \"fpfc\" }, \n"); | |
| 503 std::string error; | |
| 504 std::vector<const std::string*> attributes; | |
| 505 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | |
| 506 | |
| 507 // Rule 1 has one condition, with a url attribute | |
| 508 attributes.push_back(&kUrlAttribute); | |
| 509 rules.push_back(CreateCancellingRule(kRuleId1, attributes)); | |
| 510 | |
| 511 // Rule 2 has one condition, with a firstPartyForCookiesUrl attribute | |
| 512 attributes.clear(); | |
| 513 attributes.push_back(&kFirstPartyUrlAttribute); | |
| 514 rules.push_back(CreateCancellingRule(kRuleId2, attributes)); | |
| 515 | |
| 516 error = registry->AddRules(kExtensionId, rules); | |
| 517 EXPECT_EQ("", error); | |
| 518 EXPECT_EQ(1, registry->num_clear_cache_calls()); | |
| 519 | |
| 520 std::set<const WebRequestRule*> matches; | |
| 521 | |
| 522 const GURL urls[] = { | |
| 523 GURL("http://url.example.com"), // matching | |
| 524 GURL("http://www.example.com") // non-matching | |
| 525 }; | |
| 526 const GURL firstPartyUrls[] = { | |
| 527 GURL("http://www.example.com"), // non-matching | |
| 528 GURL("http://fpfc.example.com") // matching | |
| 529 }; | |
| 530 const char* matchingRuleIds[] = { kRuleId1, kRuleId2 }; | |
|
Jeffrey Yasskin
2013/01/24 19:54:46
It makes me nervous when a test changes both the i
vabr (Chromium)
2013/01/24 21:12:19
Clarified with Jeffrey, that both rules are used i
| |
| 531 COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls), | |
| 532 urls_and_firstPartyUrls_need_to_have_the_same_size); | |
| 533 COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds), | |
| 534 urls_and_matchingRuleIds_need_to_have_the_same_size); | |
| 535 net::TestURLRequestContext context; | |
| 536 | |
| 537 for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { | |
| 538 net::TestURLRequest http_request(urls[i], NULL, &context); | |
| 539 WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); | |
| 540 http_request.set_first_party_for_cookies(firstPartyUrls[i]); | |
| 541 matches = registry->GetMatches(request_data); | |
| 542 EXPECT_EQ(1u, matches.size()); | |
|
Jeffrey Yasskin
2013/01/24 19:54:46
When you put an EXPECT in a loop, always be sure t
vabr (Chromium)
2013/01/24 21:12:19
Thanks! Done.
| |
| 543 WebRequestRule::GlobalRuleId expected_pair = | |
| 544 std::make_pair(kExtensionId, matchingRuleIds[i]); | |
| 545 EXPECT_EQ(expected_pair, (*matches.begin())->id()); | |
|
Jeffrey Yasskin
2013/01/24 19:54:46
I tend to prefer inlining the construction of the
vabr (Chromium)
2013/01/24 21:12:19
Done. Which rule ID it is should be now clear from
| |
| 546 } | |
| 547 } | |
| 548 | |
| 518 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { | 549 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) { |
| 519 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED | 550 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED |
| 520 // but the redirect action can only be executed during ON_BEFORE_REQUEST. | 551 // but the redirect action can only be executed during ON_BEFORE_REQUEST. |
| 521 // Therefore, this is an inconsistent rule that needs to be flagged. | 552 // Therefore, this is an inconsistent rule that needs to be flagged. |
| 522 const char kRule[] = | 553 const char kRule[] = |
| 523 "{ \n" | 554 "{ \n" |
| 524 " \"id\": \"rule1\", \n" | 555 " \"id\": \"rule1\", \n" |
| 525 " \"conditions\": [ \n" | 556 " \"conditions\": [ \n" |
| 526 " { \n" | 557 " { \n" |
| 527 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 558 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 549 new TestWebRequestRulesRegistry()); | 580 new TestWebRequestRulesRegistry()); |
| 550 | 581 |
| 551 URLMatcher matcher; | 582 URLMatcher matcher; |
| 552 std::string error = registry->AddRulesImpl(kExtensionId, rules); | 583 std::string error = registry->AddRulesImpl(kExtensionId, rules); |
| 553 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); | 584 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); |
| 554 EXPECT_TRUE(registry->IsEmpty()); | 585 EXPECT_TRUE(registry->IsEmpty()); |
| 555 } | 586 } |
| 556 | 587 |
| 557 | 588 |
| 558 } // namespace extensions | 589 } // namespace extensions |
| OLD | NEW |