ChildAccountService: get service flags from AccountTrackerService instead of fetching...
[chromium-blink-merge.git] / net / url_request / url_request_test_util.cc
blobef99e99a1e57fd523602c84c4c2273cbec97f73e
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"
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_);
64 AssertNoURLRequests();
67 void TestURLRequestContext::Init() {
68 DCHECK(!initialized_);
69 initialized_ = true;
71 if (!host_resolver())
72 context_storage_.set_host_resolver(
73 scoped_ptr<HostResolver>(new MockCachingHostResolver()));
74 if (!proxy_service())
75 context_storage_.set_proxy_service(ProxyService::CreateDirect());
76 if (!cert_verifier())
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_);
97 } else {
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.
116 if (!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(make_scoped_ptr(
121 new ChannelIDService(new DefaultChannelIDStore(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 TestURLRequestContextGetter::TestURLRequestContextGetter(
133 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner)
134 : network_task_runner_(network_task_runner) {
135 DCHECK(network_task_runner_.get());
138 TestURLRequestContextGetter::TestURLRequestContextGetter(
139 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner,
140 scoped_ptr<TestURLRequestContext> context)
141 : network_task_runner_(network_task_runner), context_(context.Pass()) {
142 DCHECK(network_task_runner_.get());
145 TestURLRequestContextGetter::~TestURLRequestContextGetter() {}
147 TestURLRequestContext* TestURLRequestContextGetter::GetURLRequestContext() {
148 if (!context_.get())
149 context_.reset(new TestURLRequestContext);
150 return context_.get();
153 scoped_refptr<base::SingleThreadTaskRunner>
154 TestURLRequestContextGetter::GetNetworkTaskRunner() const {
155 return network_task_runner_;
158 TestDelegate::TestDelegate()
159 : cancel_in_rr_(false),
160 cancel_in_rs_(false),
161 cancel_in_rd_(false),
162 cancel_in_rd_pending_(false),
163 quit_on_complete_(true),
164 quit_on_redirect_(false),
165 quit_on_before_network_start_(false),
166 allow_certificate_errors_(false),
167 response_started_count_(0),
168 received_bytes_count_(0),
169 received_redirect_count_(0),
170 received_before_network_start_count_(0),
171 received_data_before_response_(false),
172 request_failed_(false),
173 have_certificate_errors_(false),
174 certificate_errors_are_fatal_(false),
175 auth_required_(false),
176 have_full_request_headers_(false),
177 buf_(new IOBuffer(kBufferSize)) {
180 TestDelegate::~TestDelegate() {}
182 void TestDelegate::ClearFullRequestHeaders() {
183 full_request_headers_.Clear();
184 have_full_request_headers_ = false;
187 void TestDelegate::OnReceivedRedirect(URLRequest* request,
188 const RedirectInfo& redirect_info,
189 bool* defer_redirect) {
190 EXPECT_TRUE(request->is_redirecting());
192 have_full_request_headers_ =
193 request->GetFullRequestHeaders(&full_request_headers_);
195 received_redirect_count_++;
196 if (quit_on_redirect_) {
197 *defer_redirect = true;
198 base::MessageLoop::current()->PostTask(FROM_HERE,
199 base::MessageLoop::QuitClosure());
200 } else if (cancel_in_rr_) {
201 request->Cancel();
205 void TestDelegate::OnBeforeNetworkStart(URLRequest* request, bool* defer) {
206 received_before_network_start_count_++;
207 if (quit_on_before_network_start_) {
208 *defer = true;
209 base::MessageLoop::current()->PostTask(FROM_HERE,
210 base::MessageLoop::QuitClosure());
214 void TestDelegate::OnAuthRequired(URLRequest* request,
215 AuthChallengeInfo* auth_info) {
216 auth_required_ = true;
217 if (!credentials_.Empty()) {
218 request->SetAuth(credentials_);
219 } else {
220 request->CancelAuth();
224 void TestDelegate::OnSSLCertificateError(URLRequest* request,
225 const SSLInfo& ssl_info,
226 bool fatal) {
227 // The caller can control whether it needs all SSL requests to go through,
228 // independent of any possible errors, or whether it wants SSL errors to
229 // cancel the request.
230 have_certificate_errors_ = true;
231 certificate_errors_are_fatal_ = fatal;
232 if (allow_certificate_errors_)
233 request->ContinueDespiteLastError();
234 else
235 request->Cancel();
238 void TestDelegate::OnResponseStarted(URLRequest* request) {
239 // It doesn't make sense for the request to have IO pending at this point.
240 DCHECK(!request->status().is_io_pending());
241 EXPECT_FALSE(request->is_redirecting());
243 have_full_request_headers_ =
244 request->GetFullRequestHeaders(&full_request_headers_);
246 response_started_count_++;
247 if (cancel_in_rs_) {
248 request->Cancel();
249 OnResponseCompleted(request);
250 } else if (!request->status().is_success()) {
251 DCHECK(request->status().status() == URLRequestStatus::FAILED ||
252 request->status().status() == URLRequestStatus::CANCELED);
253 request_failed_ = true;
254 OnResponseCompleted(request);
255 } else {
256 // Initiate the first read.
257 int bytes_read = 0;
258 if (request->Read(buf_.get(), kBufferSize, &bytes_read))
259 OnReadCompleted(request, bytes_read);
260 else if (!request->status().is_io_pending())
261 OnResponseCompleted(request);
265 void TestDelegate::OnReadCompleted(URLRequest* request, int bytes_read) {
266 // It doesn't make sense for the request to have IO pending at this point.
267 DCHECK(!request->status().is_io_pending());
269 if (response_started_count_ == 0)
270 received_data_before_response_ = true;
272 if (cancel_in_rd_)
273 request->Cancel();
275 if (bytes_read >= 0) {
276 // There is data to read.
277 received_bytes_count_ += bytes_read;
279 // consume the data
280 data_received_.append(buf_->data(), bytes_read);
283 // If it was not end of stream, request to read more.
284 if (request->status().is_success() && bytes_read > 0) {
285 bytes_read = 0;
286 while (request->Read(buf_.get(), kBufferSize, &bytes_read)) {
287 if (bytes_read > 0) {
288 data_received_.append(buf_->data(), bytes_read);
289 received_bytes_count_ += bytes_read;
290 } else {
291 break;
295 if (!request->status().is_io_pending())
296 OnResponseCompleted(request);
297 else if (cancel_in_rd_pending_)
298 request->Cancel();
301 void TestDelegate::OnResponseCompleted(URLRequest* request) {
302 if (quit_on_complete_)
303 base::MessageLoop::current()->PostTask(FROM_HERE,
304 base::MessageLoop::QuitClosure());
307 TestNetworkDelegate::TestNetworkDelegate()
308 : last_error_(0),
309 error_count_(0),
310 created_requests_(0),
311 destroyed_requests_(0),
312 completed_requests_(0),
313 canceled_requests_(0),
314 cookie_options_bit_mask_(0),
315 blocked_get_cookies_count_(0),
316 blocked_set_cookie_count_(0),
317 set_cookie_count_(0),
318 observed_before_proxy_headers_sent_callbacks_(0),
319 before_send_headers_count_(0),
320 headers_received_count_(0),
321 has_load_timing_info_before_redirect_(false),
322 has_load_timing_info_before_auth_(false),
323 can_access_files_(true),
324 can_throttle_requests_(true),
325 first_party_only_cookies_enabled_(false),
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(
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
392 before_send_headers_count_++;
393 return OK;
396 void TestNetworkDelegate::OnBeforeSendProxyHeaders(
397 URLRequest* request,
398 const ProxyInfo& proxy_info,
399 HttpRequestHeaders* headers) {
400 ++observed_before_proxy_headers_sent_callbacks_;
401 last_observed_proxy_ = proxy_info.proxy_server().host_port_pair();
404 void TestNetworkDelegate::OnSendHeaders(
405 URLRequest* request,
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;
414 else
415 next_states_[req_id] = kStageResponseStarted;
416 will_be_intercepted_on_next_error_ = false;
419 int TestNetworkDelegate::OnHeadersReceived(
420 URLRequest* request,
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 |
433 kStageAuthRequired |
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 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_++;
454 return OK;
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
477 // headers.
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) {
494 error_count_++;
495 last_error_ = request->status().error();
499 void TestNetworkDelegate::OnRawBytesRead(const URLRequest& request,
500 int bytes_read) {
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) {
519 error_count_++;
520 last_error_ = request->status().error();
521 } else if (request->status().status() == URLRequestStatus::CANCELED) {
522 canceled_requests_++;
523 } else {
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(
543 URLRequest* request,
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
561 // response.
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) {
570 bool allow = true;
571 if (cookie_options_bit_mask_ & NO_GET_COOKIES)
572 allow = false;
574 if (!allow) {
575 blocked_get_cookies_count_++;
578 return allow;
581 bool TestNetworkDelegate::OnCanSetCookie(const URLRequest& request,
582 const std::string& cookie_line,
583 CookieOptions* options) {
584 bool allow = true;
585 if (cookie_options_bit_mask_ & NO_SET_COOKIE)
586 allow = false;
588 if (!allow) {
589 blocked_set_cookie_count_++;
590 } else {
591 set_cookie_count_++;
594 return allow;
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::OnFirstPartyOnlyCookieExperimentEnabled() const {
608 return first_party_only_cookies_enabled_;
611 bool TestNetworkDelegate::OnCancelURLRequestWithPolicyViolatingReferrerHeader(
612 const URLRequest& request,
613 const GURL& target_url,
614 const GURL& referrer_url) const {
615 return cancel_request_with_policy_violating_referrer_;
618 TestJobInterceptor::TestJobInterceptor() : main_intercept_job_(NULL) {
621 URLRequestJob* TestJobInterceptor::MaybeCreateJob(
622 URLRequest* request,
623 NetworkDelegate* network_delegate) const {
624 URLRequestJob* job = main_intercept_job_;
625 main_intercept_job_ = NULL;
626 return job;
629 void TestJobInterceptor::set_main_intercept_job(URLRequestJob* job) {
630 main_intercept_job_ = job;
633 } // namespace net