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
3 // found in the LICENSE file.
5 #include "net/url_request/url_request_test_util.h"
7 #include "base/compiler_specific.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/threading/thread.h"
14 #include "base/threading/worker_pool.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/cert/cert_verifier.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/http/http_network_session.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/http/http_server_properties_impl.h"
21 #include "net/http/transport_security_state.h"
22 #include "net/ssl/channel_id_service.h"
23 #include "net/ssl/default_channel_id_store.h"
24 #include "net/url_request/static_http_user_agent_settings.h"
25 #include "net/url_request/url_request_job_factory_impl.h"
26 #include "testing/gtest/include/gtest/gtest.h"
32 // These constants put the NetworkDelegate events of TestNetworkDelegate
33 // into an order. They are used in conjunction with
34 // |TestNetworkDelegate::next_states_| to check that we do not send
35 // events in the wrong order.
36 const int kStageBeforeURLRequest
= 1 << 0;
37 const int kStageBeforeSendHeaders
= 1 << 1;
38 const int kStageSendHeaders
= 1 << 2;
39 const int kStageHeadersReceived
= 1 << 3;
40 const int kStageAuthRequired
= 1 << 4;
41 const int kStageBeforeRedirect
= 1 << 5;
42 const int kStageResponseStarted
= 1 << 6;
43 const int kStageCompletedSuccess
= 1 << 7;
44 const int kStageCompletedError
= 1 << 8;
45 const int kStageURLRequestDestroyed
= 1 << 9;
46 const int kStageDestruction
= 1 << 10;
50 TestURLRequestContext::TestURLRequestContext()
51 : initialized_(false),
52 client_socket_factory_(NULL
),
53 context_storage_(this) {
57 TestURLRequestContext::TestURLRequestContext(bool delay_initialization
)
58 : initialized_(false),
59 client_socket_factory_(NULL
),
60 context_storage_(this) {
61 if (!delay_initialization
)
65 TestURLRequestContext::~TestURLRequestContext() {
67 AssertNoURLRequests();
70 void TestURLRequestContext::Init() {
71 DCHECK(!initialized_
);
75 context_storage_
.set_host_resolver(
76 scoped_ptr
<HostResolver
>(new MockCachingHostResolver()));
78 context_storage_
.set_proxy_service(ProxyService::CreateDirect());
80 context_storage_
.set_cert_verifier(CertVerifier::CreateDefault());
81 if (!transport_security_state())
82 context_storage_
.set_transport_security_state(new TransportSecurityState
);
83 if (!ssl_config_service())
84 context_storage_
.set_ssl_config_service(new SSLConfigServiceDefaults
);
85 if (!http_auth_handler_factory()) {
86 context_storage_
.set_http_auth_handler_factory(
87 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
89 if (!http_server_properties()) {
90 context_storage_
.set_http_server_properties(
91 scoped_ptr
<HttpServerProperties
>(new HttpServerPropertiesImpl()));
93 if (!transport_security_state()) {
94 context_storage_
.set_transport_security_state(
95 new TransportSecurityState());
97 if (http_transaction_factory()) {
98 // Make sure we haven't been passed an object we're not going to use.
99 EXPECT_FALSE(client_socket_factory_
);
101 HttpNetworkSession::Params params
;
102 if (http_network_session_params_
)
103 params
= *http_network_session_params_
;
104 params
.client_socket_factory
= client_socket_factory();
105 params
.host_resolver
= host_resolver();
106 params
.cert_verifier
= cert_verifier();
107 params
.transport_security_state
= transport_security_state();
108 params
.proxy_service
= proxy_service();
109 params
.ssl_config_service
= ssl_config_service();
110 params
.http_auth_handler_factory
= http_auth_handler_factory();
111 params
.network_delegate
= network_delegate();
112 params
.http_server_properties
= http_server_properties();
113 params
.net_log
= net_log();
114 context_storage_
.set_http_transaction_factory(new HttpCache(
115 new HttpNetworkSession(params
),
116 HttpCache::DefaultBackend::InMemory(0)));
118 // In-memory cookie store.
120 context_storage_
.set_cookie_store(new CookieMonster(NULL
, NULL
));
121 // In-memory Channel ID service.
122 if (!channel_id_service()) {
123 context_storage_
.set_channel_id_service(make_scoped_ptr(
124 new ChannelIDService(new DefaultChannelIDStore(NULL
),
125 base::WorkerPool::GetTaskRunner(true))));
127 if (!http_user_agent_settings()) {
128 context_storage_
.set_http_user_agent_settings(
129 new StaticHttpUserAgentSettings("en-us,fr", std::string()));
132 context_storage_
.set_job_factory(new URLRequestJobFactoryImpl
);
135 TestURLRequestContextGetter::TestURLRequestContextGetter(
136 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
)
137 : network_task_runner_(network_task_runner
) {
138 DCHECK(network_task_runner_
.get());
141 TestURLRequestContextGetter::TestURLRequestContextGetter(
142 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
,
143 scoped_ptr
<TestURLRequestContext
> context
)
144 : network_task_runner_(network_task_runner
), context_(context
.Pass()) {
145 DCHECK(network_task_runner_
.get());
148 TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
150 TestURLRequestContext
* TestURLRequestContextGetter::GetURLRequestContext() {
152 context_
.reset(new TestURLRequestContext
);
153 return context_
.get();
156 scoped_refptr
<base::SingleThreadTaskRunner
>
157 TestURLRequestContextGetter::GetNetworkTaskRunner() const {
158 return network_task_runner_
;
161 TestDelegate::TestDelegate()
162 : cancel_in_rr_(false),
163 cancel_in_rs_(false),
164 cancel_in_rd_(false),
165 cancel_in_rd_pending_(false),
166 quit_on_complete_(true),
167 quit_on_redirect_(false),
168 quit_on_auth_required_(false),
169 quit_on_before_network_start_(false),
170 allow_certificate_errors_(false),
171 response_started_count_(0),
172 received_bytes_count_(0),
173 received_redirect_count_(0),
174 received_before_network_start_count_(0),
175 received_data_before_response_(false),
176 request_failed_(false),
177 have_certificate_errors_(false),
178 certificate_errors_are_fatal_(false),
179 auth_required_(false),
180 have_full_request_headers_(false),
181 buf_(new IOBuffer(kBufferSize
)) {
184 TestDelegate::~TestDelegate() {}
186 void TestDelegate::ClearFullRequestHeaders() {
187 full_request_headers_
.Clear();
188 have_full_request_headers_
= false;
191 void TestDelegate::OnReceivedRedirect(URLRequest
* request
,
192 const RedirectInfo
& redirect_info
,
193 bool* defer_redirect
) {
194 EXPECT_TRUE(request
->is_redirecting());
196 have_full_request_headers_
=
197 request
->GetFullRequestHeaders(&full_request_headers_
);
199 received_redirect_count_
++;
200 if (quit_on_redirect_
) {
201 *defer_redirect
= true;
202 base::ThreadTaskRunnerHandle::Get()->PostTask(
203 FROM_HERE
, base::MessageLoop::QuitClosure());
204 } else if (cancel_in_rr_
) {
209 void TestDelegate::OnBeforeNetworkStart(URLRequest
* request
, bool* defer
) {
210 received_before_network_start_count_
++;
211 if (quit_on_before_network_start_
) {
213 base::ThreadTaskRunnerHandle::Get()->PostTask(
214 FROM_HERE
, base::MessageLoop::QuitClosure());
218 void TestDelegate::OnAuthRequired(URLRequest
* request
,
219 AuthChallengeInfo
* auth_info
) {
220 auth_required_
= true;
221 if (quit_on_auth_required_
) {
222 base::ThreadTaskRunnerHandle::Get()->PostTask(
223 FROM_HERE
, base::MessageLoop::QuitClosure());
226 if (!credentials_
.Empty()) {
227 request
->SetAuth(credentials_
);
229 request
->CancelAuth();
233 void TestDelegate::OnSSLCertificateError(URLRequest
* request
,
234 const SSLInfo
& ssl_info
,
236 // The caller can control whether it needs all SSL requests to go through,
237 // independent of any possible errors, or whether it wants SSL errors to
238 // cancel the request.
239 have_certificate_errors_
= true;
240 certificate_errors_are_fatal_
= fatal
;
241 if (allow_certificate_errors_
)
242 request
->ContinueDespiteLastError();
247 void TestDelegate::OnResponseStarted(URLRequest
* request
) {
248 // It doesn't make sense for the request to have IO pending at this point.
249 DCHECK(!request
->status().is_io_pending());
250 EXPECT_FALSE(request
->is_redirecting());
252 have_full_request_headers_
=
253 request
->GetFullRequestHeaders(&full_request_headers_
);
255 response_started_count_
++;
258 OnResponseCompleted(request
);
259 } else if (!request
->status().is_success()) {
260 DCHECK(request
->status().status() == URLRequestStatus::FAILED
||
261 request
->status().status() == URLRequestStatus::CANCELED
);
262 request_failed_
= true;
263 OnResponseCompleted(request
);
265 // Initiate the first read.
267 if (request
->Read(buf_
.get(), kBufferSize
, &bytes_read
))
268 OnReadCompleted(request
, bytes_read
);
269 else if (!request
->status().is_io_pending())
270 OnResponseCompleted(request
);
274 void TestDelegate::OnReadCompleted(URLRequest
* request
, int bytes_read
) {
275 // It doesn't make sense for the request to have IO pending at this point.
276 DCHECK(!request
->status().is_io_pending());
278 if (response_started_count_
== 0)
279 received_data_before_response_
= true;
284 if (bytes_read
>= 0) {
285 // There is data to read.
286 received_bytes_count_
+= bytes_read
;
289 data_received_
.append(buf_
->data(), bytes_read
);
292 // If it was not end of stream, request to read more.
293 if (request
->status().is_success() && bytes_read
> 0) {
295 while (request
->Read(buf_
.get(), kBufferSize
, &bytes_read
)) {
296 if (bytes_read
> 0) {
297 data_received_
.append(buf_
->data(), bytes_read
);
298 received_bytes_count_
+= bytes_read
;
304 if (!request
->status().is_io_pending())
305 OnResponseCompleted(request
);
306 else if (cancel_in_rd_pending_
)
310 void TestDelegate::OnResponseCompleted(URLRequest
* request
) {
311 if (quit_on_complete_
)
312 base::ThreadTaskRunnerHandle::Get()->PostTask(
313 FROM_HERE
, base::MessageLoop::QuitClosure());
316 TestNetworkDelegate::TestNetworkDelegate()
319 created_requests_(0),
320 destroyed_requests_(0),
321 completed_requests_(0),
322 canceled_requests_(0),
323 cookie_options_bit_mask_(0),
324 blocked_get_cookies_count_(0),
325 blocked_set_cookie_count_(0),
326 set_cookie_count_(0),
327 observed_before_proxy_headers_sent_callbacks_(0),
328 before_send_headers_count_(0),
329 headers_received_count_(0),
330 total_network_bytes_received_(0),
331 has_load_timing_info_before_redirect_(false),
332 has_load_timing_info_before_auth_(false),
333 can_access_files_(true),
334 first_party_only_cookies_enabled_(false),
335 cancel_request_with_policy_violating_referrer_(false),
336 will_be_intercepted_on_next_error_(false) {}
338 TestNetworkDelegate::~TestNetworkDelegate() {
339 for (std::map
<int, int>::iterator i
= next_states_
.begin();
340 i
!= next_states_
.end(); ++i
) {
341 event_order_
[i
->first
] += "~TestNetworkDelegate\n";
342 EXPECT_TRUE(i
->second
& kStageDestruction
) << event_order_
[i
->first
];
346 bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect(
347 LoadTimingInfo
* load_timing_info_before_redirect
) const {
348 *load_timing_info_before_redirect
= load_timing_info_before_redirect_
;
349 return has_load_timing_info_before_redirect_
;
352 bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth(
353 LoadTimingInfo
* load_timing_info_before_auth
) const {
354 *load_timing_info_before_auth
= load_timing_info_before_auth_
;
355 return has_load_timing_info_before_auth_
;
358 void TestNetworkDelegate::InitRequestStatesIfNew(int request_id
) {
359 if (next_states_
.find(request_id
) == next_states_
.end()) {
360 // TODO(davidben): Although the URLRequest documentation does not allow
361 // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's
362 // destructor also calls Cancel. Either officially support this or fix the
363 // ResourceLoader code.
364 next_states_
[request_id
] = kStageBeforeURLRequest
| kStageCompletedError
;
365 event_order_
[request_id
] = "";
369 int TestNetworkDelegate::OnBeforeURLRequest(
371 const CompletionCallback
& callback
,
373 int req_id
= request
->identifier();
374 InitRequestStatesIfNew(req_id
);
375 event_order_
[req_id
] += "OnBeforeURLRequest\n";
376 EXPECT_TRUE(next_states_
[req_id
] & kStageBeforeURLRequest
) <<
377 event_order_
[req_id
];
378 next_states_
[req_id
] =
379 kStageBeforeSendHeaders
|
380 kStageResponseStarted
| // data: URLs do not trigger sending headers
381 kStageBeforeRedirect
| // a delegate can trigger a redirection
382 kStageCompletedError
| // request canceled by delegate
383 kStageAuthRequired
; // Auth can come next for FTP requests
388 int TestNetworkDelegate::OnBeforeSendHeaders(
390 const CompletionCallback
& callback
,
391 HttpRequestHeaders
* headers
) {
392 int req_id
= request
->identifier();
393 InitRequestStatesIfNew(req_id
);
394 event_order_
[req_id
] += "OnBeforeSendHeaders\n";
395 EXPECT_TRUE(next_states_
[req_id
] & kStageBeforeSendHeaders
) <<
396 event_order_
[req_id
];
397 next_states_
[req_id
] =
399 kStageCompletedError
; // request canceled by delegate
400 before_send_headers_count_
++;
404 void TestNetworkDelegate::OnBeforeSendProxyHeaders(
406 const ProxyInfo
& proxy_info
,
407 HttpRequestHeaders
* headers
) {
408 ++observed_before_proxy_headers_sent_callbacks_
;
409 last_observed_proxy_
= proxy_info
.proxy_server().host_port_pair();
412 void TestNetworkDelegate::OnSendHeaders(
414 const HttpRequestHeaders
& headers
) {
415 int req_id
= request
->identifier();
416 InitRequestStatesIfNew(req_id
);
417 event_order_
[req_id
] += "OnSendHeaders\n";
418 EXPECT_TRUE(next_states_
[req_id
] & kStageSendHeaders
) <<
419 event_order_
[req_id
];
420 if (!will_be_intercepted_on_next_error_
)
421 next_states_
[req_id
] = kStageHeadersReceived
| kStageCompletedError
;
423 next_states_
[req_id
] = kStageResponseStarted
;
424 will_be_intercepted_on_next_error_
= false;
427 int TestNetworkDelegate::OnHeadersReceived(
429 const CompletionCallback
& callback
,
430 const HttpResponseHeaders
* original_response_headers
,
431 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
432 GURL
* allowed_unsafe_redirect_url
) {
433 int req_id
= request
->identifier();
434 event_order_
[req_id
] += "OnHeadersReceived\n";
435 InitRequestStatesIfNew(req_id
);
436 EXPECT_TRUE(next_states_
[req_id
] & kStageHeadersReceived
) <<
437 event_order_
[req_id
];
438 next_states_
[req_id
] =
439 kStageBeforeRedirect
|
440 kStageResponseStarted
|
442 kStageCompletedError
; // e.g. proxy resolution problem
444 // Basic authentication sends a second request from the URLRequestHttpJob
445 // layer before the URLRequest reports that a response has started.
446 next_states_
[req_id
] |= kStageBeforeSendHeaders
;
448 if (!redirect_on_headers_received_url_
.is_empty()) {
449 *override_response_headers
=
450 new HttpResponseHeaders(original_response_headers
->raw_headers());
451 (*override_response_headers
)->ReplaceStatusLine("HTTP/1.1 302 Found");
452 (*override_response_headers
)->RemoveHeader("Location");
453 (*override_response_headers
)->AddHeader(
454 "Location: " + redirect_on_headers_received_url_
.spec());
456 redirect_on_headers_received_url_
= GURL();
458 if (!allowed_unsafe_redirect_url_
.is_empty())
459 *allowed_unsafe_redirect_url
= allowed_unsafe_redirect_url_
;
461 headers_received_count_
++;
465 void TestNetworkDelegate::OnBeforeRedirect(URLRequest
* request
,
466 const GURL
& new_location
) {
467 load_timing_info_before_redirect_
= LoadTimingInfo();
468 request
->GetLoadTimingInfo(&load_timing_info_before_redirect_
);
469 has_load_timing_info_before_redirect_
= true;
470 EXPECT_FALSE(load_timing_info_before_redirect_
.request_start_time
.is_null());
471 EXPECT_FALSE(load_timing_info_before_redirect_
.request_start
.is_null());
473 int req_id
= request
->identifier();
474 InitRequestStatesIfNew(req_id
);
475 event_order_
[req_id
] += "OnBeforeRedirect\n";
476 EXPECT_TRUE(next_states_
[req_id
] & kStageBeforeRedirect
) <<
477 event_order_
[req_id
];
478 next_states_
[req_id
] =
479 kStageBeforeURLRequest
| // HTTP redirects trigger this.
480 kStageBeforeSendHeaders
| // Redirects from the network delegate do not
481 // trigger onBeforeURLRequest.
482 kStageCompletedError
;
484 // A redirect can lead to a file or a data URL. In this case, we do not send
486 next_states_
[req_id
] |= kStageResponseStarted
;
489 void TestNetworkDelegate::OnResponseStarted(URLRequest
* request
) {
490 LoadTimingInfo load_timing_info
;
491 request
->GetLoadTimingInfo(&load_timing_info
);
492 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
493 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
495 int req_id
= request
->identifier();
496 InitRequestStatesIfNew(req_id
);
497 event_order_
[req_id
] += "OnResponseStarted\n";
498 EXPECT_TRUE(next_states_
[req_id
] & kStageResponseStarted
) <<
499 event_order_
[req_id
];
500 next_states_
[req_id
] = kStageCompletedSuccess
| kStageCompletedError
;
501 if (request
->status().status() == URLRequestStatus::FAILED
) {
503 last_error_
= request
->status().error();
507 void TestNetworkDelegate::OnNetworkBytesReceived(const URLRequest
& request
,
508 int64_t bytes_received
) {
509 event_order_
[request
.identifier()] += "OnNetworkBytesReceived\n";
510 total_network_bytes_received_
+= bytes_received
;
513 void TestNetworkDelegate::OnCompleted(URLRequest
* request
, bool started
) {
514 int req_id
= request
->identifier();
515 InitRequestStatesIfNew(req_id
);
516 event_order_
[req_id
] += "OnCompleted\n";
517 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
518 // is logically identical to
519 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
520 EXPECT_TRUE(!request
->status().is_success() ||
521 (next_states_
[req_id
] & kStageCompletedSuccess
)) <<
522 event_order_
[req_id
];
523 EXPECT_TRUE(request
->status().is_success() ||
524 (next_states_
[req_id
] & kStageCompletedError
)) <<
525 event_order_
[req_id
];
526 next_states_
[req_id
] = kStageURLRequestDestroyed
;
527 completed_requests_
++;
528 if (request
->status().status() == URLRequestStatus::FAILED
) {
530 last_error_
= request
->status().error();
531 } else if (request
->status().status() == URLRequestStatus::CANCELED
) {
532 canceled_requests_
++;
534 DCHECK_EQ(URLRequestStatus::SUCCESS
, request
->status().status());
538 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest
* request
) {
539 int req_id
= request
->identifier();
540 InitRequestStatesIfNew(req_id
);
541 event_order_
[req_id
] += "OnURLRequestDestroyed\n";
542 EXPECT_TRUE(next_states_
[req_id
] & kStageURLRequestDestroyed
) <<
543 event_order_
[req_id
];
544 next_states_
[req_id
] = kStageDestruction
;
545 destroyed_requests_
++;
548 void TestNetworkDelegate::OnPACScriptError(int line_number
,
549 const base::string16
& error
) {
552 NetworkDelegate::AuthRequiredResponse
TestNetworkDelegate::OnAuthRequired(
554 const AuthChallengeInfo
& auth_info
,
555 const AuthCallback
& callback
,
556 AuthCredentials
* credentials
) {
557 load_timing_info_before_auth_
= LoadTimingInfo();
558 request
->GetLoadTimingInfo(&load_timing_info_before_auth_
);
559 has_load_timing_info_before_auth_
= true;
560 EXPECT_FALSE(load_timing_info_before_auth_
.request_start_time
.is_null());
561 EXPECT_FALSE(load_timing_info_before_auth_
.request_start
.is_null());
563 int req_id
= request
->identifier();
564 InitRequestStatesIfNew(req_id
);
565 event_order_
[req_id
] += "OnAuthRequired\n";
566 EXPECT_TRUE(next_states_
[req_id
] & kStageAuthRequired
) <<
567 event_order_
[req_id
];
568 next_states_
[req_id
] = kStageBeforeSendHeaders
|
569 kStageAuthRequired
| // For example, proxy auth followed by server auth.
570 kStageHeadersReceived
| // Request canceled by delegate simulates empty
572 kStageResponseStarted
| // data: URLs do not trigger sending headers
573 kStageBeforeRedirect
| // a delegate can trigger a redirection
574 kStageCompletedError
; // request cancelled before callback
575 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
578 bool TestNetworkDelegate::OnCanGetCookies(const URLRequest
& request
,
579 const CookieList
& cookie_list
) {
581 if (cookie_options_bit_mask_
& NO_GET_COOKIES
)
585 blocked_get_cookies_count_
++;
591 bool TestNetworkDelegate::OnCanSetCookie(const URLRequest
& request
,
592 const std::string
& cookie_line
,
593 CookieOptions
* options
) {
595 if (cookie_options_bit_mask_
& NO_SET_COOKIE
)
599 blocked_set_cookie_count_
++;
607 bool TestNetworkDelegate::OnCanAccessFile(const URLRequest
& request
,
608 const base::FilePath
& path
) const {
609 return can_access_files_
;
612 bool TestNetworkDelegate::OnFirstPartyOnlyCookieExperimentEnabled() const {
613 return first_party_only_cookies_enabled_
;
616 bool TestNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
617 const URLRequest
& request
,
618 const GURL
& target_url
,
619 const GURL
& referrer_url
) const {
620 return cancel_request_with_policy_violating_referrer_
;
623 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL
) {
626 URLRequestJob
* TestJobInterceptor::MaybeCreateJob(
628 NetworkDelegate
* network_delegate
) const {
629 URLRequestJob
* job
= main_intercept_job_
;
630 main_intercept_job_
= NULL
;
634 void TestJobInterceptor::set_main_intercept_job(URLRequestJob
* job
) {
635 main_intercept_job_
= job
;