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, &enable_safesearch_, NULL)); | |
96 } | |
97 | |
98 void SetSafesearch(bool value) { | |
99 enable_safesearch_.SetValue(value); | |
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::kSafeSearchEnabled, false, | |
113 PrefService::UNSYNCABLE_PREF); | |
114 enable_safesearch_.Init(prefs::kSafeSearchEnabled, | |
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_ = new MessageLoopForIO(); | |
121 } | |
122 | |
123 void CheckAddedParameters(const std::string& url_string, const char* expected, | |
124 const bool exact) { | |
battre
2012/10/16 16:05:49
style nit: all parameters should be aligned at the
Sergiu
2012/10/17 14:48:58
Done.
| |
125 // Show the URL in the trace so we know where we failed. | |
126 SCOPED_TRACE(url_string); | |
127 | |
128 TestURLRequest url_req( | |
battre
2012/10/16 16:05:49
s/url_req/request/
Sergiu
2012/10/17 14:48:58
Done.
| |
129 GURL(url_string), &tdelegate_, &context_); | |
Pam (message me for reviews)
2012/10/17 11:37:37
Does this not fit up on the line above?
| |
130 | |
131 url_req.Start(); | |
132 MessageLoop::current()->RunAllPending(); | |
133 | |
134 if (exact) | |
135 EXPECT_STREQ(expected, url_req.url().query().c_str()); | |
136 else | |
137 EXPECT_TRUE(url_req.url().query().rfind(expected)); | |
138 } | |
139 | |
140 virtual void TearDown() OVERRIDE { | |
141 delete loop_; | |
142 loop_ = NULL; | |
battre
2012/10/16 16:05:49
how about a scoped_ptr? do you need to use a point
Sergiu
2012/10/17 14:48:58
Done.
| |
143 } | |
144 | |
145 private: | |
146 scoped_refptr<extensions::EventRouterForwarder> forwarder_; | |
147 TestingProfile profile_; | |
148 TestingPrefService prefs_; | |
149 BooleanPrefMember enable_referrers_; | |
150 BooleanPrefMember enable_safesearch_; | |
151 scoped_ptr<net::URLRequest> request_; | |
152 TestURLRequestContext context_; | |
153 TestDelegate tdelegate_; | |
battre
2012/10/16 16:05:49
s/tdelegate_/delegate_/
Pam (message me for reviews)
2012/10/17 11:37:37
Or test_delegate_
Sergiu
2012/10/17 14:48:58
Done.
| |
154 MessageLoopForIO* loop_; | |
Joao da Silva
2012/10/16 16:26:46
Use a scoped_ptr
Sergiu
2012/10/17 14:48:58
Done.
| |
155 }; | |
156 | |
157 TEST_F(ChromeNetworkDelegateSafesearchTest, SafesearchOn) { | |
158 // Tests with SafeSearch on, request parameters should be rewritten. | |
159 SetSafesearch(true); | |
160 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); | |
161 SetDelegate(delegate.get()); | |
162 | |
163 // Test the home page. | |
164 CheckAddedParameters("http://google.com/", chrome::kSSearchParamsSingle, | |
165 true); | |
166 | |
167 // Test the search home page. | |
168 CheckAddedParameters("http://google.com/webhp", | |
169 chrome::kSSearchParamsSingle, | |
170 true); | |
171 | |
172 // Test different vaild search pages with parameters. | |
173 CheckAddedParameters("http://google.com/search?q=google", | |
174 chrome::kSSearchParamsMultiple, | |
175 false); | |
176 | |
177 CheckAddedParameters("http://google.com/?q=google", | |
178 chrome::kSSearchParamsMultiple, | |
179 false); | |
180 | |
181 CheckAddedParameters("http://google.com/webhp?q=google", | |
182 chrome::kSSearchParamsMultiple, | |
183 false); | |
184 | |
185 // Test the valid pages with safe set to off. | |
186 CheckAddedParameters("http://google.com/search?q=google&safe=off", | |
187 chrome::kSSearchParamsMultiple, | |
188 false); | |
189 | |
190 CheckAddedParameters("http://google.com/?q=google&safe=off", | |
191 chrome::kSSearchParamsMultiple, | |
192 false); | |
193 | |
194 CheckAddedParameters("http://google.com/webhp?q=google&safe=off", | |
195 chrome::kSSearchParamsMultiple, | |
196 false); | |
197 | |
198 // Test the home page, different TLDs. | |
199 CheckAddedParameters("http://google.de/", chrome::kSSearchParamsSingle, true); | |
200 CheckAddedParameters("http://google.ro/", chrome::kSSearchParamsSingle, true); | |
201 CheckAddedParameters("http://google.nl/", chrome::kSSearchParamsSingle, true); | |
202 | |
203 // Test the search home page, different TLD. | |
204 CheckAddedParameters("http://google.de/webhp", chrome::kSSearchParamsSingle, | |
205 true); | |
206 | |
207 // Test the home page with parameters, different TLD. | |
208 CheckAddedParameters("http://google.de/search?q=google", | |
209 chrome::kSSearchParamsMultiple, | |
210 false); | |
211 | |
212 // Test the search page with parameters, different TLD. | |
213 CheckAddedParameters("http://google.de/?q=google", | |
214 chrome::kSSearchParamsMultiple, | |
215 false); | |
216 | |
217 // Test that another website is not affected, without parameters | |
Joao da Silva
2012/10/16 16:26:46
nit: terminate sentence
Sergiu
2012/10/17 14:48:58
Done.
| |
218 CheckAddedParameters("http://google.com/finance", "", true); | |
219 | |
220 // Test that another website is not affected, with parameters | |
Joao da Silva
2012/10/16 16:26:46
nit: terminate sentence
Sergiu
2012/10/17 14:48:58
Done.
| |
221 CheckAddedParameters("http://google.com/finance?q=goog", "q=goog", true); | |
222 | |
223 // Test that another website is not affected with redirects, with parameters | |
Joao da Silva
2012/10/16 16:26:46
nit: terminate sentence
Sergiu
2012/10/17 14:48:58
Done.
| |
224 CheckAddedParameters("http://finance.google.com/?q=goog", "q=goog", true); | |
225 } | |
226 | |
227 TEST_F(ChromeNetworkDelegateSafesearchTest, SafesearchOff) { | |
228 // Tests with SafeSearch settings off, delegate should not alter requests. | |
229 SetSafesearch(false); | |
230 scoped_ptr<net::NetworkDelegate> delegate(CreateNetworkDelegate()); | |
231 SetDelegate(delegate.get()); | |
232 | |
233 // Test the home page | |
Joao da Silva
2012/10/16 16:26:46
nit: terminate sentences in this test too
Sergiu
2012/10/17 14:48:58
Done.
| |
234 CheckAddedParameters("http://google.com/", "", true); | |
235 | |
236 // Test the search home page | |
237 CheckAddedParameters("http://google.com/webhp", "", true); | |
238 | |
239 // Test the home page with parameters | |
240 CheckAddedParameters("http://google.com/search?q=google", | |
241 "q=google", | |
242 true); | |
243 | |
244 // Test the search page with parameters | |
245 CheckAddedParameters("http://google.com/?q=google", | |
246 "q=google", | |
247 true); | |
248 | |
249 // Test the search webhp page with parameters | |
250 CheckAddedParameters("http://google.com/webhp?q=google", | |
251 "q=google", | |
252 true); | |
253 | |
254 // Test the home page with parameters and safe set to off | |
255 CheckAddedParameters("http://google.com/search?q=google&safe=off", | |
256 "q=google&safe=off", | |
257 true); | |
258 | |
259 // Test the home page with parameters and safe set to active | |
260 CheckAddedParameters("http://google.com/search?q=google&safe=active", | |
261 "q=google&safe=active", | |
262 true); | |
263 } | |
OLD | NEW |