OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_
registry.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/memory/linked_ptr.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/values.h" |
| 12 #include "content/test/test_browser_thread.h" |
| 13 #include "net/url_request/url_request_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace { |
| 17 const char kExtensionId[] = "ext1"; |
| 18 const char kRuleId1[] = "rule1"; |
| 19 const char kRuleId2[] = "rule2"; |
| 20 |
| 21 const char kCancelRequestType[] = "experimental.webRequest.CancelRequest"; |
| 22 const char kRequestMatcher[] = "experimental.webRequest.RequestMatcher"; |
| 23 const char kInstanceType[] = "instanceType"; |
| 24 } |
| 25 |
| 26 namespace extensions { |
| 27 |
| 28 class WebRequestRulesRegistryTest : public testing::Test { |
| 29 public: |
| 30 public: |
| 31 WebRequestRulesRegistryTest() |
| 32 : message_loop(MessageLoop::TYPE_IO), |
| 33 ui(content::BrowserThread::UI, &message_loop), |
| 34 io(content::BrowserThread::IO, &message_loop) {} |
| 35 |
| 36 virtual ~WebRequestRulesRegistryTest() {} |
| 37 |
| 38 virtual void TearDown() OVERRIDE { |
| 39 // Make sure that deletion traits of all registries are executed. |
| 40 message_loop.RunAllPending(); |
| 41 } |
| 42 |
| 43 // Returns a rule that roughly matches http://*.example.com and |
| 44 // https://www.example.com and cancels it |
| 45 linked_ptr<RulesRegistry::Rule> CreateRule1() { |
| 46 DictionaryValue http_condition_dict; |
| 47 http_condition_dict.SetString("scheme", "http"); |
| 48 http_condition_dict.SetString("host_suffix", "example.com"); |
| 49 http_condition_dict.SetString(kInstanceType, kRequestMatcher); |
| 50 |
| 51 DictionaryValue https_condition_dict; |
| 52 https_condition_dict.SetString("scheme", "https"); |
| 53 https_condition_dict.SetString("host_suffix", "example.com"); |
| 54 https_condition_dict.SetString("host_prefix", "www"); |
| 55 https_condition_dict.SetString(kInstanceType, kRequestMatcher); |
| 56 |
| 57 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
| 58 new json_schema_compiler::any::Any); |
| 59 condition1->Init(http_condition_dict); |
| 60 |
| 61 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( |
| 62 new json_schema_compiler::any::Any); |
| 63 condition2->Init(https_condition_dict); |
| 64 |
| 65 DictionaryValue action_dict; |
| 66 action_dict.SetString(kInstanceType, kCancelRequestType); |
| 67 |
| 68 linked_ptr<json_schema_compiler::any::Any> action1 = make_linked_ptr( |
| 69 new json_schema_compiler::any::Any); |
| 70 action1->Init(action_dict); |
| 71 |
| 72 linked_ptr<RulesRegistry::Rule> rule = |
| 73 make_linked_ptr(new RulesRegistry::Rule); |
| 74 rule->id.reset(new std::string(kRuleId1)); |
| 75 rule->priority.reset(new int(100)); |
| 76 rule->actions.push_back(action1); |
| 77 rule->conditions.push_back(condition1); |
| 78 rule->conditions.push_back(condition2); |
| 79 return rule; |
| 80 } |
| 81 |
| 82 // Returns a rule that matches anything and cancels it. |
| 83 linked_ptr<RulesRegistry::Rule> CreateRule2() { |
| 84 DictionaryValue condition_dict; |
| 85 condition_dict.SetString(kInstanceType, kRequestMatcher); |
| 86 |
| 87 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( |
| 88 new json_schema_compiler::any::Any); |
| 89 condition->Init(condition_dict); |
| 90 |
| 91 DictionaryValue action_dict; |
| 92 action_dict.SetString(kInstanceType, kCancelRequestType); |
| 93 |
| 94 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( |
| 95 new json_schema_compiler::any::Any); |
| 96 action->Init(action_dict); |
| 97 |
| 98 linked_ptr<RulesRegistry::Rule> rule = |
| 99 make_linked_ptr(new RulesRegistry::Rule); |
| 100 rule->id.reset(new std::string(kRuleId2)); |
| 101 rule->priority.reset(new int(100)); |
| 102 rule->actions.push_back(action); |
| 103 rule->conditions.push_back(condition); |
| 104 return rule; |
| 105 } |
| 106 |
| 107 protected: |
| 108 MessageLoop message_loop; |
| 109 content::TestBrowserThread ui; |
| 110 content::TestBrowserThread io; |
| 111 }; |
| 112 |
| 113 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
| 114 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); |
| 115 std::string error; |
| 116 |
| 117 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 118 rules.push_back(CreateRule1()); |
| 119 rules.push_back(CreateRule2()); |
| 120 |
| 121 error = registry->AddRules(kExtensionId, rules); |
| 122 EXPECT_TRUE(error.empty()); |
| 123 |
| 124 std::set<WebRequestRule::GlobalRuleId> matches; |
| 125 |
| 126 GURL http_url("http://www.example.com"); |
| 127 TestURLRequest http_request(http_url, NULL); |
| 128 matches = registry->GetMatches(&http_request); |
| 129 EXPECT_EQ(2u, matches.size()); |
| 130 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != |
| 131 matches.end()); |
| 132 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != |
| 133 matches.end()); |
| 134 |
| 135 GURL foobar_url("http://www.foobar.com"); |
| 136 TestURLRequest foobar_request(foobar_url, NULL); |
| 137 matches = registry->GetMatches(&foobar_request); |
| 138 EXPECT_EQ(1u, matches.size()); |
| 139 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != |
| 140 matches.end()); |
| 141 } |
| 142 |
| 143 } // namespace extensions |
OLD | NEW |