| 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" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h
" | |
| 14 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 13 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
| 15 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 17 |
| 19 namespace extensions { | 18 namespace extensions { |
| 20 | 19 |
| 21 namespace keys = declarative_webrequest_constants; | 20 namespace keys = declarative_webrequest_constants; |
| 22 namespace keys2 = url_matcher_constants; | 21 namespace keys2 = url_matcher_constants; |
| 23 | 22 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 &error); | 67 &error); |
| 69 EXPECT_EQ("", error); | 68 EXPECT_EQ("", error); |
| 70 ASSERT_TRUE(result.get()); | 69 ASSERT_TRUE(result.get()); |
| 71 | 70 |
| 72 net::TestURLRequestContext context; | 71 net::TestURLRequestContext context; |
| 73 net::TestURLRequest match_request( | 72 net::TestURLRequest match_request( |
| 74 GURL("http://www.example.com"), NULL, &context); | 73 GURL("http://www.example.com"), NULL, &context); |
| 75 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 74 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
| 76 ResourceType::MAIN_FRAME, NULL, -1, -1); | 75 ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 77 EXPECT_TRUE(result->IsFulfilled( | 76 EXPECT_TRUE(result->IsFulfilled( |
| 78 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); | 77 DeclarativeWebRequestData(&match_request, ON_BEFORE_REQUEST))); |
| 79 | 78 |
| 80 net::TestURLRequest wrong_resource_type( | 79 net::TestURLRequest wrong_resource_type( |
| 81 GURL("https://www.example.com"), NULL, &context); | 80 GURL("https://www.example.com"), NULL, &context); |
| 82 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 81 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
| 83 ResourceType::SUB_FRAME, NULL, -1, -1); | 82 ResourceType::SUB_FRAME, NULL, -1, -1); |
| 84 EXPECT_FALSE(result->IsFulfilled( | 83 EXPECT_FALSE(result->IsFulfilled( |
| 85 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | 84 DeclarativeWebRequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); |
| 86 } | 85 } |
| 87 | 86 |
| 88 TEST(WebRequestConditionTest, CreateConditionSet) { | 87 TEST(WebRequestConditionTest, CreateConditionSet) { |
| 89 // Necessary for TestURLRequest. | 88 // Necessary for TestURLRequest. |
| 90 MessageLoop message_loop(MessageLoop::TYPE_IO); | 89 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 91 URLMatcher matcher; | 90 URLMatcher matcher; |
| 92 | 91 |
| 93 WebRequestConditionSet::AnyVector conditions; | 92 WebRequestConditionSet::AnyVector conditions; |
| 94 | 93 |
| 95 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 94 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 135 |
| 137 // Test that the result is correct and matches http://www.example.com and | 136 // Test that the result is correct and matches http://www.example.com and |
| 138 // https://www.example.com | 137 // https://www.example.com |
| 139 GURL http_url("http://www.example.com"); | 138 GURL http_url("http://www.example.com"); |
| 140 net::TestURLRequestContext context; | 139 net::TestURLRequestContext context; |
| 141 net::TestURLRequest http_request(http_url, NULL, &context); | 140 net::TestURLRequest http_request(http_url, NULL, &context); |
| 142 url_match_ids = matcher.MatchURL(http_url); | 141 url_match_ids = matcher.MatchURL(http_url); |
| 143 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 142 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); |
| 144 i != url_match_ids.end(); ++i) { | 143 i != url_match_ids.end(); ++i) { |
| 145 if (result->IsFulfilled( | 144 if (result->IsFulfilled( |
| 146 *i, WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST))) | 145 *i, DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST))) |
| 147 ++number_matches; | 146 ++number_matches; |
| 148 } | 147 } |
| 149 EXPECT_EQ(1, number_matches); | 148 EXPECT_EQ(1, number_matches); |
| 150 | 149 |
| 151 GURL https_url("https://www.example.com"); | 150 GURL https_url("https://www.example.com"); |
| 152 url_match_ids = matcher.MatchURL(https_url); | 151 url_match_ids = matcher.MatchURL(https_url); |
| 153 net::TestURLRequest https_request(https_url, NULL, &context); | 152 net::TestURLRequest https_request(https_url, NULL, &context); |
| 154 number_matches = 0; | 153 number_matches = 0; |
| 155 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 154 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); |
| 156 i != url_match_ids.end(); ++i) { | 155 i != url_match_ids.end(); ++i) { |
| 157 if (result->IsFulfilled( | 156 if (result->IsFulfilled( |
| 158 *i, WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))) | 157 *i, DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))) |
| 159 ++number_matches; | 158 ++number_matches; |
| 160 } | 159 } |
| 161 EXPECT_EQ(1, number_matches); | 160 EXPECT_EQ(1, number_matches); |
| 162 | 161 |
| 163 // Check that both, hostPrefix and hostSuffix are evaluated. | 162 // Check that both, hostPrefix and hostSuffix are evaluated. |
| 164 GURL https_foo_url("https://foo.example.com"); | 163 GURL https_foo_url("https://foo.example.com"); |
| 165 url_match_ids = matcher.MatchURL(https_foo_url); | 164 url_match_ids = matcher.MatchURL(https_foo_url); |
| 166 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 165 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
| 167 number_matches = 0; | 166 number_matches = 0; |
| 168 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 167 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); |
| 169 i != url_match_ids.end(); ++i) { | 168 i != url_match_ids.end(); ++i) { |
| 170 if (result->IsFulfilled( | 169 if (result->IsFulfilled( |
| 171 *i, WebRequestRule::RequestData( | 170 *i, DeclarativeWebRequestData( |
| 172 &https_foo_request, ON_BEFORE_REQUEST))) | 171 &https_foo_request, ON_BEFORE_REQUEST))) |
| 173 ++number_matches; | 172 ++number_matches; |
| 174 } | 173 } |
| 175 EXPECT_EQ(0, number_matches); | 174 EXPECT_EQ(0, number_matches); |
| 176 } | 175 } |
| 177 | 176 |
| 178 TEST(WebRequestConditionTest, TestPortFilter) { | 177 TEST(WebRequestConditionTest, TestPortFilter) { |
| 179 // Necessary for TestURLRequest. | 178 // Necessary for TestURLRequest. |
| 180 MessageLoop message_loop(MessageLoop::TYPE_IO); | 179 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 181 URLMatcher matcher; | 180 URLMatcher matcher; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 // filters. | 257 // filters. |
| 259 " \"requestHeaders\": [{}], \n" | 258 " \"requestHeaders\": [{}], \n" |
| 260 " \"responseHeaders\": [{}], \n" | 259 " \"responseHeaders\": [{}], \n" |
| 261 "}"), | 260 "}"), |
| 262 &error); | 261 &error); |
| 263 EXPECT_FALSE(error.empty()); | 262 EXPECT_FALSE(error.empty()); |
| 264 EXPECT_FALSE(result.get()); | 263 EXPECT_FALSE(result.get()); |
| 265 } | 264 } |
| 266 | 265 |
| 267 } // namespace extensions | 266 } // namespace extensions |
| OLD | NEW |