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/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/threading/thread.h"
11 #include "base/threading/worker_pool.h"
12 #include "net/base/host_port_pair.h"
13 #include "net/cert/cert_verifier.h"
14 #include "net/dns/mock_host_resolver.h"
15 #include "net/http/http_network_session.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/http/http_server_properties_impl.h"
18 #include "net/http/transport_security_state.h"
19 #include "net/ssl/channel_id_service.h"
20 #include "net/ssl/default_channel_id_store.h"
21 #include "net/url_request/static_http_user_agent_settings.h"
22 #include "net/url_request/url_request_job_factory_impl.h"
23 #include "testing/gtest/include/gtest/gtest.h"
29 // These constants put the NetworkDelegate events of TestNetworkDelegate
30 // into an order. They are used in conjunction with
31 // |TestNetworkDelegate::next_states_| to check that we do not send
32 // events in the wrong order.
33 const int kStageBeforeURLRequest
= 1 << 0;
34 const int kStageBeforeSendHeaders
= 1 << 1;
35 const int kStageSendHeaders
= 1 << 2;
36 const int kStageHeadersReceived
= 1 << 3;
37 const int kStageAuthRequired
= 1 << 4;
38 const int kStageBeforeRedirect
= 1 << 5;
39 const int kStageResponseStarted
= 1 << 6;
40 const int kStageCompletedSuccess
= 1 << 7;
41 const int kStageCompletedError
= 1 << 8;
42 const int kStageURLRequestDestroyed
= 1 << 9;
43 const int kStageDestruction
= 1 << 10;
47 TestURLRequestContext::TestURLRequestContext()
48 : initialized_(false),
49 client_socket_factory_(NULL
),
50 context_storage_(this) {
54 TestURLRequestContext::TestURLRequestContext(bool delay_initialization
)
55 : initialized_(false),
56 client_socket_factory_(NULL
),
57 context_storage_(this) {
58 if (!delay_initialization
)
62 TestURLRequestContext::~TestURLRequestContext() {
64 AssertNoURLRequests();
67 void TestURLRequestContext::Init() {
68 DCHECK(!initialized_
);
72 context_storage_
.set_host_resolver(
73 scoped_ptr
<HostResolver
>(new MockCachingHostResolver()));
75 context_storage_
.set_proxy_service(ProxyService::CreateDirect());
77 context_storage_
.set_cert_verifier(CertVerifier::CreateDefault());
78 if (!transport_security_state())
79 context_storage_
.set_transport_security_state(new TransportSecurityState
);
80 if (!ssl_config_service())
81 context_storage_
.set_ssl_config_service(new SSLConfigServiceDefaults
);
82 if (!http_auth_handler_factory()) {
83 context_storage_
.set_http_auth_handler_factory(
84 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
86 if (!http_server_properties()) {
87 context_storage_
.set_http_server_properties(
88 scoped_ptr
<HttpServerProperties
>(new HttpServerPropertiesImpl()));
90 if (!transport_security_state()) {
91 context_storage_
.set_transport_security_state(
92 new TransportSecurityState());
94 if (http_transaction_factory()) {
95 // Make sure we haven't been passed an object we're not going to use.
96 EXPECT_FALSE(client_socket_factory_
);
98 HttpNetworkSession::Params params
;
99 if (http_network_session_params_
)
100 params
= *http_network_session_params_
;
101 params
.client_socket_factory
= client_socket_factory();
102 params
.host_resolver
= host_resolver();
103 params
.cert_verifier
= cert_verifier();
104 params
.transport_security_state
= transport_security_state();
105 params
.proxy_service
= proxy_service();
106 params
.ssl_config_service
= ssl_config_service();
107 params
.http_auth_handler_factory
= http_auth_handler_factory();
108 params
.network_delegate
= network_delegate();
109 params
.http_server_properties
= http_server_properties();
110 params
.net_log
= net_log();
111 context_storage_
.set_http_transaction_factory(new HttpCache(
112 new HttpNetworkSession(params
),
113 HttpCache::DefaultBackend::InMemory(0)));
115 // In-memory cookie store.
117 context_storage_
.set_cookie_store(new CookieMonster(NULL
, NULL
));
118 // In-memory Channel ID service.
119 if (!channel_id_service()) {
120 context_storage_
.set_channel_id_service(
121 new ChannelIDService(
122 new DefaultChannelIDStore(NULL
),
123 base::WorkerPool::GetTaskRunner(true)));
125 if (!http_user_agent_settings()) {
126 context_storage_
.set_http_user_agent_settings(
127 new StaticHttpUserAgentSettings("en-us,fr", std::string()));
130 context_storage_
.set_job_factory(new URLRequestJobFactoryImpl
);
133 TestURLRequestContextGetter::TestURLRequestContextGetter(
134 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
)
135 : network_task_runner_(network_task_runner
) {
136 DCHECK(network_task_runner_
.get());
139 TestURLRequestContextGetter::TestURLRequestContextGetter(
140 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
,
141 scoped_ptr
<TestURLRequestContext
> context
)
142 : network_task_runner_(network_task_runner
), context_(context
.Pass()) {
143 DCHECK(network_task_runner_
.get());
146 TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
148 TestURLRequestContext
* TestURLRequestContextGetter::GetURLRequestContext() {
150 context_
.reset(new TestURLRequestContext
);
151 return context_
.get();
154 scoped_refptr
<base::SingleThreadTaskRunner
>
155 TestURLRequestContextGetter::GetNetworkTaskRunner() const {
156 return network_task_runner_
;
159 TestDelegate::TestDelegate()
160 : cancel_in_rr_(false),
161 cancel_in_rs_(false),
162 cancel_in_rd_(false),
163 cancel_in_rd_pending_(false),
164 quit_on_complete_(true),
165 quit_on_redirect_(false),
166 quit_on_before_network_start_(false),
167 allow_certificate_errors_(false),
168 response_started_count_(0),
169 received_bytes_count_(0),
170 received_redirect_count_(0),
171 received_before_network_start_count_(0),
172 received_data_before_response_(false),
173 request_failed_(false),
174 have_certificate_errors_(false),
175 certificate_errors_are_fatal_(false),
176 auth_required_(false),
177 have_full_request_headers_(false),
178 buf_(new IOBuffer(kBufferSize
)) {
181 TestDelegate::~TestDelegate() {}
183 void TestDelegate::ClearFullRequestHeaders() {
184 full_request_headers_
.Clear();
185 have_full_request_headers_
= false;
188 void TestDelegate::OnReceivedRedirect(URLRequest
* request
,
189 const RedirectInfo
& redirect_info
,
190 bool* defer_redirect
) {
191 EXPECT_TRUE(request
->is_redirecting());
193 have_full_request_headers_
=
194 request
->GetFullRequestHeaders(&full_request_headers_
);
196 received_redirect_count_
++;
197 if (quit_on_redirect_
) {
198 *defer_redirect
= true;
199 base::MessageLoop::current()->PostTask(FROM_HERE
,
200 base::MessageLoop::QuitClosure());
201 } else if (cancel_in_rr_
) {
206 void TestDelegate::OnBeforeNetworkStart(URLRequest
* request
, bool* defer
) {
207 received_before_network_start_count_
++;
208 if (quit_on_before_network_start_
) {
210 base::MessageLoop::current()->PostTask(FROM_HERE
,
211 base::MessageLoop::QuitClosure());
215 void TestDelegate::OnAuthRequired(URLRequest
* request
,
216 AuthChallengeInfo
* auth_info
) {
217 auth_required_
= true;
218 if (!credentials_
.Empty()) {
219 request
->SetAuth(credentials_
);
221 request
->CancelAuth();
225 void TestDelegate::OnSSLCertificateError(URLRequest
* request
,
226 const SSLInfo
& ssl_info
,
228 // The caller can control whether it needs all SSL requests to go through,
229 // independent of any possible errors, or whether it wants SSL errors to
230 // cancel the request.
231 have_certificate_errors_
= true;
232 certificate_errors_are_fatal_
= fatal
;
233 if (allow_certificate_errors_
)
234 request
->ContinueDespiteLastError();
239 void TestDelegate::OnResponseStarted(URLRequest
* request
) {
240 // It doesn't make sense for the request to have IO pending at this point.
241 DCHECK(!request
->status().is_io_pending());
242 EXPECT_FALSE(request
->is_redirecting());
244 have_full_request_headers_
=
245 request
->GetFullRequestHeaders(&full_request_headers_
);
247 response_started_count_
++;
250 OnResponseCompleted(request
);
251 } else if (!request
->status().is_success()) {
252 DCHECK(request
->status().status() == URLRequestStatus::FAILED
||
253 request
->status().status() == URLRequestStatus::CANCELED
);
254 request_failed_
= true;
255 OnResponseCompleted(request
);
257 // Initiate the first read.
259 if (request
->Read(buf_
.get(), kBufferSize
, &bytes_read
))
260 OnReadCompleted(request
, bytes_read
);
261 else if (!request
->status().is_io_pending())
262 OnResponseCompleted(request
);
266 void TestDelegate::OnReadCompleted(URLRequest
* request
, int bytes_read
) {
267 // It doesn't make sense for the request to have IO pending at this point.
268 DCHECK(!request
->status().is_io_pending());
270 if (response_started_count_
== 0)
271 received_data_before_response_
= true;
276 if (bytes_read
>= 0) {
277 // There is data to read.
278 received_bytes_count_
+= bytes_read
;
281 data_received_
.append(buf_
->data(), bytes_read
);
284 // If it was not end of stream, request to read more.
285 if (request
->status().is_success() && bytes_read
> 0) {
287 while (request
->Read(buf_
.get(), kBufferSize
, &bytes_read
)) {
288 if (bytes_read
> 0) {
289 data_received_
.append(buf_
->data(), bytes_read
);
290 received_bytes_count_
+= bytes_read
;
296 if (!request
->status().is_io_pending())
297 OnResponseCompleted(request
);
298 else if (cancel_in_rd_pending_
)
302 void TestDelegate::OnResponseCompleted(URLRequest
* request
) {
303 if (quit_on_complete_
)
304 base::MessageLoop::current()->PostTask(FROM_HERE
,
305 base::MessageLoop::QuitClosure());
308 TestNetworkDelegate::TestNetworkDelegate()
311 created_requests_(0),
312 destroyed_requests_(0),
313 completed_requests_(0),
314 canceled_requests_(0),
315 cookie_options_bit_mask_(0),
316 blocked_get_cookies_count_(0),
317 blocked_set_cookie_count_(0),
318 set_cookie_count_(0),
319 observed_before_proxy_headers_sent_callbacks_(0),
320 before_send_headers_count_(0),
321 headers_received_count_(0),
322 has_load_timing_info_before_redirect_(false),
323 has_load_timing_info_before_auth_(false),
324 can_access_files_(true),
325 can_throttle_requests_(true),
326 cancel_request_with_policy_violating_referrer_(false),
327 will_be_intercepted_on_next_error_(false) {
330 TestNetworkDelegate::~TestNetworkDelegate() {
331 for (std::map
<int, int>::iterator i
= next_states_
.begin();
332 i
!= next_states_
.end(); ++i
) {
333 event_order_
[i
->first
] += "~TestNetworkDelegate\n";
334 EXPECT_TRUE(i
->second
& kStageDestruction
) << event_order_
[i
->first
];
338 bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect(
339 LoadTimingInfo
* load_timing_info_before_redirect
) const {
340 *load_timing_info_before_redirect
= load_timing_info_before_redirect_
;
341 return has_load_timing_info_before_redirect_
;
344 bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth(
345 LoadTimingInfo
* load_timing_info_before_auth
) const {
346 *load_timing_info_before_auth
= load_timing_info_before_auth_
;
347 return has_load_timing_info_before_auth_
;
350 void TestNetworkDelegate::InitRequestStatesIfNew(int request_id
) {
351 if (next_states_
.find(request_id
) == next_states_
.end()) {
352 // TODO(davidben): Although the URLRequest documentation does not allow
353 // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's
354 // destructor also calls Cancel. Either officially support this or fix the
355 // ResourceLoader code.
356 next_states_
[request_id
] = kStageBeforeURLRequest
| kStageCompletedError
;
357 event_order_
[request_id
] = "";
361 int TestNetworkDelegate::OnBeforeURLRequest(
363 const CompletionCallback
& callback
,
365 int req_id
= request
->identifier();
366 InitRequestStatesIfNew(req_id
);
367 event_order_
[req_id
] += "OnBeforeURLRequest\n";
368 EXPECT_TRUE(next_states_
[req_id
] & kStageBeforeURLRequest
) <<
369 event_order_
[req_id
];
370 next_states_
[req_id
] =
371 kStageBeforeSendHeaders
|
372 kStageResponseStarted
| // data: URLs do not trigger sending headers
373 kStageBeforeRedirect
| // a delegate can trigger a redirection
374 kStageCompletedError
| // request canceled by delegate
375 kStageAuthRequired
; // Auth can come next for FTP requests
380 int TestNetworkDelegate::OnBeforeSendHeaders(
382 const CompletionCallback
& callback
,
383 HttpRequestHeaders
* headers
) {
384 int req_id
= request
->identifier();
385 InitRequestStatesIfNew(req_id
);
386 event_order_
[req_id
] += "OnBeforeSendHeaders\n";
387 EXPECT_TRUE(next_states_
[req_id
] & kStageBeforeSendHeaders
) <<
388 event_order_
[req_id
];
389 next_states_
[req_id
] =
391 kStageCompletedError
; // request canceled by delegate
392 before_send_headers_count_
++;
396 void TestNetworkDelegate::OnBeforeSendProxyHeaders(
397 net::URLRequest
* request
,
398 const net::ProxyInfo
& proxy_info
,
399 net::HttpRequestHeaders
* headers
) {
400 ++observed_before_proxy_headers_sent_callbacks_
;
401 last_observed_proxy_
= proxy_info
.proxy_server().host_port_pair();
404 void TestNetworkDelegate::OnSendHeaders(
406 const HttpRequestHeaders
& headers
) {
407 int req_id
= request
->identifier();
408 InitRequestStatesIfNew(req_id
);
409 event_order_
[req_id
] += "OnSendHeaders\n";
410 EXPECT_TRUE(next_states_
[req_id
] & kStageSendHeaders
) <<
411 event_order_
[req_id
];
412 if (!will_be_intercepted_on_next_error_
)
413 next_states_
[req_id
] = kStageHeadersReceived
| kStageCompletedError
;
415 next_states_
[req_id
] = kStageResponseStarted
;
416 will_be_intercepted_on_next_error_
= false;
419 int TestNetworkDelegate::OnHeadersReceived(
421 const CompletionCallback
& callback
,
422 const HttpResponseHeaders
* original_response_headers
,
423 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
424 GURL
* allowed_unsafe_redirect_url
) {
425 int req_id
= request
->identifier();
426 event_order_
[req_id
] += "OnHeadersReceived\n";
427 InitRequestStatesIfNew(req_id
);
428 EXPECT_TRUE(next_states_
[req_id
] & kStageHeadersReceived
) <<
429 event_order_
[req_id
];
430 next_states_
[req_id
] =
431 kStageBeforeRedirect
|
432 kStageResponseStarted
|
434 kStageCompletedError
; // e.g. proxy resolution problem
436 // Basic authentication sends a second request from the URLRequestHttpJob
437 // layer before the URLRequest reports that a response has started.
438 next_states_
[req_id
] |= kStageBeforeSendHeaders
;
440 if (!redirect_on_headers_received_url_
.is_empty()) {
441 *override_response_headers
=
442 new net::HttpResponseHeaders(original_response_headers
->raw_headers());
443 (*override_response_headers
)->ReplaceStatusLine("HTTP/1.1 302 Found");
444 (*override_response_headers
)->RemoveHeader("Location");
445 (*override_response_headers
)->AddHeader(
446 "Location: " + redirect_on_headers_received_url_
.spec());
448 redirect_on_headers_received_url_
= GURL();
450 if (!allowed_unsafe_redirect_url_
.is_empty())
451 *allowed_unsafe_redirect_url
= allowed_unsafe_redirect_url_
;
453 headers_received_count_
++;
457 void TestNetworkDelegate::OnBeforeRedirect(URLRequest
* request
,
458 const GURL
& new_location
) {
459 load_timing_info_before_redirect_
= LoadTimingInfo();
460 request
->GetLoadTimingInfo(&load_timing_info_before_redirect_
);
461 has_load_timing_info_before_redirect_
= true;
462 EXPECT_FALSE(load_timing_info_before_redirect_
.request_start_time
.is_null());
463 EXPECT_FALSE(load_timing_info_before_redirect_
.request_start
.is_null());
465 int req_id
= request
->identifier();
466 InitRequestStatesIfNew(req_id
);
467 event_order_
[req_id
] += "OnBeforeRedirect\n";
468 EXPECT_TRUE(next_states_
[req_id
] & kStageBeforeRedirect
) <<
469 event_order_
[req_id
];
470 next_states_
[req_id
] =
471 kStageBeforeURLRequest
| // HTTP redirects trigger this.
472 kStageBeforeSendHeaders
| // Redirects from the network delegate do not
473 // trigger onBeforeURLRequest.
474 kStageCompletedError
;
476 // A redirect can lead to a file or a data URL. In this case, we do not send
478 next_states_
[req_id
] |= kStageResponseStarted
;
481 void TestNetworkDelegate::OnResponseStarted(URLRequest
* request
) {
482 LoadTimingInfo load_timing_info
;
483 request
->GetLoadTimingInfo(&load_timing_info
);
484 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
485 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
487 int req_id
= request
->identifier();
488 InitRequestStatesIfNew(req_id
);
489 event_order_
[req_id
] += "OnResponseStarted\n";
490 EXPECT_TRUE(next_states_
[req_id
] & kStageResponseStarted
) <<
491 event_order_
[req_id
];
492 next_states_
[req_id
] = kStageCompletedSuccess
| kStageCompletedError
;
493 if (request
->status().status() == URLRequestStatus::FAILED
) {
495 last_error_
= request
->status().error();
499 void TestNetworkDelegate::OnRawBytesRead(const URLRequest
& request
,
503 void TestNetworkDelegate::OnCompleted(URLRequest
* request
, bool started
) {
504 int req_id
= request
->identifier();
505 InitRequestStatesIfNew(req_id
);
506 event_order_
[req_id
] += "OnCompleted\n";
507 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
508 // is logically identical to
509 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
510 EXPECT_TRUE(!request
->status().is_success() ||
511 (next_states_
[req_id
] & kStageCompletedSuccess
)) <<
512 event_order_
[req_id
];
513 EXPECT_TRUE(request
->status().is_success() ||
514 (next_states_
[req_id
] & kStageCompletedError
)) <<
515 event_order_
[req_id
];
516 next_states_
[req_id
] = kStageURLRequestDestroyed
;
517 completed_requests_
++;
518 if (request
->status().status() == URLRequestStatus::FAILED
) {
520 last_error_
= request
->status().error();
521 } else if (request
->status().status() == URLRequestStatus::CANCELED
) {
522 canceled_requests_
++;
524 DCHECK_EQ(URLRequestStatus::SUCCESS
, request
->status().status());
528 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest
* request
) {
529 int req_id
= request
->identifier();
530 InitRequestStatesIfNew(req_id
);
531 event_order_
[req_id
] += "OnURLRequestDestroyed\n";
532 EXPECT_TRUE(next_states_
[req_id
] & kStageURLRequestDestroyed
) <<
533 event_order_
[req_id
];
534 next_states_
[req_id
] = kStageDestruction
;
535 destroyed_requests_
++;
538 void TestNetworkDelegate::OnPACScriptError(int line_number
,
539 const base::string16
& error
) {
542 NetworkDelegate::AuthRequiredResponse
TestNetworkDelegate::OnAuthRequired(
544 const AuthChallengeInfo
& auth_info
,
545 const AuthCallback
& callback
,
546 AuthCredentials
* credentials
) {
547 load_timing_info_before_auth_
= LoadTimingInfo();
548 request
->GetLoadTimingInfo(&load_timing_info_before_auth_
);
549 has_load_timing_info_before_auth_
= true;
550 EXPECT_FALSE(load_timing_info_before_auth_
.request_start_time
.is_null());
551 EXPECT_FALSE(load_timing_info_before_auth_
.request_start
.is_null());
553 int req_id
= request
->identifier();
554 InitRequestStatesIfNew(req_id
);
555 event_order_
[req_id
] += "OnAuthRequired\n";
556 EXPECT_TRUE(next_states_
[req_id
] & kStageAuthRequired
) <<
557 event_order_
[req_id
];
558 next_states_
[req_id
] = kStageBeforeSendHeaders
|
559 kStageAuthRequired
| // For example, proxy auth followed by server auth.
560 kStageHeadersReceived
| // Request canceled by delegate simulates empty
562 kStageResponseStarted
| // data: URLs do not trigger sending headers
563 kStageBeforeRedirect
| // a delegate can trigger a redirection
564 kStageCompletedError
; // request cancelled before callback
565 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
568 bool TestNetworkDelegate::OnCanGetCookies(const URLRequest
& request
,
569 const CookieList
& cookie_list
) {
571 if (cookie_options_bit_mask_
& NO_GET_COOKIES
)
575 blocked_get_cookies_count_
++;
581 bool TestNetworkDelegate::OnCanSetCookie(const URLRequest
& request
,
582 const std::string
& cookie_line
,
583 CookieOptions
* options
) {
585 if (cookie_options_bit_mask_
& NO_SET_COOKIE
)
589 blocked_set_cookie_count_
++;
597 bool TestNetworkDelegate::OnCanAccessFile(const URLRequest
& request
,
598 const base::FilePath
& path
) const {
599 return can_access_files_
;
602 bool TestNetworkDelegate::OnCanThrottleRequest(
603 const URLRequest
& request
) const {
604 return can_throttle_requests_
;
607 bool TestNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
608 const URLRequest
& request
,
609 const GURL
& target_url
,
610 const GURL
& referrer_url
) const {
611 return cancel_request_with_policy_violating_referrer_
;
615 std::string
ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1");
617 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost(
618 const std::string
& new_value
)
619 : old_value_(value_
),
620 new_value_(new_value
) {
624 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() {
625 DCHECK_EQ(value_
, new_value_
);
630 const std::string
& ScopedCustomUrlRequestTestHttpHost::value() {
634 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL
) {
637 URLRequestJob
* TestJobInterceptor::MaybeCreateJob(
639 NetworkDelegate
* network_delegate
) const {
640 URLRequestJob
* job
= main_intercept_job_
;
641 main_intercept_job_
= NULL
;
645 void TestJobInterceptor::set_main_intercept_job(URLRequestJob
* job
) {
646 main_intercept_job_
= job
;