Chromium Code Reviews| 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/net/chrome_network_delegate.h" | 5 #include "chrome/browser/net/chrome_network_delegate.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/api/prefs/pref_member.h" | 10 #include "chrome/browser/api/prefs/pref_member.h" |
| 11 #include "chrome/browser/extensions/event_router_forwarder.h" | 11 #include "chrome/browser/extensions/event_router_forwarder.h" |
| 12 #include "chrome/common/pref_names.h" | |
| 13 #include "chrome/common/url_constants.h" | |
| 14 #include "chrome/test/base/testing_pref_service.h" | |
| 15 #include "chrome/test/base/testing_profile.h" | |
| 16 #include "net/base/completion_callback.h" | |
| 17 #include "net/url_request/url_request.h" | |
| 12 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 20 |
| 15 class ChromeNetworkDelegateTest : public testing::Test { | 21 class ChromeNetworkDelegateTest : public testing::Test { |
| 16 protected: | 22 protected: |
| 17 ChromeNetworkDelegateTest() | 23 ChromeNetworkDelegateTest() |
| 18 : forwarder_(new extensions::EventRouterForwarder()) { | 24 : forwarder_(new extensions::EventRouterForwarder()) { |
| 19 } | 25 } |
| 20 | 26 |
| 21 virtual void SetUp() OVERRIDE { | 27 virtual void SetUp() OVERRIDE { |
| 22 never_throttle_requests_original_value_ = | 28 never_throttle_requests_original_value_ = |
| 23 ChromeNetworkDelegate::g_never_throttle_requests_; | 29 ChromeNetworkDelegate::g_never_throttle_requests_; |
| 24 ChromeNetworkDelegate::g_never_throttle_requests_ = false; | 30 ChromeNetworkDelegate::g_never_throttle_requests_ = false; |
| 25 } | 31 } |
| 26 | 32 |
| 27 virtual void TearDown() OVERRIDE { | 33 virtual void TearDown() OVERRIDE { |
| 28 ChromeNetworkDelegate::g_never_throttle_requests_ = | 34 ChromeNetworkDelegate::g_never_throttle_requests_ = |
| 29 never_throttle_requests_original_value_; | 35 never_throttle_requests_original_value_; |
| 30 } | 36 } |
| 31 | 37 |
| 32 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { | 38 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { |
| 33 return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate( | 39 return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate( |
| 34 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &pref_member_, NULL, | 40 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &pref_member_, NULL, |
| 35 NULL)); | 41 NULL, NULL)); |
| 36 } | 42 } |
| 37 | 43 |
| 38 // Implementation moved here for access to private bits. | 44 // Implementation moved here for access to private bits. |
| 39 void NeverThrottleLogicImpl() { | 45 void NeverThrottleLogicImpl() { |
| 40 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); | 46 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); |
| 41 | 47 |
| 42 TestURLRequestContext context; | 48 TestURLRequestContext context; |
| 43 TestURLRequest extension_request( | 49 TestURLRequest extension_request( |
| 44 GURL("http://example.com/"), NULL, &context); | 50 GURL("http://example.com/"), NULL, &context); |
| 45 extension_request.set_first_party_for_cookies( | 51 extension_request.set_first_party_for_cookies( |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 73 bool never_throttle_requests_original_value_; | 79 bool never_throttle_requests_original_value_; |
| 74 MessageLoopForIO message_loop_; | 80 MessageLoopForIO message_loop_; |
| 75 | 81 |
| 76 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | 82 scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
| 77 BooleanPrefMember pref_member_; | 83 BooleanPrefMember pref_member_; |
| 78 }; | 84 }; |
| 79 | 85 |
| 80 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { | 86 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { |
| 81 NeverThrottleLogicImpl(); | 87 NeverThrottleLogicImpl(); |
| 82 } | 88 } |
| 89 | |
| 90 class ChromeNetworkDelegateSafesearchTest : public testing::Test { | |
| 91 public: | |
| 92 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() { | |
| 93 return scoped_ptr<net::NetworkDelegate>(new ChromeNetworkDelegate( | |
| 94 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &enable_referrers_, | |
| 95 NULL, &force_google_safesearch_, NULL)); | |
| 96 } | |
| 97 | |
| 98 void SetSafesearch(bool value) { | |
|
battre
2012/10/30 12:56:10
In the chrome_network_delegate.cc you use the spel
Sergiu
2012/10/30 21:58:17
Done.
| |
| 99 force_google_safesearch_.SetValue(value); | |
|
battre
2012/10/30 12:56:10
if you use SafeSearch, this should be force_google
Sergiu
2012/10/30 21:58:17
Done.
| |
| 100 } | |
| 101 | |
| 102 void SetDelegate(net::NetworkDelegate* delegate) { | |
| 103 context_.set_network_delegate(delegate); | |
| 104 } | |
| 105 | |
| 106 protected: | |
| 107 ChromeNetworkDelegateSafesearchTest() | |
| 108 : forwarder_(new extensions::EventRouterForwarder()) { | |
| 109 } | |
| 110 | |
| 111 virtual void SetUp() OVERRIDE { | |
| 112 prefs_.RegisterBooleanPref(prefs::kForceSafeSearch, false, | |
| 113 PrefService::UNSYNCABLE_PREF); | |
| 114 force_google_safesearch_.Init(prefs::kForceSafeSearch, | |
| 115 profile_.GetTestingPrefService(), NULL); | |
| 116 prefs_.RegisterBooleanPref(prefs::kEnableReferrers, false, | |
| 117 PrefService::UNSYNCABLE_PREF); | |
| 118 enable_referrers_.Init(prefs::kEnableReferrers, | |
| 119 profile_.GetTestingPrefService(), NULL); | |
| 120 loop_.reset(new MessageLoopForIO()); | |
| 121 } | |
| 122 | |
| 123 // Verifies that the expected string is equal to the query part (between | |
| 124 // ? and #) of the url_string. | |
|
battre
2012/10/30 12:56:10
This comment is incorrect. The function does not c
Sergiu
2012/10/30 21:58:17
Done.
| |
| 125 void CheckAddedParameters(const std::string& url_string, | |
| 126 const std::string& expected) { | |
|
battre
2012/10/30 12:56:10
nit: rename |expected| to |expected_query_paramete
Sergiu
2012/10/30 21:58:17
Done.
| |
| 127 // Show the URL in the trace so we know where we failed. | |
| 128 SCOPED_TRACE(url_string); | |
| 129 | |
| 130 TestURLRequest request(GURL(url_string), &delegate_, &context_); | |
| 131 | |
| 132 request.Start(); | |
| 133 MessageLoop::current()->RunAllPending(); | |
| 134 | |
| 135 EXPECT_EQ(expected, request.url().query()); | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | |
| 140 TestingProfile profile_; | |
| 141 TestingPrefService prefs_; | |
| 142 BooleanPrefMember enable_referrers_; | |
|
battre
2012/10/30 12:56:10
do you use the referrers anywhere?
Sergiu
2012/10/30 21:58:17
They are used in the delegate constructor which do
battre
2012/10/31 13:16:55
I think we should make enable_referrers in the Chr
Sergiu
2012/11/01 18:01:52
Since this is a long commit already with a lot of
| |
| 143 BooleanPrefMember force_google_safesearch_; | |
| 144 scoped_ptr<net::URLRequest> request_; | |
| 145 TestURLRequestContext context_; | |
| 146 TestDelegate delegate_; | |
| 147 scoped_ptr<MessageLoopForIO> loop_; | |
| 148 }; | |
| 149 | |
| 150 TEST_F(ChromeNetworkDelegateSafesearchTest, SafesearchOn) { | |
| 151 // Tests with SafeSearch on, request parameters should be rewritten. | |
| 152 const std::string kSafeParameter = chrome::kSafeSearchSafeParameter; | |
| 153 const std::string kSsuiParameter = chrome::kSafeSearchSsuiParameter; | |
| 154 const std::string kBothParameters = kSafeParameter + "&" + kSsuiParameter; | |
| 155 SetSafesearch(true); | |
| 156 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); | |
| 157 SetDelegate(delegate.get()); | |
|
battre
2012/10/30 12:56:10
how about this?
net::NetworkDelegate delegate;
Set
Sergiu
2012/10/30 21:58:17
Could you explain a bit more here? Do you mean use
battre
2012/10/31 13:16:55
Oh, I am sorry, I did not notice this. This soluti
| |
| 158 | |
| 159 // Test the home page. | |
| 160 CheckAddedParameters("http://google.com/", kBothParameters); | |
| 161 | |
| 162 // Test the search home page. | |
| 163 CheckAddedParameters("http://google.com/webhp", | |
| 164 kBothParameters); | |
| 165 | |
| 166 // Test different valid search pages with parameters. | |
| 167 CheckAddedParameters("http://google.com/search?q=google", | |
| 168 "q=google&" + kBothParameters); | |
| 169 | |
| 170 CheckAddedParameters("http://google.com/?q=google", | |
| 171 "q=google&" + kBothParameters); | |
| 172 | |
| 173 CheckAddedParameters("http://google.com/webhp?q=google", | |
| 174 "q=google&" + kBothParameters); | |
| 175 | |
| 176 // Test the valid pages with safe set to off. | |
| 177 CheckAddedParameters("http://google.com/search?q=google&safe=off", | |
| 178 "q=google&" + kBothParameters); | |
| 179 | |
| 180 CheckAddedParameters("http://google.com/?q=google&safe=off", | |
| 181 "q=google&" + kBothParameters); | |
| 182 | |
| 183 CheckAddedParameters("http://google.com/webhp?q=google&safe=off", | |
| 184 "q=google&" + kBothParameters); | |
|
battre
2012/10/30 12:56:10
do the same thing with "safe" replaced with "%73af
Sergiu
2012/10/30 21:58:17
See comment in the chrome_network_delegate.cc file
| |
| 185 | |
| 186 // Test the home page, different TLDs. | |
| 187 CheckAddedParameters("http://google.de/", kBothParameters); | |
| 188 CheckAddedParameters("http://google.ro/", kBothParameters); | |
| 189 CheckAddedParameters("http://google.nl/", kBothParameters); | |
| 190 | |
| 191 // Test the search home page, different TLD. | |
| 192 CheckAddedParameters("http://google.de/webhp", kBothParameters); | |
| 193 | |
| 194 // Test the search page with parameters, different TLD. | |
| 195 CheckAddedParameters("http://google.de/search?q=google", | |
| 196 "q=google&" + kBothParameters); | |
| 197 | |
| 198 // Test the home page with parameters, different TLD. | |
| 199 CheckAddedParameters("http://google.de/?q=google", | |
| 200 "q=google&" + kBothParameters); | |
| 201 | |
| 202 // Test the search page with the parameters set. | |
| 203 CheckAddedParameters("http://google.de/?q=google&" + kBothParameters, | |
| 204 "q=google&" + kBothParameters); | |
| 205 | |
| 206 // Test some possibly tricky combinations. | |
| 207 CheckAddedParameters("http://google.com/?q=goog&" + kSafeParameter + | |
| 208 "&ssui=one", | |
| 209 "q=goog&" + kBothParameters); | |
| 210 | |
| 211 CheckAddedParameters("http://google.de/?q=goog&unsafe=active&" + | |
| 212 kSsuiParameter, | |
| 213 "q=goog&unsafe=active&" + kBothParameters); | |
| 214 | |
| 215 CheckAddedParameters("http://google.de/?q=goog&safe=off&ssui=off", | |
| 216 "q=goog&" + kBothParameters); | |
| 217 | |
| 218 // Test various combinations where we should not add anything. | |
| 219 CheckAddedParameters("http://google.com/?q=goog&" + kSsuiParameter + "&" + | |
| 220 kSafeParameter, | |
| 221 "q=goog&" + kBothParameters); | |
| 222 | |
| 223 CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&q=goog&" + | |
| 224 kSafeParameter, | |
| 225 "q=goog&" + kBothParameters); | |
| 226 | |
| 227 CheckAddedParameters("http://google.com/?" + kSsuiParameter + "&" + | |
| 228 kSafeParameter + "&q=goog", | |
| 229 "q=goog&" + kBothParameters); | |
| 230 | |
| 231 // Test that another website is not affected, without parameters. | |
| 232 CheckAddedParameters("http://google.com/finance", ""); | |
| 233 | |
| 234 // Test that another website is not affected, with parameters. | |
| 235 CheckAddedParameters("http://google.com/finance?q=goog", "q=goog"); | |
| 236 | |
| 237 // Test that another website is not affected with redirects, with parameters. | |
| 238 CheckAddedParameters("http://finance.google.com/?q=goog", "q=goog"); | |
| 239 | |
| 240 // Test with percent-encoded data (%26 is &) | |
| 241 CheckAddedParameters("http://google.com/?q=%26%26%26&" + kSsuiParameter + | |
| 242 "&" + kSafeParameter + "¶m=%26%26%26", | |
| 243 "q=%26%26%26¶m=%26%26%26&" + kBothParameters); | |
| 244 } | |
| 245 | |
| 246 TEST_F(ChromeNetworkDelegateSafesearchTest, SafesearchOff) { | |
| 247 // Tests with SafeSearch settings off, delegate should not alter requests. | |
| 248 SetSafesearch(false); | |
| 249 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); | |
| 250 SetDelegate(delegate.get()); | |
| 251 | |
| 252 // Test the home page. | |
| 253 CheckAddedParameters("http://google.com/", ""); | |
| 254 | |
| 255 // Test the search home page. | |
| 256 CheckAddedParameters("http://google.com/webhp", ""); | |
| 257 | |
| 258 // Test the home page with parameters. | |
| 259 CheckAddedParameters("http://google.com/search?q=google", | |
| 260 "q=google"); | |
| 261 | |
| 262 // Test the search page with parameters. | |
| 263 CheckAddedParameters("http://google.com/?q=google", | |
| 264 "q=google"); | |
| 265 | |
| 266 // Test the search webhp page with parameters. | |
| 267 CheckAddedParameters("http://google.com/webhp?q=google", | |
| 268 "q=google"); | |
| 269 | |
| 270 // Test the home page with parameters and safe set to off. | |
| 271 CheckAddedParameters("http://google.com/search?q=google&safe=off", | |
| 272 "q=google&safe=off"); | |
| 273 | |
| 274 // Test the home page with parameters and safe set to active. | |
| 275 CheckAddedParameters("http://google.com/search?q=google&safe=active", | |
| 276 "q=google&safe=active"); | |
| 277 } | |
| OLD | NEW |