Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: net/base/wrapping_network_delegate_unittest.cc

Issue 734263003: Move data reduction proxy logic out of chrome and android webview network delegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use SingleThreadTaskRunner instead of MessageLoopProxy Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/wrapping_network_delegate.h"
6
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "net/base/auth.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/network_delegate_impl.h"
14 #include "net/base/request_priority.h"
15 #include "net/proxy/proxy_config_service.h"
16 #include "net/proxy/proxy_service.h"
17 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h"
22
23 namespace {
mmenke 2014/11/26 15:23:53 Should nest this in the net namespace, and remove
megjablon 2014/12/01 19:26:56 Done.
24
25 typedef std::map<const char*, int64> CountersMap;
mmenke 2014/11/26 15:23:53 Don't think you really need an int64 here
megjablon 2014/12/01 19:26:56 Done.
26
27 void VerifyCounters(CountersMap counters, int64 count) {
28 for (CountersMap::iterator iter = counters.begin();
29 iter != counters.end(); ++iter) {
30 EXPECT_EQ(count, iter->second);
31 }
32 }
33
34 class TestProxyConfigService : public net::ProxyConfigService {
35 public:
36 TestProxyConfigService() {
37 }
38
39 void AddObserver(net::ProxyConfigService::Observer* observer) override {
40 }
41
42 void RemoveObserver(net::ProxyConfigService::Observer* observer) override {
43 }
44
45 ConfigAvailability GetLatestProxyConfig(net::ProxyConfig* config) override {
46 return ProxyConfigService::CONFIG_VALID;
47 }
48 };
49
50 class TestNetworkDelegateImpl : public net::NetworkDelegateImpl {
51 public:
52 TestNetworkDelegateImpl() {
53 }
54
55 virtual ~TestNetworkDelegateImpl() {
mmenke 2014/11/26 15:23:53 -virtual + override
megjablon 2014/12/01 19:26:56 Done.
56 }
57
58 void VerifyCalled(int64 count) {
59 VerifyCounters(counters_, count);
60 }
61
62 private:
63 int OnBeforeURLRequest(net::URLRequest* request,
64 const net::CompletionCallback& callback,
65 GURL* new_url) override {
66 ++counters_["on_before_url_request_count"];
67 return net::OK;
68 }
69
70 void OnResolveProxy(const GURL& url,
71 int load_flags,
72 const net::ProxyService& proxy_service,
73 net::ProxyInfo* result) override {
74 ++counters_["on_resolve_proxy_count"];
75 }
76
77 void OnProxyFallback(const net::ProxyServer& bad_proxy,
78 int net_error) override {
79 ++counters_["on_proxy_fallback_count"];
80 }
81
82 int OnBeforeSendHeaders(net::URLRequest* request,
83 const net::CompletionCallback& callback,
84 net::HttpRequestHeaders* headers) override {
85 ++counters_["on_before_send_headers_count"];
86 return net::OK;
87 }
88
89 void OnBeforeSendProxyHeaders(net::URLRequest* request,
90 const net::ProxyInfo& proxy_info,
91 net::HttpRequestHeaders* headers) override {
92 ++counters_["on_before_send_proxy_headers_count"];
93 }
94
95 void OnSendHeaders(net::URLRequest* request,
96 const net::HttpRequestHeaders& headers) override {
97
98 ++counters_["on_send_headers_count"];
99 }
100
101 int OnHeadersReceived(
102 net::URLRequest* request,
103 const net::CompletionCallback& callback,
104 const net::HttpResponseHeaders* original_response_headers,
105 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
106 GURL* allowed_unsafe_redirect_url) override {
107 ++counters_["on_headers_received_count"];
108 return net::OK;
109 }
110
111 void OnBeforeRedirect(net::URLRequest* request,
112 const GURL& new_location) override {
113 ++counters_["on_before_redirect_count"];
114 }
115
116 void OnResponseStarted(net::URLRequest* request) override {
117 ++counters_["on_response_started_count"];
118 }
119
120 void OnRawBytesRead(const net::URLRequest& request,
121 int bytes_read) override {
122 ++counters_["on_raw_bytes_read_count"];
123 }
124
125 void OnCompleted(net::URLRequest* request, bool started) override {
126 ++counters_["on_completed_count"];
127 }
128
129 void OnURLRequestDestroyed(net::URLRequest* request) override {
130 ++counters_["on_url_request_destroyed_count"];
131 }
132
133 void OnPACScriptError(int line_number,
134 const base::string16& error) override {
135 ++counters_["on_pac_script_error_count"];
136 }
137
138 AuthRequiredResponse OnAuthRequired(
139 net::URLRequest* request,
140 const net::AuthChallengeInfo& auth_info,
141 const AuthCallback& callback,
142 net::AuthCredentials* credentials) override {
143 ++counters_["on_auth_required_count"];
144 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
145 }
146
147 bool OnCanGetCookies(const net::URLRequest& request,
148 const net::CookieList& cookie_list) override {
149 ++counters_["on_can_get_cookies_count"];
150 return false;
151 }
152
153 bool OnCanSetCookie(const net::URLRequest& request,
154 const std::string& cookie_line,
155 net::CookieOptions* options) override {
156 ++counters_["on_can_set_cookie_count"];
157 return false;
158 }
159
160 CountersMap counters_;
161 };
162
163 class TestNetworkDelegateImplMock : public TestNetworkDelegateImpl {
164 public:
165 TestNetworkDelegateImplMock() {
166 }
167 virtual ~TestNetworkDelegateImplMock() {}
168
169 MOCK_CONST_METHOD2(OnCanAccessFile,
170 bool(const net::URLRequest& request,
171 const base::FilePath& path));
172
173 MOCK_CONST_METHOD1(OnCanThrottleRequest,
174 bool(const net::URLRequest& request));
175
176 MOCK_CONST_METHOD2(OnCanEnablePrivacyMode,
177 bool(const GURL& url,
178 const GURL& first_party_for_cookies));
179
180 MOCK_CONST_METHOD3(
181 OnCancelURLRequestWithPolicyViolatingReferrerHeader,
182 bool(const net::URLRequest& request,
183 const GURL& target_url,
184 const GURL& referrer_url));
185 };
186
187 class WrappedNetworkDelegate : public net::WrappingNetworkDelegate {
188 public:
189 WrappedNetworkDelegate(scoped_ptr<net::NetworkDelegate> network_delegate)
190 : net::WrappingNetworkDelegate(network_delegate.Pass()),
191 context_(true) {
192 context_.Init();
193 }
194
195 virtual ~WrappedNetworkDelegate() {
196 }
197
198 void Call() {
199 scoped_refptr<net::AuthChallengeInfo> auth_challenge(
200 new net::AuthChallengeInfo());
201 scoped_ptr<net::URLRequest> request = context_.CreateRequest(
202 GURL(), net::IDLE, &delegate_, NULL);
203 OnBeforeURLRequest(NULL, net::CompletionCallback(), NULL);
204 OnResolveProxy(GURL(), 0,
205 net::ProxyService(new TestProxyConfigService(), NULL, NULL),
206 NULL);
207 OnProxyFallback(net::ProxyServer(), 0);
208 OnBeforeSendHeaders(NULL, net::CompletionCallback(), NULL);
209 OnBeforeSendProxyHeaders(NULL, net::ProxyInfo(), NULL);
210 OnSendHeaders(NULL, net::HttpRequestHeaders());
211 OnHeadersReceived(
212 NULL, net::CompletionCallback(), NULL, NULL, NULL);
213 OnBeforeRedirect(NULL, GURL());
214 OnResponseStarted(NULL);
215 OnRawBytesRead(*request.get(), 0);
216 OnCompleted(NULL, false);
217 OnURLRequestDestroyed(NULL);
218 OnPACScriptError(0, base::string16());
219 OnAuthRequired(NULL, *auth_challenge.get(), AuthCallback(), NULL);
220 OnCanGetCookies(*request.get(), net::CookieList());
221 OnCanSetCookie(*request.get(), std::string(), NULL);
222 OnCanAccessFile(*request.get(), base::FilePath());
223 OnCanThrottleRequest(*request.get());
224 OnCanEnablePrivacyMode(GURL(), GURL());
225 OnCancelURLRequestWithPolicyViolatingReferrerHeader(
226 *request.get(), GURL(), GURL());
227 }
228
229 void VerifyCalled(int64 count) {
230 VerifyCounters(counters_, count);
231 }
232
233 private:
234 void OnBeforeURLRequestInternal(net::URLRequest* request,
235 const net::CompletionCallback& callback,
236 GURL* new_url) override {
237 ++counters_["on_before_url_request_count"];
238 }
239
240 void OnResolveProxyInternal(const GURL& url,
241 int load_flags,
242 const net::ProxyService& proxy_service,
243 net::ProxyInfo* result) override {
244 ++counters_["on_resolve_proxy_count"];
245 }
246
247 void OnProxyFallbackInternal(const net::ProxyServer& bad_proxy,
248 int net_error) override {
249 ++counters_["on_proxy_fallback_count"];
250 }
251
252 void OnBeforeSendHeadersInternal(net::URLRequest* request,
253 const net::CompletionCallback& callback,
254 net::HttpRequestHeaders* headers) override {
255 ++counters_["on_before_send_headers_count"];
256 }
257
258 void OnBeforeSendProxyHeadersInternal(
259 net::URLRequest* request,
260 const net::ProxyInfo& proxy_info,
261 net::HttpRequestHeaders* headers) override {
262 ++counters_["on_before_send_proxy_headers_count"];
263 }
264
265 void OnSendHeadersInternal(net::URLRequest* request,
266 const net::HttpRequestHeaders& headers) override {
267
268 ++counters_["on_send_headers_count"];
269 }
270
271 void OnHeadersReceivedInternal(
272 net::URLRequest* request,
273 const net::CompletionCallback& callback,
274 const net::HttpResponseHeaders* original_response_headers,
275 scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
276 GURL* allowed_unsafe_redirect_url) override {
277 ++counters_["on_headers_received_count"];
278 }
279
280 void OnBeforeRedirectInternal(net::URLRequest* request,
281 const GURL& new_location) override {
282 ++counters_["on_before_redirect_count"];
283 }
284
285 void OnResponseStartedInternal(net::URLRequest* request) override {
286 ++counters_["on_response_started_count"];
287 }
288
289 void OnRawBytesReadInternal(const net::URLRequest& request,
290 int bytes_read) override {
291 ++counters_["on_raw_bytes_read_count"];
292 }
293
294 void OnCompletedInternal(net::URLRequest* request, bool started) override {
295 ++counters_["on_completed_count"];
296 }
297
298 void OnURLRequestDestroyedInternal(net::URLRequest* request) override {
299 ++counters_["on_url_request_destroyed_count"];
300 }
301
302 void OnPACScriptErrorInternal(int line_number,
303 const base::string16& error) override {
304 ++counters_["on_pac_script_error_count"];
305 }
306
307 void OnAuthRequiredInternal(net::URLRequest* request,
308 const net::AuthChallengeInfo& auth_info,
309 const AuthCallback& callback,
310 net::AuthCredentials* credentials) override {
311 ++counters_["on_auth_required_count"];
312 }
313
314 void OnCanGetCookiesInternal(const net::URLRequest& request,
315 const net::CookieList& cookie_list) override {
316 ++counters_["on_can_get_cookies_count"];
317 }
318
319 void OnCanSetCookieInternal(const net::URLRequest& request,
320 const std::string& cookie_line,
321 net::CookieOptions* options) override {
322 ++counters_["on_can_set_cookie_count"];
323 }
324
325 net::TestURLRequestContext context_;
326 net::TestDelegate delegate_;
327 CountersMap counters_;
328 };
329
330 class WrappedNetworkDelegateMock : public WrappedNetworkDelegate {
331 public:
332 WrappedNetworkDelegateMock(scoped_ptr<NetworkDelegate> network_delegate)
333 : WrappedNetworkDelegate(network_delegate.Pass()) {}
334 virtual ~WrappedNetworkDelegateMock() {}
335
336 MOCK_CONST_METHOD2(OnCanAccessFileInternal,
337 void(const net::URLRequest& request,
338 const base::FilePath& path));
339
340 MOCK_CONST_METHOD1(OnCanThrottleRequestInternal,
341 void(const net::URLRequest& request));
342
343 MOCK_CONST_METHOD2(OnCanEnablePrivacyModeInternal,
344 void(const GURL& url,
345 const GURL& first_party_for_cookies));
346
347 MOCK_CONST_METHOD3(
348 OnCancelURLRequestWithPolicyViolatingReferrerHeaderInternal,
349 void(const net::URLRequest& request,
350 const GURL& target_url,
351 const GURL& referrer_url));
352 };
353
354 } // namespace
355
356 namespace net {
357
358 class WrappingNetworkDelegateTest : public testing::Test {
359 public:
360 WrappingNetworkDelegateTest() {
361 scoped_ptr<TestNetworkDelegateImplMock> test_network_delegate(
362 new TestNetworkDelegateImplMock());
363 test_network_delegate_ = test_network_delegate.get();
364 wrapped_network_delegate_ = scoped_ptr<WrappedNetworkDelegateMock>(
365 new WrappedNetworkDelegateMock(test_network_delegate.Pass()));
366 }
367
368 TestNetworkDelegateImplMock* test_network_delegate_;
369 scoped_ptr<WrappedNetworkDelegateMock> wrapped_network_delegate_;
370 };
371
372 TEST_F(WrappingNetworkDelegateTest, VerifyWrappingNetworkDelegateInternal) {
373 EXPECT_CALL(*test_network_delegate_, OnCanAccessFile(testing::_, testing::_));
374 EXPECT_CALL(*test_network_delegate_, OnCanThrottleRequest(testing::_));
375 EXPECT_CALL(*test_network_delegate_,
376 OnCanEnablePrivacyMode(testing::_, testing::_));
377 EXPECT_CALL(*test_network_delegate_,
378 OnCancelURLRequestWithPolicyViolatingReferrerHeader(
379 testing::_, testing::_, testing::_));
380
381 EXPECT_CALL(*wrapped_network_delegate_.get(),
382 OnCanAccessFileInternal(testing::_, testing::_));
383 EXPECT_CALL(*wrapped_network_delegate_.get(),
384 OnCanThrottleRequestInternal(testing::_));
385 EXPECT_CALL(*wrapped_network_delegate_.get(),
386 OnCanEnablePrivacyModeInternal(testing::_, testing::_));
387 EXPECT_CALL(*wrapped_network_delegate_.get(),
388 OnCancelURLRequestWithPolicyViolatingReferrerHeaderInternal(
389 testing::_, testing::_, testing::_));
390
391 wrapped_network_delegate_->Call();
392
393 test_network_delegate_->VerifyCalled(1);
394 wrapped_network_delegate_->VerifyCalled(1);
395 }
396
397 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698