| 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_condit
ion.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 11 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 12 #include "content/public/browser/resource_request_info.h" |
| 12 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace extensions { | 16 namespace extensions { |
| 16 | 17 |
| 17 namespace keys = declarative_webrequest_constants; | 18 namespace keys = declarative_webrequest_constants; |
| 18 | 19 |
| 19 TEST(WebRequestConditionTest, CreateCondition) { | 20 TEST(WebRequestConditionTest, CreateCondition) { |
| 20 // Necessary for TestURLRequest. | 21 // Necessary for TestURLRequest. |
| 21 MessageLoop message_loop(MessageLoop::TYPE_IO); | 22 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 22 URLMatcher matcher; | 23 URLMatcher matcher; |
| 23 | 24 |
| 24 std::string error; | 25 std::string error; |
| 25 scoped_ptr<WebRequestCondition> result; | 26 scoped_ptr<WebRequestCondition> result; |
| 26 | 27 |
| 27 DictionaryValue invalid_condition; | 28 DictionaryValue invalid_condition; |
| 28 invalid_condition.SetString("invalid", "foobar"); | 29 invalid_condition.SetString("invalid", "foobar"); |
| 29 invalid_condition.SetString("host_suffix", "example.com"); | 30 invalid_condition.SetString("host_suffix", "example.com"); |
| 30 invalid_condition.SetString(keys::kInstanceTypeKey, | 31 invalid_condition.SetString(keys::kInstanceTypeKey, |
| 31 keys::kRequestMatcherType); | 32 keys::kRequestMatcherType); |
| 32 | 33 |
| 33 DictionaryValue invalid_condition2; | 34 DictionaryValue invalid_condition2; |
| 34 invalid_condition2.Set("host_suffix", new ListValue); | 35 invalid_condition2.Set("host_suffix", new ListValue); |
| 35 invalid_condition2.SetString(keys::kInstanceTypeKey, | 36 invalid_condition2.SetString(keys::kInstanceTypeKey, |
| 36 keys::kRequestMatcherType); | 37 keys::kRequestMatcherType); |
| 37 | 38 |
| 39 ListValue* resource_type_list = new ListValue(); |
| 40 resource_type_list->Append(Value::CreateStringValue("main_frame")); |
| 38 DictionaryValue valid_condition; | 41 DictionaryValue valid_condition; |
| 39 valid_condition.SetString("scheme", "http"); | 42 valid_condition.Set(keys::kResourceTypeKey, resource_type_list); |
| 40 valid_condition.SetString("host_suffix", "example.com"); | 43 valid_condition.SetString("host_suffix", "example.com"); |
| 41 valid_condition.SetString(keys::kInstanceTypeKey, | 44 valid_condition.SetString(keys::kInstanceTypeKey, |
| 42 keys::kRequestMatcherType); | 45 keys::kRequestMatcherType); |
| 43 | 46 |
| 44 // Test wrong condition name passed. | 47 // Test wrong condition name passed. |
| 45 error.clear(); | 48 error.clear(); |
| 46 result = WebRequestCondition::Create(matcher.condition_factory(), | 49 result = WebRequestCondition::Create(matcher.condition_factory(), |
| 47 invalid_condition, &error); | 50 invalid_condition, &error); |
| 48 EXPECT_FALSE(error.empty()); | 51 EXPECT_FALSE(error.empty()); |
| 49 EXPECT_FALSE(result.get()); | 52 EXPECT_FALSE(result.get()); |
| 50 | 53 |
| 51 // Test wrong datatype in host_suffix. | 54 // Test wrong datatype in host_suffix. |
| 52 error.clear(); | 55 error.clear(); |
| 53 result = WebRequestCondition::Create(matcher.condition_factory(), | 56 result = WebRequestCondition::Create(matcher.condition_factory(), |
| 54 invalid_condition2, &error); | 57 invalid_condition2, &error); |
| 55 EXPECT_FALSE(error.empty()); | 58 EXPECT_FALSE(error.empty()); |
| 56 EXPECT_FALSE(result.get()); | 59 EXPECT_FALSE(result.get()); |
| 57 | 60 |
| 58 // Test success (can we support multiple criteria?) | 61 // Test success (can we support multiple criteria?) |
| 59 error.clear(); | 62 error.clear(); |
| 60 result = WebRequestCondition::Create(matcher.condition_factory(), | 63 result = WebRequestCondition::Create(matcher.condition_factory(), |
| 61 valid_condition, &error); | 64 valid_condition, &error); |
| 62 EXPECT_TRUE(error.empty()); | 65 EXPECT_EQ("", error); |
| 63 ASSERT_TRUE(result.get()); | 66 ASSERT_TRUE(result.get()); |
| 64 | 67 |
| 65 TestURLRequest match_request(GURL("http://www.example.com"), NULL); | 68 TestURLRequest match_request(GURL("http://www.example.com"), NULL); |
| 69 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
| 70 ResourceType::MAIN_FRAME, NULL); |
| 66 EXPECT_TRUE(result->IsFulfilled(&match_request, ON_BEFORE_REQUEST)); | 71 EXPECT_TRUE(result->IsFulfilled(&match_request, ON_BEFORE_REQUEST)); |
| 67 | 72 |
| 68 TestURLRequest wrong_scheme(GURL("https://www.example.com"), NULL); | 73 TestURLRequest wrong_resource_type(GURL("https://www.example.com"), NULL); |
| 69 EXPECT_FALSE(result->IsFulfilled(&wrong_scheme, ON_BEFORE_REQUEST)); | 74 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
| 75 ResourceType::SUB_FRAME, NULL); |
| 76 EXPECT_FALSE(result->IsFulfilled(&wrong_resource_type, ON_BEFORE_REQUEST)); |
| 70 } | 77 } |
| 71 | 78 |
| 72 TEST(WebRequestConditionTest, CreateConditionSet) { | 79 TEST(WebRequestConditionTest, CreateConditionSet) { |
| 73 // Necessary for TestURLRequest. | 80 // Necessary for TestURLRequest. |
| 74 MessageLoop message_loop(MessageLoop::TYPE_IO); | 81 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 75 URLMatcher matcher; | 82 URLMatcher matcher; |
| 76 | 83 |
| 84 ListValue* http_scheme_list = new ListValue(); |
| 85 http_scheme_list->Append(Value::CreateStringValue("http")); |
| 77 DictionaryValue http_condition; | 86 DictionaryValue http_condition; |
| 78 http_condition.SetString("scheme", "http"); | 87 http_condition.Set(keys::kSchemesKey, http_scheme_list); |
| 79 http_condition.SetString("host_suffix", "example.com"); | 88 http_condition.SetString("host_suffix", "example.com"); |
| 80 http_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 89 http_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 81 | 90 |
| 91 ListValue* https_scheme_list = new ListValue(); |
| 92 https_scheme_list->Append(Value::CreateStringValue("https")); |
| 82 DictionaryValue https_condition; | 93 DictionaryValue https_condition; |
| 83 https_condition.SetString("scheme", "https"); | 94 https_condition.Set(keys::kSchemesKey, https_scheme_list); |
| 84 https_condition.SetString("host_suffix", "example.com"); | 95 https_condition.SetString("host_suffix", "example.com"); |
| 85 https_condition.SetString("host_prefix", "www"); | 96 https_condition.SetString("host_prefix", "www"); |
| 86 https_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 97 https_condition.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 87 | 98 |
| 88 WebRequestConditionSet::AnyVector conditions; | 99 WebRequestConditionSet::AnyVector conditions; |
| 89 | 100 |
| 90 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 101 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
| 91 new json_schema_compiler::any::Any); | 102 new json_schema_compiler::any::Any); |
| 92 condition1->Init(http_condition); | 103 condition1->Init(http_condition); |
| 93 conditions.push_back(condition1); | 104 conditions.push_back(condition1); |
| 94 | 105 |
| 95 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( | 106 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( |
| 96 new json_schema_compiler::any::Any); | 107 new json_schema_compiler::any::Any); |
| 97 condition2->Init(https_condition); | 108 condition2->Init(https_condition); |
| 98 conditions.push_back(condition2); | 109 conditions.push_back(condition2); |
| 99 | 110 |
| 100 // Test insertion | 111 // Test insertion |
| 101 std::string error; | 112 std::string error; |
| 102 scoped_ptr<WebRequestConditionSet> result = | 113 scoped_ptr<WebRequestConditionSet> result = |
| 103 WebRequestConditionSet::Create(matcher.condition_factory(), | 114 WebRequestConditionSet::Create(matcher.condition_factory(), |
| 104 conditions, &error); | 115 conditions, &error); |
| 105 EXPECT_TRUE(error.empty()); | 116 EXPECT_EQ("", error); |
| 106 ASSERT_TRUE(result.get()); | 117 ASSERT_TRUE(result.get()); |
| 107 EXPECT_EQ(2u, result->conditions().size()); | 118 EXPECT_EQ(2u, result->conditions().size()); |
| 108 | 119 |
| 109 // Tell the URLMatcher about our shiny new patterns. | 120 // Tell the URLMatcher about our shiny new patterns. |
| 110 URLMatcherConditionSet::Vector url_matcher_condition_set; | 121 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 111 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 122 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 112 matcher.AddConditionSets(url_matcher_condition_set); | 123 matcher.AddConditionSets(url_matcher_condition_set); |
| 113 | 124 |
| 114 std::set<URLMatcherConditionSet::ID> url_match_ids; | 125 std::set<URLMatcherConditionSet::ID> url_match_ids; |
| 115 int number_matches = 0; | 126 int number_matches = 0; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 144 number_matches = 0; | 155 number_matches = 0; |
| 145 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 156 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); |
| 146 i != url_match_ids.end(); ++i) { | 157 i != url_match_ids.end(); ++i) { |
| 147 if (result->IsFulfilled(*i, &https_foo_request, ON_BEFORE_REQUEST)) | 158 if (result->IsFulfilled(*i, &https_foo_request, ON_BEFORE_REQUEST)) |
| 148 ++number_matches; | 159 ++number_matches; |
| 149 } | 160 } |
| 150 EXPECT_EQ(0, number_matches); | 161 EXPECT_EQ(0, number_matches); |
| 151 } | 162 } |
| 152 | 163 |
| 153 } // namespace extensions | 164 } // namespace extensions |
| OLD | NEW |