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 |
| 21 namespace { | |
| 22 const std::string kSingleParameters = chrome::kSafeSearchParameters; | |
| 23 const std::string kMultipleParameters = "&"+kSingleParameters; | |
| 24 } | |
| 25 | |
| 15 class ChromeNetworkDelegateTest : public testing::Test { | 26 class ChromeNetworkDelegateTest : public testing::Test { |
| 16 protected: | 27 protected: |
| 17 ChromeNetworkDelegateTest() | 28 ChromeNetworkDelegateTest() |
| 18 : forwarder_(new extensions::EventRouterForwarder()) { | 29 : forwarder_(new extensions::EventRouterForwarder()) { |
| 19 } | 30 } |
| 20 | 31 |
| 21 virtual void SetUp() OVERRIDE { | 32 virtual void SetUp() OVERRIDE { |
| 22 never_throttle_requests_original_value_ = | 33 never_throttle_requests_original_value_ = |
| 23 ChromeNetworkDelegate::g_never_throttle_requests_; | 34 ChromeNetworkDelegate::g_never_throttle_requests_; |
| 24 ChromeNetworkDelegate::g_never_throttle_requests_ = false; | 35 ChromeNetworkDelegate::g_never_throttle_requests_ = false; |
| 25 } | 36 } |
| 26 | 37 |
| 27 virtual void TearDown() OVERRIDE { | 38 virtual void TearDown() OVERRIDE { |
| 28 ChromeNetworkDelegate::g_never_throttle_requests_ = | 39 ChromeNetworkDelegate::g_never_throttle_requests_ = |
| 29 never_throttle_requests_original_value_; | 40 never_throttle_requests_original_value_; |
| 30 } | 41 } |
| 31 | 42 |
| 32 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { | 43 scoped_ptr<ChromeNetworkDelegate> CreateNetworkDelegate() { |
| 33 return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate( | 44 return scoped_ptr<ChromeNetworkDelegate>(new ChromeNetworkDelegate( |
| 34 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &pref_member_, NULL, | 45 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &pref_member_, NULL, |
| 35 NULL)); | 46 NULL, NULL)); |
| 36 } | 47 } |
| 37 | 48 |
| 38 // Implementation moved here for access to private bits. | 49 // Implementation moved here for access to private bits. |
| 39 void NeverThrottleLogicImpl() { | 50 void NeverThrottleLogicImpl() { |
| 40 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); | 51 scoped_ptr<ChromeNetworkDelegate> delegate(CreateNetworkDelegate()); |
| 41 | 52 |
| 42 TestURLRequestContext context; | 53 TestURLRequestContext context; |
| 43 TestURLRequest extension_request( | 54 TestURLRequest extension_request( |
| 44 GURL("http://example.com/"), NULL, &context); | 55 GURL("http://example.com/"), NULL, &context); |
| 45 extension_request.set_first_party_for_cookies( | 56 extension_request.set_first_party_for_cookies( |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 73 bool never_throttle_requests_original_value_; | 84 bool never_throttle_requests_original_value_; |
| 74 MessageLoopForIO message_loop_; | 85 MessageLoopForIO message_loop_; |
| 75 | 86 |
| 76 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | 87 scoped_refptr<extensions::EventRouterForwarder> forwarder_; |
| 77 BooleanPrefMember pref_member_; | 88 BooleanPrefMember pref_member_; |
| 78 }; | 89 }; |
| 79 | 90 |
| 80 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { | 91 TEST_F(ChromeNetworkDelegateTest, NeverThrottleLogic) { |
| 81 NeverThrottleLogicImpl(); | 92 NeverThrottleLogicImpl(); |
| 82 } | 93 } |
| 94 | |
| 95 class ChromeNetworkDelegateSafesearchTest : public testing::Test { | |
| 96 public: | |
| 97 scoped_ptr<net::NetworkDelegate> CreateNetworkDelegate() { | |
| 98 return scoped_ptr<net::NetworkDelegate>(new ChromeNetworkDelegate( | |
| 99 forwarder_.get(), NULL, NULL, NULL, NULL, NULL, &enable_referrers_, | |
|
Bernhard Bauer
2012/10/17 15:23:54
Do we need |enable_referrers_|?
Sergiu
2012/10/19 08:40:55
The other test has it as well, because on line 272
| |
| 100 NULL, &force_google_safesearch_, NULL)); | |
| 101 } | |
| 102 | |
| 103 void SetSafesearch(bool value) { | |
| 104 force_google_safesearch_.SetValue(value); | |
| 105 } | |
| 106 | |
| 107 void SetDelegate(net::NetworkDelegate* delegate) { | |
| 108 context_.set_network_delegate(delegate); | |
| 109 } | |
| 110 | |
| 111 protected: | |
| 112 ChromeNetworkDelegateSafesearchTest() | |
| 113 : forwarder_(new extensions::EventRouterForwarder()) { | |
| 114 } | |
| 115 | |
| 116 virtual void SetUp() OVERRIDE { | |
| 117 prefs_.RegisterBooleanPref(prefs::kForceSafeSearch, false, | |
| 118 PrefService::UNSYNCABLE_PREF); | |
| 119 force_google_safesearch_.Init(prefs::kForceSafeSearch, | |
| 120 profile_.GetTestingPrefService(), NULL); | |
|
Bernhard Bauer
2012/10/17 15:23:54
Nit: align with parameter on previous line.
Sergiu
2012/10/19 08:40:55
Done.
| |
| 121 prefs_.RegisterBooleanPref(prefs::kEnableReferrers, false, | |
| 122 PrefService::UNSYNCABLE_PREF); | |
| 123 enable_referrers_.Init(prefs::kEnableReferrers, | |
| 124 profile_.GetTestingPrefService(), NULL); | |
| 125 loop_.reset(new MessageLoopForIO()); | |
| 126 } | |
| 127 | |
| 128 // Verifies that the expected string in present only once in the url_string | |
| 129 void CheckAddedParameters(const std::string& url_string, | |
| 130 std::string expected, | |
| 131 const bool exact) { | |
| 132 // Show the URL in the trace so we know where we failed. | |
| 133 SCOPED_TRACE(url_string); | |
| 134 | |
| 135 TestURLRequest request(GURL(url_string), &delegate_, &context_); | |
| 136 | |
| 137 request.Start(); | |
| 138 MessageLoop::current()->RunAllPending(); | |
| 139 | |
| 140 if (exact) { | |
| 141 EXPECT_STREQ(expected.c_str(), request.url().query().c_str()); | |
|
Bernhard Bauer
2012/10/17 15:23:54
You should be able just to do EXPECT_EQ.
Sergiu
2012/10/19 08:40:55
True, didn't modify it since one of them was a str
| |
| 142 } else { | |
| 143 EXPECT_TRUE(request.url().query().rfind(expected)); | |
|
Bernhard Bauer
2012/10/17 15:23:54
rfind() returns the position, so we check here tha
Sergiu
2012/10/19 08:40:55
Makes sense, I've refactored the code such that we
| |
| 144 // Make sure that it gets put only once. | |
| 145 EXPECT_EQ(request.url().query().find(expected), | |
| 146 request.url().query().rfind(expected)); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 virtual void TearDown() OVERRIDE { | |
|
Bernhard Bauer
2012/10/17 15:23:54
This is unnecessary.
Sergiu
2012/10/19 08:40:55
Done.
| |
| 151 } | |
| 152 | |
| 153 private: | |
| 154 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | |
| 155 TestingProfile profile_; | |
| 156 TestingPrefService prefs_; | |
| 157 BooleanPrefMember enable_referrers_; | |
| 158 BooleanPrefMember force_google_safesearch_; | |
| 159 scoped_ptr<net::URLRequest> request_; | |
| 160 TestURLRequestContext context_; | |
| 161 TestDelegate delegate_; | |
| 162 scoped_ptr<MessageLoopForIO> loop_; | |
| 163 }; | |
| 164 | |
| 165 TEST_F(ChromeNetworkDelegateSafesearchTest, SafesearchOn) { | |
|
Bernhard Bauer
2012/10/17 15:23:54
Some more test cases:
?q=google&safe=active&ssui=
Sergiu
2012/10/19 08:40:55
Done with the new way.
| |
| 166 // Tests with SafeSearch on, request parameters should be rewritten. | |
| 167 SetSafesearch(true); | |
| 168 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); | |
| 169 SetDelegate(delegate.get()); | |
| 170 | |
| 171 // Test the home page. | |
| 172 CheckAddedParameters("http://google.com/", kSingleParameters, | |
|
Bernhard Bauer
2012/10/17 15:23:54
I think we could directly test the ForceGoogleSafe
Sergiu
2012/10/19 08:40:55
I think it's better to test it this way, at least
| |
| 173 true); | |
| 174 | |
| 175 // Test the search home page. | |
| 176 CheckAddedParameters("http://google.com/webhp", | |
| 177 kSingleParameters, | |
| 178 true); | |
| 179 | |
| 180 // Test different vaild search pages with parameters. | |
|
Bernhard Bauer
2012/10/17 15:23:54
Nit: "valid"
Sergiu
2012/10/19 08:40:55
Done.
| |
| 181 CheckAddedParameters("http://google.com/search?q=google", | |
| 182 kMultipleParameters, | |
| 183 false); | |
| 184 | |
| 185 CheckAddedParameters("http://google.com/?q=google", | |
| 186 kMultipleParameters, | |
| 187 false); | |
| 188 | |
| 189 CheckAddedParameters("http://google.com/webhp?q=google", | |
| 190 kMultipleParameters, | |
| 191 false); | |
| 192 | |
| 193 // Test the valid pages with safe set to off. | |
| 194 CheckAddedParameters("http://google.com/search?q=google&safe=off", | |
| 195 kMultipleParameters, | |
| 196 false); | |
| 197 | |
| 198 CheckAddedParameters("http://google.com/?q=google&safe=off", | |
| 199 kMultipleParameters, | |
| 200 false); | |
| 201 | |
| 202 CheckAddedParameters("http://google.com/webhp?q=google&safe=off", | |
| 203 kMultipleParameters, | |
| 204 false); | |
| 205 | |
| 206 // Test the home page, different TLDs. | |
| 207 CheckAddedParameters("http://google.de/", kSingleParameters, true); | |
| 208 CheckAddedParameters("http://google.ro/", kSingleParameters, true); | |
| 209 CheckAddedParameters("http://google.nl/", kSingleParameters, true); | |
| 210 | |
| 211 // Test the search home page, different TLD. | |
| 212 CheckAddedParameters("http://google.de/webhp", kSingleParameters, | |
| 213 true); | |
| 214 | |
| 215 // Test the home page with parameters, different TLD. | |
| 216 CheckAddedParameters("http://google.de/search?q=google", | |
| 217 kMultipleParameters, | |
| 218 false); | |
| 219 | |
| 220 // Test the search page with parameters, different TLD. | |
| 221 CheckAddedParameters("http://google.de/?q=google", | |
| 222 kMultipleParameters, | |
| 223 false); | |
| 224 | |
| 225 // Test that another website is not affected, without parameters | |
| 226 CheckAddedParameters("http://google.com/finance", "", true); | |
| 227 | |
| 228 // Test that another website is not affected, with parameters | |
| 229 CheckAddedParameters("http://google.com/finance?q=goog", "q=goog", true); | |
| 230 | |
| 231 // Test that another website is not affected with redirects, with parameters | |
| 232 CheckAddedParameters("http://finance.google.com/?q=goog", "q=goog", true); | |
| 233 } | |
| 234 | |
| 235 TEST_F(ChromeNetworkDelegateSafesearchTest, SafesearchOff) { | |
| 236 // Tests with SafeSearch settings off, delegate should not alter requests. | |
| 237 SetSafesearch(false); | |
| 238 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); | |
| 239 SetDelegate(delegate.get()); | |
| 240 | |
| 241 // Test the home page. | |
| 242 CheckAddedParameters("http://google.com/", "", true); | |
| 243 | |
| 244 // Test the search home page. | |
| 245 CheckAddedParameters("http://google.com/webhp", "", true); | |
| 246 | |
| 247 // Test the home page with parameters. | |
| 248 CheckAddedParameters("http://google.com/search?q=google", | |
| 249 "q=google", | |
| 250 true); | |
| 251 | |
| 252 // Test the search page with parameters. | |
| 253 CheckAddedParameters("http://google.com/?q=google", | |
| 254 "q=google", | |
| 255 true); | |
| 256 | |
| 257 // Test the search webhp page with parameters. | |
| 258 CheckAddedParameters("http://google.com/webhp?q=google", | |
| 259 "q=google", | |
| 260 true); | |
| 261 | |
| 262 // Test the home page with parameters and safe set to off. | |
| 263 CheckAddedParameters("http://google.com/search?q=google&safe=off", | |
| 264 "q=google&safe=off", | |
| 265 true); | |
| 266 | |
| 267 // Test the home page with parameters and safe set to active. | |
| 268 CheckAddedParameters("http://google.com/search?q=google&safe=active", | |
| 269 "q=google&safe=active", | |
| 270 true); | |
| 271 } | |
| OLD | NEW |