Remove muting for extreme playbackRates.
[chromium-blink-merge.git] / net / url_request / url_request_test_util.cc
blob419816bfa95bd08e12cdc09da861092a8c392726
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_server_properties_impl.h"
17 #include "net/http/transport_security_state.h"
18 #include "net/ssl/default_server_bound_cert_store.h"
19 #include "net/ssl/server_bound_cert_service.h"
20 #include "net/url_request/static_http_user_agent_settings.h"
21 #include "net/url_request/url_request_job_factory_impl.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 namespace net {
26 namespace {
28 // These constants put the NetworkDelegate events of TestNetworkDelegate
29 // into an order. They are used in conjunction with
30 // |TestNetworkDelegate::next_states_| to check that we do not send
31 // events in the wrong order.
32 const int kStageBeforeURLRequest = 1 << 0;
33 const int kStageBeforeSendHeaders = 1 << 1;
34 const int kStageSendHeaders = 1 << 2;
35 const int kStageHeadersReceived = 1 << 3;
36 const int kStageAuthRequired = 1 << 4;
37 const int kStageBeforeRedirect = 1 << 5;
38 const int kStageResponseStarted = 1 << 6;
39 const int kStageCompletedSuccess = 1 << 7;
40 const int kStageCompletedError = 1 << 8;
41 const int kStageURLRequestDestroyed = 1 << 9;
42 const int kStageDestruction = 1 << 10;
44 } // namespace
46 TestURLRequestContext::TestURLRequestContext()
47 : initialized_(false),
48 client_socket_factory_(NULL),
49 context_storage_(this) {
50 Init();
53 TestURLRequestContext::TestURLRequestContext(bool delay_initialization)
54 : initialized_(false),
55 client_socket_factory_(NULL),
56 context_storage_(this) {
57 if (!delay_initialization)
58 Init();
61 TestURLRequestContext::~TestURLRequestContext() {
62 DCHECK(initialized_);
65 void TestURLRequestContext::Init() {
66 DCHECK(!initialized_);
67 initialized_ = true;
69 if (!host_resolver())
70 context_storage_.set_host_resolver(
71 scoped_ptr<HostResolver>(new MockCachingHostResolver()));
72 if (!proxy_service())
73 context_storage_.set_proxy_service(ProxyService::CreateDirect());
74 if (!cert_verifier())
75 context_storage_.set_cert_verifier(CertVerifier::CreateDefault());
76 if (!transport_security_state())
77 context_storage_.set_transport_security_state(new TransportSecurityState);
78 if (!ssl_config_service())
79 context_storage_.set_ssl_config_service(new SSLConfigServiceDefaults);
80 if (!http_auth_handler_factory()) {
81 context_storage_.set_http_auth_handler_factory(
82 HttpAuthHandlerFactory::CreateDefault(host_resolver()));
84 if (!http_server_properties()) {
85 context_storage_.set_http_server_properties(
86 scoped_ptr<HttpServerProperties>(new HttpServerPropertiesImpl()));
88 if (!transport_security_state()) {
89 context_storage_.set_transport_security_state(
90 new TransportSecurityState());
92 if (http_transaction_factory()) {
93 // Make sure we haven't been passed an object we're not going to use.
94 EXPECT_FALSE(client_socket_factory_);
95 } else {
96 HttpNetworkSession::Params params;
97 params.client_socket_factory = client_socket_factory();
98 params.host_resolver = host_resolver();
99 params.cert_verifier = cert_verifier();
100 params.transport_security_state = transport_security_state();
101 params.proxy_service = proxy_service();
102 params.ssl_config_service = ssl_config_service();
103 params.http_auth_handler_factory = http_auth_handler_factory();
104 params.network_delegate = network_delegate();
105 params.http_server_properties = http_server_properties();
106 params.net_log = net_log();
107 context_storage_.set_http_transaction_factory(new HttpCache(
108 new HttpNetworkSession(params),
109 HttpCache::DefaultBackend::InMemory(0)));
111 // In-memory cookie store.
112 if (!cookie_store())
113 context_storage_.set_cookie_store(new CookieMonster(NULL, NULL));
114 // In-memory origin bound cert service.
115 if (!server_bound_cert_service()) {
116 context_storage_.set_server_bound_cert_service(
117 new ServerBoundCertService(
118 new DefaultServerBoundCertStore(NULL),
119 base::WorkerPool::GetTaskRunner(true)));
121 if (!http_user_agent_settings()) {
122 context_storage_.set_http_user_agent_settings(
123 new StaticHttpUserAgentSettings("en-us,fr", std::string()));
125 if (!job_factory())
126 context_storage_.set_job_factory(new URLRequestJobFactoryImpl);
129 TestURLRequest::TestURLRequest(const GURL& url,
130 RequestPriority priority,
131 Delegate* delegate,
132 TestURLRequestContext* context)
133 : URLRequest(url, priority, delegate, context) {}
135 TestURLRequest::~TestURLRequest() {
138 TestURLRequestContextGetter::TestURLRequestContextGetter(
139 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
140 : network_task_runner_(network_task_runner) {
141 DCHECK(network_task_runner_.get());
144 TestURLRequestContextGetter::TestURLRequestContextGetter(
145 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
146 scoped_ptr<TestURLRequestContext> context)
147 : network_task_runner_(network_task_runner), context_(context.Pass()) {
148 DCHECK(network_task_runner_.get());
151 TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
153 TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() {
154 if (!context_.get())
155 context_.reset(new TestURLRequestContext);
156 return context_.get();
159 scoped_refptr<base::SingleThreadTaskRunner>
160 TestURLRequestContextGetter::GetNetworkTaskRunner() const {
161 return network_task_runner_;
164 TestDelegate::TestDelegate()
165 : cancel_in_rr_(false),
166 cancel_in_rs_(false),
167 cancel_in_rd_(false),
168 cancel_in_rd_pending_(false),
169 quit_on_complete_(true),
170 quit_on_redirect_(false),
171 quit_on_before_network_start_(false),
172 allow_certificate_errors_(false),
173 response_started_count_(0),
174 received_bytes_count_(0),
175 received_redirect_count_(0),
176 received_before_network_start_count_(0),
177 received_data_before_response_(false),
178 request_failed_(false),
179 have_certificate_errors_(false),
180 certificate_errors_are_fatal_(false),
181 auth_required_(false),
182 have_full_request_headers_(false),
183 buf_(new IOBuffer(kBufferSize)) {
186 TestDelegate::~TestDelegate() {}
188 void TestDelegate::ClearFullRequestHeaders() {
189 full_request_headers_.Clear();
190 have_full_request_headers_ = false;
193 void TestDelegate::OnReceivedRedirect(URLRequest* request,
194 const GURL& new_url,
195 bool* defer_redirect) {
196 EXPECT_TRUE(request->is_redirecting());
198 have_full_request_headers_ =
199 request->GetFullRequestHeaders(&full_request_headers_);
201 received_redirect_count_++;
202 if (quit_on_redirect_) {
203 *defer_redirect = true;
204 base::MessageLoop::current()->PostTask(FROM_HERE,
205 base::MessageLoop::QuitClosure());
206 } else if (cancel_in_rr_) {
207 request->Cancel();
211 void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) {
212 received_before_network_start_count_++;
213 if (quit_on_before_network_start_) {
214 *defer = true;
215 base::MessageLoop::current()->PostTask(FROM_HERE,
216 base::MessageLoop::QuitClosure());
220 void TestDelegate::OnAuthRequired(URLRequest* request,
221 AuthChallengeInfo* auth_info) {
222 auth_required_ = true;
223 if (!credentials_.Empty()) {
224 request->SetAuth(credentials_);
225 } else {
226 request->CancelAuth();
230 void TestDelegate::OnSSLCertificateError(URLRequest* request,
231 const SSLInfo& ssl_info,
232 bool fatal) {
233 // The caller can control whether it needs all SSL requests to go through,
234 // independent of any possible errors, or whether it wants SSL errors to
235 // cancel the request.
236 have_certificate_errors_ = true;
237 certificate_errors_are_fatal_ = fatal;
238 if (allow_certificate_errors_)
239 request->ContinueDespiteLastError();
240 else
241 request->Cancel();
244 void TestDelegate::OnResponseStarted(URLRequest* request) {
245 // It doesn't make sense for the request to have IO pending at this point.
246 DCHECK(!request->status().is_io_pending());
247 EXPECT_FALSE(request->is_redirecting());
249 have_full_request_headers_ =
250 request->GetFullRequestHeaders(&full_request_headers_);
252 response_started_count_++;
253 if (cancel_in_rs_) {
254 request->Cancel();
255 OnResponseCompleted(request);
256 } else if (!request->status().is_success()) {
257 DCHECK(request->status().status() == URLRequestStatus::FAILED ||
258 request->status().status() == URLRequestStatus::CANCELED);
259 request_failed_ = true;
260 OnResponseCompleted(request);
261 } else {
262 // Initiate the first read.
263 int bytes_read = 0;
264 if (request->Read(buf_.get(), kBufferSize, &bytes_read))
265 OnReadCompleted(request, bytes_read);
266 else if (!request->status().is_io_pending())
267 OnResponseCompleted(request);
271 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) {
272 // It doesn't make sense for the request to have IO pending at this point.
273 DCHECK(!request->status().is_io_pending());
275 if (response_started_count_ == 0)
276 received_data_before_response_ = true;
278 if (cancel_in_rd_)
279 request->Cancel();
281 if (bytes_read >= 0) {
282 // There is data to read.
283 received_bytes_count_ += bytes_read;
285 // consume the data
286 data_received_.append(buf_->data(), bytes_read);
289 // If it was not end of stream, request to read more.
290 if (request->status().is_success() && bytes_read > 0) {
291 bytes_read = 0;
292 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) {
293 if (bytes_read > 0) {
294 data_received_.append(buf_->data(), bytes_read);
295 received_bytes_count_ += bytes_read;
296 } else {
297 break;
301 if (!request->status().is_io_pending())
302 OnResponseCompleted(request);
303 else if (cancel_in_rd_pending_)
304 request->Cancel();
307 void TestDelegate::OnResponseCompleted(URLRequest* request) {
308 if (quit_on_complete_)
309 base::MessageLoop::current()->PostTask(FROM_HERE,
310 base::MessageLoop::QuitClosure());
313 TestNetworkDelegate::TestNetworkDelegate()
314 : last_error_(0),
315 error_count_(0),
316 created_requests_(0),
317 destroyed_requests_(0),
318 completed_requests_(0),
319 canceled_requests_(0),
320 cookie_options_bit_mask_(0),
321 blocked_get_cookies_count_(0),
322 blocked_set_cookie_count_(0),
323 set_cookie_count_(0),
324 has_load_timing_info_before_redirect_(false),
325 has_load_timing_info_before_auth_(false),
326 can_access_files_(true),
327 can_throttle_requests_(true) {
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(
362 URLRequest* request,
363 const CompletionCallback& callback,
364 GURL* new_url ) {
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
376 created_requests_++;
377 return OK;
380 int TestNetworkDelegate::OnBeforeSendHeaders(
381 URLRequest* request,
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] =
390 kStageSendHeaders |
391 kStageCompletedError; // request canceled by delegate
393 return OK;
396 void TestNetworkDelegate::OnSendHeaders(
397 URLRequest* request,
398 const HttpRequestHeaders& headers) {
399 int req_id = request->identifier();
400 InitRequestStatesIfNew(req_id);
401 event_order_[req_id] += "OnSendHeaders\n";
402 EXPECT_TRUE(next_states_[req_id] & kStageSendHeaders) <<
403 event_order_[req_id];
404 next_states_[req_id] =
405 kStageHeadersReceived |
406 kStageCompletedError;
409 int TestNetworkDelegate::OnHeadersReceived(
410 URLRequest* request,
411 const CompletionCallback& callback,
412 const HttpResponseHeaders* original_response_headers,
413 scoped_refptr<HttpResponseHeaders>* override_response_headers) {
414 int req_id = request->identifier();
415 event_order_[req_id] += "OnHeadersReceived\n";
416 InitRequestStatesIfNew(req_id);
417 EXPECT_TRUE(next_states_[req_id] & kStageHeadersReceived) <<
418 event_order_[req_id];
419 next_states_[req_id] =
420 kStageBeforeRedirect |
421 kStageResponseStarted |
422 kStageAuthRequired |
423 kStageCompletedError; // e.g. proxy resolution problem
425 // Basic authentication sends a second request from the URLRequestHttpJob
426 // layer before the URLRequest reports that a response has started.
427 next_states_[req_id] |= kStageBeforeSendHeaders;
429 return OK;
432 void TestNetworkDelegate::OnBeforeRedirect(URLRequest* request,
433 const GURL& new_location) {
434 load_timing_info_before_redirect_ = LoadTimingInfo();
435 request->GetLoadTimingInfo(&load_timing_info_before_redirect_);
436 has_load_timing_info_before_redirect_ = true;
437 EXPECT_FALSE(load_timing_info_before_redirect_.request_start_time.is_null());
438 EXPECT_FALSE(load_timing_info_before_redirect_.request_start.is_null());
440 int req_id = request->identifier();
441 InitRequestStatesIfNew(req_id);
442 event_order_[req_id] += "OnBeforeRedirect\n";
443 EXPECT_TRUE(next_states_[req_id] & kStageBeforeRedirect) <<
444 event_order_[req_id];
445 next_states_[req_id] =
446 kStageBeforeURLRequest | // HTTP redirects trigger this.
447 kStageBeforeSendHeaders | // Redirects from the network delegate do not
448 // trigger onBeforeURLRequest.
449 kStageCompletedError;
451 // A redirect can lead to a file or a data URL. In this case, we do not send
452 // headers.
453 next_states_[req_id] |= kStageResponseStarted;
456 void TestNetworkDelegate::OnResponseStarted(URLRequest* request) {
457 LoadTimingInfo load_timing_info;
458 request->GetLoadTimingInfo(&load_timing_info);
459 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
460 EXPECT_FALSE(load_timing_info.request_start.is_null());
462 int req_id = request->identifier();
463 InitRequestStatesIfNew(req_id);
464 event_order_[req_id] += "OnResponseStarted\n";
465 EXPECT_TRUE(next_states_[req_id] & kStageResponseStarted) <<
466 event_order_[req_id];
467 next_states_[req_id] = kStageCompletedSuccess | kStageCompletedError;
468 if (request->status().status() == URLRequestStatus::FAILED) {
469 error_count_++;
470 last_error_ = request->status().error();
474 void TestNetworkDelegate::OnRawBytesRead(const URLRequest& request,
475 int bytes_read) {
478 void TestNetworkDelegate::OnCompleted(URLRequest* request, bool started) {
479 int req_id = request->identifier();
480 InitRequestStatesIfNew(req_id);
481 event_order_[req_id] += "OnCompleted\n";
482 // Expect "Success -> (next_states_ & kStageCompletedSuccess)"
483 // is logically identical to
484 // Expect "!(Success) || (next_states_ & kStageCompletedSuccess)"
485 EXPECT_TRUE(!request->status().is_success() ||
486 (next_states_[req_id] & kStageCompletedSuccess)) <<
487 event_order_[req_id];
488 EXPECT_TRUE(request->status().is_success() ||
489 (next_states_[req_id] & kStageCompletedError)) <<
490 event_order_[req_id];
491 next_states_[req_id] = kStageURLRequestDestroyed;
492 completed_requests_++;
493 if (request->status().status() == URLRequestStatus::FAILED) {
494 error_count_++;
495 last_error_ = request->status().error();
496 } else if (request->status().status() == URLRequestStatus::CANCELED) {
497 canceled_requests_++;
498 } else {
499 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status());
503 void TestNetworkDelegate::OnURLRequestDestroyed(URLRequest* request) {
504 int req_id = request->identifier();
505 InitRequestStatesIfNew(req_id);
506 event_order_[req_id] += "OnURLRequestDestroyed\n";
507 EXPECT_TRUE(next_states_[req_id] & kStageURLRequestDestroyed) <<
508 event_order_[req_id];
509 next_states_[req_id] = kStageDestruction;
510 destroyed_requests_++;
513 void TestNetworkDelegate::OnPACScriptError(int line_number,
514 const base::string16& error) {
517 NetworkDelegate::AuthRequiredResponse TestNetworkDelegate::OnAuthRequired(
518 URLRequest* request,
519 const AuthChallengeInfo& auth_info,
520 const AuthCallback& callback,
521 AuthCredentials* credentials) {
522 load_timing_info_before_auth_ = LoadTimingInfo();
523 request->GetLoadTimingInfo(&load_timing_info_before_auth_);
524 has_load_timing_info_before_auth_ = true;
525 EXPECT_FALSE(load_timing_info_before_auth_.request_start_time.is_null());
526 EXPECT_FALSE(load_timing_info_before_auth_.request_start.is_null());
528 int req_id = request->identifier();
529 InitRequestStatesIfNew(req_id);
530 event_order_[req_id] += "OnAuthRequired\n";
531 EXPECT_TRUE(next_states_[req_id] & kStageAuthRequired) <<
532 event_order_[req_id];
533 next_states_[req_id] = kStageBeforeSendHeaders |
534 kStageAuthRequired | // For example, proxy auth followed by server auth.
535 kStageHeadersReceived | // Request canceled by delegate simulates empty
536 // response.
537 kStageResponseStarted | // data: URLs do not trigger sending headers
538 kStageBeforeRedirect | // a delegate can trigger a redirection
539 kStageCompletedError; // request cancelled before callback
540 return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION;
543 bool TestNetworkDelegate::OnCanGetCookies(const URLRequest& request,
544 const CookieList& cookie_list) {
545 bool allow = true;
546 if (cookie_options_bit_mask_ & NO_GET_COOKIES)
547 allow = false;
549 if (!allow) {
550 blocked_get_cookies_count_++;
553 return allow;
556 bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
557 const std::string& cookie_line,
558 CookieOptions* options) {
559 bool allow = true;
560 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
561 allow = false;
563 if (!allow) {
564 blocked_set_cookie_count_++;
565 } else {
566 set_cookie_count_++;
569 return allow;
572 bool TestNetworkDelegate::OnCanAccessFile(const URLRequest& request,
573 const base::FilePath& path) const {
574 return can_access_files_;
577 bool TestNetworkDelegate::OnCanThrottleRequest(
578 const URLRequest& request) const {
579 return can_throttle_requests_;
582 int TestNetworkDelegate::OnBeforeSocketStreamConnect(
583 SocketStream* socket,
584 const CompletionCallback& callback) {
585 return OK;
588 // static
589 std::string ScopedCustomUrlRequestTestHttpHost::value_("127.0.0.1");
591 ScopedCustomUrlRequestTestHttpHost::ScopedCustomUrlRequestTestHttpHost(
592 const std::string& new_value)
593 : old_value_(value_),
594 new_value_(new_value) {
595 value_ = new_value_;
598 ScopedCustomUrlRequestTestHttpHost::~ScopedCustomUrlRequestTestHttpHost() {
599 DCHECK_EQ(value_, new_value_);
600 value_ = old_value_;
603 // static
604 const std::string& ScopedCustomUrlRequestTestHttpHost::value() {
605 return value_;
608 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) {
611 URLRequestJob* TestJobInterceptor::MaybeCreateJob(
612 URLRequest* request,
613 NetworkDelegate* network_delegate) const {
614 URLRequestJob* job = main_intercept_job_;
615 main_intercept_job_ = NULL;
616 return job;
619 void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) {
620 main_intercept_job_ = job;
623 } // namespace net