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.
5 #include "net/base/layered_network_delegate.h"
10 #include "base/files/file_path.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/auth.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/network_delegate_impl.h"
17 #include "net/base/request_priority.h"
18 #include "net/base/test_completion_callback.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/proxy/proxy_config_service.h"
21 #include "net/proxy/proxy_info.h"
22 #include "net/proxy/proxy_service.h"
23 #include "net/url_request/url_request.h"
24 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gtest/include/gtest/gtest.h"
31 typedef std::map
<const char*, int> CountersMap
;
33 class TestNetworkDelegateImpl
: public NetworkDelegateImpl
{
35 TestNetworkDelegateImpl(CountersMap
* layered_network_delegate_counters
)
36 : layered_network_delegate_counters_(layered_network_delegate_counters
) {}
38 ~TestNetworkDelegateImpl() override
{}
40 // NetworkDelegateImpl implementation:
41 int OnBeforeURLRequest(URLRequest
* request
,
42 const CompletionCallback
& callback
,
43 GURL
* new_url
) override
{
44 IncrementAndCompareCounter("on_before_url_request_count");
48 void OnResolveProxy(const GURL
& url
,
50 const ProxyService
& proxy_service
,
51 ProxyInfo
* result
) override
{
52 IncrementAndCompareCounter("on_resolve_proxy_count");
55 void OnProxyFallback(const ProxyServer
& bad_proxy
, int net_error
) override
{
56 IncrementAndCompareCounter("on_proxy_fallback_count");
59 int OnBeforeSendHeaders(URLRequest
* request
,
60 const CompletionCallback
& callback
,
61 HttpRequestHeaders
* headers
) override
{
62 IncrementAndCompareCounter("on_before_send_headers_count");
66 void OnBeforeSendProxyHeaders(URLRequest
* request
,
67 const ProxyInfo
& proxy_info
,
68 HttpRequestHeaders
* headers
) override
{
69 IncrementAndCompareCounter("on_before_send_proxy_headers_count");
72 void OnSendHeaders(URLRequest
* request
,
73 const HttpRequestHeaders
& headers
) override
{
74 IncrementAndCompareCounter("on_send_headers_count");
77 int OnHeadersReceived(
79 const CompletionCallback
& callback
,
80 const HttpResponseHeaders
* original_response_headers
,
81 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
82 GURL
* allowed_unsafe_redirect_url
) override
{
83 IncrementAndCompareCounter("on_headers_received_count");
87 void OnBeforeRedirect(URLRequest
* request
,
88 const GURL
& new_location
) override
{
89 IncrementAndCompareCounter("on_before_redirect_count");
92 void OnResponseStarted(URLRequest
* request
) override
{
93 IncrementAndCompareCounter("on_response_started_count");
96 void OnNetworkBytesReceived(const URLRequest
& request
,
97 int64_t bytes_received
) override
{
98 IncrementAndCompareCounter("on_network_bytes_received_count");
101 void OnCompleted(URLRequest
* request
, bool started
) override
{
102 IncrementAndCompareCounter("on_completed_count");
105 void OnURLRequestDestroyed(URLRequest
* request
) override
{
106 IncrementAndCompareCounter("on_url_request_destroyed_count");
109 void OnPACScriptError(int line_number
, const base::string16
& error
) override
{
110 IncrementAndCompareCounter("on_pac_script_error_count");
113 AuthRequiredResponse
OnAuthRequired(URLRequest
* request
,
114 const AuthChallengeInfo
& auth_info
,
115 const AuthCallback
& callback
,
116 AuthCredentials
* credentials
) override
{
117 IncrementAndCompareCounter("on_auth_required_count");
118 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
121 bool OnCanGetCookies(const URLRequest
& request
,
122 const CookieList
& cookie_list
) override
{
123 IncrementAndCompareCounter("on_can_get_cookies_count");
127 bool OnCanSetCookie(const URLRequest
& request
,
128 const std::string
& cookie_line
,
129 CookieOptions
* options
) override
{
130 IncrementAndCompareCounter("on_can_set_cookie_count");
134 bool OnCanAccessFile(const URLRequest
& request
,
135 const base::FilePath
& path
) const override
{
136 IncrementAndCompareCounter("on_can_access_file_count");
140 bool OnCanEnablePrivacyMode(
142 const GURL
& first_party_for_cookies
) const override
{
143 IncrementAndCompareCounter("on_can_enable_privacy_mode_count");
147 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader(
148 const URLRequest
& request
,
149 const GURL
& target_url
,
150 const GURL
& referrer_url
) const override
{
151 IncrementAndCompareCounter(
152 "on_cancel_url_request_with_policy_violating_referrer_header_count");
157 void IncrementAndCompareCounter(const char* counter_name
) const {
158 ++counters_
[counter_name
];
159 EXPECT_EQ((*layered_network_delegate_counters_
)[counter_name
],
160 counters_
[counter_name
]);
163 mutable CountersMap counters_
;
164 mutable CountersMap
* layered_network_delegate_counters_
;
166 DISALLOW_COPY_AND_ASSIGN(TestNetworkDelegateImpl
);
169 class TestLayeredNetworkDelegate
: public LayeredNetworkDelegate
{
171 TestLayeredNetworkDelegate(scoped_ptr
<NetworkDelegate
> network_delegate
,
172 CountersMap
* counters
)
173 : LayeredNetworkDelegate(network_delegate
.Pass()),
175 counters_(counters
) {
179 ~TestLayeredNetworkDelegate() override
{}
181 void CallAndVerify() {
182 scoped_refptr
<AuthChallengeInfo
> auth_challenge(new AuthChallengeInfo());
183 scoped_ptr
<URLRequest
> request
=
184 context_
.CreateRequest(GURL(), IDLE
, &delegate_
);
185 scoped_ptr
<HttpRequestHeaders
> request_headers(new HttpRequestHeaders());
186 scoped_refptr
<HttpResponseHeaders
> response_headers(
187 new HttpResponseHeaders(""));
188 TestCompletionCallback completion_callback
;
189 scoped_ptr
<ProxyService
> proxy_service(ProxyService::CreateDirect());
190 scoped_ptr
<ProxyInfo
> proxy_info(new ProxyInfo());
192 EXPECT_EQ(OK
, OnBeforeURLRequest(request
.get(),
193 completion_callback
.callback(), NULL
));
194 OnResolveProxy(GURL(), 0, *proxy_service
, proxy_info
.get());
195 OnProxyFallback(ProxyServer(), 0);
196 EXPECT_EQ(OK
, OnBeforeSendHeaders(NULL
, completion_callback
.callback(),
197 request_headers
.get()));
198 OnBeforeSendProxyHeaders(NULL
, ProxyInfo(), request_headers
.get());
199 OnSendHeaders(NULL
, *request_headers
);
200 EXPECT_EQ(OK
, OnHeadersReceived(NULL
, completion_callback
.callback(),
201 response_headers
.get(), NULL
, NULL
));
202 OnResponseStarted(request
.get());
203 OnNetworkBytesReceived(*request
, 42);
204 OnCompleted(request
.get(), false);
205 OnURLRequestDestroyed(request
.get());
206 OnPACScriptError(0, base::string16());
208 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
,
209 OnAuthRequired(request
.get(), *auth_challenge
, AuthCallback(), NULL
));
210 EXPECT_FALSE(OnCanGetCookies(*request
, CookieList()));
211 EXPECT_FALSE(OnCanSetCookie(*request
, std::string(), NULL
));
212 EXPECT_FALSE(OnCanAccessFile(*request
, base::FilePath()));
213 EXPECT_FALSE(OnCanEnablePrivacyMode(GURL(), GURL()));
214 EXPECT_FALSE(OnCancelURLRequestWithPolicyViolatingReferrerHeader(
215 *request
, GURL(), GURL()));
219 void OnBeforeURLRequestInternal(URLRequest
* request
,
220 const CompletionCallback
& callback
,
221 GURL
* new_url
) override
{
222 ++(*counters_
)["on_before_url_request_count"];
223 EXPECT_EQ(1, (*counters_
)["on_before_url_request_count"]);
226 void OnResolveProxyInternal(const GURL
& url
,
228 const ProxyService
& proxy_service
,
229 ProxyInfo
* result
) override
{
230 ++(*counters_
)["on_resolve_proxy_count"];
231 EXPECT_EQ(1, (*counters_
)["on_resolve_proxy_count"]);
234 void OnProxyFallbackInternal(const ProxyServer
& bad_proxy
,
235 int net_error
) override
{
236 ++(*counters_
)["on_proxy_fallback_count"];
237 EXPECT_EQ(1, (*counters_
)["on_proxy_fallback_count"]);
240 void OnBeforeSendHeadersInternal(URLRequest
* request
,
241 const CompletionCallback
& callback
,
242 HttpRequestHeaders
* headers
) override
{
243 ++(*counters_
)["on_before_send_headers_count"];
244 EXPECT_EQ(1, (*counters_
)["on_before_send_headers_count"]);
247 void OnBeforeSendProxyHeadersInternal(URLRequest
* request
,
248 const ProxyInfo
& proxy_info
,
249 HttpRequestHeaders
* headers
) override
{
250 ++(*counters_
)["on_before_send_proxy_headers_count"];
251 EXPECT_EQ(1, (*counters_
)["on_before_send_proxy_headers_count"]);
254 void OnSendHeadersInternal(URLRequest
* request
,
255 const HttpRequestHeaders
& headers
) override
{
256 ++(*counters_
)["on_send_headers_count"];
257 EXPECT_EQ(1, (*counters_
)["on_send_headers_count"]);
260 void OnHeadersReceivedInternal(
262 const CompletionCallback
& callback
,
263 const HttpResponseHeaders
* original_response_headers
,
264 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
265 GURL
* allowed_unsafe_redirect_url
) override
{
266 ++(*counters_
)["on_headers_received_count"];
267 EXPECT_EQ(1, (*counters_
)["on_headers_received_count"]);
270 void OnBeforeRedirectInternal(URLRequest
* request
,
271 const GURL
& new_location
) override
{
272 ++(*counters_
)["on_before_redirect_count"];
273 EXPECT_EQ(1, (*counters_
)["on_before_redirect_count"]);
276 void OnResponseStartedInternal(URLRequest
* request
) override
{
277 ++(*counters_
)["on_response_started_count"];
278 EXPECT_EQ(1, (*counters_
)["on_response_started_count"]);
281 void OnNetworkBytesReceivedInternal(const URLRequest
& request
,
282 int64_t bytes_received
) override
{
283 ++(*counters_
)["on_network_bytes_received_count"];
284 EXPECT_EQ(1, (*counters_
)["on_network_bytes_received_count"]);
287 void OnCompletedInternal(URLRequest
* request
, bool started
) override
{
288 ++(*counters_
)["on_completed_count"];
289 EXPECT_EQ(1, (*counters_
)["on_completed_count"]);
292 void OnURLRequestDestroyedInternal(URLRequest
* request
) override
{
293 ++(*counters_
)["on_url_request_destroyed_count"];
294 EXPECT_EQ(1, (*counters_
)["on_url_request_destroyed_count"]);
297 void OnPACScriptErrorInternal(int line_number
,
298 const base::string16
& error
) override
{
299 ++(*counters_
)["on_pac_script_error_count"];
300 EXPECT_EQ(1, (*counters_
)["on_pac_script_error_count"]);
303 void OnAuthRequiredInternal(URLRequest
* request
,
304 const AuthChallengeInfo
& auth_info
,
305 const AuthCallback
& callback
,
306 AuthCredentials
* credentials
) override
{
307 ++(*counters_
)["on_auth_required_count"];
308 EXPECT_EQ(1, (*counters_
)["on_auth_required_count"]);
311 void OnCanGetCookiesInternal(const URLRequest
& request
,
312 const CookieList
& cookie_list
) override
{
313 ++(*counters_
)["on_can_get_cookies_count"];
314 EXPECT_EQ(1, (*counters_
)["on_can_get_cookies_count"]);
317 void OnCanSetCookieInternal(const URLRequest
& request
,
318 const std::string
& cookie_line
,
319 CookieOptions
* options
) override
{
320 ++(*counters_
)["on_can_set_cookie_count"];
321 EXPECT_EQ(1, (*counters_
)["on_can_set_cookie_count"]);
324 void OnCanAccessFileInternal(const URLRequest
& request
,
325 const base::FilePath
& path
) const override
{
326 ++(*counters_
)["on_can_access_file_count"];
327 EXPECT_EQ(1, (*counters_
)["on_can_access_file_count"]);
330 void OnCanEnablePrivacyModeInternal(
332 const GURL
& first_party_for_cookies
) const override
{
333 ++(*counters_
)["on_can_enable_privacy_mode_count"];
334 EXPECT_EQ(1, (*counters_
)["on_can_enable_privacy_mode_count"]);
337 void OnCancelURLRequestWithPolicyViolatingReferrerHeaderInternal(
338 const URLRequest
& request
,
339 const GURL
& target_url
,
340 const GURL
& referrer_url
) const override
{
342 ["on_cancel_url_request_with_policy_"
343 "violating_referrer_header_count"];
344 EXPECT_EQ(1, (*counters_
)
345 ["on_cancel_url_request_with_policy_"
346 "violating_referrer_header_count"]);
350 TestURLRequestContext context_
;
351 TestDelegate delegate_
;
352 mutable CountersMap
* counters_
;
354 DISALLOW_COPY_AND_ASSIGN(TestLayeredNetworkDelegate
);
359 class LayeredNetworkDelegateTest
: public testing::Test
{
361 LayeredNetworkDelegateTest() {
362 scoped_ptr
<TestNetworkDelegateImpl
> test_network_delegate(
363 new TestNetworkDelegateImpl(&layered_network_delegate_counters
));
364 test_network_delegate_
= test_network_delegate
.get();
365 layered_network_delegate_
=
366 scoped_ptr
<TestLayeredNetworkDelegate
>(new TestLayeredNetworkDelegate(
367 test_network_delegate
.Pass(), &layered_network_delegate_counters
));
370 CountersMap layered_network_delegate_counters
;
371 TestNetworkDelegateImpl
* test_network_delegate_
;
372 scoped_ptr
<TestLayeredNetworkDelegate
> layered_network_delegate_
;
375 TEST_F(LayeredNetworkDelegateTest
, VerifyLayeredNetworkDelegateInternal
) {
376 layered_network_delegate_
->CallAndVerify();