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

Side by Side Diff: net/base/layered_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: Adding inits to DRPNetworkDelegate 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/layered_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/base/test_completion_callback.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/proxy/proxy_config_service.h"
18 #include "net/proxy/proxy_info.h"
19 #include "net/proxy/proxy_service.h"
20 #include "net/url_request/url_request.h"
21 #include "net/url_request/url_request_test_util.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "url/gurl.h"
25
26 namespace net {
27
28 typedef std::map<const char*, int> CountersMap;
mmenke 2014/12/03 18:50:38 include map.
megjablon 2014/12/04 01:23:46 Done.
29
30 void VerifyCounters(CountersMap counters, int64 count) {
mmenke 2014/12/03 18:50:38 int64 not needed
mmenke 2014/12/03 18:50:38 Actually, can you just get rid of this method, and
megjablon 2014/12/04 01:23:46 Done.
31 for (CountersMap::iterator iter = counters.begin();
32 iter != counters.end(); ++iter) {
33 EXPECT_EQ(count, iter->second);
34 }
35 }
36
37 class TestProxyConfigService : public ProxyConfigService {
mmenke 2014/12/03 18:50:38 Not needed. Can just use ProxyService::CreateDire
megjablon 2014/12/04 01:23:46 Done.
38 public:
39 TestProxyConfigService() {
40 }
41
42 void AddObserver(ProxyConfigService::Observer* observer) override {
43 }
44
45 void RemoveObserver(ProxyConfigService::Observer* observer) override {
46 }
47
48 ConfigAvailability GetLatestProxyConfig(ProxyConfig* config) override {
49 return ProxyConfigService::CONFIG_VALID;
50 }
51 };
52
53 class TestNetworkDelegateImpl : public NetworkDelegateImpl {
54 public:
55 TestNetworkDelegateImpl() {
56 }
57
58 ~TestNetworkDelegateImpl() override {
59 }
60
61 void VerifyCalled(int64 count) {
62 VerifyCounters(counters_, count);
63 }
64
65 private:
66 int OnBeforeURLRequest(URLRequest* request,
67 const CompletionCallback& callback,
68 GURL* new_url) override {
69 ++counters_["on_before_url_request_count"];
mmenke 2014/12/03 18:50:38 Hrm... It's a pain, but if we want to be complete
megjablon 2014/12/04 01:23:46 Done.
70 return OK;
71 }
72
73 void OnResolveProxy(const GURL& url,
74 int load_flags,
75 const ProxyService& proxy_service,
76 ProxyInfo* result) override {
77 ++counters_["on_resolve_proxy_count"];
78 }
79
80 void OnProxyFallback(const ProxyServer& bad_proxy, int net_error) override {
81 ++counters_["on_proxy_fallback_count"];
82 }
83
84 int OnBeforeSendHeaders(URLRequest* request,
85 const CompletionCallback& callback,
86 HttpRequestHeaders* headers) override {
87 ++counters_["on_before_send_headers_count"];
88 return OK;
89 }
90
91 void OnBeforeSendProxyHeaders(URLRequest* request,
92 const ProxyInfo& proxy_info,
93 HttpRequestHeaders* headers) override {
94 ++counters_["on_before_send_proxy_headers_count"];
95 }
96
97 void OnSendHeaders(URLRequest* request,
98 const HttpRequestHeaders& headers) override {
99 ++counters_["on_send_headers_count"];
100 }
101
102 int OnHeadersReceived(
103 URLRequest* request,
104 const CompletionCallback& callback,
105 const HttpResponseHeaders* original_response_headers,
106 scoped_refptr<HttpResponseHeaders>* override_response_headers,
107 GURL* allowed_unsafe_redirect_url) override {
108 ++counters_["on_headers_received_count"];
109 return OK;
110 }
111
112 void OnBeforeRedirect(URLRequest* request,
113 const GURL& new_location) override {
114 ++counters_["on_before_redirect_count"];
115 }
116
117 void OnResponseStarted(URLRequest* request) override {
118 ++counters_["on_response_started_count"];
119 }
120
121 void OnRawBytesRead(const URLRequest& request, int bytes_read) override {
122 ++counters_["on_raw_bytes_read_count"];
123 }
124
125 void OnCompleted(URLRequest* request, bool started) override {
126 ++counters_["on_completed_count"];
127 }
128
129 void OnURLRequestDestroyed(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(URLRequest* request,
139 const AuthChallengeInfo& auth_info,
140 const AuthCallback& callback,
141 AuthCredentials* credentials) override {
142 ++counters_["on_auth_required_count"];
143 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
144 }
145
146 bool OnCanGetCookies(const URLRequest& request,
147 const CookieList& cookie_list) override {
148 ++counters_["on_can_get_cookies_count"];
149 return false;
150 }
151
152 bool OnCanSetCookie(const URLRequest& request,
153 const std::string& cookie_line,
154 CookieOptions* options) override {
155 ++counters_["on_can_set_cookie_count"];
156 return false;
157 }
158
159 CountersMap counters_;
160 };
161
162 class TestNetworkDelegateImplMock : public TestNetworkDelegateImpl {
163 public:
164 TestNetworkDelegateImplMock() {
165 }
166
167 ~TestNetworkDelegateImplMock() override {
168 }
169
170 MOCK_CONST_METHOD2(OnCanAccessFile,
171 bool(const URLRequest& request,
172 const base::FilePath& path));
173
174 MOCK_CONST_METHOD1(OnCanThrottleRequest,
175 bool(const URLRequest& request));
176
177 MOCK_CONST_METHOD2(OnCanEnablePrivacyMode,
178 bool(const GURL& url,
179 const GURL& first_party_for_cookies));
180
181 MOCK_CONST_METHOD3(
182 OnCancelURLRequestWithPolicyViolatingReferrerHeader,
183 bool(const URLRequest& request,
184 const GURL& target_url,
185 const GURL& referrer_url));
186 };
187
188 class TestLayeredNetworkDelegate : public LayeredNetworkDelegate {
189 public:
190 TestLayeredNetworkDelegate(scoped_ptr<NetworkDelegate> network_delegate)
191 : LayeredNetworkDelegate(network_delegate.Pass()),
192 context_(true) {
193 context_.Init();
194 }
195
196 ~TestLayeredNetworkDelegate() override {
197 }
198
199 void MakeCalls() {
200 scoped_refptr<AuthChallengeInfo> auth_challenge( new AuthChallengeInfo());
201 scoped_ptr<URLRequest> request = context_.CreateRequest(
202 GURL(), IDLE, &delegate_, NULL);
203 scoped_ptr<HttpRequestHeaders> request_headers(new HttpRequestHeaders());
204 scoped_refptr<HttpResponseHeaders> response_headers(
205 new HttpResponseHeaders(""));
206 TestCompletionCallback completion_callback;
207 OnBeforeURLRequest(request.get(), completion_callback.callback(), NULL);
208 OnResolveProxy(GURL(), 0,
209 ProxyService(new TestProxyConfigService(), NULL, NULL),
210 new ProxyInfo());
211 OnProxyFallback(ProxyServer(), 0);
212 OnBeforeSendHeaders(
213 NULL, completion_callback.callback(), request_headers.get());
214 OnBeforeSendProxyHeaders(NULL, ProxyInfo(), request_headers.get());
215 OnSendHeaders(NULL, *request_headers.get());
216 OnHeadersReceived(
217 NULL, completion_callback.callback(),
218 response_headers.get(), NULL, NULL);
219 OnResponseStarted(request.get());
220 OnRawBytesRead(*request.get(), 0);
mmenke 2014/12/03 18:50:38 Think *request can be used instead of *request.get
megjablon 2014/12/04 01:23:46 Done.
221 OnCompleted(request.get(), false);
222 OnURLRequestDestroyed(request.get());
223 OnPACScriptError(0, base::string16());
224 OnAuthRequired(request.get(), *auth_challenge.get(), AuthCallback(), NULL);
225 OnCanGetCookies(*request.get(), CookieList());
226 OnCanSetCookie(*request.get(), std::string(), NULL);
227 OnCanAccessFile(*request.get(), base::FilePath());
228 OnCanThrottleRequest(*request.get());
229 OnCanEnablePrivacyMode(GURL(), GURL());
230 OnCancelURLRequestWithPolicyViolatingReferrerHeader(
231 *request.get(), GURL(), GURL());
mmenke 2014/12/03 18:50:38 Maybe use EXPECT_EQ for those that return values?
megjablon 2014/12/04 01:23:46 Done.
232 }
233
234 void VerifyCalled(int64 count) {
235 VerifyCounters(counters_, count);
236 }
237
238 private:
239 void OnBeforeURLRequestInternal(URLRequest* request,
240 const CompletionCallback& callback,
241 GURL* new_url) override {
242 ++counters_["on_before_url_request_count"];
243 }
244
245 void OnResolveProxyInternal(const GURL& url,
246 int load_flags,
247 const ProxyService& proxy_service,
248 ProxyInfo* result) override {
249 ++counters_["on_resolve_proxy_count"];
250 }
251
252 void OnProxyFallbackInternal(const ProxyServer& bad_proxy,
253 int net_error) override {
254 ++counters_["on_proxy_fallback_count"];
255 }
256
257 void OnBeforeSendHeadersInternal(URLRequest* request,
258 const CompletionCallback& callback,
259 HttpRequestHeaders* headers) override {
260 ++counters_["on_before_send_headers_count"];
261 }
262
263 void OnBeforeSendProxyHeadersInternal(URLRequest* request,
264 const ProxyInfo& proxy_info,
265 HttpRequestHeaders* headers) override {
266 ++counters_["on_before_send_proxy_headers_count"];
267 }
268
269 void OnSendHeadersInternal(URLRequest* request,
270 const HttpRequestHeaders& headers) override {
271
272 ++counters_["on_send_headers_count"];
273 }
274
275 void OnHeadersReceivedInternal(
276 URLRequest* request,
277 const CompletionCallback& callback,
278 const HttpResponseHeaders* original_response_headers,
279 scoped_refptr<HttpResponseHeaders>* override_response_headers,
280 GURL* allowed_unsafe_redirect_url) override {
281 ++counters_["on_headers_received_count"];
282 }
283
284 void OnBeforeRedirectInternal(URLRequest* request,
285 const GURL& new_location) override {
286 ++counters_["on_before_redirect_count"];
287 }
288
289 void OnResponseStartedInternal(URLRequest* request) override {
290 ++counters_["on_response_started_count"];
291 }
292
293 void OnRawBytesReadInternal(const URLRequest& request,
294 int bytes_read) override {
295 ++counters_["on_raw_bytes_read_count"];
296 }
297
298 void OnCompletedInternal(URLRequest* request, bool started) override {
299 ++counters_["on_completed_count"];
300 }
301
302 void OnURLRequestDestroyedInternal(URLRequest* request) override {
303 ++counters_["on_url_request_destroyed_count"];
304 }
305
306 void OnPACScriptErrorInternal(int line_number,
307 const base::string16& error) override {
308 ++counters_["on_pac_script_error_count"];
309 }
310
311 void OnAuthRequiredInternal(URLRequest* request,
312 const AuthChallengeInfo& auth_info,
313 const AuthCallback& callback,
314 AuthCredentials* credentials) override {
315 ++counters_["on_auth_required_count"];
316 }
317
318 void OnCanGetCookiesInternal(const URLRequest& request,
319 const CookieList& cookie_list) override {
320 ++counters_["on_can_get_cookies_count"];
321 }
322
323 void OnCanSetCookieInternal(const URLRequest& request,
324 const std::string& cookie_line,
325 CookieOptions* options) override {
326 ++counters_["on_can_set_cookie_count"];
327 }
328
329 TestURLRequestContext context_;
330 TestDelegate delegate_;
331 CountersMap counters_;
mmenke 2014/12/03 18:50:38 DISALLOW_COPY_AND_ASSIGN
megjablon 2014/12/04 01:23:46 Done.
332 };
333
334 class TestLayeredNetworkDelegateMock : public TestLayeredNetworkDelegate {
335 public:
336 TestLayeredNetworkDelegateMock(scoped_ptr<NetworkDelegate> network_delegate)
337 : TestLayeredNetworkDelegate(network_delegate.Pass()) {
338 }
339
340 ~TestLayeredNetworkDelegateMock() override {
341 }
342
343 MOCK_CONST_METHOD2(OnCanAccessFileInternal,
mmenke 2014/12/03 18:50:38 Probably shouldn't be using gmock, so hacking on C
megjablon 2014/12/04 01:23:46 Done.
344 void(const URLRequest& request,
345 const base::FilePath& path));
346
347 MOCK_CONST_METHOD1(OnCanThrottleRequestInternal,
348 void(const URLRequest& request));
349
350 MOCK_CONST_METHOD2(OnCanEnablePrivacyModeInternal,
351 void(const GURL& url,
352 const GURL& first_party_for_cookies));
353
354 MOCK_CONST_METHOD3(
355 OnCancelURLRequestWithPolicyViolatingReferrerHeaderInternal,
356 void(const URLRequest& request,
357 const GURL& target_url,
358 const GURL& referrer_url));
359 };
360
361 class LayeredNetworkDelegateTest : public testing::Test {
362 public:
363 LayeredNetworkDelegateTest() {
364 scoped_ptr<TestNetworkDelegateImplMock> test_network_delegate(
365 new TestNetworkDelegateImplMock());
366 test_network_delegate_ = test_network_delegate.get();
367 layered_network_delegate_ = scoped_ptr<TestLayeredNetworkDelegateMock>(
368 new TestLayeredNetworkDelegateMock(test_network_delegate.Pass()));
369 }
370
371 TestNetworkDelegateImplMock* test_network_delegate_;
372 scoped_ptr<TestLayeredNetworkDelegateMock> layered_network_delegate_;
373 };
374
375 TEST_F(LayeredNetworkDelegateTest, VerifyLayeredNetworkDelegateInternal) {
376 EXPECT_CALL(*test_network_delegate_, OnCanAccessFile(testing::_, testing::_));
377 EXPECT_CALL(*test_network_delegate_, OnCanThrottleRequest(testing::_));
378 EXPECT_CALL(*test_network_delegate_,
379 OnCanEnablePrivacyMode(testing::_, testing::_));
380 EXPECT_CALL(*test_network_delegate_,
381 OnCancelURLRequestWithPolicyViolatingReferrerHeader(
382 testing::_, testing::_, testing::_));
383
384 EXPECT_CALL(*layered_network_delegate_.get(),
385 OnCanAccessFileInternal(testing::_, testing::_));
mmenke 2014/12/03 18:50:38 Why are these calls separated out?
megjablon 2014/12/04 01:23:46 Done.
386 EXPECT_CALL(*layered_network_delegate_.get(),
387 OnCanThrottleRequestInternal(testing::_));
388 EXPECT_CALL(*layered_network_delegate_.get(),
389 OnCanEnablePrivacyModeInternal(testing::_, testing::_));
390 EXPECT_CALL(*layered_network_delegate_.get(),
391 OnCancelURLRequestWithPolicyViolatingReferrerHeaderInternal(
392 testing::_, testing::_, testing::_));
393
394 layered_network_delegate_->MakeCalls();
395
396 test_network_delegate_->VerifyCalled(1);
397 layered_network_delegate_->VerifyCalled(1);
mmenke 2014/12/03 18:50:38 Should compare the total number of calls to the tw
megjablon 2014/12/04 01:23:46 This is now checked in IncrementAndCompareCounter
398 }
399
400 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698