Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / url_request / url_request_test_util.cc
blobe4a7edd1be1e7dbd3969aa7a013e1a00062b83f4
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/default_server_bound_cert_store.h"
20 #include "net/ssl/server_bound_cert_service.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"
25 namespace net {
27 namespace {
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;
45 } // namespace
47 TestURLRequestContext::TestURLRequestContext()
48 : initialized_(false),
49 client_socket_factory_(NULL),
50 context_storage_(this) {
51 Init();
54 TestURLRequestContext::TestURLRequestContext(bool delay_initialization)
55 : initialized_(false),
56 client_socket_factory_(NULL),
57 context_storage_(this) {
58 if (!delay_initialization)
59 Init();
62 TestURLRequestContext::~TestURLRequestContext() {
63 DCHECK(initialized_);
66 void TestURLRequestContext::Init() {
67 DCHECK(!initialized_);
68 initialized_ = true;
70 if (!host_resolver())
71 context_storage_.set_host_resolver(
72 scoped_ptr<HostResolver>(new MockCachingHostResolver()));
73 if (!proxy_service())
74 context_storage_.set_proxy_service(ProxyService::CreateDirect());
75 if (!cert_verifier())
76 context_storage_.set_cert_verifier(CertVerifier::CreateDefault());
77 if (!transport_security_state())
78 context_storage_.set_transport_security_state(new TransportSecurityState);
79 if (!ssl_config_service())
80 context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
81 if (!http_auth_handler_factory()) {
82 context_storage_.set_http_auth_handler_factory(
83 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
85 if (!http_server_properties()) {
86 context_storage_.set_http_server_properties(
87 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
89 if (!transport_security_state()) {
90 context_storage_.set_transport_security_state(
91 new TransportSecurityState());
93 if (http_transaction_factory()) {
94 // Make sure we haven't been passed an object we're not going to use.
95 EXPECT_FALSE(client_socket_factory_);
96 } else {
97 HttpNetworkSession::Params params;
98 if (http_network_session_params_)
99 params = *http_network_session_params_;
100 params.client_socket_factory = client_socket_factory();
101 params.host_resolver = host_resolver();
102 params.cert_verifier = cert_verifier();
103 params.transport_security_state = transport_security_state();
104 params.proxy_service = proxy_service();
105 params.ssl_config_service = ssl_config_service();
106 params.http_auth_handler_factory = http_auth_handler_factory();
107 params.network_delegate = network_delegate();
108 params.http_server_properties = http_server_properties();
109 params.net_log = net_log();
110 context_storage_.set_http_transaction_factory(new HttpCache(
111 new HttpNetworkSession(params),
112 HttpCache::DefaultBackend::InMemory(0)));
114 // In-memory cookie store.
115 if (!cookie_store())
116 context_storage_.set_cookie_store(new CookieMonster(NULL, NULL));
117 // In-memory origin bound cert service.
118 if (!server_bound_cert_service()) {
119 context_storage_.set_server_bound_cert_service(
120 new ServerBoundCertService(
121 new DefaultServerBoundCertStore(NULL),
122 base::WorkerPool::GetTaskRunner(true)));
124 if (!http_user_agent_settings()) {
125 context_storage_.set_http_user_agent_settings(
126 new StaticHttpUserAgentSettings("en-us,fr", std::string()));
128 if (!job_factory())
129 context_storage_.set_job_factory(new URLRequestJobFactoryImpl);
132 TestURLRequest::TestURLRequest(const GURL& url,
133 RequestPriority priority,
134 Delegate* delegate,
135 TestURLRequestContext* context)
136 : URLRequest(url, priority, delegate, context) {}
138 TestURLRequest::~TestURLRequest() {
141 TestURLRequestContextGetter::TestURLRequestContextGetter(
142 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
143 : network_task_runner_(network_task_runner) {
144 DCHECK(network_task_runner_.get());
147 TestURLRequestContextGetter::TestURLRequestContextGetter(
148 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
149 scoped_ptr<TestURLRequestContext> context)
150 : network_task_runner_(network_task_runner), context_(context.Pass()) {
151 DCHECK(network_task_runner_.get());
154 TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
156 TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() {
157 if (!context_.get())
158 context_.reset(new TestURLRequestContext);
159 return context_.get();
162 scoped_refptr<base::SingleThreadTaskRunner>
163 TestURLRequestContextGetter::GetNetworkTaskRunner() const {
164 return network_task_runner_;
167 TestDelegate::TestDelegate()
168 : cancel_in_rr_(false),
169 cancel_in_rs_(false),
170 cancel_in_rd_(false),
171 cancel_in_rd_pending_(false),
172 quit_on_complete_(true),
173 quit_on_redirect_(false),
174 quit_on_before_network_start_(false),
175 allow_certificate_errors_(false),
176 response_started_count_(0),
177 received_bytes_count_(0),
178 received_redirect_count_(0),
179 received_before_network_start_count_(0),
180 received_data_before_response_(false),
181 request_failed_(false),
182 have_certificate_errors_(false),
183 certificate_errors_are_fatal_(false),
184 auth_required_(false),
185 have_full_request_headers_(false),
186 buf_(new IOBuffer(kBufferSize)) {
189 TestDelegate::~TestDelegate() {}
191 void TestDelegate::ClearFullRequestHeaders() {
192 full_request_headers_.Clear();
193 have_full_request_headers_ = false;
196 void TestDelegate::OnReceivedRedirect(URLRequest* request,
197 const GURL& new_url,
198 bool* defer_redirect) {
199 EXPECT_TRUE(request->is_redirecting());
201 have_full_request_headers_ =
202 request->GetFullRequestHeaders(&full_request_headers_);
204 received_redirect_count_++;
205 if (quit_on_redirect_) {
206 *defer_redirect = true;
207 base::MessageLoop::current()->PostTask(FROM_HERE,
208 base::MessageLoop::QuitClosure());
209 } else if (cancel_in_rr_) {
210 request->Cancel();
214 void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) {
215 received_before_network_start_count_++;
216 if (quit_on_before_network_start_) {
217 *defer = true;
218 base::MessageLoop::current()->PostTask(FROM_HERE,
219 base::MessageLoop::QuitClosure());
223 void TestDelegate::OnAuthRequired(URLRequest* request,
224 AuthChallengeInfo* auth_info) {
225 auth_required_ = true;
226 if (!credentials_.Empty()) {
227 request->SetAuth(credentials_);
228 } else {
229 request->CancelAuth();
233 void TestDelegate::OnSSLCertificateError(URLRequest* request,
234 const SSLInfo& ssl_info,
235 bool fatal) {
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();
243 else
244 request->Cancel();
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_++;
256 if (cancel_in_rs_) {
257 request->Cancel();
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);
264 } else {
265 // Initiate the first read.
266 int bytes_read = 0;
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;
281 if (cancel_in_rd_)
282 request->Cancel();
284 if (bytes_read >= 0) {
285 // There is data to read.
286 received_bytes_count_ += bytes_read;
288 // consume the data
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) {
294 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;
299 } else {
300 break;
304 if (!request->status().is_io_pending())
305 OnResponseCompleted(request);
306 else if (cancel_in_rd_pending_)
307 request->Cancel();
310 void TestDelegate::OnResponseCompleted(URLRequest* request) {
311 if (quit_on_complete_)
312 base::MessageLoop::current()->PostTask(FROM_HERE,
313 base::MessageLoop::QuitClosure());
316 TestNetworkDelegate::TestNetworkDelegate()
317 : last_error_(0),
318 error_count_(0),
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 has_load_timing_info_before_redirect_(false),
329 has_load_timing_info_before_auth_(false),
330 can_access_files_(true),
331 can_throttle_requests_(true) {
334 TestNetworkDelegate::~TestNetworkDelegate() {
335 for (std::map<int, int>::iterator i = next_states_.begin();
336 i != next_states_.end(); ++i) {
337 event_order_[i->first] += "~TestNetworkDelegate\n";
338 EXPECT_TRUE(i->second & kStageDestruction) << event_order_[i->first];
342 bool TestNetworkDelegate::GetLoadTimingInfoBeforeRedirect(
343 LoadTimingInfo* load_timing_info_before_redirect) const {
344 *load_timing_info_before_redirect = load_timing_info_before_redirect_;
345 return has_load_timing_info_before_redirect_;
348 bool TestNetworkDelegate::GetLoadTimingInfoBeforeAuth(
349 LoadTimingInfo* load_timing_info_before_auth) const {
350 *load_timing_info_before_auth = load_timing_info_before_auth_;
351 return has_load_timing_info_before_auth_;
354 void TestNetworkDelegate::InitRequestStatesIfNew(int request_id) {
355 if (next_states_.find(request_id) == next_states_.end()) {
356 // TODO(davidben): Although the URLRequest documentation does not allow
357 // calling Cancel() before Start(), the ResourceLoader does so. URLRequest's
358 // destructor also calls Cancel. Either officially support this or fix the
359 // ResourceLoader code.
360 next_states_[request_id] = kStageBeforeURLRequest | kStageCompletedError;
361 event_order_[request_id] = "";
365 int TestNetworkDelegate::OnBeforeURLRequest(
366 URLRequest* request,
367 const CompletionCallback& callback,
368 GURL* new_url ) {
369 int req_id = request->identifier();
370 InitRequestStatesIfNew(req_id);
371 event_order_[req_id] += "OnBeforeURLRequest\n";
372 EXPECT_TRUE(next_states_[req_id] & kStageBeforeURLRequest) <<
373 event_order_[req_id];
374 next_states_[req_id] =
375 kStageBeforeSendHeaders |
376 kStageResponseStarted | // data: URLs do not trigger sending headers
377 kStageBeforeRedirect | // a delegate can trigger a redirection
378 kStageCompletedError | // request canceled by delegate
379 kStageAuthRequired; // Auth can come next for FTP requests
380 created_requests_++;
381 return OK;
384 int TestNetworkDelegate::OnBeforeSendHeaders(
385 URLRequest* request,
386 const CompletionCallback& callback,
387 HttpRequestHeaders* headers) {
388 int req_id = request->identifier();
389 InitRequestStatesIfNew(req_id);
390 event_order_[req_id] += "OnBeforeSendHeaders\n";
391 EXPECT_TRUE(next_states_[req_id] & kStageBeforeSendHeaders) <<
392 event_order_[req_id];
393 next_states_[req_id] =
394 kStageSendHeaders |
395 kStageCompletedError; // request canceled by delegate
397 return OK;
400 void TestNetworkDelegate::OnBeforeSendProxyHeaders(
401 net::URLRequest* request,
402 const net::ProxyInfo& proxy_info,
403 net::HttpRequestHeaders* headers) {
404 ++observed_before_proxy_headers_sent_callbacks_;
405 last_observed_proxy_ = proxy_info.proxy_server().host_port_pair();
408 void TestNetworkDelegate::OnSendHeaders(
409 URLRequest* request,
410 const HttpRequestHeaders& headers) {
411 int req_id = request->identifier();
412 InitRequestStatesIfNew(req_id);
413 event_order_[req_id] += "OnSendHeaders\n";
414 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
415 event_order_[req_id];
416 next_states_[req_id] =
417 kStageHeadersReceived |
418 kStageCompletedError;
421 int TestNetworkDelegate::OnHeadersReceived(
422 URLRequest* request,
423 const CompletionCallback& callback,
424 const HttpResponseHeaders* original_response_headers,
425 scoped_refptr<HttpResponseHeaders>* override_response_headers,
426 GURL* allowed_unsafe_redirect_url) {
427 int req_id = request->identifier();
428 event_order_[req_id] += "OnHeadersReceived\n";
429 InitRequestStatesIfNew(req_id);
430 EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) <<
431 event_order_[req_id];
432 next_states_[req_id] =
433 kStageBeforeRedirect |
434 kStageResponseStarted |
435 kStageAuthRequired |
436 kStageCompletedError; // e.g. proxy resolution problem
438 // Basic authentication sends a second request from the URLRequestHttpJob
439 // layer before the URLRequest reports that a response has started.
440 next_states_[req_id] |= kStageBeforeSendHeaders;
442 if (!redirect_on_headers_received_url_.is_empty()) {
443 *override_response_headers =
444 new net::HttpResponseHeaders(original_response_headers->raw_headers());
445 (*override_response_headers)->ReplaceStatusLine("HTTP/1.1 302 Found");
446 (*override_response_headers)->RemoveHeader("Location");
447 (*override_response_headers)->AddHeader(
448 "Location: " + redirect_on_headers_received_url_.spec());
450 redirect_on_headers_received_url_ = GURL();
452 if (!allowed_unsafe_redirect_url_.is_empty())
453 *allowed_unsafe_redirect_url = allowed_unsafe_redirect_url_;
456 return OK;
459 void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request,
460 const GURL& new_location) {
461 load_timing_info_before_redirect_ = LoadTimingInfo();
462 request->GetLoadTimingInfo(&load_timing_info_before_redirect_);
463 has_load_timing_info_before_redirect_ = true;
464 EXPECT_FALSE(load_timing_info_before_redirect_.request_start_time.is_null());
465 EXPECT_FALSE(load_timing_info_before_redirect_.request_start.is_null());
467 int req_id = request->identifier();
468 InitRequestStatesIfNew(req_id);
469 event_order_[req_id] += "OnBeforeRedirect\n";
470 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) <<
471 event_order_[req_id];
472 next_states_[req_id] =
473 kStageBeforeURLRequest | // HTTP redirects trigger this.
474 kStageBeforeSendHeaders | // Redirects from the network delegate do not
475 // trigger onBeforeURLRequest.
476 kStageCompletedError;
478 // A redirect can lead to a file or a data URL. In this case, we do not send
479 // headers.
480 next_states_[req_id] |= kStageResponseStarted;
483 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) {
484 LoadTimingInfo load_timing_info;
485 request->GetLoadTimingInfo(&load_timing_info);
486 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
487 EXPECT_FALSE(load_timing_info.request_start.is_null());
489 int req_id = request->identifier();
490 InitRequestStatesIfNew(req_id);
491 event_order_[req_id] += "OnResponseStarted\n";
492 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
493 event_order_[req_id];
494 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
495 if (request->status().status() == URLRequestStatus::FAILED) {
496 error_count_++;
497 last_error_ = request->status().error();
501 void TestNetworkDelegate::OnRawBytesRead(const URLRequest& request,
502 int bytes_read) {
505 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
506 int req_id = request->identifier();
507 InitRequestStatesIfNew(req_id);
508 event_order_[req_id] += "OnCompleted\n";
509 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
510 // is logically identical to
511 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
512 EXPECT_TRUE(!request->status().is_success() ||
513 (next_states_[req_id] & kStageCompletedSuccess)) <<
514 event_order_[req_id];
515 EXPECT_TRUE(request->status().is_success() ||
516 (next_states_[req_id] & kStageCompletedError)) <<
517 event_order_[req_id];
518 next_states_[req_id] = kStageURLRequestDestroyed;
519 completed_requests_++;
520 if (request->status().status() == URLRequestStatus::FAILED) {
521 error_count_++;
522 last_error_ = request->status().error();
523 } else if (request->status().status() == URLRequestStatus::CANCELED) {
524 canceled_requests_++;
525 } else {
526 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status());
530 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) {
531 int req_id = request->identifier();
532 InitRequestStatesIfNew(req_id);
533 event_order_[req_id] += "OnURLRequestDestroyed\n";
534 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
535 event_order_[req_id];
536 next_states_[req_id] = kStageDestruction;
537 destroyed_requests_++;
540 void TestNetworkDelegate::OnPACScriptError(int line_number,
541 const base::string16& error) {
544 NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired(
545 URLRequest* request,
546 const AuthChallengeInfo& auth_info,
547 const AuthCallback& callback,
548 AuthCredentials* credentials) {
549 load_timing_info_before_auth_ = LoadTimingInfo();
550 request->GetLoadTimingInfo(&load_timing_info_before_auth_);
551 has_load_timing_info_before_auth_ = true;
552 EXPECT_FALSE(load_timing_info_before_auth_.request_start_time.is_null());
553 EXPECT_FALSE(load_timing_info_before_auth_.request_start.is_null());
555 int req_id = request->identifier();
556 InitRequestStatesIfNew(req_id);
557 event_order_[req_id] += "OnAuthRequired\n";
558 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) <<
559 event_order_[req_id];
560 next_states_[req_id] = kStageBeforeSendHeaders |
561 kStageAuthRequired | // For example, proxy auth followed by server auth.
562 kStageHeadersReceived | // Request canceled by delegate simulates empty
563 // response.
564 kStageResponseStarted | // data: URLs do not trigger sending headers
565 kStageBeforeRedirect | // a delegate can trigger a redirection
566 kStageCompletedError; // request cancelled before callback
567 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
570 bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request,
571 const CookieList& cookie_list) {
572 bool allow = true;
573 if (cookie_options_bit_mask_ & NO_GET_COOKIES)
574 allow = false;
576 if (!allow) {
577 blocked_get_cookies_count_++;
580 return allow;
583 bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
584 const std::string& cookie_line,
585 CookieOptions* options) {
586 bool allow = true;
587 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
588 allow = false;
590 if (!allow) {
591 blocked_set_cookie_count_++;
592 } else {
593 set_cookie_count_++;
596 return allow;
599 bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request,
600 const base::FilePath& path) const {
601 return can_access_files_;
604 bool TestNetworkDelegate::OnCanThrottleRequest(
605 const URLRequest& request) const {
606 return can_throttle_requests_;
609 int TestNetworkDelegate::OnBeforeSocketStreamConnect(
610 SocketStream* socket,
611 const CompletionCallback& callback) {
612 return OK;
615 // static
616 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1");
618 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost(
619 const std::string& new_value)
620 : old_value_(value_),
621 new_value_(new_value) {
622 value_ = new_value_;
625 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() {
626 DCHECK_EQ(value_, new_value_);
627 value_ = old_value_;
630 // static
631 const std::string& ScopedCustomUrlRequestTestHttpHost::value() {
632 return value_;
635 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) {
638 URLRequestJob* TestJobInterceptor::MaybeCreateJob(
639 URLRequest* request,
640 NetworkDelegate* network_delegate) const {
641 URLRequestJob* job = main_intercept_job_;
642 main_intercept_job_ = NULL;
643 return job;
646 void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) {
647 main_intercept_job_ = job;
650 } // namespace net