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" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 EXPECT_FALSE(error.empty()); | 61 EXPECT_FALSE(error.empty()); |
62 EXPECT_FALSE(result.get()); | 62 EXPECT_FALSE(result.get()); |
63 | 63 |
64 // Test success (can we support multiple criteria?) | 64 // Test success (can we support multiple criteria?) |
65 error.clear(); | 65 error.clear(); |
66 result = WebRequestCondition::Create(matcher.condition_factory(), | 66 result = WebRequestCondition::Create(matcher.condition_factory(), |
67 valid_condition, &error); | 67 valid_condition, &error); |
68 EXPECT_EQ("", error); | 68 EXPECT_EQ("", error); |
69 ASSERT_TRUE(result.get()); | 69 ASSERT_TRUE(result.get()); |
70 | 70 |
71 TestURLRequest match_request(GURL("http://www.example.com"), NULL); | 71 TestURLRequestContext context; |
| 72 TestURLRequest match_request(GURL("http://www.example.com"), NULL, &context); |
72 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 73 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
73 ResourceType::MAIN_FRAME, NULL, -1, -1); | 74 ResourceType::MAIN_FRAME, NULL, -1, -1); |
74 EXPECT_TRUE(result->IsFulfilled(&match_request, ON_BEFORE_REQUEST)); | 75 EXPECT_TRUE(result->IsFulfilled(&match_request, ON_BEFORE_REQUEST)); |
75 | 76 |
76 TestURLRequest wrong_resource_type(GURL("https://www.example.com"), NULL); | 77 TestURLRequest wrong_resource_type( |
| 78 GURL("https://www.example.com"), NULL, &context); |
77 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 79 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
78 ResourceType::SUB_FRAME, NULL, -1, -1); | 80 ResourceType::SUB_FRAME, NULL, -1, -1); |
79 EXPECT_FALSE(result->IsFulfilled(&wrong_resource_type, ON_BEFORE_REQUEST)); | 81 EXPECT_FALSE(result->IsFulfilled(&wrong_resource_type, ON_BEFORE_REQUEST)); |
80 } | 82 } |
81 | 83 |
82 TEST(WebRequestConditionTest, CreateConditionSet) { | 84 TEST(WebRequestConditionTest, CreateConditionSet) { |
83 // Necessary for TestURLRequest. | 85 // Necessary for TestURLRequest. |
84 MessageLoop message_loop(MessageLoop::TYPE_IO); | 86 MessageLoop message_loop(MessageLoop::TYPE_IO); |
85 URLMatcher matcher; | 87 URLMatcher matcher; |
86 | 88 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 URLMatcherConditionSet::Vector url_matcher_condition_set; | 130 URLMatcherConditionSet::Vector url_matcher_condition_set; |
129 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 131 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
130 matcher.AddConditionSets(url_matcher_condition_set); | 132 matcher.AddConditionSets(url_matcher_condition_set); |
131 | 133 |
132 std::set<URLMatcherConditionSet::ID> url_match_ids; | 134 std::set<URLMatcherConditionSet::ID> url_match_ids; |
133 int number_matches = 0; | 135 int number_matches = 0; |
134 | 136 |
135 // Test that the result is correct and matches http://www.example.com and | 137 // Test that the result is correct and matches http://www.example.com and |
136 // https://www.example.com | 138 // https://www.example.com |
137 GURL http_url("http://www.example.com"); | 139 GURL http_url("http://www.example.com"); |
138 TestURLRequest http_request(http_url, NULL); | 140 TestURLRequestContext context; |
| 141 TestURLRequest http_request(http_url, NULL, &context); |
139 url_match_ids = matcher.MatchURL(http_url); | 142 url_match_ids = matcher.MatchURL(http_url); |
140 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 143 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); |
141 i != url_match_ids.end(); ++i) { | 144 i != url_match_ids.end(); ++i) { |
142 if (result->IsFulfilled(*i, &http_request, ON_BEFORE_REQUEST)) | 145 if (result->IsFulfilled(*i, &http_request, ON_BEFORE_REQUEST)) |
143 ++number_matches; | 146 ++number_matches; |
144 } | 147 } |
145 EXPECT_EQ(1, number_matches); | 148 EXPECT_EQ(1, number_matches); |
146 | 149 |
147 GURL https_url("https://www.example.com"); | 150 GURL https_url("https://www.example.com"); |
148 url_match_ids = matcher.MatchURL(https_url); | 151 url_match_ids = matcher.MatchURL(https_url); |
149 TestURLRequest https_request(https_url, NULL); | 152 TestURLRequest https_request(https_url, NULL, &context); |
150 number_matches = 0; | 153 number_matches = 0; |
151 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 154 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); |
152 i != url_match_ids.end(); ++i) { | 155 i != url_match_ids.end(); ++i) { |
153 if (result->IsFulfilled(*i, &https_request, ON_BEFORE_REQUEST)) | 156 if (result->IsFulfilled(*i, &https_request, ON_BEFORE_REQUEST)) |
154 ++number_matches; | 157 ++number_matches; |
155 } | 158 } |
156 EXPECT_EQ(1, number_matches); | 159 EXPECT_EQ(1, number_matches); |
157 | 160 |
158 // Check that both, hostPrefix and hostSuffix are evaluated. | 161 // Check that both, hostPrefix and hostSuffix are evaluated. |
159 GURL https_foo_url("https://foo.example.com"); | 162 GURL https_foo_url("https://foo.example.com"); |
160 url_match_ids = matcher.MatchURL(https_foo_url); | 163 url_match_ids = matcher.MatchURL(https_foo_url); |
161 TestURLRequest https_foo_request(https_foo_url, NULL); | 164 TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
162 number_matches = 0; | 165 number_matches = 0; |
163 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 166 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); |
164 i != url_match_ids.end(); ++i) { | 167 i != url_match_ids.end(); ++i) { |
165 if (result->IsFulfilled(*i, &https_foo_request, ON_BEFORE_REQUEST)) | 168 if (result->IsFulfilled(*i, &https_foo_request, ON_BEFORE_REQUEST)) |
166 ++number_matches; | 169 ++number_matches; |
167 } | 170 } |
168 EXPECT_EQ(0, number_matches); | 171 EXPECT_EQ(0, number_matches); |
169 } | 172 } |
170 | 173 |
171 TEST(WebRequestConditionTest, TestPortFilter) { | 174 TEST(WebRequestConditionTest, TestPortFilter) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 | 209 |
207 // Tell the URLMatcher about our shiny new patterns. | 210 // Tell the URLMatcher about our shiny new patterns. |
208 URLMatcherConditionSet::Vector url_matcher_condition_set; | 211 URLMatcherConditionSet::Vector url_matcher_condition_set; |
209 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 212 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
210 matcher.AddConditionSets(url_matcher_condition_set); | 213 matcher.AddConditionSets(url_matcher_condition_set); |
211 | 214 |
212 std::set<URLMatcherConditionSet::ID> url_match_ids; | 215 std::set<URLMatcherConditionSet::ID> url_match_ids; |
213 | 216 |
214 // Test various URLs. | 217 // Test various URLs. |
215 GURL http_url("http://www.example.com"); | 218 GURL http_url("http://www.example.com"); |
216 TestURLRequest http_request(http_url, NULL); | 219 TestURLRequestContext context; |
| 220 TestURLRequest http_request(http_url, NULL, &context); |
217 url_match_ids = matcher.MatchURL(http_url); | 221 url_match_ids = matcher.MatchURL(http_url); |
218 ASSERT_EQ(1u, url_match_ids.size()); | 222 ASSERT_EQ(1u, url_match_ids.size()); |
219 | 223 |
220 GURL http_url_80("http://www.example.com:80"); | 224 GURL http_url_80("http://www.example.com:80"); |
221 TestURLRequest http_request_80(http_url_80, NULL); | 225 TestURLRequest http_request_80(http_url_80, NULL, &context); |
222 url_match_ids = matcher.MatchURL(http_url_80); | 226 url_match_ids = matcher.MatchURL(http_url_80); |
223 ASSERT_EQ(1u, url_match_ids.size()); | 227 ASSERT_EQ(1u, url_match_ids.size()); |
224 | 228 |
225 GURL http_url_1000("http://www.example.com:1000"); | 229 GURL http_url_1000("http://www.example.com:1000"); |
226 TestURLRequest http_request_1000(http_url_1000, NULL); | 230 TestURLRequest http_request_1000(http_url_1000, NULL, &context); |
227 url_match_ids = matcher.MatchURL(http_url_1000); | 231 url_match_ids = matcher.MatchURL(http_url_1000); |
228 ASSERT_EQ(1u, url_match_ids.size()); | 232 ASSERT_EQ(1u, url_match_ids.size()); |
229 | 233 |
230 GURL http_url_2000("http://www.example.com:2000"); | 234 GURL http_url_2000("http://www.example.com:2000"); |
231 TestURLRequest http_request_2000(http_url_2000, NULL); | 235 TestURLRequest http_request_2000(http_url_2000, NULL, &context); |
232 url_match_ids = matcher.MatchURL(http_url_2000); | 236 url_match_ids = matcher.MatchURL(http_url_2000); |
233 ASSERT_EQ(0u, url_match_ids.size()); | 237 ASSERT_EQ(0u, url_match_ids.size()); |
234 } | 238 } |
235 | 239 |
236 } // namespace extensions | 240 } // namespace extensions |
OLD | NEW |