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/test/values_test_util.h" | 10 #include "base/test/values_test_util.h" |
(...skipping 10 matching lines...) Expand all Loading... | |
21 namespace keys2 = url_matcher_constants; | 21 namespace keys2 = url_matcher_constants; |
22 | 22 |
23 TEST(WebRequestConditionTest, CreateCondition) { | 23 TEST(WebRequestConditionTest, CreateCondition) { |
24 // Necessary for TestURLRequest. | 24 // Necessary for TestURLRequest. |
25 MessageLoop message_loop(MessageLoop::TYPE_IO); | 25 MessageLoop message_loop(MessageLoop::TYPE_IO); |
26 URLMatcher matcher; | 26 URLMatcher matcher; |
27 | 27 |
28 std::string error; | 28 std::string error; |
29 scoped_ptr<WebRequestCondition> result; | 29 scoped_ptr<WebRequestCondition> result; |
30 | 30 |
31 | |
32 // Test wrong condition name passed. | 31 // Test wrong condition name passed. |
33 error.clear(); | 32 error.clear(); |
34 result = WebRequestCondition::Create( | 33 result = WebRequestCondition::Create( |
35 matcher.condition_factory(), | 34 matcher.condition_factory(), |
36 *base::test::ParseJson( | 35 *base::test::ParseJson( |
37 "{ \"invalid\": \"foobar\", \n" | 36 "{ \"invalid\": \"foobar\", \n" |
38 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 37 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
39 "}"), | 38 "}"), |
40 &error); | 39 &error); |
41 EXPECT_FALSE(error.empty()); | 40 EXPECT_FALSE(error.empty()); |
(...skipping 13 matching lines...) Expand all Loading... | |
55 EXPECT_FALSE(result.get()); | 54 EXPECT_FALSE(result.get()); |
56 | 55 |
57 // Test success (can we support multiple criteria?) | 56 // Test success (can we support multiple criteria?) |
58 error.clear(); | 57 error.clear(); |
59 result = WebRequestCondition::Create( | 58 result = WebRequestCondition::Create( |
60 matcher.condition_factory(), | 59 matcher.condition_factory(), |
61 *base::test::ParseJson( | 60 *base::test::ParseJson( |
62 "{ \n" | 61 "{ \n" |
63 " \"resourceType\": [\"main_frame\"], \n" | 62 " \"resourceType\": [\"main_frame\"], \n" |
64 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" | 63 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" |
64 " \"firstPartyForCookiesUrl\": { \"hostPrefix\": \"fpfc\"}, \n" | |
65 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 65 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
66 "}"), | 66 "}"), |
67 &error); | 67 &error); |
68 EXPECT_EQ("", error); | 68 EXPECT_EQ("", error); |
69 ASSERT_TRUE(result.get()); | 69 ASSERT_TRUE(result.get()); |
70 | 70 |
71 URLMatcherConditionSet::Vector url_matcher_condition_set; | 71 URLMatcherConditionSet::Vector url_matcher_condition_set; |
72 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 72 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
73 matcher.AddConditionSets(url_matcher_condition_set); | 73 matcher.AddConditionSets(url_matcher_condition_set); |
74 std::set<URLMatcherConditionSet::ID> url_match_ids; | |
75 | 74 |
76 net::TestURLRequestContext context; | 75 net::TestURLRequestContext context; |
77 GURL http_url("http://www.example.com"); | 76 GURL http_url("http://www.example.com"); |
77 GURL first_party_url("http://fpfc.example.com"); | |
78 net::TestURLRequest match_request(http_url, NULL, &context); | 78 net::TestURLRequest match_request(http_url, NULL, &context); |
79 url_match_ids = matcher.MatchURL(http_url); | 79 DeclarativeWebRequestData request_data(&match_request, ON_BEFORE_REQUEST); |
80 EXPECT_EQ(1u, url_match_ids.size()); | 80 request_data.url_match_ids = matcher.MatchURL(http_url); |
81 EXPECT_EQ(1u, request_data.url_match_ids.size()); | |
82 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); | |
83 EXPECT_EQ(1u, request_data.url_match_ids.size()); | |
84 std::set<URLMatcherConditionSet::ID> url_match_ids( | |
vabr (Chromium)
2013/01/22 14:54:30
This would go away if we got rid of the now-unused
| |
85 request_data.url_match_ids); | |
86 url_match_ids.insert(request_data.first_party_url_match_ids.begin(), | |
87 request_data.first_party_url_match_ids.end()); | |
81 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 88 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
82 ResourceType::MAIN_FRAME, NULL, -1, -1); | 89 ResourceType::MAIN_FRAME, NULL, -1, -1); |
83 EXPECT_TRUE(result->IsFulfilled( | 90 EXPECT_TRUE(result->IsFulfilled(url_match_ids, request_data)); |
84 url_match_ids, | |
85 DeclarativeWebRequestData(&match_request, ON_BEFORE_REQUEST))); | |
86 | 91 |
87 GURL https_url("https://www.example.com"); | 92 GURL https_url("https://www.example.com"); |
88 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); | 93 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); |
89 url_match_ids = matcher.MatchURL(https_url); | 94 request_data.request = &wrong_resource_type; |
95 request_data.url_match_ids = matcher.MatchURL(http_url); | |
90 // Make sure IsFulfilled does not fail because of URL matching. | 96 // Make sure IsFulfilled does not fail because of URL matching. |
91 EXPECT_EQ(1u, url_match_ids.size()); | 97 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
98 url_match_ids = request_data.url_match_ids; | |
99 url_match_ids.insert(request_data.first_party_url_match_ids.begin(), | |
100 request_data.first_party_url_match_ids.end()); | |
92 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 101 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
93 ResourceType::SUB_FRAME, NULL, -1, -1); | 102 ResourceType::SUB_FRAME, NULL, -1, -1); |
94 EXPECT_FALSE(result->IsFulfilled( | 103 EXPECT_FALSE(result->IsFulfilled(url_match_ids, request_data)); |
95 url_match_ids, | |
96 DeclarativeWebRequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | |
97 } | 104 } |
98 | 105 |
99 // Conditions without UrlFilter attributes need to be independent of URL | 106 // Conditions without UrlFilter attributes need to be independent of URL |
100 // matching results. We test here that: | 107 // matching results. We test here that: |
101 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 108 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
102 // attributes are fulfilled. | 109 // attributes are fulfilled. |
103 // 2. An empty condition (in particular, without UrlFilter attributes) is | 110 // 2. An empty condition (in particular, without UrlFilter attributes) is |
104 // always fulfilled. | 111 // always fulfilled. |
105 TEST(WebRequestConditionTest, NoUrlAttributes) { | 112 TEST(WebRequestConditionTest, NoUrlAttributes) { |
106 // Necessary for TestURLRequest. | 113 // Necessary for TestURLRequest. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 | 219 |
213 std::set<URLMatcherConditionSet::ID> url_match_ids; | 220 std::set<URLMatcherConditionSet::ID> url_match_ids; |
214 | 221 |
215 // Test that the result is correct and matches http://www.example.com and | 222 // Test that the result is correct and matches http://www.example.com and |
216 // https://www.example.com | 223 // https://www.example.com |
217 GURL http_url("http://www.example.com"); | 224 GURL http_url("http://www.example.com"); |
218 net::TestURLRequestContext context; | 225 net::TestURLRequestContext context; |
219 net::TestURLRequest http_request(http_url, NULL, &context); | 226 net::TestURLRequest http_request(http_url, NULL, &context); |
220 url_match_ids = matcher.MatchURL(http_url); | 227 url_match_ids = matcher.MatchURL(http_url); |
221 EXPECT_EQ(1u, url_match_ids.size()); | 228 EXPECT_EQ(1u, url_match_ids.size()); |
222 EXPECT_TRUE(result->IsFulfilled( | 229 DeclarativeWebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
223 *(url_match_ids.begin()), | 230 request_data.url_match_ids = url_match_ids; |
224 url_match_ids, | 231 EXPECT_TRUE(result->IsFulfilled(*(url_match_ids.begin()), url_match_ids, |
225 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST))); | 232 request_data)); |
226 | 233 |
227 GURL https_url("https://www.example.com"); | 234 GURL https_url("https://www.example.com"); |
228 url_match_ids = matcher.MatchURL(https_url); | 235 url_match_ids = matcher.MatchURL(https_url); |
229 EXPECT_EQ(1u, url_match_ids.size()); | 236 EXPECT_EQ(1u, url_match_ids.size()); |
230 net::TestURLRequest https_request(https_url, NULL, &context); | 237 net::TestURLRequest https_request(https_url, NULL, &context); |
231 EXPECT_TRUE(result->IsFulfilled( | 238 request_data.request = &https_request; |
232 *(url_match_ids.begin()), | 239 request_data.url_match_ids = url_match_ids; |
233 url_match_ids, | 240 EXPECT_TRUE(result->IsFulfilled(*(url_match_ids.begin()), url_match_ids, |
234 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | 241 request_data)); |
235 | 242 |
236 // Check that both, hostPrefix and hostSuffix are evaluated. | 243 // Check that both, hostPrefix and hostSuffix are evaluated. |
237 GURL https_foo_url("https://foo.example.com"); | 244 GURL https_foo_url("https://foo.example.com"); |
238 url_match_ids = matcher.MatchURL(https_foo_url); | 245 url_match_ids = matcher.MatchURL(https_foo_url); |
239 EXPECT_EQ(0u, url_match_ids.size()); | 246 EXPECT_EQ(0u, url_match_ids.size()); |
240 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 247 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
241 EXPECT_FALSE(result->IsFulfilled( | 248 request_data.request = &https_foo_request; |
242 -1, | 249 request_data.url_match_ids = url_match_ids; |
243 url_match_ids, | 250 EXPECT_FALSE(result->IsFulfilled(-1, url_match_ids, request_data)); |
244 DeclarativeWebRequestData(&https_foo_request, ON_BEFORE_REQUEST))); | |
245 } | 251 } |
246 | 252 |
247 TEST(WebRequestConditionTest, TestPortFilter) { | 253 TEST(WebRequestConditionTest, TestPortFilter) { |
248 // Necessary for TestURLRequest. | 254 // Necessary for TestURLRequest. |
249 MessageLoop message_loop(MessageLoop::TYPE_IO); | 255 MessageLoop message_loop(MessageLoop::TYPE_IO); |
250 URLMatcher matcher; | 256 URLMatcher matcher; |
251 | 257 |
252 WebRequestConditionSet::AnyVector conditions; | 258 WebRequestConditionSet::AnyVector conditions; |
253 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 259 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
254 "{ \n" | 260 "{ \n" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 // the response header. The Create() method should fail and complain that it is | 308 // the response header. The Create() method should fail and complain that it is |
303 // impossible that both conditions are fulfilled at the same time. | 309 // impossible that both conditions are fulfilled at the same time. |
304 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 310 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
305 // Necessary for TestURLRequest. | 311 // Necessary for TestURLRequest. |
306 MessageLoop message_loop(MessageLoop::TYPE_IO); | 312 MessageLoop message_loop(MessageLoop::TYPE_IO); |
307 URLMatcher matcher; | 313 URLMatcher matcher; |
308 | 314 |
309 std::string error; | 315 std::string error; |
310 scoped_ptr<WebRequestCondition> result; | 316 scoped_ptr<WebRequestCondition> result; |
311 | 317 |
312 DictionaryValue condition_value; | |
313 condition_value.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | |
314 | |
315 | |
316 // Test error on incompatible application stages for involved attributes. | 318 // Test error on incompatible application stages for involved attributes. |
317 error.clear(); | 319 error.clear(); |
318 result = WebRequestCondition::Create( | 320 result = WebRequestCondition::Create( |
319 matcher.condition_factory(), | 321 matcher.condition_factory(), |
320 *base::test::ParseJson( | 322 *base::test::ParseJson( |
321 "{ \n" | 323 "{ \n" |
322 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 324 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
323 // Pass a JS array with one empty object to each of the header | 325 // Pass a JS array with one empty object to each of the header |
324 // filters. | 326 // filters. |
325 " \"requestHeaders\": [{}], \n" | 327 " \"requestHeaders\": [{}], \n" |
326 " \"responseHeaders\": [{}], \n" | 328 " \"responseHeaders\": [{}], \n" |
327 "}"), | 329 "}"), |
328 &error); | 330 &error); |
329 EXPECT_FALSE(error.empty()); | 331 EXPECT_FALSE(error.empty()); |
330 EXPECT_FALSE(result.get()); | 332 EXPECT_FALSE(result.get()); |
331 } | 333 } |
332 | 334 |
333 } // namespace extensions | 335 } // namespace extensions |
OLD | NEW |