url_request_unittest.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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. 4 5#include "build/build_config.h" 6 7#if defined(OS_WIN) 8#include <windows.h> 9#include <shlobj.h> 10#endif 11 12#include <algorithm> 13#include <string> 14 15#include "base/basictypes.h" 16#include "base/bind.h" 17#include "base/compiler_specific.h" 18#include "base/file_util.h" 19#include "base/format_macros.h" 20#include "base/memory/weak_ptr.h" 21#include "base/message_loop/message_loop.h" 22#include "base/message_loop/message_loop_proxy.h" 23#include "base/path_service.h" 24#include "base/run_loop.h" 25#include "base/strings/string_number_conversions.h" 26#include "base/strings/string_piece.h" 27#include "base/strings/string_split.h" 28#include "base/strings/string_util.h" 29#include "base/strings/stringprintf.h" 30#include "base/strings/utf_string_conversions.h" 31#include "net/base/capturing_net_log.h" 32#include "net/base/load_flags.h" 33#include "net/base/load_timing_info.h" 34#include "net/base/load_timing_info_test_util.h" 35#include "net/base/net_errors.h" 36#include "net/base/net_log.h" 37#include "net/base/net_log_unittest.h" 38#include "net/base/net_module.h" 39#include "net/base/net_util.h" 40#include "net/base/request_priority.h" 41#include "net/base/test_data_directory.h" 42#include "net/base/upload_bytes_element_reader.h" 43#include "net/base/upload_data_stream.h" 44#include "net/base/upload_file_element_reader.h" 45#include "net/cert/ev_root_ca_metadata.h" 46#include "net/cert/test_root_certs.h" 47#include "net/cookies/cookie_monster.h" 48#include "net/cookies/cookie_store_test_helpers.h" 49#include "net/disk_cache/disk_cache.h" 50#include "net/dns/mock_host_resolver.h" 51#include "net/ftp/ftp_network_layer.h" 52#include "net/http/http_cache.h" 53#include "net/http/http_network_layer.h" 54#include "net/http/http_network_session.h" 55#include "net/http/http_request_headers.h" 56#include "net/http/http_response_headers.h" 57#include "net/ocsp/nss_ocsp.h" 58#include "net/proxy/proxy_service.h" 59#include "net/socket/ssl_client_socket.h" 60#include "net/ssl/ssl_connection_status_flags.h" 61#include "net/test/cert_test_util.h" 62#include "net/test/spawned_test_server/spawned_test_server.h" 63#include "net/url_request/data_protocol_handler.h" 64#include "net/url_request/file_protocol_handler.h" 65#include "net/url_request/ftp_protocol_handler.h" 66#include "net/url_request/static_http_user_agent_settings.h" 67#include "net/url_request/url_request.h" 68#include "net/url_request/url_request_file_dir_job.h" 69#include "net/url_request/url_request_http_job.h" 70#include "net/url_request/url_request_job_factory_impl.h" 71#include "net/url_request/url_request_redirect_job.h" 72#include "net/url_request/url_request_test_job.h" 73#include "net/url_request/url_request_test_util.h" 74#include "testing/gtest/include/gtest/gtest.h" 75#include "testing/platform_test.h" 76 77#if defined(OS_WIN) 78#include "base/win/scoped_com_initializer.h" 79#include "base/win/scoped_comptr.h" 80#include "base/win/windows_version.h" 81#endif 82 83using base::Time; 84 85namespace net { 86 87namespace { 88 89const base::string16 kChrome(ASCIIToUTF16("chrome")); 90const base::string16 kSecret(ASCIIToUTF16("secret")); 91const base::string16 kUser(ASCIIToUTF16("user")); 92 93// Tests load timing information in the case a fresh connection was used, with 94// no proxy. 95void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info, 96 int connect_timing_flags) { 97 EXPECT_FALSE(load_timing_info.socket_reused); 98 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); 99 100 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); 101 EXPECT_FALSE(load_timing_info.request_start.is_null()); 102 103 EXPECT_LE(load_timing_info.request_start, 104 load_timing_info.connect_timing.connect_start); 105 ExpectConnectTimingHasTimes(load_timing_info.connect_timing, 106 connect_timing_flags); 107 EXPECT_LE(load_timing_info.connect_timing.connect_end, 108 load_timing_info.send_start); 109 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); 110 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); 111 112 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); 113 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); 114} 115 116// Same as above, but with proxy times. 117void TestLoadTimingNotReusedWithProxy( 118 const net::LoadTimingInfo& load_timing_info, 119 int connect_timing_flags) { 120 EXPECT_FALSE(load_timing_info.socket_reused); 121 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); 122 123 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); 124 EXPECT_FALSE(load_timing_info.request_start.is_null()); 125 126 EXPECT_LE(load_timing_info.request_start, 127 load_timing_info.proxy_resolve_start); 128 EXPECT_LE(load_timing_info.proxy_resolve_start, 129 load_timing_info.proxy_resolve_end); 130 EXPECT_LE(load_timing_info.proxy_resolve_end, 131 load_timing_info.connect_timing.connect_start); 132 ExpectConnectTimingHasTimes(load_timing_info.connect_timing, 133 connect_timing_flags); 134 EXPECT_LE(load_timing_info.connect_timing.connect_end, 135 load_timing_info.send_start); 136 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); 137 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); 138} 139 140// Same as above, but with a reused socket and proxy times. 141void TestLoadTimingReusedWithProxy( 142 const net::LoadTimingInfo& load_timing_info) { 143 EXPECT_TRUE(load_timing_info.socket_reused); 144 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); 145 146 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); 147 EXPECT_FALSE(load_timing_info.request_start.is_null()); 148 149 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); 150 151 EXPECT_LE(load_timing_info.request_start, 152 load_timing_info.proxy_resolve_start); 153 EXPECT_LE(load_timing_info.proxy_resolve_start, 154 load_timing_info.proxy_resolve_end); 155 EXPECT_LE(load_timing_info.proxy_resolve_end, 156 load_timing_info.send_start); 157 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); 158 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); 159} 160 161// Tests load timing information in the case of a cache hit, when no cache 162// validation request was sent over the wire. 163void TestLoadTimingCacheHitNoNetwork( 164 const net::LoadTimingInfo& load_timing_info) { 165 EXPECT_FALSE(load_timing_info.socket_reused); 166 EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); 167 168 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); 169 EXPECT_FALSE(load_timing_info.request_start.is_null()); 170 171 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); 172 EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); 173 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); 174 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); 175 176 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); 177 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); 178} 179 180// Tests load timing in the case that there is no HTTP response. This can be 181// used to test in the case of errors or non-HTTP requests. 182void TestLoadTimingNoHttpResponse( 183 const net::LoadTimingInfo& load_timing_info) { 184 EXPECT_FALSE(load_timing_info.socket_reused); 185 EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); 186 187 // Only the request times should be non-null. 188 EXPECT_FALSE(load_timing_info.request_start_time.is_null()); 189 EXPECT_FALSE(load_timing_info.request_start.is_null()); 190 191 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); 192 193 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); 194 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); 195 EXPECT_TRUE(load_timing_info.send_start.is_null()); 196 EXPECT_TRUE(load_timing_info.send_end.is_null()); 197 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); 198} 199 200base::StringPiece TestNetResourceProvider(int key) { 201 return "header"; 202} 203 204// Do a case-insensitive search through |haystack| for |needle|. 205bool ContainsString(const std::string& haystack, const char* needle) { 206 std::string::const_iterator it = 207 std::search(haystack.begin(), 208 haystack.end(), 209 needle, 210 needle + strlen(needle), 211 base::CaseInsensitiveCompare<char>()); 212 return it != haystack.end(); 213} 214 215void FillBuffer(char* buffer, size_t len) { 216 static bool called = false; 217 if (!called) { 218 called = true; 219 int seed = static_cast<int>(Time::Now().ToInternalValue()); 220 srand(seed); 221 } 222 223 for (size_t i = 0; i < len; i++) { 224 buffer[i] = static_cast<char>(rand()); 225 if (!buffer[i]) 226 buffer[i] = 'g'; 227 } 228} 229 230UploadDataStream* CreateSimpleUploadData(const char* data) { 231 scoped_ptr<UploadElementReader> reader( 232 new UploadBytesElementReader(data, strlen(data))); 233 return UploadDataStream::CreateWithReader(reader.Pass(), 0); 234} 235 236// Verify that the SSLInfo of a successful SSL connection has valid values. 237void CheckSSLInfo(const SSLInfo& ssl_info) { 238 // Allow ChromeFrame fake SSLInfo to get through. 239 if (ssl_info.cert.get() && 240 ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") { 241 // -1 means unknown. 242 EXPECT_EQ(ssl_info.security_bits, -1); 243 return; 244 } 245 246 // -1 means unknown. 0 means no encryption. 247 EXPECT_GT(ssl_info.security_bits, 0); 248 249 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. 250 int cipher_suite = SSLConnectionStatusToCipherSuite( 251 ssl_info.connection_status); 252 EXPECT_NE(0, cipher_suite); 253} 254 255void CheckFullRequestHeaders(const HttpRequestHeaders& headers, 256 const GURL& host_url) { 257 std::string sent_value; 258 259 EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); 260 EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); 261 262 EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); 263 EXPECT_EQ("keep-alive", sent_value); 264} 265 266bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) { 267 size_t size = a.size(); 268 269 if (size != b.size()) 270 return false; 271 272 for (size_t i = 0; i < size; ++i) { 273 if (!a[i].Equals(b[i])) 274 return false; 275 } 276 277 return true; 278} 279 280// A network delegate that allows the user to choose a subset of request stages 281// to block in. When blocking, the delegate can do one of the following: 282// * synchronously return a pre-specified error code, or 283// * asynchronously return that value via an automatically called callback, 284// or 285// * block and wait for the user to do a callback. 286// Additionally, the user may also specify a redirect URL -- then each request 287// with the current URL different from the redirect target will be redirected 288// to that target, in the on-before-URL-request stage, independent of whether 289// the delegate blocks in ON_BEFORE_URL_REQUEST or not. 290class BlockingNetworkDelegate : public TestNetworkDelegate { 291 public: 292 // Stages in which the delegate can block. 293 enum Stage { 294 NOT_BLOCKED = 0, 295 ON_BEFORE_URL_REQUEST = 1 << 0, 296 ON_BEFORE_SEND_HEADERS = 1 << 1, 297 ON_HEADERS_RECEIVED = 1 << 2, 298 ON_AUTH_REQUIRED = 1 << 3 299 }; 300 301 // Behavior during blocked stages. During other stages, just 302 // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. 303 enum BlockMode { 304 SYNCHRONOUS, // No callback, returns specified return values. 305 AUTO_CALLBACK, // |this| posts a task to run the callback using the 306 // specified return codes. 307 USER_CALLBACK, // User takes care of doing a callback. |retval_| and 308 // |auth_retval_| are ignored. In every blocking stage the 309 // message loop is quit. 310 }; 311 312 // Creates a delegate which does not block at all. 313 explicit BlockingNetworkDelegate(BlockMode block_mode); 314 315 // For users to trigger a callback returning |response|. 316 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. 317 // Only call if |block_mode_| == USER_CALLBACK. 318 void DoCallback(int response); 319 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); 320 321 // Setters. 322 void set_retval(int retval) { 323 ASSERT_NE(USER_CALLBACK, block_mode_); 324 ASSERT_NE(ERR_IO_PENDING, retval); 325 ASSERT_NE(OK, retval); 326 retval_ = retval; 327 } 328 329 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then 330 // |auth_credentials_| will be passed with the response. 331 void set_auth_retval(AuthRequiredResponse auth_retval) { 332 ASSERT_NE(USER_CALLBACK, block_mode_); 333 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); 334 auth_retval_ = auth_retval; 335 } 336 void set_auth_credentials(const AuthCredentials& auth_credentials) { 337 auth_credentials_ = auth_credentials; 338 } 339 340 void set_redirect_url(const GURL& url) { 341 redirect_url_ = url; 342 } 343 344 void set_block_on(int block_on) { 345 block_on_ = block_on; 346 } 347 348 // Allows the user to check in which state did we block. 349 Stage stage_blocked_for_callback() const { 350 EXPECT_EQ(USER_CALLBACK, block_mode_); 351 return stage_blocked_for_callback_; 352 } 353 354 private: 355 void RunCallback(int response, const CompletionCallback& callback); 356 void RunAuthCallback(AuthRequiredResponse response, 357 const AuthCallback& callback); 358 359 // TestNetworkDelegate implementation. 360 virtual int OnBeforeURLRequest(URLRequest* request, 361 const CompletionCallback& callback, 362 GURL* new_url) OVERRIDE; 363 364 virtual int OnBeforeSendHeaders(URLRequest* request, 365 const CompletionCallback& callback, 366 HttpRequestHeaders* headers) OVERRIDE; 367 368 virtual int OnHeadersReceived( 369 URLRequest* request, 370 const CompletionCallback& callback, 371 const HttpResponseHeaders* original_response_headers, 372 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE; 373 374 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 375 URLRequest* request, 376 const AuthChallengeInfo& auth_info, 377 const AuthCallback& callback, 378 AuthCredentials* credentials) OVERRIDE; 379 380 // Resets the callbacks and |stage_blocked_for_callback_|. 381 void Reset(); 382 383 // Checks whether we should block in |stage|. If yes, returns an error code 384 // and optionally sets up callback based on |block_mode_|. If no, returns OK. 385 int MaybeBlockStage(Stage stage, const CompletionCallback& callback); 386 387 // Configuration parameters, can be adjusted by public methods: 388 const BlockMode block_mode_; 389 390 // Values returned on blocking stages when mode is SYNCHRONOUS or 391 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. 392 int retval_; // To be returned in non-auth stages. 393 AuthRequiredResponse auth_retval_; 394 395 GURL redirect_url_; // Used if non-empty. 396 int block_on_; // Bit mask: in which stages to block. 397 398 // |auth_credentials_| will be copied to |*target_auth_credential_| on 399 // callback. 400 AuthCredentials auth_credentials_; 401 AuthCredentials* target_auth_credentials_; 402 403 // Internal variables, not set by not the user: 404 // Last blocked stage waiting for user callback (unused if |block_mode_| != 405 // USER_CALLBACK). 406 Stage stage_blocked_for_callback_; 407 408 // Callback objects stored during blocking stages. 409 CompletionCallback callback_; 410 AuthCallback auth_callback_; 411 412 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; 413 414 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); 415}; 416 417BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) 418 : block_mode_(block_mode), 419 retval_(OK), 420 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), 421 block_on_(0), 422 target_auth_credentials_(NULL), 423 stage_blocked_for_callback_(NOT_BLOCKED), 424 weak_factory_(this) { 425} 426 427void BlockingNetworkDelegate::DoCallback(int response) { 428 ASSERT_EQ(USER_CALLBACK, block_mode_); 429 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); 430 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); 431 CompletionCallback callback = callback_; 432 Reset(); 433 RunCallback(response, callback); 434} 435 436void BlockingNetworkDelegate::DoAuthCallback( 437 NetworkDelegate::AuthRequiredResponse response) { 438 ASSERT_EQ(USER_CALLBACK, block_mode_); 439 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); 440 AuthCallback auth_callback = auth_callback_; 441 Reset(); 442 RunAuthCallback(response, auth_callback); 443} 444 445void BlockingNetworkDelegate::RunCallback(int response, 446 const CompletionCallback& callback) { 447 callback.Run(response); 448} 449 450void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, 451 const AuthCallback& callback) { 452 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { 453 ASSERT_TRUE(target_auth_credentials_ != NULL); 454 *target_auth_credentials_ = auth_credentials_; 455 } 456 callback.Run(response); 457} 458 459int BlockingNetworkDelegate::OnBeforeURLRequest( 460 URLRequest* request, 461 const CompletionCallback& callback, 462 GURL* new_url) { 463 if (redirect_url_ == request->url()) 464 return OK; // We've already seen this request and redirected elsewhere. 465 466 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); 467 468 if (!redirect_url_.is_empty()) 469 *new_url = redirect_url_; 470 471 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); 472} 473 474int BlockingNetworkDelegate::OnBeforeSendHeaders( 475 URLRequest* request, 476 const CompletionCallback& callback, 477 HttpRequestHeaders* headers) { 478 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); 479 480 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); 481} 482 483int BlockingNetworkDelegate::OnHeadersReceived( 484 URLRequest* request, 485 const CompletionCallback& callback, 486 const HttpResponseHeaders* original_response_headers, 487 scoped_refptr<HttpResponseHeaders>* override_response_headers) { 488 TestNetworkDelegate::OnHeadersReceived( 489 request, callback, original_response_headers, 490 override_response_headers); 491 492 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); 493} 494 495NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( 496 URLRequest* request, 497 const AuthChallengeInfo& auth_info, 498 const AuthCallback& callback, 499 AuthCredentials* credentials) { 500 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, 501 credentials); 502 // Check that the user has provided callback for the previous blocked stage. 503 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); 504 505 if ((block_on_ & ON_AUTH_REQUIRED) == 0) { 506 return AUTH_REQUIRED_RESPONSE_NO_ACTION; 507 } 508 509 target_auth_credentials_ = credentials; 510 511 switch (block_mode_) { 512 case SYNCHRONOUS: 513 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) 514 *target_auth_credentials_ = auth_credentials_; 515 return auth_retval_; 516 517 case AUTO_CALLBACK: 518 base::MessageLoop::current()->PostTask( 519 FROM_HERE, 520 base::Bind(&BlockingNetworkDelegate::RunAuthCallback, 521 weak_factory_.GetWeakPtr(), auth_retval_, callback)); 522 return AUTH_REQUIRED_RESPONSE_IO_PENDING; 523 524 case USER_CALLBACK: 525 auth_callback_ = callback; 526 stage_blocked_for_callback_ = ON_AUTH_REQUIRED; 527 base::MessageLoop::current()->PostTask(FROM_HERE, 528 base::MessageLoop::QuitClosure()); 529 return AUTH_REQUIRED_RESPONSE_IO_PENDING; 530 } 531 NOTREACHED(); 532 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. 533} 534 535void BlockingNetworkDelegate::Reset() { 536 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); 537 stage_blocked_for_callback_ = NOT_BLOCKED; 538 callback_.Reset(); 539 auth_callback_.Reset(); 540} 541 542int BlockingNetworkDelegate::MaybeBlockStage( 543 BlockingNetworkDelegate::Stage stage, 544 const CompletionCallback& callback) { 545 // Check that the user has provided callback for the previous blocked stage. 546 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); 547 548 if ((block_on_ & stage) == 0) { 549 return OK; 550 } 551 552 switch (block_mode_) { 553 case SYNCHRONOUS: 554 EXPECT_NE(OK, retval_); 555 return retval_; 556 557 case AUTO_CALLBACK: 558 base::MessageLoop::current()->PostTask( 559 FROM_HERE, 560 base::Bind(&BlockingNetworkDelegate::RunCallback, 561 weak_factory_.GetWeakPtr(), retval_, callback)); 562 return ERR_IO_PENDING; 563 564 case USER_CALLBACK: 565 callback_ = callback; 566 stage_blocked_for_callback_ = stage; 567 base::MessageLoop::current()->PostTask(FROM_HERE, 568 base::MessageLoop::QuitClosure()); 569 return ERR_IO_PENDING; 570 } 571 NOTREACHED(); 572 return 0; 573} 574 575class TestURLRequestContextWithProxy : public TestURLRequestContext { 576 public: 577 // Does not own |delegate|. 578 TestURLRequestContextWithProxy(const std::string& proxy, 579 NetworkDelegate* delegate) 580 : TestURLRequestContext(true) { 581 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); 582 set_network_delegate(delegate); 583 Init(); 584 } 585 virtual ~TestURLRequestContextWithProxy() {} 586}; 587 588} // namespace 589 590// Inherit PlatformTest since we require the autorelease pool on Mac OS X. 591class URLRequestTest : public PlatformTest { 592 public: 593 URLRequestTest() : default_context_(true) { 594 default_context_.set_network_delegate(&default_network_delegate_); 595 default_context_.set_net_log(&net_log_); 596 job_factory_.SetProtocolHandler("data", new DataProtocolHandler); 597 job_factory_.SetProtocolHandler( 598 "file", new FileProtocolHandler(base::MessageLoopProxy::current())); 599 default_context_.set_job_factory(&job_factory_); 600 default_context_.Init(); 601 } 602 virtual ~URLRequestTest() { 603 // URLRequestJobs may post clean-up tasks on destruction. 604 base::RunLoop().RunUntilIdle(); 605 } 606 607 // Adds the TestJobInterceptor to the default context. 608 TestJobInterceptor* AddTestInterceptor() { 609 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); 610 job_factory_.SetProtocolHandler("http", NULL); 611 job_factory_.SetProtocolHandler("http", protocol_handler_); 612 return protocol_handler_; 613 } 614 615 protected: 616 CapturingNetLog net_log_; 617 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. 618 URLRequestJobFactoryImpl job_factory_; 619 TestURLRequestContext default_context_; 620}; 621 622TEST_F(URLRequestTest, AboutBlankTest) { 623 TestDelegate d; 624 { 625 URLRequest r(GURL("about:blank"), DEFAULT_PRIORITY, &d, &default_context_); 626 627 r.Start(); 628 EXPECT_TRUE(r.is_pending()); 629 630 base::RunLoop().Run(); 631 632 EXPECT_TRUE(!r.is_pending()); 633 EXPECT_FALSE(d.received_data_before_response()); 634 EXPECT_EQ(d.bytes_received(), 0); 635 EXPECT_EQ("", r.GetSocketAddress().host()); 636 EXPECT_EQ(0, r.GetSocketAddress().port()); 637 638 HttpRequestHeaders headers; 639 EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); 640 } 641} 642 643TEST_F(URLRequestTest, DataURLImageTest) { 644 TestDelegate d; 645 { 646 // Use our nice little Chrome logo. 647 URLRequest r( 648 GURL( 649 "data:image/png;base64," 650 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3" 651 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD" 652 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t" 653 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9" 654 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1" 655 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z" 656 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW" 657 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW" 658 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb" 659 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5" 660 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV" 661 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq" 662 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F" 663 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB" 664 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM" 665 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm" 666 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En" 667 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), 668 DEFAULT_PRIORITY, 669 &d, 670 &default_context_); 671 672 r.Start(); 673 EXPECT_TRUE(r.is_pending()); 674 675 base::RunLoop().Run(); 676 677 EXPECT_TRUE(!r.is_pending()); 678 EXPECT_FALSE(d.received_data_before_response()); 679 EXPECT_EQ(d.bytes_received(), 911); 680 EXPECT_EQ("", r.GetSocketAddress().host()); 681 EXPECT_EQ(0, r.GetSocketAddress().port()); 682 683 HttpRequestHeaders headers; 684 EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); 685 } 686} 687 688TEST_F(URLRequestTest, FileTest) { 689 base::FilePath app_path; 690 PathService::Get(base::FILE_EXE, &app_path); 691 GURL app_url = FilePathToFileURL(app_path); 692 693 TestDelegate d; 694 { 695 URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_); 696 697 r.Start(); 698 EXPECT_TRUE(r.is_pending()); 699 700 base::RunLoop().Run(); 701 702 int64 file_size = -1; 703 EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size)); 704 705 EXPECT_TRUE(!r.is_pending()); 706 EXPECT_EQ(1, d.response_started_count()); 707 EXPECT_FALSE(d.received_data_before_response()); 708 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); 709 EXPECT_EQ("", r.GetSocketAddress().host()); 710 EXPECT_EQ(0, r.GetSocketAddress().port()); 711 712 HttpRequestHeaders headers; 713 EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); 714 } 715} 716 717TEST_F(URLRequestTest, FileTestCancel) { 718 base::FilePath app_path; 719 PathService::Get(base::FILE_EXE, &app_path); 720 GURL app_url = FilePathToFileURL(app_path); 721 722 TestDelegate d; 723 { 724 URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_); 725 726 r.Start(); 727 EXPECT_TRUE(r.is_pending()); 728 r.Cancel(); 729 } 730 // Async cancellation should be safe even when URLRequest has been already 731 // destroyed. 732 base::RunLoop().RunUntilIdle(); 733} 734 735TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { 736 const size_t buffer_size = 4000; 737 scoped_ptr<char[]> buffer(new char[buffer_size]); 738 FillBuffer(buffer.get(), buffer_size); 739 740 base::FilePath temp_path; 741 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); 742 GURL temp_url = FilePathToFileURL(temp_path); 743 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); 744 745 int64 file_size; 746 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); 747 748 const size_t first_byte_position = 500; 749 const size_t last_byte_position = buffer_size - first_byte_position; 750 const size_t content_length = last_byte_position - first_byte_position + 1; 751 std::string partial_buffer_string(buffer.get() + first_byte_position, 752 buffer.get() + last_byte_position + 1); 753 754 TestDelegate d; 755 { 756 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_); 757 758 HttpRequestHeaders headers; 759 headers.SetHeader(HttpRequestHeaders::kRange, 760 base::StringPrintf( 761 "bytes=%" PRIuS "-%" PRIuS, 762 first_byte_position, last_byte_position)); 763 r.SetExtraRequestHeaders(headers); 764 r.Start(); 765 EXPECT_TRUE(r.is_pending()); 766 767 base::RunLoop().Run(); 768 EXPECT_TRUE(!r.is_pending()); 769 EXPECT_EQ(1, d.response_started_count()); 770 EXPECT_FALSE(d.received_data_before_response()); 771 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); 772 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. 773 EXPECT_TRUE(partial_buffer_string == d.data_received()); 774 } 775 776 EXPECT_TRUE(base::DeleteFile(temp_path, false)); 777} 778 779TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { 780 const size_t buffer_size = 4000; 781 scoped_ptr<char[]> buffer(new char[buffer_size]); 782 FillBuffer(buffer.get(), buffer_size); 783 784 base::FilePath temp_path; 785 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); 786 GURL temp_url = FilePathToFileURL(temp_path); 787 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); 788 789 int64 file_size; 790 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); 791 792 const size_t first_byte_position = 500; 793 const size_t last_byte_position = buffer_size - 1; 794 const size_t content_length = last_byte_position - first_byte_position + 1; 795 std::string partial_buffer_string(buffer.get() + first_byte_position, 796 buffer.get() + last_byte_position + 1); 797 798 TestDelegate d; 799 { 800 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_); 801 802 HttpRequestHeaders headers; 803 headers.SetHeader(HttpRequestHeaders::kRange, 804 base::StringPrintf("bytes=%" PRIuS "-", 805 first_byte_position)); 806 r.SetExtraRequestHeaders(headers); 807 r.Start(); 808 EXPECT_TRUE(r.is_pending()); 809 810 base::RunLoop().Run(); 811 EXPECT_TRUE(!r.is_pending()); 812 EXPECT_EQ(1, d.response_started_count()); 813 EXPECT_FALSE(d.received_data_before_response()); 814 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); 815 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. 816 EXPECT_TRUE(partial_buffer_string == d.data_received()); 817 } 818 819 EXPECT_TRUE(base::DeleteFile(temp_path, false)); 820} 821 822TEST_F(URLRequestTest, FileTestMultipleRanges) { 823 const size_t buffer_size = 400000; 824 scoped_ptr<char[]> buffer(new char[buffer_size]); 825 FillBuffer(buffer.get(), buffer_size); 826 827 base::FilePath temp_path; 828 EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); 829 GURL temp_url = FilePathToFileURL(temp_path); 830 EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); 831 832 int64 file_size; 833 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); 834 835 TestDelegate d; 836 { 837 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_); 838 839 HttpRequestHeaders headers; 840 headers.SetHeader(HttpRequestHeaders::kRange, 841 "bytes=0-0,10-200,200-300"); 842 r.SetExtraRequestHeaders(headers); 843 r.Start(); 844 EXPECT_TRUE(r.is_pending()); 845 846 base::RunLoop().Run(); 847 EXPECT_TRUE(d.request_failed()); 848 } 849 850 EXPECT_TRUE(base::DeleteFile(temp_path, false)); 851} 852 853TEST_F(URLRequestTest, InvalidUrlTest) { 854 TestDelegate d; 855 { 856 URLRequest r(GURL("invalid url"), DEFAULT_PRIORITY, &d, &default_context_); 857 858 r.Start(); 859 EXPECT_TRUE(r.is_pending()); 860 861 base::RunLoop().Run(); 862 EXPECT_TRUE(d.request_failed()); 863 } 864} 865 866#if defined(OS_WIN) 867TEST_F(URLRequestTest, ResolveShortcutTest) { 868 base::FilePath app_path; 869 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 870 app_path = app_path.AppendASCII("net"); 871 app_path = app_path.AppendASCII("data"); 872 app_path = app_path.AppendASCII("url_request_unittest"); 873 app_path = app_path.AppendASCII("with-headers.html"); 874 875 std::wstring lnk_path = app_path.value() + L".lnk"; 876 877 base::win::ScopedCOMInitializer com_initializer; 878 879 // Temporarily create a shortcut for test 880 { 881 base::win::ScopedComPtr<IShellLink> shell; 882 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, 883 CLSCTX_INPROC_SERVER))); 884 base::win::ScopedComPtr<IPersistFile> persist; 885 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); 886 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); 887 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); 888 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); 889 } 890 891 TestDelegate d; 892 { 893 URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)), 894 DEFAULT_PRIORITY, 895 &d, 896 &default_context_); 897 898 r.Start(); 899 EXPECT_TRUE(r.is_pending()); 900 901 base::RunLoop().Run(); 902 903 WIN32_FILE_ATTRIBUTE_DATA data; 904 GetFileAttributesEx(app_path.value().c_str(), 905 GetFileExInfoStandard, &data); 906 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, 907 FILE_SHARE_READ, NULL, OPEN_EXISTING, 908 FILE_ATTRIBUTE_NORMAL, NULL); 909 EXPECT_NE(INVALID_HANDLE_VALUE, file); 910 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); 911 DWORD read_size; 912 BOOL result; 913 result = ReadFile(file, buffer.get(), data.nFileSizeLow, 914 &read_size, NULL); 915 std::string content(buffer.get(), read_size); 916 CloseHandle(file); 917 918 EXPECT_TRUE(!r.is_pending()); 919 EXPECT_EQ(1, d.received_redirect_count()); 920 EXPECT_EQ(content, d.data_received()); 921 } 922 923 // Clean the shortcut 924 DeleteFile(lnk_path.c_str()); 925} 926#endif // defined(OS_WIN) 927 928TEST_F(URLRequestTest, FileDirCancelTest) { 929 // Put in mock resource provider. 930 NetModule::SetResourceProvider(TestNetResourceProvider); 931 932 TestDelegate d; 933 { 934 base::FilePath file_path; 935 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); 936 file_path = file_path.Append(FILE_PATH_LITERAL("net")); 937 file_path = file_path.Append(FILE_PATH_LITERAL("data")); 938 939 URLRequest req( 940 FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, &default_context_); 941 req.Start(); 942 EXPECT_TRUE(req.is_pending()); 943 944 d.set_cancel_in_received_data_pending(true); 945 946 base::RunLoop().Run(); 947 } 948 949 // Take out mock resource provider. 950 NetModule::SetResourceProvider(NULL); 951} 952 953TEST_F(URLRequestTest, FileDirOutputSanity) { 954 // Verify the general sanity of the the output of the file: 955 // directory lister by checking for the output of a known existing 956 // file. 957 const char sentinel_name[] = "filedir-sentinel"; 958 959 base::FilePath path; 960 PathService::Get(base::DIR_SOURCE_ROOT, &path); 961 path = path.Append(FILE_PATH_LITERAL("net")); 962 path = path.Append(FILE_PATH_LITERAL("data")); 963 path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); 964 965 TestDelegate d; 966 URLRequest req( 967 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_); 968 req.Start(); 969 base::RunLoop().Run(); 970 971 // Generate entry for the sentinel file. 972 base::FilePath sentinel_path = path.AppendASCII(sentinel_name); 973 base::PlatformFileInfo info; 974 EXPECT_TRUE(file_util::GetFileInfo(sentinel_path, &info)); 975 EXPECT_GT(info.size, 0); 976 std::string sentinel_output = GetDirectoryListingEntry( 977 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), 978 std::string(sentinel_name), 979 false /* is_dir */, 980 info.size, 981 info.last_modified); 982 983 ASSERT_LT(0, d.bytes_received()); 984 ASSERT_FALSE(d.request_failed()); 985 ASSERT_TRUE(req.status().is_success()); 986 // Check for the entry generated for the "sentinel" file. 987 const std::string& data = d.data_received(); 988 ASSERT_NE(data.find(sentinel_output), std::string::npos); 989} 990 991TEST_F(URLRequestTest, FileDirRedirectNoCrash) { 992 // There is an implicit redirect when loading a file path that matches a 993 // directory and does not end with a slash. Ensure that following such 994 // redirects does not crash. See http://crbug.com/18686. 995 996 base::FilePath path; 997 PathService::Get(base::DIR_SOURCE_ROOT, &path); 998 path = path.Append(FILE_PATH_LITERAL("net")); 999 path = path.Append(FILE_PATH_LITERAL("data")); 1000 path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); 1001 1002 TestDelegate d; 1003 URLRequest req( 1004 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_); 1005 req.Start(); 1006 base::RunLoop().Run(); 1007 1008 ASSERT_EQ(1, d.received_redirect_count()); 1009 ASSERT_LT(0, d.bytes_received()); 1010 ASSERT_FALSE(d.request_failed()); 1011 ASSERT_TRUE(req.status().is_success()); 1012} 1013 1014#if defined(OS_WIN) 1015// Don't accept the url "file:///" on windows. See http://crbug.com/1474. 1016TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { 1017 TestDelegate d; 1018 URLRequest req(GURL("file:///"), DEFAULT_PRIORITY, &d, &default_context_); 1019 req.Start(); 1020 base::RunLoop().Run(); 1021 1022 ASSERT_EQ(1, d.received_redirect_count()); 1023 ASSERT_FALSE(req.status().is_success()); 1024} 1025#endif 1026 1027// Custom URLRequestJobs for use with interceptor tests 1028class RestartTestJob : public URLRequestTestJob { 1029 public: 1030 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) 1031 : URLRequestTestJob(request, network_delegate, true) {} 1032 protected: 1033 virtual void StartAsync() OVERRIDE { 1034 this->NotifyRestartRequired(); 1035 } 1036 private: 1037 virtual ~RestartTestJob() {} 1038}; 1039 1040class CancelTestJob : public URLRequestTestJob { 1041 public: 1042 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) 1043 : URLRequestTestJob(request, network_delegate, true) {} 1044 protected: 1045 virtual void StartAsync() OVERRIDE { 1046 request_->Cancel(); 1047 } 1048 private: 1049 virtual ~CancelTestJob() {} 1050}; 1051 1052class CancelThenRestartTestJob : public URLRequestTestJob { 1053 public: 1054 explicit CancelThenRestartTestJob(URLRequest* request, 1055 NetworkDelegate* network_delegate) 1056 : URLRequestTestJob(request, network_delegate, true) { 1057 } 1058 protected: 1059 virtual void StartAsync() OVERRIDE { 1060 request_->Cancel(); 1061 this->NotifyRestartRequired(); 1062 } 1063 private: 1064 virtual ~CancelThenRestartTestJob() {} 1065}; 1066 1067// An Interceptor for use with interceptor tests 1068class TestInterceptor : URLRequest::Interceptor { 1069 public: 1070 TestInterceptor() 1071 : intercept_main_request_(false), restart_main_request_(false), 1072 cancel_main_request_(false), cancel_then_restart_main_request_(false), 1073 simulate_main_network_error_(false), 1074 intercept_redirect_(false), cancel_redirect_request_(false), 1075 intercept_final_response_(false), cancel_final_request_(false), 1076 did_intercept_main_(false), did_restart_main_(false), 1077 did_cancel_main_(false), did_cancel_then_restart_main_(false), 1078 did_simulate_error_main_(false), 1079 did_intercept_redirect_(false), did_cancel_redirect_(false), 1080 did_intercept_final_(false), did_cancel_final_(false) { 1081 URLRequest::Deprecated::RegisterRequestInterceptor(this); 1082 } 1083 1084 virtual ~TestInterceptor() { 1085 URLRequest::Deprecated::UnregisterRequestInterceptor(this); 1086 } 1087 1088 virtual URLRequestJob* MaybeIntercept( 1089 URLRequest* request, 1090 NetworkDelegate* network_delegate) OVERRIDE { 1091 if (restart_main_request_) { 1092 restart_main_request_ = false; 1093 did_restart_main_ = true; 1094 return new RestartTestJob(request, network_delegate); 1095 } 1096 if (cancel_main_request_) { 1097 cancel_main_request_ = false; 1098 did_cancel_main_ = true; 1099 return new CancelTestJob(request, network_delegate); 1100 } 1101 if (cancel_then_restart_main_request_) { 1102 cancel_then_restart_main_request_ = false; 1103 did_cancel_then_restart_main_ = true; 1104 return new CancelThenRestartTestJob(request, network_delegate); 1105 } 1106 if (simulate_main_network_error_) { 1107 simulate_main_network_error_ = false; 1108 did_simulate_error_main_ = true; 1109 // will error since the requeted url is not one of its canned urls 1110 return new URLRequestTestJob(request, network_delegate, true); 1111 } 1112 if (!intercept_main_request_) 1113 return NULL; 1114 intercept_main_request_ = false; 1115 did_intercept_main_ = true; 1116 URLRequestTestJob* job = new URLRequestTestJob(request, 1117 network_delegate, 1118 main_headers_, 1119 main_data_, 1120 true); 1121 job->set_load_timing_info(main_request_load_timing_info_); 1122 return job; 1123 } 1124 1125 virtual URLRequestJob* MaybeInterceptRedirect( 1126 URLRequest* request, 1127 NetworkDelegate* network_delegate, 1128 const GURL& location) OVERRIDE { 1129 if (cancel_redirect_request_) { 1130 cancel_redirect_request_ = false; 1131 did_cancel_redirect_ = true; 1132 return new CancelTestJob(request, network_delegate); 1133 } 1134 if (!intercept_redirect_) 1135 return NULL; 1136 intercept_redirect_ = false; 1137 did_intercept_redirect_ = true; 1138 return new URLRequestTestJob(request, 1139 network_delegate, 1140 redirect_headers_, 1141 redirect_data_, 1142 true); 1143 } 1144 1145 virtual URLRequestJob* MaybeInterceptResponse( 1146 URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE { 1147 if (cancel_final_request_) { 1148 cancel_final_request_ = false; 1149 did_cancel_final_ = true; 1150 return new CancelTestJob(request, network_delegate); 1151 } 1152 if (!intercept_final_response_) 1153 return NULL; 1154 intercept_final_response_ = false; 1155 did_intercept_final_ = true; 1156 return new URLRequestTestJob(request, 1157 network_delegate, 1158 final_headers_, 1159 final_data_, 1160 true); 1161 } 1162 1163 // Whether to intercept the main request, and if so the response to return and 1164 // the LoadTimingInfo to use. 1165 bool intercept_main_request_; 1166 std::string main_headers_; 1167 std::string main_data_; 1168 LoadTimingInfo main_request_load_timing_info_; 1169 1170 // Other actions we take at MaybeIntercept time 1171 bool restart_main_request_; 1172 bool cancel_main_request_; 1173 bool cancel_then_restart_main_request_; 1174 bool simulate_main_network_error_; 1175 1176 // Whether to intercept redirects, and if so the response to return. 1177 bool intercept_redirect_; 1178 std::string redirect_headers_; 1179 std::string redirect_data_; 1180 1181 // Other actions we can take at MaybeInterceptRedirect time 1182 bool cancel_redirect_request_; 1183 1184 // Whether to intercept final response, and if so the response to return. 1185 bool intercept_final_response_; 1186 std::string final_headers_; 1187 std::string final_data_; 1188 1189 // Other actions we can take at MaybeInterceptResponse time 1190 bool cancel_final_request_; 1191 1192 // If we did something or not 1193 bool did_intercept_main_; 1194 bool did_restart_main_; 1195 bool did_cancel_main_; 1196 bool did_cancel_then_restart_main_; 1197 bool did_simulate_error_main_; 1198 bool did_intercept_redirect_; 1199 bool did_cancel_redirect_; 1200 bool did_intercept_final_; 1201 bool did_cancel_final_; 1202 1203 // Static getters for canned response header and data strings 1204 1205 static std::string ok_data() { 1206 return URLRequestTestJob::test_data_1(); 1207 } 1208 1209 static std::string ok_headers() { 1210 return URLRequestTestJob::test_headers(); 1211 } 1212 1213 static std::string redirect_data() { 1214 return std::string(); 1215 } 1216 1217 static std::string redirect_headers() { 1218 return URLRequestTestJob::test_redirect_headers(); 1219 } 1220 1221 static std::string error_data() { 1222 return std::string("ohhh nooooo mr. bill!"); 1223 } 1224 1225 static std::string error_headers() { 1226 return URLRequestTestJob::test_error_headers(); 1227 } 1228}; 1229 1230TEST_F(URLRequestTest, Intercept) { 1231 TestInterceptor interceptor; 1232 1233 // intercept the main request and respond with a simple response 1234 interceptor.intercept_main_request_ = true; 1235 interceptor.main_headers_ = TestInterceptor::ok_headers(); 1236 interceptor.main_data_ = TestInterceptor::ok_data(); 1237 1238 TestDelegate d; 1239 URLRequest req(GURL("http://test_intercept/foo"), 1240 DEFAULT_PRIORITY, 1241 &d, 1242 &default_context_); 1243 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); 1244 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); 1245 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); 1246 req.SetUserData(NULL, user_data0); 1247 req.SetUserData(&user_data1, user_data1); 1248 req.SetUserData(&user_data2, user_data2); 1249 req.set_method("GET"); 1250 req.Start(); 1251 base::RunLoop().Run(); 1252 1253 // Make sure we can retrieve our specific user data 1254 EXPECT_EQ(user_data0, req.GetUserData(NULL)); 1255 EXPECT_EQ(user_data1, req.GetUserData(&user_data1)); 1256 EXPECT_EQ(user_data2, req.GetUserData(&user_data2)); 1257 1258 // Check the interceptor got called as expected 1259 EXPECT_TRUE(interceptor.did_intercept_main_); 1260 1261 // Check we got one good response 1262 EXPECT_TRUE(req.status().is_success()); 1263 EXPECT_EQ(200, req.response_headers()->response_code()); 1264 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); 1265 EXPECT_EQ(1, d.response_started_count()); 1266 EXPECT_EQ(0, d.received_redirect_count()); 1267} 1268 1269TEST_F(URLRequestTest, InterceptRedirect) { 1270 TestInterceptor interceptor; 1271 1272 // intercept the main request and respond with a redirect 1273 interceptor.intercept_main_request_ = true; 1274 interceptor.main_headers_ = TestInterceptor::redirect_headers(); 1275 interceptor.main_data_ = TestInterceptor::redirect_data(); 1276 1277 // intercept that redirect and respond a final OK response 1278 interceptor.intercept_redirect_ = true; 1279 interceptor.redirect_headers_ = TestInterceptor::ok_headers(); 1280 interceptor.redirect_data_ = TestInterceptor::ok_data(); 1281 1282 TestDelegate d; 1283 URLRequest req(GURL("http://test_intercept/foo"), 1284 DEFAULT_PRIORITY, 1285 &d, 1286 &default_context_); 1287 req.set_method("GET"); 1288 req.Start(); 1289 base::RunLoop().Run(); 1290 1291 // Check the interceptor got called as expected 1292 EXPECT_TRUE(interceptor.did_intercept_main_); 1293 EXPECT_TRUE(interceptor.did_intercept_redirect_); 1294 1295 // Check we got one good response 1296 EXPECT_TRUE(req.status().is_success()); 1297 if (req.status().is_success()) { 1298 EXPECT_EQ(200, req.response_headers()->response_code()); 1299 } 1300 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); 1301 EXPECT_EQ(1, d.response_started_count()); 1302 EXPECT_EQ(0, d.received_redirect_count()); 1303} 1304 1305TEST_F(URLRequestTest, InterceptServerError) { 1306 TestInterceptor interceptor; 1307 1308 // intercept the main request to generate a server error response 1309 interceptor.intercept_main_request_ = true; 1310 interceptor.main_headers_ = TestInterceptor::error_headers(); 1311 interceptor.main_data_ = TestInterceptor::error_data(); 1312 1313 // intercept that error and respond with an OK response 1314 interceptor.intercept_final_response_ = true; 1315 interceptor.final_headers_ = TestInterceptor::ok_headers(); 1316 interceptor.final_data_ = TestInterceptor::ok_data(); 1317 1318 TestDelegate d; 1319 URLRequest req(GURL("http://test_intercept/foo"), 1320 DEFAULT_PRIORITY, 1321 &d, 1322 &default_context_); 1323 req.set_method("GET"); 1324 req.Start(); 1325 base::RunLoop().Run(); 1326 1327 // Check the interceptor got called as expected 1328 EXPECT_TRUE(interceptor.did_intercept_main_); 1329 EXPECT_TRUE(interceptor.did_intercept_final_); 1330 1331 // Check we got one good response 1332 EXPECT_TRUE(req.status().is_success()); 1333 EXPECT_EQ(200, req.response_headers()->response_code()); 1334 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); 1335 EXPECT_EQ(1, d.response_started_count()); 1336 EXPECT_EQ(0, d.received_redirect_count()); 1337} 1338 1339TEST_F(URLRequestTest, InterceptNetworkError) { 1340 TestInterceptor interceptor; 1341 1342 // intercept the main request to simulate a network error 1343 interceptor.simulate_main_network_error_ = true; 1344 1345 // intercept that error and respond with an OK response 1346 interceptor.intercept_final_response_ = true; 1347 interceptor.final_headers_ = TestInterceptor::ok_headers(); 1348 interceptor.final_data_ = TestInterceptor::ok_data(); 1349 1350 TestDelegate d; 1351 URLRequest req(GURL("http://test_intercept/foo"), 1352 DEFAULT_PRIORITY, 1353 &d, 1354 &default_context_); 1355 req.set_method("GET"); 1356 req.Start(); 1357 base::RunLoop().Run(); 1358 1359 // Check the interceptor got called as expected 1360 EXPECT_TRUE(interceptor.did_simulate_error_main_); 1361 EXPECT_TRUE(interceptor.did_intercept_final_); 1362 1363 // Check we received one good response 1364 EXPECT_TRUE(req.status().is_success()); 1365 EXPECT_EQ(200, req.response_headers()->response_code()); 1366 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); 1367 EXPECT_EQ(1, d.response_started_count()); 1368 EXPECT_EQ(0, d.received_redirect_count()); 1369} 1370 1371TEST_F(URLRequestTest, InterceptRestartRequired) { 1372 TestInterceptor interceptor; 1373 1374 // restart the main request 1375 interceptor.restart_main_request_ = true; 1376 1377 // then intercept the new main request and respond with an OK response 1378 interceptor.intercept_main_request_ = true; 1379 interceptor.main_headers_ = TestInterceptor::ok_headers(); 1380 interceptor.main_data_ = TestInterceptor::ok_data(); 1381 1382 TestDelegate d; 1383 URLRequest req(GURL("http://test_intercept/foo"), 1384 DEFAULT_PRIORITY, 1385 &d, 1386 &default_context_); 1387 req.set_method("GET"); 1388 req.Start(); 1389 base::RunLoop().Run(); 1390 1391 // Check the interceptor got called as expected 1392 EXPECT_TRUE(interceptor.did_restart_main_); 1393 EXPECT_TRUE(interceptor.did_intercept_main_); 1394 1395 // Check we received one good response 1396 EXPECT_TRUE(req.status().is_success()); 1397 if (req.status().is_success()) { 1398 EXPECT_EQ(200, req.response_headers()->response_code()); 1399 } 1400 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); 1401 EXPECT_EQ(1, d.response_started_count()); 1402 EXPECT_EQ(0, d.received_redirect_count()); 1403} 1404 1405TEST_F(URLRequestTest, InterceptRespectsCancelMain) { 1406 TestInterceptor interceptor; 1407 1408 // intercept the main request and cancel from within the restarted job 1409 interceptor.cancel_main_request_ = true; 1410 1411 // setup to intercept final response and override it with an OK response 1412 interceptor.intercept_final_response_ = true; 1413 interceptor.final_headers_ = TestInterceptor::ok_headers(); 1414 interceptor.final_data_ = TestInterceptor::ok_data(); 1415 1416 TestDelegate d; 1417 URLRequest req(GURL("http://test_intercept/foo"), 1418 DEFAULT_PRIORITY, 1419 &d, 1420 &default_context_); 1421 req.set_method("GET"); 1422 req.Start(); 1423 base::RunLoop().Run(); 1424 1425 // Check the interceptor got called as expected 1426 EXPECT_TRUE(interceptor.did_cancel_main_); 1427 EXPECT_FALSE(interceptor.did_intercept_final_); 1428 1429 // Check we see a canceled request 1430 EXPECT_FALSE(req.status().is_success()); 1431 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); 1432} 1433 1434TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) { 1435 TestInterceptor interceptor; 1436 1437 // intercept the main request and respond with a redirect 1438 interceptor.intercept_main_request_ = true; 1439 interceptor.main_headers_ = TestInterceptor::redirect_headers(); 1440 interceptor.main_data_ = TestInterceptor::redirect_data(); 1441 1442 // intercept the redirect and cancel from within that job 1443 interceptor.cancel_redirect_request_ = true; 1444 1445 // setup to intercept final response and override it with an OK response 1446 interceptor.intercept_final_response_ = true; 1447 interceptor.final_headers_ = TestInterceptor::ok_headers(); 1448 interceptor.final_data_ = TestInterceptor::ok_data(); 1449 1450 TestDelegate d; 1451 URLRequest req(GURL("http://test_intercept/foo"), 1452 DEFAULT_PRIORITY, 1453 &d, 1454 &default_context_); 1455 req.set_method("GET"); 1456 req.Start(); 1457 base::RunLoop().Run(); 1458 1459 // Check the interceptor got called as expected 1460 EXPECT_TRUE(interceptor.did_intercept_main_); 1461 EXPECT_TRUE(interceptor.did_cancel_redirect_); 1462 EXPECT_FALSE(interceptor.did_intercept_final_); 1463 1464 // Check we see a canceled request 1465 EXPECT_FALSE(req.status().is_success()); 1466 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); 1467} 1468 1469TEST_F(URLRequestTest, InterceptRespectsCancelFinal) { 1470 TestInterceptor interceptor; 1471 1472 // intercept the main request to simulate a network error 1473 interceptor.simulate_main_network_error_ = true; 1474 1475 // setup to intercept final response and cancel from within that job 1476 interceptor.cancel_final_request_ = true; 1477 1478 TestDelegate d; 1479 URLRequest req(GURL("http://test_intercept/foo"), 1480 DEFAULT_PRIORITY, 1481 &d, 1482 &default_context_); 1483 req.set_method("GET"); 1484 req.Start(); 1485 base::RunLoop().Run(); 1486 1487 // Check the interceptor got called as expected 1488 EXPECT_TRUE(interceptor.did_simulate_error_main_); 1489 EXPECT_TRUE(interceptor.did_cancel_final_); 1490 1491 // Check we see a canceled request 1492 EXPECT_FALSE(req.status().is_success()); 1493 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); 1494} 1495 1496TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { 1497 TestInterceptor interceptor; 1498 1499 // intercept the main request and cancel then restart from within that job 1500 interceptor.cancel_then_restart_main_request_ = true; 1501 1502 // setup to intercept final response and override it with an OK response 1503 interceptor.intercept_final_response_ = true; 1504 interceptor.final_headers_ = TestInterceptor::ok_headers(); 1505 interceptor.final_data_ = TestInterceptor::ok_data(); 1506 1507 TestDelegate d; 1508 URLRequest req(GURL("http://test_intercept/foo"), 1509 DEFAULT_PRIORITY, 1510 &d, 1511 &default_context_); 1512 req.set_method("GET"); 1513 req.Start(); 1514 base::RunLoop().Run(); 1515 1516 // Check the interceptor got called as expected 1517 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); 1518 EXPECT_FALSE(interceptor.did_intercept_final_); 1519 1520 // Check we see a canceled request 1521 EXPECT_FALSE(req.status().is_success()); 1522 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); 1523} 1524 1525LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing, 1526 URLRequestContext* context) { 1527 TestInterceptor interceptor; 1528 interceptor.intercept_main_request_ = true; 1529 interceptor.main_request_load_timing_info_ = job_load_timing; 1530 TestDelegate d; 1531 URLRequest req( 1532 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, context); 1533 req.Start(); 1534 base::RunLoop().Run(); 1535 1536 LoadTimingInfo resulting_load_timing; 1537 req.GetLoadTimingInfo(&resulting_load_timing); 1538 1539 // None of these should be modified by the URLRequest. 1540 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); 1541 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); 1542 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); 1543 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); 1544 EXPECT_EQ(job_load_timing.receive_headers_end, 1545 resulting_load_timing.receive_headers_end); 1546 1547 return resulting_load_timing; 1548} 1549 1550// "Normal" LoadTimingInfo as returned by a job. Everything is in order, not 1551// reused. |connect_time_flags| is used to indicate if there should be dns 1552// or SSL times, and |used_proxy| is used for proxy times. 1553LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, 1554 int connect_time_flags, 1555 bool used_proxy) { 1556 LoadTimingInfo load_timing; 1557 load_timing.socket_log_id = 1; 1558 1559 if (used_proxy) { 1560 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); 1561 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); 1562 } 1563 1564 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; 1565 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { 1566 connect_timing.dns_start = now + base::TimeDelta::FromDays(3); 1567 connect_timing.dns_end = now + base::TimeDelta::FromDays(4); 1568 } 1569 connect_timing.connect_start = now + base::TimeDelta::FromDays(5); 1570 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { 1571 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); 1572 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); 1573 } 1574 connect_timing.connect_end = now + base::TimeDelta::FromDays(8); 1575 1576 load_timing.send_start = now + base::TimeDelta::FromDays(9); 1577 load_timing.send_end = now + base::TimeDelta::FromDays(10); 1578 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); 1579 return load_timing; 1580} 1581 1582// Same as above, but in the case of a reused socket. 1583LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, 1584 bool used_proxy) { 1585 LoadTimingInfo load_timing; 1586 load_timing.socket_log_id = 1; 1587 load_timing.socket_reused = true; 1588 1589 if (used_proxy) { 1590 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); 1591 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); 1592 } 1593 1594 load_timing.send_start = now + base::TimeDelta::FromDays(9); 1595 load_timing.send_end = now + base::TimeDelta::FromDays(10); 1596 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); 1597 return load_timing; 1598} 1599 1600// Basic test that the intercept + load timing tests work. 1601TEST_F(URLRequestTest, InterceptLoadTiming) { 1602 base::TimeTicks now = base::TimeTicks::Now(); 1603 LoadTimingInfo job_load_timing = 1604 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); 1605 1606 LoadTimingInfo load_timing_result = 1607 RunLoadTimingTest(job_load_timing, &default_context_); 1608 1609 // Nothing should have been changed by the URLRequest. 1610 EXPECT_EQ(job_load_timing.proxy_resolve_start, 1611 load_timing_result.proxy_resolve_start); 1612 EXPECT_EQ(job_load_timing.proxy_resolve_end, 1613 load_timing_result.proxy_resolve_end); 1614 EXPECT_EQ(job_load_timing.connect_timing.dns_start, 1615 load_timing_result.connect_timing.dns_start); 1616 EXPECT_EQ(job_load_timing.connect_timing.dns_end, 1617 load_timing_result.connect_timing.dns_end); 1618 EXPECT_EQ(job_load_timing.connect_timing.connect_start, 1619 load_timing_result.connect_timing.connect_start); 1620 EXPECT_EQ(job_load_timing.connect_timing.connect_end, 1621 load_timing_result.connect_timing.connect_end); 1622 EXPECT_EQ(job_load_timing.connect_timing.ssl_start, 1623 load_timing_result.connect_timing.ssl_start); 1624 EXPECT_EQ(job_load_timing.connect_timing.ssl_end, 1625 load_timing_result.connect_timing.ssl_end); 1626 1627 // Redundant sanity check. 1628 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); 1629} 1630 1631// Another basic test, with proxy and SSL times, but no DNS times. 1632TEST_F(URLRequestTest, InterceptLoadTimingProxy) { 1633 base::TimeTicks now = base::TimeTicks::Now(); 1634 LoadTimingInfo job_load_timing = 1635 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); 1636 1637 LoadTimingInfo load_timing_result = 1638 RunLoadTimingTest(job_load_timing, &default_context_); 1639 1640 // Nothing should have been changed by the URLRequest. 1641 EXPECT_EQ(job_load_timing.proxy_resolve_start, 1642 load_timing_result.proxy_resolve_start); 1643 EXPECT_EQ(job_load_timing.proxy_resolve_end, 1644 load_timing_result.proxy_resolve_end); 1645 EXPECT_EQ(job_load_timing.connect_timing.dns_start, 1646 load_timing_result.connect_timing.dns_start); 1647 EXPECT_EQ(job_load_timing.connect_timing.dns_end, 1648 load_timing_result.connect_timing.dns_end); 1649 EXPECT_EQ(job_load_timing.connect_timing.connect_start, 1650 load_timing_result.connect_timing.connect_start); 1651 EXPECT_EQ(job_load_timing.connect_timing.connect_end, 1652 load_timing_result.connect_timing.connect_end); 1653 EXPECT_EQ(job_load_timing.connect_timing.ssl_start, 1654 load_timing_result.connect_timing.ssl_start); 1655 EXPECT_EQ(job_load_timing.connect_timing.ssl_end, 1656 load_timing_result.connect_timing.ssl_end); 1657 1658 // Redundant sanity check. 1659 TestLoadTimingNotReusedWithProxy(load_timing_result, 1660 CONNECT_TIMING_HAS_SSL_TIMES); 1661} 1662 1663// Make sure that URLRequest correctly adjusts proxy times when they're before 1664// |request_start|, due to already having a connected socket. This happens in 1665// the case of reusing a SPDY session or HTTP pipeline. The connected socket is 1666// not considered reused in this test (May be a preconnect). 1667// 1668// To mix things up from the test above, assumes DNS times but no SSL times. 1669TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) { 1670 base::TimeTicks now = base::TimeTicks::Now(); 1671 LoadTimingInfo job_load_timing = 1672 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); 1673 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); 1674 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); 1675 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); 1676 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); 1677 job_load_timing.connect_timing.connect_start = 1678 now - base::TimeDelta::FromDays(2); 1679 job_load_timing.connect_timing.connect_end = 1680 now - base::TimeDelta::FromDays(1); 1681 1682 LoadTimingInfo load_timing_result = 1683 RunLoadTimingTest(job_load_timing, &default_context_); 1684 1685 // Proxy times, connect times, and DNS times should all be replaced with 1686 // request_start. 1687 EXPECT_EQ(load_timing_result.request_start, 1688 load_timing_result.proxy_resolve_start); 1689 EXPECT_EQ(load_timing_result.request_start, 1690 load_timing_result.proxy_resolve_end); 1691 EXPECT_EQ(load_timing_result.request_start, 1692 load_timing_result.connect_timing.dns_start); 1693 EXPECT_EQ(load_timing_result.request_start, 1694 load_timing_result.connect_timing.dns_end); 1695 EXPECT_EQ(load_timing_result.request_start, 1696 load_timing_result.connect_timing.connect_start); 1697 EXPECT_EQ(load_timing_result.request_start, 1698 load_timing_result.connect_timing.connect_end); 1699 1700 // Other times should have been left null. 1701 TestLoadTimingNotReusedWithProxy(load_timing_result, 1702 CONNECT_TIMING_HAS_DNS_TIMES); 1703} 1704 1705// Same as above, but in the reused case. 1706TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) { 1707 base::TimeTicks now = base::TimeTicks::Now(); 1708 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); 1709 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); 1710 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); 1711 1712 LoadTimingInfo load_timing_result = 1713 RunLoadTimingTest(job_load_timing, &default_context_); 1714 1715 // Proxy times and connect times should all be replaced with request_start. 1716 EXPECT_EQ(load_timing_result.request_start, 1717 load_timing_result.proxy_resolve_start); 1718 EXPECT_EQ(load_timing_result.request_start, 1719 load_timing_result.proxy_resolve_end); 1720 1721 // Other times should have been left null. 1722 TestLoadTimingReusedWithProxy(load_timing_result); 1723} 1724 1725// Make sure that URLRequest correctly adjusts connect times when they're before 1726// |request_start|, due to reusing a connected socket. The connected socket is 1727// not considered reused in this test (May be a preconnect). 1728// 1729// To mix things up, the request has SSL times, but no DNS times. 1730TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) { 1731 base::TimeTicks now = base::TimeTicks::Now(); 1732 LoadTimingInfo job_load_timing = 1733 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); 1734 job_load_timing.connect_timing.connect_start = 1735 now - base::TimeDelta::FromDays(1); 1736 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); 1737 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); 1738 job_load_timing.connect_timing.connect_end = 1739 now - base::TimeDelta::FromDays(4); 1740 1741 LoadTimingInfo load_timing_result = 1742 RunLoadTimingTest(job_load_timing, &default_context_); 1743 1744 // Connect times, and SSL times should be replaced with request_start. 1745 EXPECT_EQ(load_timing_result.request_start, 1746 load_timing_result.connect_timing.connect_start); 1747 EXPECT_EQ(load_timing_result.request_start, 1748 load_timing_result.connect_timing.ssl_start); 1749 EXPECT_EQ(load_timing_result.request_start, 1750 load_timing_result.connect_timing.ssl_end); 1751 EXPECT_EQ(load_timing_result.request_start, 1752 load_timing_result.connect_timing.connect_end); 1753 1754 // Other times should have been left null. 1755 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); 1756} 1757 1758// Make sure that URLRequest correctly adjusts connect times when they're before 1759// |request_start|, due to reusing a connected socket in the case that there 1760// are also proxy times. The connected socket is not considered reused in this 1761// test (May be a preconnect). 1762// 1763// In this test, there are no SSL or DNS times. 1764TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) { 1765 base::TimeTicks now = base::TimeTicks::Now(); 1766 LoadTimingInfo job_load_timing = 1767 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); 1768 job_load_timing.connect_timing.connect_start = 1769 now - base::TimeDelta::FromDays(1); 1770 job_load_timing.connect_timing.connect_end = 1771 now - base::TimeDelta::FromDays(2); 1772 1773 LoadTimingInfo load_timing_result = 1774 RunLoadTimingTest(job_load_timing, &default_context_); 1775 1776 // Connect times should be replaced with proxy_resolve_end. 1777 EXPECT_EQ(load_timing_result.proxy_resolve_end, 1778 load_timing_result.connect_timing.connect_start); 1779 EXPECT_EQ(load_timing_result.proxy_resolve_end, 1780 load_timing_result.connect_timing.connect_end); 1781 1782 // Other times should have been left null. 1783 TestLoadTimingNotReusedWithProxy(load_timing_result, 1784 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); 1785} 1786 1787// Check that two different URL requests have different identifiers. 1788TEST_F(URLRequestTest, Identifiers) { 1789 TestDelegate d; 1790 TestURLRequestContext context; 1791 TestURLRequest req( 1792 GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context); 1793 TestURLRequest other_req( 1794 GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context); 1795 1796 ASSERT_NE(req.identifier(), other_req.identifier()); 1797} 1798 1799// Check that a failure to connect to the proxy is reported to the network 1800// delegate. 1801TEST_F(URLRequestTest, NetworkDelegateProxyError) { 1802 MockHostResolver host_resolver; 1803 host_resolver.rules()->AddSimulatedFailure("*"); 1804 1805 TestNetworkDelegate network_delegate; // Must outlive URLRequests. 1806 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); 1807 1808 TestDelegate d; 1809 URLRequest req(GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context); 1810 req.set_method("GET"); 1811 1812 req.Start(); 1813 base::RunLoop().Run(); 1814 1815 // Check we see a failed request. 1816 EXPECT_FALSE(req.status().is_success()); 1817 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); 1818 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error()); 1819 1820 EXPECT_EQ(1, network_delegate.error_count()); 1821 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); 1822 EXPECT_EQ(1, network_delegate.completed_requests()); 1823} 1824 1825// Make sure that net::NetworkDelegate::NotifyCompleted is called if 1826// content is empty. 1827TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { 1828 TestDelegate d; 1829 URLRequest req(GURL("data:,"), DEFAULT_PRIORITY, &d, &default_context_); 1830 req.Start(); 1831 base::RunLoop().Run(); 1832 EXPECT_EQ("", d.data_received()); 1833 EXPECT_EQ(1, default_network_delegate_.completed_requests()); 1834} 1835 1836// Make sure that SetPriority actually sets the URLRequest's priority 1837// correctly, both before and after start. 1838TEST_F(URLRequestTest, SetPriorityBasic) { 1839 TestDelegate d; 1840 URLRequest req(GURL("http://test_intercept/foo"), 1841 DEFAULT_PRIORITY, 1842 &d, 1843 &default_context_); 1844 EXPECT_EQ(DEFAULT_PRIORITY, req.priority()); 1845 1846 req.SetPriority(LOW); 1847 EXPECT_EQ(LOW, req.priority()); 1848 1849 req.Start(); 1850 EXPECT_EQ(LOW, req.priority()); 1851 1852 req.SetPriority(MEDIUM); 1853 EXPECT_EQ(MEDIUM, req.priority()); 1854} 1855 1856// Make sure that URLRequest calls SetPriority on a job before calling 1857// Start on it. 1858TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { 1859 TestDelegate d; 1860 URLRequest req(GURL("http://test_intercept/foo"), 1861 DEFAULT_PRIORITY, 1862 &d, 1863 &default_context_); 1864 EXPECT_EQ(DEFAULT_PRIORITY, req.priority()); 1865 1866 scoped_refptr<URLRequestTestJob> job = 1867 new URLRequestTestJob(&req, &default_network_delegate_); 1868 AddTestInterceptor()->set_main_intercept_job(job.get()); 1869 EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); 1870 1871 req.SetPriority(LOW); 1872 1873 req.Start(); 1874 EXPECT_EQ(LOW, job->priority()); 1875} 1876 1877// Make sure that URLRequest passes on its priority updates to its 1878// job. 1879TEST_F(URLRequestTest, SetJobPriority) { 1880 TestDelegate d; 1881 URLRequest req(GURL("http://test_intercept/foo"), 1882 DEFAULT_PRIORITY, 1883 &d, 1884 &default_context_); 1885 1886 scoped_refptr<URLRequestTestJob> job = 1887 new URLRequestTestJob(&req, &default_network_delegate_); 1888 AddTestInterceptor()->set_main_intercept_job(job.get()); 1889 1890 req.SetPriority(LOW); 1891 req.Start(); 1892 EXPECT_EQ(LOW, job->priority()); 1893 1894 req.SetPriority(MEDIUM); 1895 EXPECT_EQ(MEDIUM, req.priority()); 1896 EXPECT_EQ(MEDIUM, job->priority()); 1897} 1898 1899// TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666). 1900#if !defined(OS_IOS) 1901// A subclass of SpawnedTestServer that uses a statically-configured hostname. 1902// This is to work around mysterious failures in chrome_frame_net_tests. See: 1903// http://crbug.com/114369 1904class LocalHttpTestServer : public SpawnedTestServer { 1905 public: 1906 explicit LocalHttpTestServer(const base::FilePath& document_root) 1907 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, 1908 ScopedCustomUrlRequestTestHttpHost::value(), 1909 document_root) {} 1910 LocalHttpTestServer() 1911 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, 1912 ScopedCustomUrlRequestTestHttpHost::value(), 1913 base::FilePath()) {} 1914}; 1915 1916TEST_F(URLRequestTest, DelayedCookieCallback) { 1917 LocalHttpTestServer test_server; 1918 ASSERT_TRUE(test_server.Start()); 1919 1920 TestURLRequestContext context; 1921 scoped_refptr<DelayedCookieMonster> delayed_cm = 1922 new DelayedCookieMonster(); 1923 scoped_refptr<CookieStore> cookie_store = delayed_cm; 1924 context.set_cookie_store(delayed_cm.get()); 1925 1926 // Set up a cookie. 1927 { 1928 TestNetworkDelegate network_delegate; 1929 context.set_network_delegate(&network_delegate); 1930 TestDelegate d; 1931 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), 1932 DEFAULT_PRIORITY, 1933 &d, 1934 &context); 1935 req.Start(); 1936 base::RunLoop().Run(); 1937 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 1938 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 1939 EXPECT_EQ(1, network_delegate.set_cookie_count()); 1940 } 1941 1942 // Verify that the cookie is set. 1943 { 1944 TestNetworkDelegate network_delegate; 1945 context.set_network_delegate(&network_delegate); 1946 TestDelegate d; 1947 URLRequest req(test_server.GetURL("echoheader?Cookie"), 1948 DEFAULT_PRIORITY, 1949 &d, 1950 &context); 1951 req.Start(); 1952 base::RunLoop().Run(); 1953 1954 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") 1955 != std::string::npos); 1956 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 1957 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 1958 } 1959} 1960 1961TEST_F(URLRequestTest, DoNotSendCookies) { 1962 LocalHttpTestServer test_server; 1963 ASSERT_TRUE(test_server.Start()); 1964 1965 // Set up a cookie. 1966 { 1967 TestNetworkDelegate network_delegate; 1968 default_context_.set_network_delegate(&network_delegate); 1969 TestDelegate d; 1970 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), 1971 DEFAULT_PRIORITY, 1972 &d, 1973 &default_context_); 1974 req.Start(); 1975 base::RunLoop().Run(); 1976 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 1977 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 1978 } 1979 1980 // Verify that the cookie is set. 1981 { 1982 TestNetworkDelegate network_delegate; 1983 default_context_.set_network_delegate(&network_delegate); 1984 TestDelegate d; 1985 URLRequest req(test_server.GetURL("echoheader?Cookie"), 1986 DEFAULT_PRIORITY, 1987 &d, 1988 &default_context_); 1989 req.Start(); 1990 base::RunLoop().Run(); 1991 1992 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") 1993 != std::string::npos); 1994 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 1995 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 1996 } 1997 1998 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. 1999 { 2000 TestNetworkDelegate network_delegate; 2001 default_context_.set_network_delegate(&network_delegate); 2002 TestDelegate d; 2003 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2004 DEFAULT_PRIORITY, 2005 &d, 2006 &default_context_); 2007 req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES); 2008 req.Start(); 2009 base::RunLoop().Run(); 2010 2011 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") 2012 == std::string::npos); 2013 2014 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. 2015 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2016 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2017 } 2018} 2019 2020TEST_F(URLRequestTest, DoNotSaveCookies) { 2021 LocalHttpTestServer test_server; 2022 ASSERT_TRUE(test_server.Start()); 2023 2024 // Set up a cookie. 2025 { 2026 TestNetworkDelegate network_delegate; 2027 default_context_.set_network_delegate(&network_delegate); 2028 TestDelegate d; 2029 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), 2030 DEFAULT_PRIORITY, 2031 &d, 2032 &default_context_); 2033 req.Start(); 2034 base::RunLoop().Run(); 2035 2036 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2037 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2038 EXPECT_EQ(1, network_delegate.set_cookie_count()); 2039 } 2040 2041 // Try to set-up another cookie and update the previous cookie. 2042 { 2043 TestNetworkDelegate network_delegate; 2044 default_context_.set_network_delegate(&network_delegate); 2045 TestDelegate d; 2046 URLRequest req( 2047 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), 2048 DEFAULT_PRIORITY, 2049 &d, 2050 &default_context_); 2051 req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES); 2052 req.Start(); 2053 2054 base::RunLoop().Run(); 2055 2056 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. 2057 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2058 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2059 EXPECT_EQ(0, network_delegate.set_cookie_count()); 2060 } 2061 2062 // Verify the cookies weren't saved or updated. 2063 { 2064 TestNetworkDelegate network_delegate; 2065 default_context_.set_network_delegate(&network_delegate); 2066 TestDelegate d; 2067 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2068 DEFAULT_PRIORITY, 2069 &d, 2070 &default_context_); 2071 req.Start(); 2072 base::RunLoop().Run(); 2073 2074 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") 2075 == std::string::npos); 2076 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") 2077 != std::string::npos); 2078 2079 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2080 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2081 EXPECT_EQ(0, network_delegate.set_cookie_count()); 2082 } 2083} 2084 2085TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { 2086 LocalHttpTestServer test_server; 2087 ASSERT_TRUE(test_server.Start()); 2088 2089 // Set up a cookie. 2090 { 2091 TestNetworkDelegate network_delegate; 2092 default_context_.set_network_delegate(&network_delegate); 2093 TestDelegate d; 2094 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), 2095 DEFAULT_PRIORITY, 2096 &d, 2097 &default_context_); 2098 req.Start(); 2099 base::RunLoop().Run(); 2100 2101 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2102 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2103 } 2104 2105 // Verify that the cookie is set. 2106 { 2107 TestNetworkDelegate network_delegate; 2108 default_context_.set_network_delegate(&network_delegate); 2109 TestDelegate d; 2110 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2111 DEFAULT_PRIORITY, 2112 &d, 2113 &default_context_); 2114 req.Start(); 2115 base::RunLoop().Run(); 2116 2117 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") 2118 != std::string::npos); 2119 2120 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2121 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2122 } 2123 2124 // Verify that the cookie isn't sent. 2125 { 2126 TestNetworkDelegate network_delegate; 2127 default_context_.set_network_delegate(&network_delegate); 2128 TestDelegate d; 2129 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); 2130 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2131 DEFAULT_PRIORITY, 2132 &d, 2133 &default_context_); 2134 req.Start(); 2135 base::RunLoop().Run(); 2136 2137 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") 2138 == std::string::npos); 2139 2140 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); 2141 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2142 } 2143} 2144 2145TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { 2146 LocalHttpTestServer test_server; 2147 ASSERT_TRUE(test_server.Start()); 2148 2149 // Set up a cookie. 2150 { 2151 TestNetworkDelegate network_delegate; 2152 default_context_.set_network_delegate(&network_delegate); 2153 TestDelegate d; 2154 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), 2155 DEFAULT_PRIORITY, 2156 &d, 2157 &default_context_); 2158 req.Start(); 2159 base::RunLoop().Run(); 2160 2161 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2162 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2163 } 2164 2165 // Try to set-up another cookie and update the previous cookie. 2166 { 2167 TestNetworkDelegate network_delegate; 2168 default_context_.set_network_delegate(&network_delegate); 2169 TestDelegate d; 2170 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); 2171 URLRequest req( 2172 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), 2173 DEFAULT_PRIORITY, 2174 &d, 2175 &default_context_); 2176 req.Start(); 2177 2178 base::RunLoop().Run(); 2179 2180 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2181 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); 2182 } 2183 2184 // Verify the cookies weren't saved or updated. 2185 { 2186 TestNetworkDelegate network_delegate; 2187 default_context_.set_network_delegate(&network_delegate); 2188 TestDelegate d; 2189 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2190 DEFAULT_PRIORITY, 2191 &d, 2192 &default_context_); 2193 req.Start(); 2194 base::RunLoop().Run(); 2195 2196 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") 2197 == std::string::npos); 2198 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") 2199 != std::string::npos); 2200 2201 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2202 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2203 } 2204} 2205 2206TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { 2207 LocalHttpTestServer test_server; 2208 ASSERT_TRUE(test_server.Start()); 2209 2210 // Set up an empty cookie. 2211 { 2212 TestNetworkDelegate network_delegate; 2213 default_context_.set_network_delegate(&network_delegate); 2214 TestDelegate d; 2215 URLRequest req(test_server.GetURL("set-cookie"), 2216 DEFAULT_PRIORITY, 2217 &d, 2218 &default_context_); 2219 req.Start(); 2220 base::RunLoop().Run(); 2221 2222 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2223 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2224 EXPECT_EQ(0, network_delegate.set_cookie_count()); 2225 } 2226} 2227 2228TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { 2229 LocalHttpTestServer test_server; 2230 ASSERT_TRUE(test_server.Start()); 2231 2232 // Set up a cookie. 2233 { 2234 TestNetworkDelegate network_delegate; 2235 default_context_.set_network_delegate(&network_delegate); 2236 TestDelegate d; 2237 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), 2238 DEFAULT_PRIORITY, 2239 &d, 2240 &default_context_); 2241 req.Start(); 2242 base::RunLoop().Run(); 2243 2244 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2245 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2246 } 2247 2248 // Verify that the cookie is set. 2249 { 2250 TestNetworkDelegate network_delegate; 2251 default_context_.set_network_delegate(&network_delegate); 2252 TestDelegate d; 2253 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2254 DEFAULT_PRIORITY, 2255 &d, 2256 &default_context_); 2257 req.Start(); 2258 base::RunLoop().Run(); 2259 2260 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") 2261 != std::string::npos); 2262 2263 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2264 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2265 } 2266 2267 // Verify that the cookie isn't sent. 2268 { 2269 TestNetworkDelegate network_delegate; 2270 default_context_.set_network_delegate(&network_delegate); 2271 TestDelegate d; 2272 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); 2273 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2274 DEFAULT_PRIORITY, 2275 &d, 2276 &default_context_); 2277 req.Start(); 2278 base::RunLoop().Run(); 2279 2280 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") 2281 == std::string::npos); 2282 2283 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); 2284 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2285 } 2286} 2287 2288TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { 2289 LocalHttpTestServer test_server; 2290 ASSERT_TRUE(test_server.Start()); 2291 2292 // Set up a cookie. 2293 { 2294 TestNetworkDelegate network_delegate; 2295 default_context_.set_network_delegate(&network_delegate); 2296 TestDelegate d; 2297 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), 2298 DEFAULT_PRIORITY, 2299 &d, 2300 &default_context_); 2301 req.Start(); 2302 base::RunLoop().Run(); 2303 2304 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2305 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2306 } 2307 2308 // Try to set-up another cookie and update the previous cookie. 2309 { 2310 TestNetworkDelegate network_delegate; 2311 default_context_.set_network_delegate(&network_delegate); 2312 TestDelegate d; 2313 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); 2314 URLRequest req( 2315 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), 2316 DEFAULT_PRIORITY, 2317 &d, 2318 &default_context_); 2319 req.Start(); 2320 2321 base::RunLoop().Run(); 2322 2323 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2324 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); 2325 } 2326 2327 // Verify the cookies weren't saved or updated. 2328 { 2329 TestNetworkDelegate network_delegate; 2330 default_context_.set_network_delegate(&network_delegate); 2331 TestDelegate d; 2332 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2333 DEFAULT_PRIORITY, 2334 &d, 2335 &default_context_); 2336 req.Start(); 2337 base::RunLoop().Run(); 2338 2339 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") 2340 == std::string::npos); 2341 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") 2342 != std::string::npos); 2343 2344 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); 2345 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); 2346 } 2347} 2348 2349// FixedDateNetworkDelegate swaps out the server's HTTP Date response header 2350// value for the |fixed_date| argument given to the constructor. 2351class FixedDateNetworkDelegate : public TestNetworkDelegate { 2352 public: 2353 explicit FixedDateNetworkDelegate(const std::string& fixed_date) 2354 : fixed_date_(fixed_date) {} 2355 virtual ~FixedDateNetworkDelegate() {} 2356 2357 // net::NetworkDelegate implementation 2358 virtual int OnHeadersReceived( 2359 net::URLRequest* request, 2360 const net::CompletionCallback& callback, 2361 const net::HttpResponseHeaders* original_response_headers, 2362 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) 2363 OVERRIDE; 2364 2365 private: 2366 std::string fixed_date_; 2367 2368 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); 2369}; 2370 2371int FixedDateNetworkDelegate::OnHeadersReceived( 2372 net::URLRequest* request, 2373 const net::CompletionCallback& callback, 2374 const net::HttpResponseHeaders* original_response_headers, 2375 scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { 2376 net::HttpResponseHeaders* new_response_headers = 2377 new net::HttpResponseHeaders(original_response_headers->raw_headers()); 2378 2379 new_response_headers->RemoveHeader("Date"); 2380 new_response_headers->AddHeader("Date: " + fixed_date_); 2381 2382 *override_response_headers = new_response_headers; 2383 return TestNetworkDelegate::OnHeadersReceived(request, 2384 callback, 2385 original_response_headers, 2386 override_response_headers); 2387} 2388 2389// Test that cookie expiration times are adjusted for server/client clock 2390// skew and that we handle incorrect timezone specifier "UTC" in HTTP Date 2391// headers by defaulting to GMT. (crbug.com/135131) 2392TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { 2393 LocalHttpTestServer test_server; 2394 ASSERT_TRUE(test_server.Start()); 2395 2396 // Set up an expired cookie. 2397 { 2398 TestNetworkDelegate network_delegate; 2399 default_context_.set_network_delegate(&network_delegate); 2400 TestDelegate d; 2401 URLRequest req( 2402 test_server.GetURL( 2403 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), 2404 DEFAULT_PRIORITY, 2405 &d, 2406 &default_context_); 2407 req.Start(); 2408 base::RunLoop().Run(); 2409 } 2410 // Verify that the cookie is not set. 2411 { 2412 TestNetworkDelegate network_delegate; 2413 default_context_.set_network_delegate(&network_delegate); 2414 TestDelegate d; 2415 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2416 DEFAULT_PRIORITY, 2417 &d, 2418 &default_context_); 2419 req.Start(); 2420 base::RunLoop().Run(); 2421 2422 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); 2423 } 2424 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. 2425 { 2426 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); 2427 default_context_.set_network_delegate(&network_delegate); 2428 TestDelegate d; 2429 URLRequest req( 2430 test_server.GetURL( 2431 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), 2432 DEFAULT_PRIORITY, 2433 &d, 2434 &default_context_); 2435 req.Start(); 2436 base::RunLoop().Run(); 2437 } 2438 // Verify that the cookie is set. 2439 { 2440 TestNetworkDelegate network_delegate; 2441 default_context_.set_network_delegate(&network_delegate); 2442 TestDelegate d; 2443 URLRequest req(test_server.GetURL("echoheader?Cookie"), 2444 DEFAULT_PRIORITY, 2445 &d, 2446 &default_context_); 2447 req.Start(); 2448 base::RunLoop().Run(); 2449 2450 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); 2451 } 2452} 2453 2454 2455// Check that it is impossible to change the referrer in the extra headers of 2456// an URLRequest. 2457TEST_F(URLRequestTest, DoNotOverrideReferrer) { 2458 LocalHttpTestServer test_server; 2459 ASSERT_TRUE(test_server.Start()); 2460 2461 // If extra headers contain referer and the request contains a referer, 2462 // only the latter shall be respected. 2463 { 2464 TestDelegate d; 2465 URLRequest req(test_server.GetURL("echoheader?Referer"), 2466 DEFAULT_PRIORITY, 2467 &d, 2468 &default_context_); 2469 req.SetReferrer("http://foo.com/"); 2470 2471 HttpRequestHeaders headers; 2472 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); 2473 req.SetExtraRequestHeaders(headers); 2474 2475 req.Start(); 2476 base::RunLoop().Run(); 2477 2478 EXPECT_EQ("http://foo.com/", d.data_received()); 2479 } 2480 2481 // If extra headers contain a referer but the request does not, no referer 2482 // shall be sent in the header. 2483 { 2484 TestDelegate d; 2485 URLRequest req(test_server.GetURL("echoheader?Referer"), 2486 DEFAULT_PRIORITY, 2487 &d, 2488 &default_context_); 2489 2490 HttpRequestHeaders headers; 2491 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); 2492 req.SetExtraRequestHeaders(headers); 2493 req.set_load_flags(LOAD_VALIDATE_CACHE); 2494 2495 req.Start(); 2496 base::RunLoop().Run(); 2497 2498 EXPECT_EQ("None", d.data_received()); 2499 } 2500} 2501 2502class URLRequestTestHTTP : public URLRequestTest { 2503 public: 2504 URLRequestTestHTTP() 2505 : test_server_(base::FilePath(FILE_PATH_LITERAL( 2506 "net/data/url_request_unittest"))) { 2507 } 2508 2509 protected: 2510 // Requests |redirect_url|, which must return a HTTP 3xx redirect. 2511 // |request_method| is the method to use for the initial request. 2512 // |redirect_method| is the method that is expected to be used for the second 2513 // request, after redirection. 2514 // If |include_data| is true, data is uploaded with the request. The 2515 // response body is expected to match it exactly, if and only if 2516 // |request_method| == |redirect_method|. 2517 void HTTPRedirectMethodTest(const GURL& redirect_url, 2518 const std::string& request_method, 2519 const std::string& redirect_method, 2520 bool include_data) { 2521 static const char kData[] = "hello world"; 2522 TestDelegate d; 2523 URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_); 2524 req.set_method(request_method); 2525 if (include_data) { 2526 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); 2527 HttpRequestHeaders headers; 2528 headers.SetHeader(HttpRequestHeaders::kContentLength, 2529 base::UintToString(arraysize(kData) - 1)); 2530 req.SetExtraRequestHeaders(headers); 2531 } 2532 req.Start(); 2533 base::RunLoop().Run(); 2534 EXPECT_EQ(redirect_method, req.method()); 2535 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); 2536 EXPECT_EQ(OK, req.status().error()); 2537 if (include_data) { 2538 if (request_method == redirect_method) { 2539 EXPECT_EQ(kData, d.data_received()); 2540 } else { 2541 EXPECT_NE(kData, d.data_received()); 2542 } 2543 } 2544 if (HasFailure()) 2545 LOG(WARNING) << "Request method was: " << request_method; 2546 } 2547 2548 void HTTPUploadDataOperationTest(const std::string& method) { 2549 const int kMsgSize = 20000; // multiple of 10 2550 const int kIterations = 50; 2551 char* uploadBytes = new char[kMsgSize+1]; 2552 char* ptr = uploadBytes; 2553 char marker = 'a'; 2554 for (int idx = 0; idx < kMsgSize/10; idx++) { 2555 memcpy(ptr, "----------", 10); 2556 ptr += 10; 2557 if (idx % 100 == 0) { 2558 ptr--; 2559 *ptr++ = marker; 2560 if (++marker > 'z') 2561 marker = 'a'; 2562 } 2563 } 2564 uploadBytes[kMsgSize] = '\0'; 2565 2566 for (int i = 0; i < kIterations; ++i) { 2567 TestDelegate d; 2568 URLRequest r( 2569 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); 2570 r.set_method(method.c_str()); 2571 2572 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes))); 2573 2574 r.Start(); 2575 EXPECT_TRUE(r.is_pending()); 2576 2577 base::RunLoop().Run(); 2578 2579 ASSERT_EQ(1, d.response_started_count()) 2580 << "request failed: " << r.status().status() 2581 << ", os error: " << r.status().error(); 2582 2583 EXPECT_FALSE(d.received_data_before_response()); 2584 EXPECT_EQ(uploadBytes, d.data_received()); 2585 } 2586 delete[] uploadBytes; 2587 } 2588 2589 void AddChunksToUpload(URLRequest* r) { 2590 r->AppendChunkToUpload("a", 1, false); 2591 r->AppendChunkToUpload("bcd", 3, false); 2592 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); 2593 r->AppendChunkToUpload("\r\n\r\n", 4, false); 2594 r->AppendChunkToUpload("0", 1, false); 2595 r->AppendChunkToUpload("2323", 4, true); 2596 } 2597 2598 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { 2599 // This should match the chunks sent by AddChunksToUpload(). 2600 const std::string expected_data = 2601 "abcdthis is a longer chunk than before.\r\n\r\n02323"; 2602 2603 ASSERT_EQ(1, d->response_started_count()) 2604 << "request failed: " << r->status().status() 2605 << ", os error: " << r->status().error(); 2606 2607 EXPECT_FALSE(d->received_data_before_response()); 2608 2609 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); 2610 EXPECT_EQ(expected_data, d->data_received()); 2611 } 2612 2613 bool DoManyCookiesRequest(int num_cookies) { 2614 TestDelegate d; 2615 URLRequest r(test_server_.GetURL("set-many-cookies?" + 2616 base::IntToString(num_cookies)), 2617 DEFAULT_PRIORITY, 2618 &d, 2619 &default_context_); 2620 2621 r.Start(); 2622 EXPECT_TRUE(r.is_pending()); 2623 2624 base::RunLoop().Run(); 2625 2626 bool is_success = r.status().is_success(); 2627 2628 if (!is_success) { 2629 // Requests handled by ChromeFrame send a less precise error message, 2630 // ERR_CONNECTION_ABORTED. 2631 EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG || 2632 r.status().error() == ERR_CONNECTION_ABORTED); 2633 // The test server appears to be unable to handle subsequent requests 2634 // after this error is triggered. Force it to restart. 2635 EXPECT_TRUE(test_server_.Stop()); 2636 EXPECT_TRUE(test_server_.Start()); 2637 } 2638 2639 return is_success; 2640 } 2641 2642 LocalHttpTestServer test_server_; 2643}; 2644 2645// In this unit test, we're using the HTTPTestServer as a proxy server and 2646// issuing a CONNECT request with the magic host name "www.redirect.com". 2647// The HTTPTestServer will return a 302 response, which we should not 2648// follow. 2649TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { 2650 ASSERT_TRUE(test_server_.Start()); 2651 2652 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 2653 TestURLRequestContextWithProxy context( 2654 test_server_.host_port_pair().ToString(), &network_delegate); 2655 2656 TestDelegate d; 2657 { 2658 URLRequest r( 2659 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context); 2660 r.Start(); 2661 EXPECT_TRUE(r.is_pending()); 2662 2663 base::RunLoop().Run(); 2664 2665 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2666 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); 2667 EXPECT_EQ(1, d.response_started_count()); 2668 // We should not have followed the redirect. 2669 EXPECT_EQ(0, d.received_redirect_count()); 2670 } 2671} 2672 2673// This is the same as the previous test, but checks that the network delegate 2674// registers the error. 2675TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { 2676 ASSERT_TRUE(test_server_.Start()); 2677 2678 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 2679 TestURLRequestContextWithProxy context( 2680 test_server_.host_port_pair().ToString(), &network_delegate); 2681 2682 TestDelegate d; 2683 { 2684 URLRequest r( 2685 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context); 2686 r.Start(); 2687 EXPECT_TRUE(r.is_pending()); 2688 2689 base::RunLoop().Run(); 2690 2691 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2692 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); 2693 EXPECT_EQ(1, d.response_started_count()); 2694 // We should not have followed the redirect. 2695 EXPECT_EQ(0, d.received_redirect_count()); 2696 2697 EXPECT_EQ(1, network_delegate.error_count()); 2698 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); 2699 } 2700} 2701 2702// Tests that we can block and asynchronously return OK in various stages. 2703TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { 2704 static const BlockingNetworkDelegate::Stage blocking_stages[] = { 2705 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, 2706 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, 2707 BlockingNetworkDelegate::ON_HEADERS_RECEIVED 2708 }; 2709 static const size_t blocking_stages_length = arraysize(blocking_stages); 2710 2711 ASSERT_TRUE(test_server_.Start()); 2712 2713 TestDelegate d; 2714 BlockingNetworkDelegate network_delegate( 2715 BlockingNetworkDelegate::USER_CALLBACK); 2716 network_delegate.set_block_on( 2717 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | 2718 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | 2719 BlockingNetworkDelegate::ON_HEADERS_RECEIVED); 2720 2721 TestURLRequestContext context(true); 2722 context.set_network_delegate(&network_delegate); 2723 context.Init(); 2724 2725 { 2726 URLRequest r( 2727 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, &context); 2728 2729 r.Start(); 2730 for (size_t i = 0; i < blocking_stages_length; ++i) { 2731 base::RunLoop().Run(); 2732 EXPECT_EQ(blocking_stages[i], 2733 network_delegate.stage_blocked_for_callback()); 2734 network_delegate.DoCallback(OK); 2735 } 2736 base::RunLoop().Run(); 2737 EXPECT_EQ(200, r.GetResponseCode()); 2738 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2739 EXPECT_EQ(1, network_delegate.created_requests()); 2740 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2741 } 2742 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2743} 2744 2745// Tests that the network delegate can block and cancel a request. 2746TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { 2747 ASSERT_TRUE(test_server_.Start()); 2748 2749 TestDelegate d; 2750 BlockingNetworkDelegate network_delegate( 2751 BlockingNetworkDelegate::AUTO_CALLBACK); 2752 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); 2753 network_delegate.set_retval(ERR_EMPTY_RESPONSE); 2754 2755 TestURLRequestContextWithProxy context( 2756 test_server_.host_port_pair().ToString(), &network_delegate); 2757 2758 { 2759 URLRequest r( 2760 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); 2761 2762 r.Start(); 2763 base::RunLoop().Run(); 2764 2765 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2766 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); 2767 EXPECT_EQ(1, network_delegate.created_requests()); 2768 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2769 } 2770 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2771} 2772 2773// Helper function for NetworkDelegateCancelRequestAsynchronously and 2774// NetworkDelegateCancelRequestSynchronously. Sets up a blocking network 2775// delegate operating in |block_mode| and a request for |url|. It blocks the 2776// request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. 2777void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, 2778 BlockingNetworkDelegate::Stage stage, 2779 const GURL& url) { 2780 TestDelegate d; 2781 BlockingNetworkDelegate network_delegate(block_mode); 2782 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); 2783 network_delegate.set_block_on(stage); 2784 2785 TestURLRequestContext context(true); 2786 context.set_network_delegate(&network_delegate); 2787 context.Init(); 2788 2789 { 2790 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 2791 2792 r.Start(); 2793 base::RunLoop().Run(); 2794 2795 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 2796 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error()); 2797 EXPECT_EQ(1, network_delegate.created_requests()); 2798 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2799 } 2800 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2801} 2802 2803// The following 3 tests check that the network delegate can cancel a request 2804// synchronously in various stages of the request. 2805TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { 2806 ASSERT_TRUE(test_server_.Start()); 2807 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, 2808 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, 2809 test_server_.GetURL(std::string())); 2810} 2811 2812TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { 2813 ASSERT_TRUE(test_server_.Start()); 2814 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, 2815 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, 2816 test_server_.GetURL(std::string())); 2817} 2818 2819TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { 2820 ASSERT_TRUE(test_server_.Start()); 2821 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, 2822 BlockingNetworkDelegate::ON_HEADERS_RECEIVED, 2823 test_server_.GetURL(std::string())); 2824} 2825 2826// The following 3 tests check that the network delegate can cancel a request 2827// asynchronously in various stages of the request. 2828TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { 2829 ASSERT_TRUE(test_server_.Start()); 2830 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, 2831 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, 2832 test_server_.GetURL(std::string())); 2833} 2834 2835TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { 2836 ASSERT_TRUE(test_server_.Start()); 2837 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, 2838 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, 2839 test_server_.GetURL(std::string())); 2840} 2841 2842TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { 2843 ASSERT_TRUE(test_server_.Start()); 2844 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, 2845 BlockingNetworkDelegate::ON_HEADERS_RECEIVED, 2846 test_server_.GetURL(std::string())); 2847} 2848 2849// Tests that the network delegate can block and redirect a request to a new 2850// URL. 2851TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { 2852 ASSERT_TRUE(test_server_.Start()); 2853 2854 TestDelegate d; 2855 BlockingNetworkDelegate network_delegate( 2856 BlockingNetworkDelegate::AUTO_CALLBACK); 2857 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); 2858 GURL redirect_url(test_server_.GetURL("simple.html")); 2859 network_delegate.set_redirect_url(redirect_url); 2860 2861 TestURLRequestContextWithProxy context( 2862 test_server_.host_port_pair().ToString(), &network_delegate); 2863 2864 { 2865 GURL original_url(test_server_.GetURL("empty.html")); 2866 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context); 2867 2868 r.Start(); 2869 base::RunLoop().Run(); 2870 2871 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2872 EXPECT_EQ(0, r.status().error()); 2873 EXPECT_EQ(redirect_url, r.url()); 2874 EXPECT_EQ(original_url, r.original_url()); 2875 EXPECT_EQ(2U, r.url_chain().size()); 2876 EXPECT_EQ(1, network_delegate.created_requests()); 2877 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2878 } 2879 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2880} 2881 2882// Tests that the network delegate can block and redirect a request to a new 2883// URL by setting a redirect_url and returning in OnBeforeURLRequest directly. 2884TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { 2885 ASSERT_TRUE(test_server_.Start()); 2886 2887 TestDelegate d; 2888 BlockingNetworkDelegate network_delegate( 2889 BlockingNetworkDelegate::SYNCHRONOUS); 2890 GURL redirect_url(test_server_.GetURL("simple.html")); 2891 network_delegate.set_redirect_url(redirect_url); 2892 2893 TestURLRequestContextWithProxy context( 2894 test_server_.host_port_pair().ToString(), &network_delegate); 2895 2896 { 2897 GURL original_url(test_server_.GetURL("empty.html")); 2898 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context); 2899 2900 r.Start(); 2901 base::RunLoop().Run(); 2902 2903 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2904 EXPECT_EQ(0, r.status().error()); 2905 EXPECT_EQ(redirect_url, r.url()); 2906 EXPECT_EQ(original_url, r.original_url()); 2907 EXPECT_EQ(2U, r.url_chain().size()); 2908 EXPECT_EQ(1, network_delegate.created_requests()); 2909 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2910 } 2911 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2912} 2913 2914// Tests that redirects caused by the network delegate preserve POST data. 2915TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { 2916 ASSERT_TRUE(test_server_.Start()); 2917 2918 const char kData[] = "hello world"; 2919 2920 TestDelegate d; 2921 BlockingNetworkDelegate network_delegate( 2922 BlockingNetworkDelegate::AUTO_CALLBACK); 2923 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); 2924 GURL redirect_url(test_server_.GetURL("echo")); 2925 network_delegate.set_redirect_url(redirect_url); 2926 2927 TestURLRequestContext context(true); 2928 context.set_network_delegate(&network_delegate); 2929 context.Init(); 2930 2931 { 2932 GURL original_url(test_server_.GetURL("empty.html")); 2933 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context); 2934 r.set_method("POST"); 2935 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); 2936 HttpRequestHeaders headers; 2937 headers.SetHeader(HttpRequestHeaders::kContentLength, 2938 base::UintToString(arraysize(kData) - 1)); 2939 r.SetExtraRequestHeaders(headers); 2940 r.Start(); 2941 base::RunLoop().Run(); 2942 2943 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2944 EXPECT_EQ(0, r.status().error()); 2945 EXPECT_EQ(redirect_url, r.url()); 2946 EXPECT_EQ(original_url, r.original_url()); 2947 EXPECT_EQ(2U, r.url_chain().size()); 2948 EXPECT_EQ(1, network_delegate.created_requests()); 2949 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2950 EXPECT_EQ("POST", r.method()); 2951 EXPECT_EQ(kData, d.data_received()); 2952 } 2953 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2954} 2955 2956// Tests that the network delegate can synchronously complete OnAuthRequired 2957// by taking no action. This indicates that the NetworkDelegate does not want to 2958// handle the challenge, and is passing the buck along to the 2959// URLRequest::Delegate. 2960TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { 2961 ASSERT_TRUE(test_server_.Start()); 2962 2963 TestDelegate d; 2964 BlockingNetworkDelegate network_delegate( 2965 BlockingNetworkDelegate::SYNCHRONOUS); 2966 2967 TestURLRequestContext context(true); 2968 context.set_network_delegate(&network_delegate); 2969 context.Init(); 2970 2971 d.set_credentials(AuthCredentials(kUser, kSecret)); 2972 2973 { 2974 GURL url(test_server_.GetURL("auth-basic")); 2975 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 2976 r.Start(); 2977 2978 base::RunLoop().Run(); 2979 2980 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 2981 EXPECT_EQ(0, r.status().error()); 2982 EXPECT_EQ(200, r.GetResponseCode()); 2983 EXPECT_TRUE(d.auth_required_called()); 2984 EXPECT_EQ(1, network_delegate.created_requests()); 2985 EXPECT_EQ(0, network_delegate.destroyed_requests()); 2986 } 2987 EXPECT_EQ(1, network_delegate.destroyed_requests()); 2988} 2989 2990TEST_F(URLRequestTestHTTP, 2991 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { 2992 ASSERT_TRUE(test_server_.Start()); 2993 2994 TestDelegate d; 2995 BlockingNetworkDelegate network_delegate( 2996 BlockingNetworkDelegate::SYNCHRONOUS); 2997 2998 TestURLRequestContext context(true); 2999 context.set_network_delegate(&network_delegate); 3000 context.Init(); 3001 3002 d.set_credentials(AuthCredentials(kUser, kSecret)); 3003 3004 { 3005 GURL url(test_server_.GetURL("auth-basic")); 3006 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 3007 r.Start(); 3008 3009 { 3010 HttpRequestHeaders headers; 3011 EXPECT_TRUE(r.GetFullRequestHeaders(&headers)); 3012 EXPECT_FALSE(headers.HasHeader("Authorization")); 3013 } 3014 3015 base::RunLoop().Run(); 3016 3017 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 3018 EXPECT_EQ(0, r.status().error()); 3019 EXPECT_EQ(200, r.GetResponseCode()); 3020 EXPECT_TRUE(d.auth_required_called()); 3021 EXPECT_EQ(1, network_delegate.created_requests()); 3022 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3023 } 3024 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3025} 3026 3027// Tests that the network delegate can synchronously complete OnAuthRequired 3028// by setting credentials. 3029TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { 3030 ASSERT_TRUE(test_server_.Start()); 3031 3032 TestDelegate d; 3033 BlockingNetworkDelegate network_delegate( 3034 BlockingNetworkDelegate::SYNCHRONOUS); 3035 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); 3036 network_delegate.set_auth_retval( 3037 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 3038 3039 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); 3040 3041 TestURLRequestContext context(true); 3042 context.set_network_delegate(&network_delegate); 3043 context.Init(); 3044 3045 { 3046 GURL url(test_server_.GetURL("auth-basic")); 3047 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 3048 r.Start(); 3049 base::RunLoop().Run(); 3050 3051 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 3052 EXPECT_EQ(0, r.status().error()); 3053 EXPECT_EQ(200, r.GetResponseCode()); 3054 EXPECT_FALSE(d.auth_required_called()); 3055 EXPECT_EQ(1, network_delegate.created_requests()); 3056 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3057 } 3058 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3059} 3060 3061// Same as above, but also tests that GetFullRequestHeaders returns the proper 3062// headers (for the first or second request) when called at the proper times. 3063TEST_F(URLRequestTestHTTP, 3064 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { 3065 ASSERT_TRUE(test_server_.Start()); 3066 3067 TestDelegate d; 3068 BlockingNetworkDelegate network_delegate( 3069 BlockingNetworkDelegate::SYNCHRONOUS); 3070 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); 3071 network_delegate.set_auth_retval( 3072 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 3073 3074 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); 3075 3076 TestURLRequestContext context(true); 3077 context.set_network_delegate(&network_delegate); 3078 context.Init(); 3079 3080 { 3081 GURL url(test_server_.GetURL("auth-basic")); 3082 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 3083 r.Start(); 3084 base::RunLoop().Run(); 3085 3086 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 3087 EXPECT_EQ(0, r.status().error()); 3088 EXPECT_EQ(200, r.GetResponseCode()); 3089 EXPECT_FALSE(d.auth_required_called()); 3090 EXPECT_EQ(1, network_delegate.created_requests()); 3091 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3092 3093 { 3094 HttpRequestHeaders headers; 3095 EXPECT_TRUE(r.GetFullRequestHeaders(&headers)); 3096 EXPECT_TRUE(headers.HasHeader("Authorization")); 3097 } 3098 } 3099 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3100} 3101 3102// Tests that the network delegate can synchronously complete OnAuthRequired 3103// by cancelling authentication. 3104TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { 3105 ASSERT_TRUE(test_server_.Start()); 3106 3107 TestDelegate d; 3108 BlockingNetworkDelegate network_delegate( 3109 BlockingNetworkDelegate::SYNCHRONOUS); 3110 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); 3111 network_delegate.set_auth_retval( 3112 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); 3113 3114 TestURLRequestContext context(true); 3115 context.set_network_delegate(&network_delegate); 3116 context.Init(); 3117 3118 { 3119 GURL url(test_server_.GetURL("auth-basic")); 3120 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 3121 r.Start(); 3122 base::RunLoop().Run(); 3123 3124 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 3125 EXPECT_EQ(OK, r.status().error()); 3126 EXPECT_EQ(401, r.GetResponseCode()); 3127 EXPECT_FALSE(d.auth_required_called()); 3128 EXPECT_EQ(1, network_delegate.created_requests()); 3129 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3130 } 3131 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3132} 3133 3134// Tests that the network delegate can asynchronously complete OnAuthRequired 3135// by taking no action. This indicates that the NetworkDelegate does not want 3136// to handle the challenge, and is passing the buck along to the 3137// URLRequest::Delegate. 3138TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { 3139 ASSERT_TRUE(test_server_.Start()); 3140 3141 TestDelegate d; 3142 BlockingNetworkDelegate network_delegate( 3143 BlockingNetworkDelegate::AUTO_CALLBACK); 3144 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); 3145 3146 TestURLRequestContext context(true); 3147 context.set_network_delegate(&network_delegate); 3148 context.Init(); 3149 3150 d.set_credentials(AuthCredentials(kUser, kSecret)); 3151 3152 { 3153 GURL url(test_server_.GetURL("auth-basic")); 3154 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 3155 r.Start(); 3156 base::RunLoop().Run(); 3157 3158 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 3159 EXPECT_EQ(0, r.status().error()); 3160 EXPECT_EQ(200, r.GetResponseCode()); 3161 EXPECT_TRUE(d.auth_required_called()); 3162 EXPECT_EQ(1, network_delegate.created_requests()); 3163 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3164 } 3165 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3166} 3167 3168// Tests that the network delegate can asynchronously complete OnAuthRequired 3169// by setting credentials. 3170TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { 3171 ASSERT_TRUE(test_server_.Start()); 3172 3173 TestDelegate d; 3174 BlockingNetworkDelegate network_delegate( 3175 BlockingNetworkDelegate::AUTO_CALLBACK); 3176 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); 3177 network_delegate.set_auth_retval( 3178 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 3179 3180 AuthCredentials auth_credentials(kUser, kSecret); 3181 network_delegate.set_auth_credentials(auth_credentials); 3182 3183 TestURLRequestContext context(true); 3184 context.set_network_delegate(&network_delegate); 3185 context.Init(); 3186 3187 { 3188 GURL url(test_server_.GetURL("auth-basic")); 3189 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 3190 r.Start(); 3191 base::RunLoop().Run(); 3192 3193 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 3194 EXPECT_EQ(0, r.status().error()); 3195 3196 EXPECT_EQ(200, r.GetResponseCode()); 3197 EXPECT_FALSE(d.auth_required_called()); 3198 EXPECT_EQ(1, network_delegate.created_requests()); 3199 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3200 } 3201 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3202} 3203 3204// Tests that the network delegate can asynchronously complete OnAuthRequired 3205// by cancelling authentication. 3206TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { 3207 ASSERT_TRUE(test_server_.Start()); 3208 3209 TestDelegate d; 3210 BlockingNetworkDelegate network_delegate( 3211 BlockingNetworkDelegate::AUTO_CALLBACK); 3212 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); 3213 network_delegate.set_auth_retval( 3214 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); 3215 3216 TestURLRequestContext context(true); 3217 context.set_network_delegate(&network_delegate); 3218 context.Init(); 3219 3220 { 3221 GURL url(test_server_.GetURL("auth-basic")); 3222 URLRequest r(url, DEFAULT_PRIORITY, &d, &context); 3223 r.Start(); 3224 base::RunLoop().Run(); 3225 3226 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 3227 EXPECT_EQ(OK, r.status().error()); 3228 EXPECT_EQ(401, r.GetResponseCode()); 3229 EXPECT_FALSE(d.auth_required_called()); 3230 EXPECT_EQ(1, network_delegate.created_requests()); 3231 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3232 } 3233 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3234} 3235 3236// Tests that we can handle when a network request was canceled while we were 3237// waiting for the network delegate. 3238// Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. 3239TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { 3240 ASSERT_TRUE(test_server_.Start()); 3241 3242 TestDelegate d; 3243 BlockingNetworkDelegate network_delegate( 3244 BlockingNetworkDelegate::USER_CALLBACK); 3245 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); 3246 3247 TestURLRequestContext context(true); 3248 context.set_network_delegate(&network_delegate); 3249 context.Init(); 3250 3251 { 3252 URLRequest r( 3253 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); 3254 3255 r.Start(); 3256 base::RunLoop().Run(); 3257 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, 3258 network_delegate.stage_blocked_for_callback()); 3259 EXPECT_EQ(0, network_delegate.completed_requests()); 3260 // Cancel before callback. 3261 r.Cancel(); 3262 // Ensure that network delegate is notified. 3263 EXPECT_EQ(1, network_delegate.completed_requests()); 3264 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 3265 EXPECT_EQ(ERR_ABORTED, r.status().error()); 3266 EXPECT_EQ(1, network_delegate.created_requests()); 3267 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3268 } 3269 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3270} 3271 3272// Tests that we can handle when a network request was canceled while we were 3273// waiting for the network delegate. 3274// Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. 3275TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { 3276 ASSERT_TRUE(test_server_.Start()); 3277 3278 TestDelegate d; 3279 BlockingNetworkDelegate network_delegate( 3280 BlockingNetworkDelegate::USER_CALLBACK); 3281 network_delegate.set_block_on( 3282 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); 3283 3284 TestURLRequestContext context(true); 3285 context.set_network_delegate(&network_delegate); 3286 context.Init(); 3287 3288 { 3289 URLRequest r( 3290 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); 3291 3292 r.Start(); 3293 base::RunLoop().Run(); 3294 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, 3295 network_delegate.stage_blocked_for_callback()); 3296 EXPECT_EQ(0, network_delegate.completed_requests()); 3297 // Cancel before callback. 3298 r.Cancel(); 3299 // Ensure that network delegate is notified. 3300 EXPECT_EQ(1, network_delegate.completed_requests()); 3301 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 3302 EXPECT_EQ(ERR_ABORTED, r.status().error()); 3303 EXPECT_EQ(1, network_delegate.created_requests()); 3304 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3305 } 3306 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3307} 3308 3309// Tests that we can handle when a network request was canceled while we were 3310// waiting for the network delegate. 3311// Part 3: Request is cancelled while waiting for OnHeadersReceived callback. 3312TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { 3313 ASSERT_TRUE(test_server_.Start()); 3314 3315 TestDelegate d; 3316 BlockingNetworkDelegate network_delegate( 3317 BlockingNetworkDelegate::USER_CALLBACK); 3318 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); 3319 3320 TestURLRequestContext context(true); 3321 context.set_network_delegate(&network_delegate); 3322 context.Init(); 3323 3324 { 3325 URLRequest r( 3326 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); 3327 3328 r.Start(); 3329 base::RunLoop().Run(); 3330 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, 3331 network_delegate.stage_blocked_for_callback()); 3332 EXPECT_EQ(0, network_delegate.completed_requests()); 3333 // Cancel before callback. 3334 r.Cancel(); 3335 // Ensure that network delegate is notified. 3336 EXPECT_EQ(1, network_delegate.completed_requests()); 3337 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 3338 EXPECT_EQ(ERR_ABORTED, r.status().error()); 3339 EXPECT_EQ(1, network_delegate.created_requests()); 3340 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3341 } 3342 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3343} 3344 3345// Tests that we can handle when a network request was canceled while we were 3346// waiting for the network delegate. 3347// Part 4: Request is cancelled while waiting for OnAuthRequired callback. 3348TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { 3349 ASSERT_TRUE(test_server_.Start()); 3350 3351 TestDelegate d; 3352 BlockingNetworkDelegate network_delegate( 3353 BlockingNetworkDelegate::USER_CALLBACK); 3354 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); 3355 3356 TestURLRequestContext context(true); 3357 context.set_network_delegate(&network_delegate); 3358 context.Init(); 3359 3360 { 3361 URLRequest r( 3362 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, &context); 3363 3364 r.Start(); 3365 base::RunLoop().Run(); 3366 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, 3367 network_delegate.stage_blocked_for_callback()); 3368 EXPECT_EQ(0, network_delegate.completed_requests()); 3369 // Cancel before callback. 3370 r.Cancel(); 3371 // Ensure that network delegate is notified. 3372 EXPECT_EQ(1, network_delegate.completed_requests()); 3373 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 3374 EXPECT_EQ(ERR_ABORTED, r.status().error()); 3375 EXPECT_EQ(1, network_delegate.created_requests()); 3376 EXPECT_EQ(0, network_delegate.destroyed_requests()); 3377 } 3378 EXPECT_EQ(1, network_delegate.destroyed_requests()); 3379} 3380 3381// In this unit test, we're using the HTTPTestServer as a proxy server and 3382// issuing a CONNECT request with the magic host name "www.server-auth.com". 3383// The HTTPTestServer will return a 401 response, which we should balk at. 3384TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { 3385 ASSERT_TRUE(test_server_.Start()); 3386 3387 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 3388 TestURLRequestContextWithProxy context( 3389 test_server_.host_port_pair().ToString(), &network_delegate); 3390 3391 TestDelegate d; 3392 { 3393 URLRequest r( 3394 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, &context); 3395 3396 r.Start(); 3397 EXPECT_TRUE(r.is_pending()); 3398 3399 base::RunLoop().Run(); 3400 3401 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 3402 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); 3403 } 3404} 3405 3406TEST_F(URLRequestTestHTTP, GetTest_NoCache) { 3407 ASSERT_TRUE(test_server_.Start()); 3408 3409 TestDelegate d; 3410 { 3411 URLRequest r(test_server_.GetURL(std::string()), 3412 DEFAULT_PRIORITY, 3413 &d, 3414 &default_context_); 3415 3416 r.Start(); 3417 EXPECT_TRUE(r.is_pending()); 3418 3419 base::RunLoop().Run(); 3420 3421 EXPECT_EQ(1, d.response_started_count()); 3422 EXPECT_FALSE(d.received_data_before_response()); 3423 EXPECT_NE(0, d.bytes_received()); 3424 EXPECT_EQ(test_server_.host_port_pair().host(), 3425 r.GetSocketAddress().host()); 3426 EXPECT_EQ(test_server_.host_port_pair().port(), 3427 r.GetSocketAddress().port()); 3428 3429 // TODO(eroman): Add back the NetLog tests... 3430 } 3431} 3432 3433// This test has the server send a large number of cookies to the client. 3434// To ensure that no number of cookies causes a crash, a galloping binary 3435// search is used to estimate that maximum number of cookies that are accepted 3436// by the browser. Beyond the maximum number, the request will fail with 3437// ERR_RESPONSE_HEADERS_TOO_BIG. 3438#if defined(OS_WIN) 3439// http://crbug.com/177916 3440#define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies 3441#else 3442#define MAYBE_GetTest_ManyCookies GetTest_ManyCookies 3443#endif // defined(OS_WIN) 3444TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { 3445 ASSERT_TRUE(test_server_.Start()); 3446 3447 int lower_bound = 0; 3448 int upper_bound = 1; 3449 3450 // Double the number of cookies until the response header limits are 3451 // exceeded. 3452 while (DoManyCookiesRequest(upper_bound)) { 3453 lower_bound = upper_bound; 3454 upper_bound *= 2; 3455 ASSERT_LT(upper_bound, 1000000); 3456 } 3457 3458 int tolerance = upper_bound * 0.005; 3459 if (tolerance < 2) 3460 tolerance = 2; 3461 3462 // Perform a binary search to find the highest possible number of cookies, 3463 // within the desired tolerance. 3464 while (upper_bound - lower_bound >= tolerance) { 3465 int num_cookies = (lower_bound + upper_bound) / 2; 3466 3467 if (DoManyCookiesRequest(num_cookies)) 3468 lower_bound = num_cookies; 3469 else 3470 upper_bound = num_cookies; 3471 } 3472 // Success: the test did not crash. 3473} 3474 3475TEST_F(URLRequestTestHTTP, GetTest) { 3476 ASSERT_TRUE(test_server_.Start()); 3477 3478 TestDelegate d; 3479 { 3480 URLRequest r(test_server_.GetURL(std::string()), 3481 DEFAULT_PRIORITY, 3482 &d, 3483 &default_context_); 3484 3485 r.Start(); 3486 EXPECT_TRUE(r.is_pending()); 3487 3488 base::RunLoop().Run(); 3489 3490 EXPECT_EQ(1, d.response_started_count()); 3491 EXPECT_FALSE(d.received_data_before_response()); 3492 EXPECT_NE(0, d.bytes_received()); 3493 EXPECT_EQ(test_server_.host_port_pair().host(), 3494 r.GetSocketAddress().host()); 3495 EXPECT_EQ(test_server_.host_port_pair().port(), 3496 r.GetSocketAddress().port()); 3497 } 3498} 3499 3500TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { 3501 ASSERT_TRUE(test_server_.Start()); 3502 3503 TestDelegate d; 3504 { 3505 GURL test_url(test_server_.GetURL(std::string())); 3506 URLRequest r(test_url, DEFAULT_PRIORITY, &d, &default_context_); 3507 3508 HttpRequestHeaders headers; 3509 EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); 3510 3511 r.Start(); 3512 EXPECT_TRUE(r.is_pending()); 3513 3514 base::RunLoop().Run(); 3515 3516 EXPECT_EQ(1, d.response_started_count()); 3517 EXPECT_FALSE(d.received_data_before_response()); 3518 EXPECT_NE(0, d.bytes_received()); 3519 EXPECT_EQ(test_server_.host_port_pair().host(), 3520 r.GetSocketAddress().host()); 3521 EXPECT_EQ(test_server_.host_port_pair().port(), 3522 r.GetSocketAddress().port()); 3523 3524 EXPECT_TRUE(d.have_full_request_headers()); 3525 CheckFullRequestHeaders(d.full_request_headers(), test_url); 3526 } 3527} 3528 3529TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { 3530 ASSERT_TRUE(test_server_.Start()); 3531 3532 TestDelegate d; 3533 { 3534 URLRequest r(test_server_.GetURL(std::string()), 3535 DEFAULT_PRIORITY, 3536 &d, 3537 &default_context_); 3538 3539 r.Start(); 3540 EXPECT_TRUE(r.is_pending()); 3541 3542 base::RunLoop().Run(); 3543 3544 LoadTimingInfo load_timing_info; 3545 r.GetLoadTimingInfo(&load_timing_info); 3546 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); 3547 3548 EXPECT_EQ(1, d.response_started_count()); 3549 EXPECT_FALSE(d.received_data_before_response()); 3550 EXPECT_NE(0, d.bytes_received()); 3551 EXPECT_EQ(test_server_.host_port_pair().host(), 3552 r.GetSocketAddress().host()); 3553 EXPECT_EQ(test_server_.host_port_pair().port(), 3554 r.GetSocketAddress().port()); 3555 } 3556} 3557 3558TEST_F(URLRequestTestHTTP, GetZippedTest) { 3559 ASSERT_TRUE(test_server_.Start()); 3560 3561 // Parameter that specifies the Content-Length field in the response: 3562 // C - Compressed length. 3563 // U - Uncompressed length. 3564 // L - Large length (larger than both C & U). 3565 // M - Medium length (between C & U). 3566 // S - Small length (smaller than both C & U). 3567 const char test_parameters[] = "CULMS"; 3568 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. 3569 // C & U should be OK. 3570 // L & M are larger than the data sent, and show an error. 3571 // S has too little data, but we seem to accept it. 3572 const bool test_expect_success[num_tests] = 3573 { true, true, false, false, true }; 3574 3575 for (int i = 0; i < num_tests ; i++) { 3576 TestDelegate d; 3577 { 3578 std::string test_file = 3579 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c", 3580 test_parameters[i]); 3581 3582 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 3583 TestURLRequestContext context(true); 3584 context.set_network_delegate(&network_delegate); 3585 context.Init(); 3586 3587 URLRequest r( 3588 test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, &context); 3589 r.Start(); 3590 EXPECT_TRUE(r.is_pending()); 3591 3592 base::RunLoop().Run(); 3593 3594 EXPECT_EQ(1, d.response_started_count()); 3595 EXPECT_FALSE(d.received_data_before_response()); 3596 VLOG(1) << " Received " << d.bytes_received() << " bytes" 3597 << " status = " << r.status().status() 3598 << " error = " << r.status().error(); 3599 if (test_expect_success[i]) { 3600 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()) 3601 << " Parameter = \"" << test_file << "\""; 3602 } else { 3603 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 3604 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error()) 3605 << " Parameter = \"" << test_file << "\""; 3606 } 3607 } 3608 } 3609} 3610 3611TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) { 3612 ASSERT_TRUE(test_server_.Start()); 3613 3614 SpawnedTestServer https_test_server( 3615 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost, 3616 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 3617 ASSERT_TRUE(https_test_server.Start()); 3618 3619 // An https server is sent a request with an https referer, 3620 // and responds with a redirect to an http url. The http 3621 // server should not be sent the referer. 3622 GURL http_destination = test_server_.GetURL(std::string()); 3623 TestDelegate d; 3624 URLRequest req( 3625 https_test_server.GetURL("server-redirect?" + http_destination.spec()), 3626 DEFAULT_PRIORITY, 3627 &d, 3628 &default_context_); 3629 req.SetReferrer("https://www.referrer.com/"); 3630 req.Start(); 3631 base::RunLoop().Run(); 3632 3633 EXPECT_EQ(1, d.response_started_count()); 3634 EXPECT_EQ(1, d.received_redirect_count()); 3635 EXPECT_EQ(http_destination, req.url()); 3636 EXPECT_EQ(std::string(), req.referrer()); 3637} 3638 3639TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { 3640 ASSERT_TRUE(test_server_.Start()); 3641 3642 GURL destination_url = test_server_.GetURL(std::string()); 3643 GURL original_url = 3644 test_server_.GetURL("server-redirect?" + destination_url.spec()); 3645 TestDelegate d; 3646 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_); 3647 req.Start(); 3648 base::RunLoop().Run(); 3649 3650 EXPECT_EQ(1, d.response_started_count()); 3651 EXPECT_EQ(1, d.received_redirect_count()); 3652 EXPECT_EQ(destination_url, req.url()); 3653 EXPECT_EQ(original_url, req.original_url()); 3654 ASSERT_EQ(2U, req.url_chain().size()); 3655 EXPECT_EQ(original_url, req.url_chain()[0]); 3656 EXPECT_EQ(destination_url, req.url_chain()[1]); 3657 3658 LoadTimingInfo load_timing_info_before_redirect; 3659 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( 3660 &load_timing_info_before_redirect)); 3661 TestLoadTimingNotReused(load_timing_info_before_redirect, 3662 CONNECT_TIMING_HAS_DNS_TIMES); 3663 3664 LoadTimingInfo load_timing_info; 3665 req.GetLoadTimingInfo(&load_timing_info); 3666 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); 3667 3668 // Check that a new socket was used on redirect, since the server does not 3669 // supposed keep-alive sockets, and that the times before the redirect are 3670 // before the ones recorded for the second request. 3671 EXPECT_NE(load_timing_info_before_redirect.socket_log_id, 3672 load_timing_info.socket_log_id); 3673 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, 3674 load_timing_info.connect_timing.connect_start); 3675} 3676 3677TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { 3678 ASSERT_TRUE(test_server_.Start()); 3679 3680 GURL destination_url = test_server_.GetURL(std::string()); 3681 GURL middle_redirect_url = 3682 test_server_.GetURL("server-redirect?" + destination_url.spec()); 3683 GURL original_url = test_server_.GetURL( 3684 "server-redirect?" + middle_redirect_url.spec()); 3685 TestDelegate d; 3686 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_); 3687 req.Start(); 3688 base::RunLoop().Run(); 3689 3690 EXPECT_EQ(1, d.response_started_count()); 3691 EXPECT_EQ(2, d.received_redirect_count()); 3692 EXPECT_EQ(destination_url, req.url()); 3693 EXPECT_EQ(original_url, req.original_url()); 3694 ASSERT_EQ(3U, req.url_chain().size()); 3695 EXPECT_EQ(original_url, req.url_chain()[0]); 3696 EXPECT_EQ(middle_redirect_url, req.url_chain()[1]); 3697 EXPECT_EQ(destination_url, req.url_chain()[2]); 3698} 3699 3700// First and second pieces of information logged by delegates to URLRequests. 3701const char kFirstDelegateInfo[] = "Wonderful delegate"; 3702const char kSecondDelegateInfo[] = "Exciting delegate"; 3703 3704// Logs delegate information to a URLRequest. The first string is logged 3705// synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is 3706// logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then 3707// another asynchronous call is used to clear the delegate information 3708// before calling a callback. The object then deletes itself. 3709class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { 3710 public: 3711 typedef base::Callback<void()> Callback; 3712 3713 // Each time delegate information is added to the URLRequest, the resulting 3714 // load state is checked. The expected load state after each request is 3715 // passed in as an argument. 3716 static void Run(URLRequest* url_request, 3717 LoadState expected_first_load_state, 3718 LoadState expected_second_load_state, 3719 LoadState expected_third_load_state, 3720 const Callback& callback) { 3721 AsyncDelegateLogger* logger = new AsyncDelegateLogger( 3722 url_request, 3723 expected_first_load_state, 3724 expected_second_load_state, 3725 expected_third_load_state, 3726 callback); 3727 logger->Start(); 3728 } 3729 3730 // Checks that the log entries, starting with log_position, contain the 3731 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have 3732 // recorded. Returns the index of entry after the expected number of 3733 // events this logged, or entries.size() if there aren't enough entries. 3734 static size_t CheckDelegateInfo( 3735 const CapturingNetLog::CapturedEntryList& entries, size_t log_position) { 3736 // There should be 4 DELEGATE_INFO events: Two begins and two ends. 3737 if (log_position + 3 >= entries.size()) { 3738 ADD_FAILURE() << "Not enough log entries"; 3739 return entries.size(); 3740 } 3741 std::string delegate_info; 3742 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); 3743 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); 3744 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", 3745 &delegate_info)); 3746 EXPECT_EQ(kFirstDelegateInfo, delegate_info); 3747 3748 ++log_position; 3749 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); 3750 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 3751 3752 ++log_position; 3753 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); 3754 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); 3755 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", 3756 &delegate_info)); 3757 EXPECT_EQ(kSecondDelegateInfo, delegate_info); 3758 3759 ++log_position; 3760 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); 3761 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 3762 3763 return log_position + 1; 3764 } 3765 3766 private: 3767 friend class base::RefCounted<AsyncDelegateLogger>; 3768 3769 AsyncDelegateLogger(URLRequest* url_request, 3770 LoadState expected_first_load_state, 3771 LoadState expected_second_load_state, 3772 LoadState expected_third_load_state, 3773 const Callback& callback) 3774 : url_request_(url_request), 3775 expected_first_load_state_(expected_first_load_state), 3776 expected_second_load_state_(expected_second_load_state), 3777 expected_third_load_state_(expected_third_load_state), 3778 callback_(callback) { 3779 } 3780 3781 ~AsyncDelegateLogger() {} 3782 3783 void Start() { 3784 url_request_->SetDelegateInfo(kFirstDelegateInfo, 3785 URLRequest::DELEGATE_INFO_DEBUG_ONLY); 3786 LoadStateWithParam load_state = url_request_->GetLoadState(); 3787 EXPECT_EQ(expected_first_load_state_, load_state.state); 3788 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); 3789 base::MessageLoop::current()->PostTask( 3790 FROM_HERE, 3791 base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); 3792 } 3793 3794 void LogSecondDelegate() { 3795 url_request_->SetDelegateInfo(kSecondDelegateInfo, 3796 URLRequest::DELEGATE_INFO_DISPLAY_TO_USER); 3797 LoadStateWithParam load_state = url_request_->GetLoadState(); 3798 EXPECT_EQ(expected_second_load_state_, load_state.state); 3799 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { 3800 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); 3801 } else { 3802 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); 3803 } 3804 base::MessageLoop::current()->PostTask( 3805 FROM_HERE, 3806 base::Bind(&AsyncDelegateLogger::LogComplete, this)); 3807 } 3808 3809 void LogComplete() { 3810 url_request_->SetDelegateInfo( 3811 NULL, URLRequest::DELEGATE_INFO_DISPLAY_TO_USER); 3812 LoadStateWithParam load_state = url_request_->GetLoadState(); 3813 EXPECT_EQ(expected_third_load_state_, load_state.state); 3814 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) 3815 EXPECT_EQ(string16(), load_state.param); 3816 callback_.Run(); 3817 } 3818 3819 URLRequest* url_request_; 3820 const int expected_first_load_state_; 3821 const int expected_second_load_state_; 3822 const int expected_third_load_state_; 3823 const Callback callback_; 3824 3825 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); 3826}; 3827 3828// NetworkDelegate that logs delegate information before a request is started, 3829// before headers are sent, when headers are read, and when auth information 3830// is requested. Uses AsyncDelegateLogger. 3831class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { 3832 public: 3833 AsyncLoggingNetworkDelegate() {} 3834 virtual ~AsyncLoggingNetworkDelegate() {} 3835 3836 // NetworkDelegate implementation. 3837 virtual int OnBeforeURLRequest(URLRequest* request, 3838 const CompletionCallback& callback, 3839 GURL* new_url) OVERRIDE { 3840 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); 3841 return RunCallbackAsynchronously(request, callback); 3842 } 3843 3844 virtual int OnBeforeSendHeaders(URLRequest* request, 3845 const CompletionCallback& callback, 3846 HttpRequestHeaders* headers) OVERRIDE { 3847 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); 3848 return RunCallbackAsynchronously(request, callback); 3849 } 3850 3851 virtual int OnHeadersReceived( 3852 URLRequest* request, 3853 const CompletionCallback& callback, 3854 const HttpResponseHeaders* original_response_headers, 3855 scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE { 3856 TestNetworkDelegate::OnHeadersReceived(request, callback, 3857 original_response_headers, 3858 override_response_headers); 3859 return RunCallbackAsynchronously(request, callback); 3860 } 3861 3862 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( 3863 URLRequest* request, 3864 const AuthChallengeInfo& auth_info, 3865 const AuthCallback& callback, 3866 AuthCredentials* credentials) OVERRIDE { 3867 AsyncDelegateLogger::Run( 3868 request, 3869 LOAD_STATE_WAITING_FOR_DELEGATE, 3870 LOAD_STATE_WAITING_FOR_DELEGATE, 3871 LOAD_STATE_WAITING_FOR_DELEGATE, 3872 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, 3873 callback, credentials)); 3874 return AUTH_REQUIRED_RESPONSE_IO_PENDING; 3875 } 3876 3877 private: 3878 static int RunCallbackAsynchronously( 3879 URLRequest* request, 3880 const CompletionCallback& callback) { 3881 AsyncDelegateLogger::Run( 3882 request, 3883 LOAD_STATE_WAITING_FOR_DELEGATE, 3884 LOAD_STATE_WAITING_FOR_DELEGATE, 3885 LOAD_STATE_WAITING_FOR_DELEGATE, 3886 base::Bind(callback, OK)); 3887 return ERR_IO_PENDING; 3888 } 3889 3890 static void SetAuthAndResume(const AuthCallback& callback, 3891 AuthCredentials* credentials) { 3892 *credentials = AuthCredentials(kUser, kSecret); 3893 callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); 3894 } 3895 3896 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); 3897}; 3898 3899// URLRequest::Delegate that logs delegate information when the headers 3900// are received, when each read completes, and during redirects. Uses 3901// AsyncDelegateLogger. Can optionally cancel a request in any phase. 3902// 3903// Inherits from TestDelegate to reuse the TestDelegate code to handle 3904// advancing to the next step in most cases, as well as cancellation. 3905class AsyncLoggingUrlRequestDelegate : public TestDelegate { 3906 public: 3907 enum CancelStage { 3908 NO_CANCEL = 0, 3909 CANCEL_ON_RECEIVED_REDIRECT, 3910 CANCEL_ON_RESPONSE_STARTED, 3911 CANCEL_ON_READ_COMPLETED 3912 }; 3913 3914 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) 3915 : cancel_stage_(cancel_stage) { 3916 if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) 3917 set_cancel_in_received_redirect(true); 3918 else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) 3919 set_cancel_in_response_started(true); 3920 else if (cancel_stage == CANCEL_ON_READ_COMPLETED) 3921 set_cancel_in_received_data(true); 3922 } 3923 virtual ~AsyncLoggingUrlRequestDelegate() {} 3924 3925 // URLRequest::Delegate implementation: 3926 void virtual OnReceivedRedirect(URLRequest* request, 3927 const GURL& new_url, 3928 bool* defer_redirect) OVERRIDE { 3929 *defer_redirect = true; 3930 AsyncDelegateLogger::Run( 3931 request, 3932 LOAD_STATE_WAITING_FOR_DELEGATE, 3933 LOAD_STATE_WAITING_FOR_DELEGATE, 3934 LOAD_STATE_WAITING_FOR_DELEGATE, 3935 base::Bind( 3936 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, 3937 base::Unretained(this), request, new_url)); 3938 } 3939 3940 virtual void OnResponseStarted(URLRequest* request) OVERRIDE { 3941 AsyncDelegateLogger::Run( 3942 request, 3943 LOAD_STATE_WAITING_FOR_DELEGATE, 3944 LOAD_STATE_WAITING_FOR_DELEGATE, 3945 LOAD_STATE_WAITING_FOR_DELEGATE, 3946 base::Bind( 3947 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, 3948 base::Unretained(this), request)); 3949 } 3950 3951 virtual void OnReadCompleted(URLRequest* request, 3952 int bytes_read) OVERRIDE { 3953 AsyncDelegateLogger::Run( 3954 request, 3955 LOAD_STATE_IDLE, 3956 LOAD_STATE_IDLE, 3957 LOAD_STATE_IDLE, 3958 base::Bind( 3959 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, 3960 base::Unretained(this), request, bytes_read)); 3961 } 3962 3963 private: 3964 void OnReceivedRedirectLoggingComplete(URLRequest* request, 3965 const GURL& new_url) { 3966 bool defer_redirect = false; 3967 TestDelegate::OnReceivedRedirect(request, new_url, &defer_redirect); 3968 // FollowDeferredRedirect should not be called after cancellation. 3969 if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) 3970 return; 3971 if (!defer_redirect) 3972 request->FollowDeferredRedirect(); 3973 } 3974 3975 void OnResponseStartedLoggingComplete(URLRequest* request) { 3976 // The parent class continues the request. 3977 TestDelegate::OnResponseStarted(request); 3978 } 3979 3980 void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { 3981 // The parent class continues the request. 3982 TestDelegate::OnReadCompleted(request, bytes_read); 3983 } 3984 3985 const CancelStage cancel_stage_; 3986 3987 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); 3988}; 3989 3990// Tests handling of delegate info before a request starts. 3991TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { 3992 ASSERT_TRUE(test_server_.Start()); 3993 3994 TestDelegate request_delegate; 3995 TestURLRequestContext context(true); 3996 context.set_network_delegate(NULL); 3997 context.set_net_log(&net_log_); 3998 context.Init(); 3999 4000 { 4001 URLRequest r(test_server_.GetURL("empty.html"), 4002 DEFAULT_PRIORITY, 4003 &request_delegate, 4004 &context); 4005 LoadStateWithParam load_state = r.GetLoadState(); 4006 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); 4007 EXPECT_EQ(string16(), load_state.param); 4008 4009 AsyncDelegateLogger::Run( 4010 &r, 4011 LOAD_STATE_WAITING_FOR_DELEGATE, 4012 LOAD_STATE_WAITING_FOR_DELEGATE, 4013 LOAD_STATE_IDLE, 4014 base::Bind(&URLRequest::Start, base::Unretained(&r))); 4015 4016 base::RunLoop().Run(); 4017 4018 EXPECT_EQ(200, r.GetResponseCode()); 4019 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4020 } 4021 4022 CapturingNetLog::CapturedEntryList entries; 4023 net_log_.GetEntries(&entries); 4024 size_t log_position = ExpectLogContainsSomewhereAfter( 4025 entries, 4026 0, 4027 NetLog::TYPE_DELEGATE_INFO, 4028 NetLog::PHASE_BEGIN); 4029 4030 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); 4031 4032 // Nothing else should add any delegate info to the request. 4033 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4034 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4035} 4036 4037// Tests handling of delegate info from a network delegate. 4038TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { 4039 ASSERT_TRUE(test_server_.Start()); 4040 4041 TestDelegate request_delegate; 4042 AsyncLoggingNetworkDelegate network_delegate; 4043 TestURLRequestContext context(true); 4044 context.set_network_delegate(&network_delegate); 4045 context.set_net_log(&net_log_); 4046 context.Init(); 4047 4048 { 4049 URLRequest r(test_server_.GetURL("simple.html"), 4050 DEFAULT_PRIORITY, 4051 &request_delegate, 4052 &context); 4053 LoadStateWithParam load_state = r.GetLoadState(); 4054 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); 4055 EXPECT_EQ(string16(), load_state.param); 4056 4057 r.Start(); 4058 base::RunLoop().Run(); 4059 4060 EXPECT_EQ(200, r.GetResponseCode()); 4061 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4062 EXPECT_EQ(1, network_delegate.created_requests()); 4063 EXPECT_EQ(0, network_delegate.destroyed_requests()); 4064 } 4065 EXPECT_EQ(1, network_delegate.destroyed_requests()); 4066 4067 size_t log_position = 0; 4068 CapturingNetLog::CapturedEntryList entries; 4069 net_log_.GetEntries(&entries); 4070 for (size_t i = 0; i < 3; ++i) { 4071 log_position = ExpectLogContainsSomewhereAfter( 4072 entries, 4073 log_position + 1, 4074 NetLog::TYPE_URL_REQUEST_DELEGATE, 4075 NetLog::PHASE_BEGIN); 4076 4077 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4078 log_position + 1); 4079 4080 ASSERT_LT(log_position, entries.size()); 4081 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4082 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4083 } 4084 4085 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4086 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4087} 4088 4089// Tests handling of delegate info from a network delegate in the case of an 4090// HTTP redirect. 4091TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { 4092 ASSERT_TRUE(test_server_.Start()); 4093 4094 TestDelegate request_delegate; 4095 AsyncLoggingNetworkDelegate network_delegate; 4096 TestURLRequestContext context(true); 4097 context.set_network_delegate(&network_delegate); 4098 context.set_net_log(&net_log_); 4099 context.Init(); 4100 4101 { 4102 URLRequest r(test_server_.GetURL("server-redirect?simple.html"), 4103 DEFAULT_PRIORITY, 4104 &request_delegate, 4105 &context); 4106 LoadStateWithParam load_state = r.GetLoadState(); 4107 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); 4108 EXPECT_EQ(string16(), load_state.param); 4109 4110 r.Start(); 4111 base::RunLoop().Run(); 4112 4113 EXPECT_EQ(200, r.GetResponseCode()); 4114 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4115 EXPECT_EQ(2, network_delegate.created_requests()); 4116 EXPECT_EQ(0, network_delegate.destroyed_requests()); 4117 } 4118 EXPECT_EQ(1, network_delegate.destroyed_requests()); 4119 4120 size_t log_position = 0; 4121 CapturingNetLog::CapturedEntryList entries; 4122 net_log_.GetEntries(&entries); 4123 // The NetworkDelegate logged information in OnBeforeURLRequest, 4124 // OnBeforeSendHeaders, and OnHeadersReceived. 4125 for (size_t i = 0; i < 3; ++i) { 4126 log_position = ExpectLogContainsSomewhereAfter( 4127 entries, 4128 log_position + 1, 4129 NetLog::TYPE_URL_REQUEST_DELEGATE, 4130 NetLog::PHASE_BEGIN); 4131 4132 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4133 log_position + 1); 4134 4135 ASSERT_LT(log_position, entries.size()); 4136 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4137 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4138 } 4139 4140 // The URLRequest::Delegate then gets informed about the redirect. 4141 log_position = ExpectLogContainsSomewhereAfter( 4142 entries, 4143 log_position + 1, 4144 NetLog::TYPE_URL_REQUEST_DELEGATE, 4145 NetLog::PHASE_BEGIN); 4146 4147 // The NetworkDelegate logged information in the same three events as before. 4148 for (size_t i = 0; i < 3; ++i) { 4149 log_position = ExpectLogContainsSomewhereAfter( 4150 entries, 4151 log_position + 1, 4152 NetLog::TYPE_URL_REQUEST_DELEGATE, 4153 NetLog::PHASE_BEGIN); 4154 4155 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4156 log_position + 1); 4157 4158 ASSERT_LT(log_position, entries.size()); 4159 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4160 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4161 } 4162 4163 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4164 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4165} 4166 4167// Tests handling of delegate info from a network delegate in the case of HTTP 4168// AUTH. 4169TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { 4170 ASSERT_TRUE(test_server_.Start()); 4171 4172 TestDelegate request_delegate; 4173 AsyncLoggingNetworkDelegate network_delegate; 4174 TestURLRequestContext context(true); 4175 context.set_network_delegate(&network_delegate); 4176 context.set_net_log(&net_log_); 4177 context.Init(); 4178 4179 { 4180 URLRequest r(test_server_.GetURL("auth-basic"), 4181 DEFAULT_PRIORITY, 4182 &request_delegate, 4183 &context); 4184 LoadStateWithParam load_state = r.GetLoadState(); 4185 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); 4186 EXPECT_EQ(string16(), load_state.param); 4187 4188 r.Start(); 4189 base::RunLoop().Run(); 4190 4191 EXPECT_EQ(200, r.GetResponseCode()); 4192 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4193 EXPECT_EQ(1, network_delegate.created_requests()); 4194 EXPECT_EQ(0, network_delegate.destroyed_requests()); 4195 } 4196 EXPECT_EQ(1, network_delegate.destroyed_requests()); 4197 4198 size_t log_position = 0; 4199 CapturingNetLog::CapturedEntryList entries; 4200 net_log_.GetEntries(&entries); 4201 // The NetworkDelegate should have logged information in OnBeforeURLRequest, 4202 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in 4203 // OnBeforeURLRequest and OnBeforeSendHeaders. 4204 for (size_t i = 0; i < 6; ++i) { 4205 log_position = ExpectLogContainsSomewhereAfter( 4206 entries, 4207 log_position + 1, 4208 NetLog::TYPE_URL_REQUEST_DELEGATE, 4209 NetLog::PHASE_BEGIN); 4210 4211 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4212 log_position + 1); 4213 4214 ASSERT_LT(log_position, entries.size()); 4215 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4216 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4217 } 4218 4219 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4220 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4221} 4222 4223// Tests handling of delegate info from a URLRequest::Delegate. 4224TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { 4225 ASSERT_TRUE(test_server_.Start()); 4226 4227 AsyncLoggingUrlRequestDelegate request_delegate( 4228 AsyncLoggingUrlRequestDelegate::NO_CANCEL); 4229 TestURLRequestContext context(true); 4230 context.set_network_delegate(NULL); 4231 context.set_net_log(&net_log_); 4232 context.Init(); 4233 4234 { 4235 // A chunked response with delays between chunks is used to make sure that 4236 // attempts by the URLRequest delegate to log information while reading the 4237 // body are ignored. Since they are ignored, this test is robust against 4238 // the possability of multiple reads being combined in the unlikely event 4239 // that it occurs. 4240 URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"), 4241 DEFAULT_PRIORITY, 4242 &request_delegate, 4243 &context); 4244 LoadStateWithParam load_state = r.GetLoadState(); 4245 r.Start(); 4246 base::RunLoop().Run(); 4247 4248 EXPECT_EQ(200, r.GetResponseCode()); 4249 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4250 } 4251 4252 CapturingNetLog::CapturedEntryList entries; 4253 net_log_.GetEntries(&entries); 4254 4255 // The delegate info should only have been logged on header complete. Other 4256 // times it should silently be ignored. 4257 4258 size_t log_position = ExpectLogContainsSomewhereAfter( 4259 entries, 4260 0, 4261 NetLog::TYPE_URL_REQUEST_DELEGATE, 4262 NetLog::PHASE_BEGIN); 4263 4264 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4265 log_position + 1); 4266 4267 ASSERT_LT(log_position, entries.size()); 4268 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4269 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4270 4271 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4272 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4273 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4274 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); 4275} 4276 4277// Tests handling of delegate info from a URLRequest::Delegate in the case of 4278// an HTTP redirect. 4279TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { 4280 ASSERT_TRUE(test_server_.Start()); 4281 4282 AsyncLoggingUrlRequestDelegate request_delegate( 4283 AsyncLoggingUrlRequestDelegate::NO_CANCEL); 4284 TestURLRequestContext context(true); 4285 context.set_network_delegate(NULL); 4286 context.set_net_log(&net_log_); 4287 context.Init(); 4288 4289 { 4290 URLRequest r(test_server_.GetURL("server-redirect?simple.html"), 4291 DEFAULT_PRIORITY, 4292 &request_delegate, 4293 &context); 4294 LoadStateWithParam load_state = r.GetLoadState(); 4295 r.Start(); 4296 base::RunLoop().Run(); 4297 4298 EXPECT_EQ(200, r.GetResponseCode()); 4299 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4300 } 4301 4302 CapturingNetLog::CapturedEntryList entries; 4303 net_log_.GetEntries(&entries); 4304 4305 // Delegate info should only have been logged in OnReceivedRedirect and 4306 // OnResponseStarted. 4307 size_t log_position = 0; 4308 for (int i = 0; i < 2; ++i) { 4309 log_position = ExpectLogContainsSomewhereAfter( 4310 entries, 4311 log_position, 4312 NetLog::TYPE_URL_REQUEST_DELEGATE, 4313 NetLog::PHASE_BEGIN); 4314 4315 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4316 log_position + 1); 4317 4318 ASSERT_LT(log_position, entries.size()); 4319 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4320 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4321 } 4322 4323 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4324 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4325 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4326 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); 4327} 4328 4329// Tests handling of delegate info from a URLRequest::Delegate in the case of 4330// an HTTP redirect, with cancellation at various points. 4331TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { 4332 ASSERT_TRUE(test_server_.Start()); 4333 4334 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { 4335 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, 4336 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, 4337 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, 4338 }; 4339 4340 for (size_t test_case = 0; test_case < arraysize(kCancelStages); 4341 ++test_case) { 4342 AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); 4343 TestURLRequestContext context(true); 4344 CapturingNetLog net_log; 4345 context.set_network_delegate(NULL); 4346 context.set_net_log(&net_log); 4347 context.Init(); 4348 4349 { 4350 URLRequest r(test_server_.GetURL("server-redirect?simple.html"), 4351 DEFAULT_PRIORITY, 4352 &request_delegate, 4353 &context); 4354 LoadStateWithParam load_state = r.GetLoadState(); 4355 r.Start(); 4356 base::RunLoop().Run(); 4357 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 4358 } 4359 4360 CapturingNetLog::CapturedEntryList entries; 4361 net_log.GetEntries(&entries); 4362 4363 // Delegate info is always logged in both OnReceivedRedirect and 4364 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the 4365 // OnResponseStarted delegate call is after cancellation, but logging is 4366 // still currently supported in that call. 4367 size_t log_position = 0; 4368 for (int i = 0; i < 2; ++i) { 4369 log_position = ExpectLogContainsSomewhereAfter( 4370 entries, 4371 log_position, 4372 NetLog::TYPE_URL_REQUEST_DELEGATE, 4373 NetLog::PHASE_BEGIN); 4374 4375 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, 4376 log_position + 1); 4377 4378 ASSERT_LT(log_position, entries.size()); 4379 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); 4380 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); 4381 } 4382 4383 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4384 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); 4385 EXPECT_FALSE(LogContainsEntryWithTypeAfter( 4386 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); 4387 } 4388} 4389 4390namespace { 4391 4392const char kExtraHeader[] = "Allow-Snafu"; 4393const char kExtraValue[] = "fubar"; 4394 4395class RedirectWithAdditionalHeadersDelegate : public TestDelegate { 4396 virtual void OnReceivedRedirect(net::URLRequest* request, 4397 const GURL& new_url, 4398 bool* defer_redirect) OVERRIDE { 4399 TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect); 4400 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); 4401 } 4402}; 4403 4404} // namespace 4405 4406TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { 4407 ASSERT_TRUE(test_server_.Start()); 4408 4409 GURL destination_url = test_server_.GetURL( 4410 "echoheader?" + std::string(kExtraHeader)); 4411 GURL original_url = test_server_.GetURL( 4412 "server-redirect?" + destination_url.spec()); 4413 RedirectWithAdditionalHeadersDelegate d; 4414 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_); 4415 req.Start(); 4416 base::RunLoop().Run(); 4417 4418 std::string value; 4419 const HttpRequestHeaders& headers = req.extra_request_headers(); 4420 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); 4421 EXPECT_EQ(kExtraValue, value); 4422 EXPECT_FALSE(req.is_pending()); 4423 EXPECT_FALSE(req.is_redirecting()); 4424 EXPECT_EQ(kExtraValue, d.data_received()); 4425} 4426 4427namespace { 4428 4429const char kExtraHeaderToRemove[] = "To-Be-Removed"; 4430 4431class RedirectWithHeaderRemovalDelegate : public TestDelegate { 4432 virtual void OnReceivedRedirect(net::URLRequest* request, 4433 const GURL& new_url, 4434 bool* defer_redirect) OVERRIDE { 4435 TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect); 4436 request->RemoveRequestHeaderByName(kExtraHeaderToRemove); 4437 } 4438}; 4439 4440} // namespace 4441 4442TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { 4443 ASSERT_TRUE(test_server_.Start()); 4444 4445 GURL destination_url = test_server_.GetURL( 4446 "echoheader?" + std::string(kExtraHeaderToRemove)); 4447 GURL original_url = test_server_.GetURL( 4448 "server-redirect?" + destination_url.spec()); 4449 RedirectWithHeaderRemovalDelegate d; 4450 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_); 4451 req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); 4452 req.Start(); 4453 base::RunLoop().Run(); 4454 4455 std::string value; 4456 const HttpRequestHeaders& headers = req.extra_request_headers(); 4457 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); 4458 EXPECT_FALSE(req.is_pending()); 4459 EXPECT_FALSE(req.is_redirecting()); 4460 EXPECT_EQ("None", d.data_received()); 4461} 4462 4463TEST_F(URLRequestTestHTTP, CancelTest) { 4464 TestDelegate d; 4465 { 4466 URLRequest r(GURL("http://www.google.com/"), 4467 DEFAULT_PRIORITY, 4468 &d, 4469 &default_context_); 4470 4471 r.Start(); 4472 EXPECT_TRUE(r.is_pending()); 4473 4474 r.Cancel(); 4475 4476 base::RunLoop().Run(); 4477 4478 // We expect to receive OnResponseStarted even though the request has been 4479 // cancelled. 4480 EXPECT_EQ(1, d.response_started_count()); 4481 EXPECT_EQ(0, d.bytes_received()); 4482 EXPECT_FALSE(d.received_data_before_response()); 4483 } 4484} 4485 4486TEST_F(URLRequestTestHTTP, CancelTest2) { 4487 ASSERT_TRUE(test_server_.Start()); 4488 4489 TestDelegate d; 4490 { 4491 URLRequest r(test_server_.GetURL(std::string()), 4492 DEFAULT_PRIORITY, 4493 &d, 4494 &default_context_); 4495 4496 d.set_cancel_in_response_started(true); 4497 4498 r.Start(); 4499 EXPECT_TRUE(r.is_pending()); 4500 4501 base::RunLoop().Run(); 4502 4503 EXPECT_EQ(1, d.response_started_count()); 4504 EXPECT_EQ(0, d.bytes_received()); 4505 EXPECT_FALSE(d.received_data_before_response()); 4506 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 4507 } 4508} 4509 4510TEST_F(URLRequestTestHTTP, CancelTest3) { 4511 ASSERT_TRUE(test_server_.Start()); 4512 4513 TestDelegate d; 4514 { 4515 URLRequest r(test_server_.GetURL(std::string()), 4516 DEFAULT_PRIORITY, 4517 &d, 4518 &default_context_); 4519 4520 d.set_cancel_in_received_data(true); 4521 4522 r.Start(); 4523 EXPECT_TRUE(r.is_pending()); 4524 4525 base::RunLoop().Run(); 4526 4527 EXPECT_EQ(1, d.response_started_count()); 4528 // There is no guarantee about how much data was received 4529 // before the cancel was issued. It could have been 0 bytes, 4530 // or it could have been all the bytes. 4531 // EXPECT_EQ(0, d.bytes_received()); 4532 EXPECT_FALSE(d.received_data_before_response()); 4533 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 4534 } 4535} 4536 4537TEST_F(URLRequestTestHTTP, CancelTest4) { 4538 ASSERT_TRUE(test_server_.Start()); 4539 4540 TestDelegate d; 4541 { 4542 URLRequest r(test_server_.GetURL(std::string()), 4543 DEFAULT_PRIORITY, 4544 &d, 4545 &default_context_); 4546 4547 r.Start(); 4548 EXPECT_TRUE(r.is_pending()); 4549 4550 // The request will be implicitly canceled when it is destroyed. The 4551 // test delegate must not post a quit message when this happens because 4552 // this test doesn't actually have a message loop. The quit message would 4553 // get put on this thread's message queue and the next test would exit 4554 // early, causing problems. 4555 d.set_quit_on_complete(false); 4556 } 4557 // expect things to just cleanup properly. 4558 4559 // we won't actually get a received reponse here because we've never run the 4560 // message loop 4561 EXPECT_FALSE(d.received_data_before_response()); 4562 EXPECT_EQ(0, d.bytes_received()); 4563} 4564 4565TEST_F(URLRequestTestHTTP, CancelTest5) { 4566 ASSERT_TRUE(test_server_.Start()); 4567 4568 // populate cache 4569 { 4570 TestDelegate d; 4571 URLRequest r(test_server_.GetURL("cachetime"), 4572 DEFAULT_PRIORITY, 4573 &d, 4574 &default_context_); 4575 r.Start(); 4576 base::RunLoop().Run(); 4577 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); 4578 } 4579 4580 // cancel read from cache (see bug 990242) 4581 { 4582 TestDelegate d; 4583 URLRequest r(test_server_.GetURL("cachetime"), 4584 DEFAULT_PRIORITY, 4585 &d, 4586 &default_context_); 4587 r.Start(); 4588 r.Cancel(); 4589 base::RunLoop().Run(); 4590 4591 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); 4592 EXPECT_EQ(1, d.response_started_count()); 4593 EXPECT_EQ(0, d.bytes_received()); 4594 EXPECT_FALSE(d.received_data_before_response()); 4595 } 4596} 4597 4598TEST_F(URLRequestTestHTTP, PostTest) { 4599 ASSERT_TRUE(test_server_.Start()); 4600 HTTPUploadDataOperationTest("POST"); 4601} 4602 4603TEST_F(URLRequestTestHTTP, PutTest) { 4604 ASSERT_TRUE(test_server_.Start()); 4605 HTTPUploadDataOperationTest("PUT"); 4606} 4607 4608TEST_F(URLRequestTestHTTP, PostEmptyTest) { 4609 ASSERT_TRUE(test_server_.Start()); 4610 4611 TestDelegate d; 4612 { 4613 URLRequest r( 4614 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); 4615 r.set_method("POST"); 4616 4617 r.Start(); 4618 EXPECT_TRUE(r.is_pending()); 4619 4620 base::RunLoop().Run(); 4621 4622 ASSERT_EQ(1, d.response_started_count()) 4623 << "request failed: " << r.status().status() 4624 << ", error: " << r.status().error(); 4625 4626 EXPECT_FALSE(d.received_data_before_response()); 4627 EXPECT_TRUE(d.data_received().empty()); 4628 } 4629} 4630 4631TEST_F(URLRequestTestHTTP, PostFileTest) { 4632 ASSERT_TRUE(test_server_.Start()); 4633 4634 TestDelegate d; 4635 { 4636 URLRequest r( 4637 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); 4638 r.set_method("POST"); 4639 4640 base::FilePath dir; 4641 PathService::Get(base::DIR_EXE, &dir); 4642 file_util::SetCurrentDirectory(dir); 4643 4644 ScopedVector<UploadElementReader> element_readers; 4645 4646 base::FilePath path; 4647 PathService::Get(base::DIR_SOURCE_ROOT, &path); 4648 path = path.Append(FILE_PATH_LITERAL("net")); 4649 path = path.Append(FILE_PATH_LITERAL("data")); 4650 path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); 4651 path = path.Append(FILE_PATH_LITERAL("with-headers.html")); 4652 element_readers.push_back( 4653 new UploadFileElementReader(base::MessageLoopProxy::current().get(), 4654 path, 4655 0, 4656 kuint64max, 4657 base::Time())); 4658 4659 // This file should just be ignored in the upload stream. 4660 element_readers.push_back(new UploadFileElementReader( 4661 base::MessageLoopProxy::current().get(), 4662 base::FilePath(FILE_PATH_LITERAL( 4663 "c:\\path\\to\\non\\existant\\file.randomness.12345")), 4664 0, 4665 kuint64max, 4666 base::Time())); 4667 r.set_upload(make_scoped_ptr( 4668 new UploadDataStream(element_readers.Pass(), 0))); 4669 4670 r.Start(); 4671 EXPECT_TRUE(r.is_pending()); 4672 4673 base::RunLoop().Run(); 4674 4675 int64 size = 0; 4676 ASSERT_EQ(true, file_util::GetFileSize(path, &size)); 4677 scoped_ptr<char[]> buf(new char[size]); 4678 4679 ASSERT_EQ(size, file_util::ReadFile(path, buf.get(), size)); 4680 4681 ASSERT_EQ(1, d.response_started_count()) 4682 << "request failed: " << r.status().status() 4683 << ", error: " << r.status().error(); 4684 4685 EXPECT_FALSE(d.received_data_before_response()); 4686 4687 EXPECT_EQ(size, d.bytes_received()); 4688 EXPECT_EQ(std::string(&buf[0], size), d.data_received()); 4689 } 4690} 4691 4692TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { 4693 ASSERT_TRUE(test_server_.Start()); 4694 4695 TestDelegate d; 4696 { 4697 URLRequest r( 4698 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); 4699 r.EnableChunkedUpload(); 4700 r.set_method("POST"); 4701 AddChunksToUpload(&r); 4702 r.Start(); 4703 EXPECT_TRUE(r.is_pending()); 4704 4705 base::RunLoop().Run(); 4706 4707 VerifyReceivedDataMatchesChunks(&r, &d); 4708 } 4709} 4710 4711TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { 4712 ASSERT_TRUE(test_server_.Start()); 4713 4714 TestDelegate d; 4715 { 4716 URLRequest r( 4717 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); 4718 r.EnableChunkedUpload(); 4719 r.set_method("POST"); 4720 r.Start(); 4721 EXPECT_TRUE(r.is_pending()); 4722 AddChunksToUpload(&r); 4723 base::RunLoop().Run(); 4724 4725 VerifyReceivedDataMatchesChunks(&r, &d); 4726 } 4727} 4728 4729TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { 4730 ASSERT_TRUE(test_server_.Start()); 4731 4732 TestDelegate d; 4733 { 4734 URLRequest r( 4735 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); 4736 r.EnableChunkedUpload(); 4737 r.set_method("POST"); 4738 r.Start(); 4739 EXPECT_TRUE(r.is_pending()); 4740 4741 base::RunLoop().RunUntilIdle(); 4742 AddChunksToUpload(&r); 4743 base::RunLoop().Run(); 4744 4745 VerifyReceivedDataMatchesChunks(&r, &d); 4746 } 4747} 4748 4749TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { 4750 ASSERT_TRUE(test_server_.Start()); 4751 4752 TestDelegate d; 4753 URLRequest req(test_server_.GetURL("files/with-headers.html"), 4754 DEFAULT_PRIORITY, 4755 &d, 4756 &default_context_); 4757 req.Start(); 4758 base::RunLoop().Run(); 4759 4760 const HttpResponseHeaders* headers = req.response_headers(); 4761 4762 // Simple sanity check that response_info() accesses the same data. 4763 EXPECT_EQ(headers, req.response_info().headers.get()); 4764 4765 std::string header; 4766 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); 4767 EXPECT_EQ("private", header); 4768 4769 header.clear(); 4770 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); 4771 EXPECT_EQ("text/html; charset=ISO-8859-1", header); 4772 4773 // The response has two "X-Multiple-Entries" headers. 4774 // This verfies our output has them concatenated together. 4775 header.clear(); 4776 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); 4777 EXPECT_EQ("a, b", header); 4778} 4779 4780TEST_F(URLRequestTestHTTP, ProcessSTS) { 4781 SpawnedTestServer::SSLOptions ssl_options; 4782 SpawnedTestServer https_test_server( 4783 SpawnedTestServer::TYPE_HTTPS, 4784 ssl_options, 4785 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 4786 ASSERT_TRUE(https_test_server.Start()); 4787 4788 TestDelegate d; 4789 URLRequest request(https_test_server.GetURL("files/hsts-headers.html"), 4790 DEFAULT_PRIORITY, 4791 &d, 4792 &default_context_); 4793 request.Start(); 4794 base::RunLoop().Run(); 4795 4796 TransportSecurityState* security_state = 4797 default_context_.transport_security_state(); 4798 bool sni_available = true; 4799 TransportSecurityState::DomainState domain_state; 4800 EXPECT_TRUE(security_state->GetDomainState( 4801 SpawnedTestServer::kLocalhost, sni_available, &domain_state)); 4802 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, 4803 domain_state.upgrade_mode); 4804 EXPECT_TRUE(domain_state.sts_include_subdomains); 4805 EXPECT_FALSE(domain_state.pkp_include_subdomains); 4806#if defined(OS_ANDROID) 4807 // Android's CertVerifyProc does not (yet) handle pins. 4808#else 4809 EXPECT_FALSE(domain_state.HasPublicKeyPins()); 4810#endif 4811} 4812 4813// Android's CertVerifyProc does not (yet) handle pins. Therefore, it will 4814// reject HPKP headers, and a test setting only HPKP headers will fail (no 4815// DomainState present because header rejected). 4816#if defined(OS_ANDROID) 4817#define MAYBE_ProcessPKP DISABLED_ProcessPKP 4818#else 4819#define MAYBE_ProcessPKP ProcessPKP 4820#endif 4821 4822// Tests that enabling HPKP on a domain does not affect the HSTS 4823// validity/expiration. 4824TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) { 4825 SpawnedTestServer::SSLOptions ssl_options; 4826 SpawnedTestServer https_test_server( 4827 SpawnedTestServer::TYPE_HTTPS, 4828 ssl_options, 4829 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 4830 ASSERT_TRUE(https_test_server.Start()); 4831 4832 TestDelegate d; 4833 URLRequest request(https_test_server.GetURL("files/hpkp-headers.html"), 4834 DEFAULT_PRIORITY, 4835 &d, 4836 &default_context_); 4837 request.Start(); 4838 base::RunLoop().Run(); 4839 4840 TransportSecurityState* security_state = 4841 default_context_.transport_security_state(); 4842 bool sni_available = true; 4843 TransportSecurityState::DomainState domain_state; 4844 EXPECT_TRUE(security_state->GetDomainState( 4845 SpawnedTestServer::kLocalhost, sni_available, &domain_state)); 4846 EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT, 4847 domain_state.upgrade_mode); 4848 EXPECT_FALSE(domain_state.sts_include_subdomains); 4849 EXPECT_FALSE(domain_state.pkp_include_subdomains); 4850 EXPECT_TRUE(domain_state.HasPublicKeyPins()); 4851 EXPECT_NE(domain_state.upgrade_expiry, 4852 domain_state.dynamic_spki_hashes_expiry); 4853} 4854 4855TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { 4856 SpawnedTestServer::SSLOptions ssl_options; 4857 SpawnedTestServer https_test_server( 4858 SpawnedTestServer::TYPE_HTTPS, 4859 ssl_options, 4860 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 4861 ASSERT_TRUE(https_test_server.Start()); 4862 4863 TestDelegate d; 4864 URLRequest request( 4865 https_test_server.GetURL("files/hsts-multiple-headers.html"), 4866 DEFAULT_PRIORITY, 4867 &d, 4868 &default_context_); 4869 request.Start(); 4870 base::RunLoop().Run(); 4871 4872 // We should have set parameters from the first header, not the second. 4873 TransportSecurityState* security_state = 4874 default_context_.transport_security_state(); 4875 bool sni_available = true; 4876 TransportSecurityState::DomainState domain_state; 4877 EXPECT_TRUE(security_state->GetDomainState( 4878 SpawnedTestServer::kLocalhost, sni_available, &domain_state)); 4879 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, 4880 domain_state.upgrade_mode); 4881 EXPECT_FALSE(domain_state.sts_include_subdomains); 4882 EXPECT_FALSE(domain_state.pkp_include_subdomains); 4883} 4884 4885TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { 4886 SpawnedTestServer::SSLOptions ssl_options; 4887 SpawnedTestServer https_test_server( 4888 SpawnedTestServer::TYPE_HTTPS, 4889 ssl_options, 4890 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 4891 ASSERT_TRUE(https_test_server.Start()); 4892 4893 TestDelegate d; 4894 URLRequest request( 4895 https_test_server.GetURL("files/hsts-and-hpkp-headers.html"), 4896 DEFAULT_PRIORITY, 4897 &d, 4898 &default_context_); 4899 request.Start(); 4900 base::RunLoop().Run(); 4901 4902 // We should have set parameters from the first header, not the second. 4903 TransportSecurityState* security_state = 4904 default_context_.transport_security_state(); 4905 bool sni_available = true; 4906 TransportSecurityState::DomainState domain_state; 4907 EXPECT_TRUE(security_state->GetDomainState( 4908 SpawnedTestServer::kLocalhost, sni_available, &domain_state)); 4909 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, 4910 domain_state.upgrade_mode); 4911#if defined(OS_ANDROID) 4912 // Android's CertVerifyProc does not (yet) handle pins. 4913#else 4914 EXPECT_TRUE(domain_state.HasPublicKeyPins()); 4915#endif 4916 EXPECT_NE(domain_state.upgrade_expiry, 4917 domain_state.dynamic_spki_hashes_expiry); 4918 4919 // Even though there is an HSTS header asserting includeSubdomains, it is 4920 // the *second* such header, and we MUST process only the first. 4921 EXPECT_FALSE(domain_state.sts_include_subdomains); 4922 // includeSubdomains does not occur in the test HPKP header. 4923 EXPECT_FALSE(domain_state.pkp_include_subdomains); 4924} 4925 4926// Tests that when multiple HPKP headers are present, asserting different 4927// policies, that only the first such policy is processed. 4928TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { 4929 SpawnedTestServer::SSLOptions ssl_options; 4930 SpawnedTestServer https_test_server( 4931 SpawnedTestServer::TYPE_HTTPS, 4932 ssl_options, 4933 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); 4934 ASSERT_TRUE(https_test_server.Start()); 4935 4936 TestDelegate d; 4937 URLRequest request( 4938 https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"), 4939 DEFAULT_PRIORITY, 4940 &d, 4941 &default_context_); 4942 request.Start(); 4943 base::RunLoop().Run(); 4944 4945 TransportSecurityState* security_state = 4946 default_context_.transport_security_state(); 4947 bool sni_available = true; 4948 TransportSecurityState::DomainState domain_state; 4949 EXPECT_TRUE(security_state->GetDomainState( 4950 SpawnedTestServer::kLocalhost, sni_available, &domain_state)); 4951 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, 4952 domain_state.upgrade_mode); 4953#if defined(OS_ANDROID) 4954 // Android's CertVerifyProc does not (yet) handle pins. 4955#else 4956 EXPECT_TRUE(domain_state.HasPublicKeyPins()); 4957#endif 4958 EXPECT_NE(domain_state.upgrade_expiry, 4959 domain_state.dynamic_spki_hashes_expiry); 4960 4961 EXPECT_TRUE(domain_state.sts_include_subdomains); 4962 EXPECT_FALSE(domain_state.pkp_include_subdomains); 4963} 4964 4965TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { 4966 ASSERT_TRUE(test_server_.Start()); 4967 4968 TestDelegate d; 4969 URLRequest req(test_server_.GetURL("files/content-type-normalization.html"), 4970 DEFAULT_PRIORITY, 4971 &d, 4972 &default_context_); 4973 req.Start(); 4974 base::RunLoop().Run(); 4975 4976 std::string mime_type; 4977 req.GetMimeType(&mime_type); 4978 EXPECT_EQ("text/html", mime_type); 4979 4980 std::string charset; 4981 req.GetCharset(&charset); 4982 EXPECT_EQ("utf-8", charset); 4983 req.Cancel(); 4984} 4985 4986TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictRedirects) { 4987 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). 4988 GURL file_url("file:///foo.txt"); 4989 GURL data_url("data:,foo"); 4990 FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current()); 4991 EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); 4992 DataProtocolHandler data_protocol_handler; 4993 EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); 4994 4995 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). 4996 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url)); 4997 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url)); 4998} 4999 5000TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { 5001 ASSERT_TRUE(test_server_.Start()); 5002 5003 TestDelegate d; 5004 URLRequest req(test_server_.GetURL("files/redirect-to-file.html"), 5005 DEFAULT_PRIORITY, 5006 &d, 5007 &default_context_); 5008 req.Start(); 5009 base::RunLoop().Run(); 5010 5011 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); 5012 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error()); 5013} 5014 5015TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { 5016 ASSERT_TRUE(test_server_.Start()); 5017 5018 TestDelegate d; 5019 URLRequest req(test_server_.GetURL("files/redirect-to-data.html"), 5020 DEFAULT_PRIORITY, 5021 &d, 5022 &default_context_); 5023 req.Start(); 5024 base::MessageLoop::current()->Run(); 5025 5026 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); 5027 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error()); 5028} 5029 5030TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { 5031 ASSERT_TRUE(test_server_.Start()); 5032 5033 TestDelegate d; 5034 URLRequest req(test_server_.GetURL("files/redirect-to-invalid-url.html"), 5035 DEFAULT_PRIORITY, 5036 &d, 5037 &default_context_); 5038 req.Start(); 5039 base::RunLoop().Run(); 5040 5041 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); 5042 EXPECT_EQ(ERR_INVALID_URL, req.status().error()); 5043} 5044 5045TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { 5046 ASSERT_TRUE(test_server_.Start()); 5047 5048 TestDelegate d; 5049 URLRequest req(test_server_.GetURL("echoheader?Referer"), 5050 DEFAULT_PRIORITY, 5051 &d, 5052 &default_context_); 5053 req.SetReferrer("http://user:pass@foo.com/"); 5054 req.Start(); 5055 base::RunLoop().Run(); 5056 5057 EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); 5058} 5059 5060TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { 5061 ASSERT_TRUE(test_server_.Start()); 5062 5063 TestDelegate d; 5064 URLRequest req(test_server_.GetURL("echoheader?Referer"), 5065 DEFAULT_PRIORITY, 5066 &d, 5067 &default_context_); 5068 req.SetReferrer("http://foo.com/test#fragment"); 5069 req.Start(); 5070 base::RunLoop().Run(); 5071 5072 EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); 5073} 5074 5075TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { 5076 ASSERT_TRUE(test_server_.Start()); 5077 5078 TestDelegate d; 5079 URLRequest req(test_server_.GetURL("echoheader?Referer"), 5080 DEFAULT_PRIORITY, 5081 &d, 5082 &default_context_); 5083 req.SetReferrer("http://foo.com/test#fragment"); 5084 req.SetReferrer(""); 5085 req.Start(); 5086 base::RunLoop().Run(); 5087 5088 EXPECT_EQ(std::string("None"), d.data_received()); 5089} 5090 5091TEST_F(URLRequestTestHTTP, CancelRedirect) { 5092 ASSERT_TRUE(test_server_.Start()); 5093 5094 TestDelegate d; 5095 { 5096 d.set_cancel_in_received_redirect(true); 5097 URLRequest req(test_server_.GetURL("files/redirect-test.html"), 5098 DEFAULT_PRIORITY, 5099 &d, 5100 &default_context_); 5101 req.Start(); 5102 base::RunLoop().Run(); 5103 5104 EXPECT_EQ(1, d.response_started_count()); 5105 EXPECT_EQ(0, d.bytes_received()); 5106 EXPECT_FALSE(d.received_data_before_response()); 5107 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); 5108 } 5109} 5110 5111TEST_F(URLRequestTestHTTP, DeferredRedirect) { 5112 ASSERT_TRUE(test_server_.Start()); 5113 5114 TestDelegate d; 5115 { 5116 d.set_quit_on_redirect(true); 5117 GURL test_url(test_server_.GetURL("files/redirect-test.html")); 5118 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_); 5119 5120 req.Start(); 5121 base::RunLoop().Run(); 5122 5123 EXPECT_EQ(1, d.received_redirect_count()); 5124 5125 req.FollowDeferredRedirect(); 5126 base::RunLoop().Run(); 5127 5128 EXPECT_EQ(1, d.response_started_count()); 5129 EXPECT_FALSE(d.received_data_before_response()); 5130 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); 5131 5132 base::FilePath path; 5133 PathService::Get(base::DIR_SOURCE_ROOT, &path); 5134 path = path.Append(FILE_PATH_LITERAL("net")); 5135 path = path.Append(FILE_PATH_LITERAL("data")); 5136 path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); 5137 path = path.Append(FILE_PATH_LITERAL("with-headers.html")); 5138 5139 std::string contents; 5140 EXPECT_TRUE(base::ReadFileToString(path, &contents)); 5141 EXPECT_EQ(contents, d.data_received()); 5142 } 5143} 5144 5145TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { 5146 ASSERT_TRUE(test_server_.Start()); 5147 5148 TestDelegate d; 5149 { 5150 d.set_quit_on_redirect(true); 5151 GURL test_url(test_server_.GetURL("files/redirect-test.html")); 5152 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_); 5153 5154 EXPECT_FALSE(d.have_full_request_headers()); 5155 5156 req.Start(); 5157 base::RunLoop().Run(); 5158 5159 EXPECT_EQ(1, d.received_redirect_count()); 5160 EXPECT_TRUE(d.have_full_request_headers()); 5161 CheckFullRequestHeaders(d.full_request_headers(), test_url); 5162 d.ClearFullRequestHeaders(); 5163 5164 req.FollowDeferredRedirect(); 5165 base::RunLoop().Run(); 5166 5167 GURL target_url(test_server_.GetURL("files/with-headers.html")); 5168 EXPECT_EQ(1, d.response_started_count()); 5169 EXPECT_TRUE(d.have_full_request_headers()); 5170 CheckFullRequestHeaders(d.full_request_headers(), target_url); 5171 EXPECT_FALSE(d.received_data_before_response()); 5172 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); 5173 5174 base::FilePath path; 5175 PathService::Get(base::DIR_SOURCE_ROOT, &path); 5176 path = path.Append(FILE_PATH_LITERAL("net")); 5177 path = path.Append(FILE_PATH_LITERAL("data")); 5178 path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); 5179 path = path.Append(FILE_PATH_LITERAL("with-headers.html")); 5180 5181 std::string contents; 5182 EXPECT_TRUE(base::ReadFileToString(path, &contents)); 5183 EXPECT_EQ(contents, d.data_received()); 5184 } 5185} 5186 5187TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { 5188 ASSERT_TRUE(test_server_.Start()); 5189 5190 TestDelegate d; 5191 { 5192 d.set_quit_on_redirect(true); 5193 URLRequest req(test_server_.GetURL("files/redirect-test.html"), 5194 DEFAULT_PRIORITY, 5195 &d, 5196 &default_context_); 5197 req.Start(); 5198 base::RunLoop().Run(); 5199 5200 EXPECT_EQ(1, d.received_redirect_count()); 5201 5202 req.Cancel(); 5203 base::RunLoop().Run(); 5204 5205 EXPECT_EQ(1, d.response_started_count()); 5206 EXPECT_EQ(0, d.bytes_received()); 5207 EXPECT_FALSE(d.received_data_before_response()); 5208 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); 5209 } 5210} 5211 5212TEST_F(URLRequestTestHTTP, VaryHeader) { 5213 ASSERT_TRUE(test_server_.Start()); 5214 5215 // Populate the cache. 5216 { 5217 TestDelegate d; 5218 URLRequest req(test_server_.GetURL("echoheadercache?foo"), 5219 DEFAULT_PRIORITY, 5220 &d, 5221 &default_context_); 5222 HttpRequestHeaders headers; 5223 headers.SetHeader("foo", "1"); 5224 req.SetExtraRequestHeaders(headers); 5225 req.Start(); 5226 base::RunLoop().Run(); 5227 5228 LoadTimingInfo load_timing_info; 5229 req.GetLoadTimingInfo(&load_timing_info); 5230 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); 5231 } 5232 5233 // Expect a cache hit. 5234 { 5235 TestDelegate d; 5236 URLRequest req(test_server_.GetURL("echoheadercache?foo"), 5237 DEFAULT_PRIORITY, 5238 &d, 5239 &default_context_); 5240 HttpRequestHeaders headers; 5241 headers.SetHeader("foo", "1"); 5242 req.SetExtraRequestHeaders(headers); 5243 req.Start(); 5244 base::RunLoop().Run(); 5245 5246 EXPECT_TRUE(req.was_cached()); 5247 5248 LoadTimingInfo load_timing_info; 5249 req.GetLoadTimingInfo(&load_timing_info); 5250 TestLoadTimingCacheHitNoNetwork(load_timing_info); 5251 } 5252 5253 // Expect a cache miss. 5254 { 5255 TestDelegate d; 5256 URLRequest req(test_server_.GetURL("echoheadercache?foo"), 5257 DEFAULT_PRIORITY, 5258 &d, 5259 &default_context_); 5260 HttpRequestHeaders headers; 5261 headers.SetHeader("foo", "2"); 5262 req.SetExtraRequestHeaders(headers); 5263 req.Start(); 5264 base::RunLoop().Run(); 5265 5266 EXPECT_FALSE(req.was_cached()); 5267 5268 LoadTimingInfo load_timing_info; 5269 req.GetLoadTimingInfo(&load_timing_info); 5270 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); 5271 } 5272} 5273 5274TEST_F(URLRequestTestHTTP, BasicAuth) { 5275 ASSERT_TRUE(test_server_.Start()); 5276 5277 // populate the cache 5278 { 5279 TestDelegate d; 5280 d.set_credentials(AuthCredentials(kUser, kSecret)); 5281 5282 URLRequest r(test_server_.GetURL("auth-basic"), 5283 DEFAULT_PRIORITY, 5284 &d, 5285 &default_context_); 5286 r.Start(); 5287 5288 base::RunLoop().Run(); 5289 5290 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); 5291 } 5292 5293 // repeat request with end-to-end validation. since auth-basic results in a 5294 // cachable page, we expect this test to result in a 304. in which case, the 5295 // response should be fetched from the cache. 5296 { 5297 TestDelegate d; 5298 d.set_credentials(AuthCredentials(kUser, kSecret)); 5299 5300 URLRequest r(test_server_.GetURL("auth-basic"), 5301 DEFAULT_PRIORITY, 5302 &d, 5303 &default_context_); 5304 r.set_load_flags(LOAD_VALIDATE_CACHE); 5305 r.Start(); 5306 5307 base::RunLoop().Run(); 5308 5309 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); 5310 5311 // Should be the same cached document. 5312 EXPECT_TRUE(r.was_cached()); 5313 } 5314} 5315 5316// Check that Set-Cookie headers in 401 responses are respected. 5317// http://crbug.com/6450 5318TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { 5319 ASSERT_TRUE(test_server_.Start()); 5320 5321 GURL url_requiring_auth = 5322 test_server_.GetURL("auth-basic?set-cookie-if-challenged"); 5323 5324 // Request a page that will give a 401 containing a Set-Cookie header. 5325 // Verify that when the transaction is restarted, it includes the new cookie. 5326 { 5327 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 5328 TestURLRequestContext context(true); 5329 context.set_network_delegate(&network_delegate); 5330 context.Init(); 5331 5332 TestDelegate d; 5333 d.set_credentials(AuthCredentials(kUser, kSecret)); 5334 5335 URLRequest r(url_requiring_auth, DEFAULT_PRIORITY, &d, &context); 5336 r.Start(); 5337 5338 base::RunLoop().Run(); 5339 5340 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); 5341 5342 // Make sure we sent the cookie in the restarted transaction. 5343 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") 5344 != std::string::npos); 5345 } 5346 5347 // Same test as above, except this time the restart is initiated earlier 5348 // (without user intervention since identity is embedded in the URL). 5349 { 5350 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 5351 TestURLRequestContext context(true); 5352 context.set_network_delegate(&network_delegate); 5353 context.Init(); 5354 5355 TestDelegate d; 5356 5357 GURL::Replacements replacements; 5358 std::string username("user2"); 5359 std::string password("secret"); 5360 replacements.SetUsernameStr(username); 5361 replacements.SetPasswordStr(password); 5362 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); 5363 5364 URLRequest r(url_with_identity, DEFAULT_PRIORITY, &d, &context); 5365 r.Start(); 5366 5367 base::RunLoop().Run(); 5368 5369 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); 5370 5371 // Make sure we sent the cookie in the restarted transaction. 5372 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") 5373 != std::string::npos); 5374 } 5375} 5376 5377// Tests that load timing works as expected with auth and the cache. 5378TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { 5379 ASSERT_TRUE(test_server_.Start()); 5380 5381 // populate the cache 5382 { 5383 TestDelegate d; 5384 d.set_credentials(AuthCredentials(kUser, kSecret)); 5385 5386 URLRequest r(test_server_.GetURL("auth-basic"), 5387 DEFAULT_PRIORITY, 5388 &d, 5389 &default_context_); 5390 r.Start(); 5391 5392 base::RunLoop().Run(); 5393 5394 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); 5395 5396 LoadTimingInfo load_timing_info_before_auth; 5397 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( 5398 &load_timing_info_before_auth)); 5399 TestLoadTimingNotReused(load_timing_info_before_auth, 5400 CONNECT_TIMING_HAS_DNS_TIMES); 5401 5402 LoadTimingInfo load_timing_info; 5403 r.GetLoadTimingInfo(&load_timing_info); 5404 // The test server does not support keep alive sockets, so the second 5405 // request with auth should use a new socket. 5406 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); 5407 EXPECT_NE(load_timing_info_before_auth.socket_log_id, 5408 load_timing_info.socket_log_id); 5409 EXPECT_LE(load_timing_info_before_auth.receive_headers_end, 5410 load_timing_info.connect_timing.connect_start); 5411 } 5412 5413 // Repeat request with end-to-end validation. Since auth-basic results in a 5414 // cachable page, we expect this test to result in a 304. In which case, the 5415 // response should be fetched from the cache. 5416 { 5417 TestDelegate d; 5418 d.set_credentials(AuthCredentials(kUser, kSecret)); 5419 5420 URLRequest r(test_server_.GetURL("auth-basic"), 5421 DEFAULT_PRIORITY, 5422 &d, 5423 &default_context_); 5424 r.set_load_flags(LOAD_VALIDATE_CACHE); 5425 r.Start(); 5426 5427 base::RunLoop().Run(); 5428 5429 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); 5430 5431 // Should be the same cached document. 5432 EXPECT_TRUE(r.was_cached()); 5433 5434 // Since there was a request that went over the wire, the load timing 5435 // information should include connection times. 5436 LoadTimingInfo load_timing_info; 5437 r.GetLoadTimingInfo(&load_timing_info); 5438 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); 5439 } 5440} 5441 5442// In this test, we do a POST which the server will 302 redirect. 5443// The subsequent transaction should use GET, and should not send the 5444// Content-Type header. 5445// http://code.google.com/p/chromium/issues/detail?id=843 5446TEST_F(URLRequestTestHTTP, Post302RedirectGet) { 5447 ASSERT_TRUE(test_server_.Start()); 5448 5449 const char kData[] = "hello world"; 5450 5451 TestDelegate d; 5452 URLRequest req(test_server_.GetURL("files/redirect-to-echoall"), 5453 DEFAULT_PRIORITY, 5454 &d, 5455 &default_context_); 5456 req.set_method("POST"); 5457 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); 5458 5459 // Set headers (some of which are specific to the POST). 5460 HttpRequestHeaders headers; 5461 headers.AddHeadersFromString( 5462 "Content-Type: multipart/form-data; " 5463 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" 5464 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," 5465 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" 5466 "Accept-Language: en-US,en\r\n" 5467 "Accept-Charset: ISO-8859-1,*,utf-8\r\n" 5468 "Content-Length: 11\r\n" 5469 "Origin: http://localhost:1337/"); 5470 req.SetExtraRequestHeaders(headers); 5471 req.Start(); 5472 base::RunLoop().Run(); 5473 5474 std::string mime_type; 5475 req.GetMimeType(&mime_type); 5476 EXPECT_EQ("text/html", mime_type); 5477 5478 const std::string& data = d.data_received(); 5479 5480 // Check that the post-specific headers were stripped: 5481 EXPECT_FALSE(ContainsString(data, "Content-Length:")); 5482 EXPECT_FALSE(ContainsString(data, "Content-Type:")); 5483 EXPECT_FALSE(ContainsString(data, "Origin:")); 5484 5485 // These extra request headers should not have been stripped. 5486 EXPECT_TRUE(ContainsString(data, "Accept:")); 5487 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); 5488 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); 5489} 5490 5491// The following tests check that we handle mutating the request method for 5492// HTTP redirects as expected. 5493// See http://crbug.com/56373 and http://crbug.com/102130. 5494 5495TEST_F(URLRequestTestHTTP, Redirect301Tests) { 5496 ASSERT_TRUE(test_server_.Start()); 5497 5498 const GURL url = test_server_.GetURL("files/redirect301-to-echo"); 5499 5500 HTTPRedirectMethodTest(url, "POST", "GET", true); 5501 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 5502 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 5503} 5504 5505TEST_F(URLRequestTestHTTP, Redirect302Tests) { 5506 ASSERT_TRUE(test_server_.Start()); 5507 5508 const GURL url = test_server_.GetURL("files/redirect302-to-echo"); 5509 5510 HTTPRedirectMethodTest(url, "POST", "GET", true); 5511 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 5512 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 5513} 5514 5515TEST_F(URLRequestTestHTTP, Redirect303Tests) { 5516 ASSERT_TRUE(test_server_.Start()); 5517 5518 const GURL url = test_server_.GetURL("files/redirect303-to-echo"); 5519 5520 HTTPRedirectMethodTest(url, "POST", "GET", true); 5521 HTTPRedirectMethodTest(url, "PUT", "GET", true); 5522 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 5523} 5524 5525TEST_F(URLRequestTestHTTP, Redirect307Tests) { 5526 ASSERT_TRUE(test_server_.Start()); 5527 5528 const GURL url = test_server_.GetURL("files/redirect307-to-echo"); 5529 5530 HTTPRedirectMethodTest(url, "POST", "POST", true); 5531 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 5532 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 5533} 5534 5535TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { 5536 ASSERT_TRUE(test_server_.Start()); 5537 5538 const char kData[] = "hello world"; 5539 5540 TestDelegate d; 5541 URLRequest req(test_server_.GetURL("empty.html"), 5542 DEFAULT_PRIORITY, 5543 &d, 5544 &default_context_); 5545 req.set_method("POST"); 5546 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); 5547 HttpRequestHeaders headers; 5548 headers.SetHeader(HttpRequestHeaders::kContentLength, 5549 base::UintToString(arraysize(kData) - 1)); 5550 req.SetExtraRequestHeaders(headers); 5551 5552 URLRequestRedirectJob* job = new URLRequestRedirectJob( 5553 &req, &default_network_delegate_, test_server_.GetURL("echo"), 5554 URLRequestRedirectJob::REDIRECT_302_FOUND); 5555 AddTestInterceptor()->set_main_intercept_job(job); 5556 5557 req.Start(); 5558 base::RunLoop().Run(); 5559 EXPECT_EQ("GET", req.method()); 5560} 5561 5562TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { 5563 ASSERT_TRUE(test_server_.Start()); 5564 5565 const char kData[] = "hello world"; 5566 5567 TestDelegate d; 5568 URLRequest req(test_server_.GetURL("empty.html"), 5569 DEFAULT_PRIORITY, 5570 &d, 5571 &default_context_); 5572 req.set_method("POST"); 5573 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); 5574 HttpRequestHeaders headers; 5575 headers.SetHeader(HttpRequestHeaders::kContentLength, 5576 base::UintToString(arraysize(kData) - 1)); 5577 req.SetExtraRequestHeaders(headers); 5578 5579 URLRequestRedirectJob* job = new URLRequestRedirectJob( 5580 &req, &default_network_delegate_, test_server_.GetURL("echo"), 5581 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); 5582 AddTestInterceptor()->set_main_intercept_job(job); 5583 5584 req.Start(); 5585 base::RunLoop().Run(); 5586 EXPECT_EQ("POST", req.method()); 5587 EXPECT_EQ(kData, d.data_received()); 5588} 5589 5590// Check that default A-L header is sent. 5591TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { 5592 ASSERT_TRUE(test_server_.Start()); 5593 5594 StaticHttpUserAgentSettings settings("en", EmptyString()); 5595 TestNetworkDelegate network_delegate; // Must outlive URLRequests. 5596 TestURLRequestContext context(true); 5597 context.set_network_delegate(&network_delegate); 5598 context.set_http_user_agent_settings(&settings); 5599 context.Init(); 5600 5601 TestDelegate d; 5602 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"), 5603 DEFAULT_PRIORITY, 5604 &d, 5605 &context); 5606 req.Start(); 5607 base::RunLoop().Run(); 5608 EXPECT_EQ("en", d.data_received()); 5609} 5610 5611// Check that an empty A-L header is not sent. http://crbug.com/77365. 5612TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { 5613 ASSERT_TRUE(test_server_.Start()); 5614 5615 StaticHttpUserAgentSettings settings(EmptyString(), EmptyString()); 5616 TestNetworkDelegate network_delegate; // Must outlive URLRequests. 5617 TestURLRequestContext context(true); 5618 context.set_network_delegate(&network_delegate); 5619 context.Init(); 5620 // We override the language after initialization because empty entries 5621 // get overridden by Init(). 5622 context.set_http_user_agent_settings(&settings); 5623 5624 TestDelegate d; 5625 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"), 5626 DEFAULT_PRIORITY, 5627 &d, 5628 &context); 5629 req.Start(); 5630 base::RunLoop().Run(); 5631 EXPECT_EQ("None", d.data_received()); 5632} 5633 5634// Check that if request overrides the A-L header, the default is not appended. 5635// See http://crbug.com/20894 5636TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { 5637 ASSERT_TRUE(test_server_.Start()); 5638 5639 TestDelegate d; 5640 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"), 5641 DEFAULT_PRIORITY, 5642 &d, 5643 &default_context_); 5644 HttpRequestHeaders headers; 5645 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); 5646 req.SetExtraRequestHeaders(headers); 5647 req.Start(); 5648 base::RunLoop().Run(); 5649 EXPECT_EQ(std::string("ru"), d.data_received()); 5650} 5651 5652// Check that default A-E header is sent. 5653TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { 5654 ASSERT_TRUE(test_server_.Start()); 5655 5656 TestDelegate d; 5657 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), 5658 DEFAULT_PRIORITY, 5659 &d, 5660 &default_context_); 5661 HttpRequestHeaders headers; 5662 req.SetExtraRequestHeaders(headers); 5663 req.Start(); 5664 base::RunLoop().Run(); 5665 EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); 5666} 5667 5668// Check that if request overrides the A-E header, the default is not appended. 5669// See http://crbug.com/47381 5670TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { 5671 ASSERT_TRUE(test_server_.Start()); 5672 5673 TestDelegate d; 5674 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), 5675 DEFAULT_PRIORITY, 5676 &d, 5677 &default_context_); 5678 HttpRequestHeaders headers; 5679 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); 5680 req.SetExtraRequestHeaders(headers); 5681 req.Start(); 5682 base::RunLoop().Run(); 5683 EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); 5684 EXPECT_TRUE(ContainsString(d.data_received(), "identity")); 5685} 5686 5687// Check that setting the A-C header sends the proper header. 5688TEST_F(URLRequestTestHTTP, SetAcceptCharset) { 5689 ASSERT_TRUE(test_server_.Start()); 5690 5691 TestDelegate d; 5692 URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), 5693 DEFAULT_PRIORITY, 5694 &d, 5695 &default_context_); 5696 HttpRequestHeaders headers; 5697 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); 5698 req.SetExtraRequestHeaders(headers); 5699 req.Start(); 5700 base::RunLoop().Run(); 5701 EXPECT_EQ(std::string("koi-8r"), d.data_received()); 5702} 5703 5704// Check that default User-Agent header is sent. 5705TEST_F(URLRequestTestHTTP, DefaultUserAgent) { 5706 ASSERT_TRUE(test_server_.Start()); 5707 5708 TestDelegate d; 5709 URLRequest req(test_server_.GetURL("echoheader?User-Agent"), 5710 DEFAULT_PRIORITY, 5711 &d, 5712 &default_context_); 5713 req.Start(); 5714 base::RunLoop().Run(); 5715 EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received()); 5716} 5717 5718// Check that if request overrides the User-Agent header, 5719// the default is not appended. 5720TEST_F(URLRequestTestHTTP, OverrideUserAgent) { 5721 ASSERT_TRUE(test_server_.Start()); 5722 5723 TestDelegate d; 5724 URLRequest req(test_server_.GetURL("echoheader?User-Agent"), 5725 DEFAULT_PRIORITY, 5726 &d, 5727 &default_context_); 5728 HttpRequestHeaders headers; 5729 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); 5730 req.SetExtraRequestHeaders(headers); 5731 req.Start(); 5732 base::RunLoop().Run(); 5733 // If the net tests are being run with ChromeFrame then we need to allow for 5734 // the 'chromeframe' suffix which is added to the user agent before the 5735 // closing parentheses. 5736 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); 5737} 5738 5739// Check that a NULL HttpUserAgentSettings causes the corresponding empty 5740// User-Agent header to be sent but does not send the Accept-Language and 5741// Accept-Charset headers. 5742TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { 5743 ASSERT_TRUE(test_server_.Start()); 5744 5745 TestNetworkDelegate network_delegate; // Must outlive URLRequests. 5746 TestURLRequestContext context(true); 5747 context.set_network_delegate(&network_delegate); 5748 context.Init(); 5749 // We override the HttpUserAgentSettings after initialization because empty 5750 // entries get overridden by Init(). 5751 context.set_http_user_agent_settings(NULL); 5752 5753 struct { 5754 const char* request; 5755 const char* expected_response; 5756 } tests[] = { { "echoheader?Accept-Language", "None" }, 5757 { "echoheader?Accept-Charset", "None" }, 5758 { "echoheader?User-Agent", "" } }; 5759 5760 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { 5761 TestDelegate d; 5762 URLRequest req( 5763 test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, &context); 5764 req.Start(); 5765 base::RunLoop().Run(); 5766 EXPECT_EQ(tests[i].expected_response, d.data_received()) 5767 << " Request = \"" << tests[i].request << "\""; 5768 } 5769} 5770 5771// Make sure that URLRequest passes on its priority updates to 5772// newly-created jobs after the first one. 5773TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { 5774 ASSERT_TRUE(test_server_.Start()); 5775 5776 TestDelegate d; 5777 URLRequest req(test_server_.GetURL("empty.html"), 5778 DEFAULT_PRIORITY, 5779 &d, 5780 &default_context_); 5781 EXPECT_EQ(DEFAULT_PRIORITY, req.priority()); 5782 5783 scoped_refptr<URLRequestRedirectJob> redirect_job = 5784 new URLRequestRedirectJob( 5785 &req, &default_network_delegate_, test_server_.GetURL("echo"), 5786 URLRequestRedirectJob::REDIRECT_302_FOUND); 5787 AddTestInterceptor()->set_main_intercept_job(redirect_job.get()); 5788 5789 req.SetPriority(LOW); 5790 req.Start(); 5791 EXPECT_TRUE(req.is_pending()); 5792 5793 scoped_refptr<URLRequestTestJob> job = 5794 new URLRequestTestJob(&req, &default_network_delegate_); 5795 AddTestInterceptor()->set_main_intercept_job(job.get()); 5796 5797 // Should trigger |job| to be started. 5798 base::RunLoop().Run(); 5799 EXPECT_EQ(LOW, job->priority()); 5800} 5801 5802class HTTPSRequestTest : public testing::Test { 5803 public: 5804 HTTPSRequestTest() : default_context_(true) { 5805 default_context_.set_network_delegate(&default_network_delegate_); 5806 default_context_.Init(); 5807 } 5808 virtual ~HTTPSRequestTest() {} 5809 5810 protected: 5811 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. 5812 TestURLRequestContext default_context_; 5813}; 5814 5815TEST_F(HTTPSRequestTest, HTTPSGetTest) { 5816 SpawnedTestServer test_server( 5817 SpawnedTestServer::TYPE_HTTPS, 5818 SpawnedTestServer::kLocalhost, 5819 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 5820 ASSERT_TRUE(test_server.Start()); 5821 5822 TestDelegate d; 5823 { 5824 URLRequest r(test_server.GetURL(std::string()), 5825 DEFAULT_PRIORITY, 5826 &d, 5827 &default_context_); 5828 r.Start(); 5829 EXPECT_TRUE(r.is_pending()); 5830 5831 base::RunLoop().Run(); 5832 5833 EXPECT_EQ(1, d.response_started_count()); 5834 EXPECT_FALSE(d.received_data_before_response()); 5835 EXPECT_NE(0, d.bytes_received()); 5836 CheckSSLInfo(r.ssl_info()); 5837 EXPECT_EQ(test_server.host_port_pair().host(), 5838 r.GetSocketAddress().host()); 5839 EXPECT_EQ(test_server.host_port_pair().port(), 5840 r.GetSocketAddress().port()); 5841 } 5842} 5843 5844TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { 5845 SpawnedTestServer::SSLOptions ssl_options( 5846 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); 5847 SpawnedTestServer test_server( 5848 SpawnedTestServer::TYPE_HTTPS, 5849 ssl_options, 5850 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 5851 ASSERT_TRUE(test_server.Start()); 5852 5853 bool err_allowed = true; 5854 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { 5855 TestDelegate d; 5856 { 5857 d.set_allow_certificate_errors(err_allowed); 5858 URLRequest r(test_server.GetURL(std::string()), 5859 DEFAULT_PRIORITY, 5860 &d, 5861 &default_context_); 5862 5863 r.Start(); 5864 EXPECT_TRUE(r.is_pending()); 5865 5866 base::RunLoop().Run(); 5867 5868 EXPECT_EQ(1, d.response_started_count()); 5869 EXPECT_FALSE(d.received_data_before_response()); 5870 EXPECT_TRUE(d.have_certificate_errors()); 5871 if (err_allowed) { 5872 EXPECT_NE(0, d.bytes_received()); 5873 CheckSSLInfo(r.ssl_info()); 5874 } else { 5875 EXPECT_EQ(0, d.bytes_received()); 5876 } 5877 } 5878 } 5879} 5880 5881TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { 5882 SpawnedTestServer::SSLOptions ssl_options( 5883 SpawnedTestServer::SSLOptions::CERT_EXPIRED); 5884 SpawnedTestServer test_server( 5885 SpawnedTestServer::TYPE_HTTPS, 5886 ssl_options, 5887 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 5888 ASSERT_TRUE(test_server.Start()); 5889 5890 // Iterate from false to true, just so that we do the opposite of the 5891 // previous test in order to increase test coverage. 5892 bool err_allowed = false; 5893 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { 5894 TestDelegate d; 5895 { 5896 d.set_allow_certificate_errors(err_allowed); 5897 URLRequest r(test_server.GetURL(std::string()), 5898 DEFAULT_PRIORITY, 5899 &d, 5900 &default_context_); 5901 5902 r.Start(); 5903 EXPECT_TRUE(r.is_pending()); 5904 5905 base::RunLoop().Run(); 5906 5907 EXPECT_EQ(1, d.response_started_count()); 5908 EXPECT_FALSE(d.received_data_before_response()); 5909 EXPECT_TRUE(d.have_certificate_errors()); 5910 if (err_allowed) { 5911 EXPECT_NE(0, d.bytes_received()); 5912 CheckSSLInfo(r.ssl_info()); 5913 } else { 5914 EXPECT_EQ(0, d.bytes_received()); 5915 } 5916 } 5917 } 5918} 5919 5920// Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more 5921// than necessary. 5922TEST_F(HTTPSRequestTest, TLSv1Fallback) { 5923 uint16 default_version_max = SSLConfigService::default_version_max(); 5924 // The OpenSSL library in use may not support TLS 1.1. 5925#if !defined(USE_OPENSSL) 5926 EXPECT_GT(default_version_max, SSL_PROTOCOL_VERSION_TLS1); 5927#endif 5928 if (default_version_max <= SSL_PROTOCOL_VERSION_TLS1) 5929 return; 5930 5931 SpawnedTestServer::SSLOptions ssl_options( 5932 SpawnedTestServer::SSLOptions::CERT_OK); 5933 ssl_options.tls_intolerant = 5934 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; 5935 SpawnedTestServer test_server( 5936 SpawnedTestServer::TYPE_HTTPS, 5937 ssl_options, 5938 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 5939 ASSERT_TRUE(test_server.Start()); 5940 5941 TestDelegate d; 5942 TestURLRequestContext context(true); 5943 context.Init(); 5944 d.set_allow_certificate_errors(true); 5945 URLRequest r( 5946 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); 5947 r.Start(); 5948 5949 base::RunLoop().Run(); 5950 5951 EXPECT_EQ(1, d.response_started_count()); 5952 EXPECT_NE(0, d.bytes_received()); 5953 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_TLS1), 5954 SSLConnectionStatusToVersion(r.ssl_info().connection_status)); 5955 EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK); 5956} 5957 5958// This tests that a load of www.google.com with a certificate error sets 5959// the |certificate_errors_are_fatal| flag correctly. This flag will cause 5960// the interstitial to be fatal. 5961TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { 5962 SpawnedTestServer::SSLOptions ssl_options( 5963 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); 5964 SpawnedTestServer test_server( 5965 SpawnedTestServer::TYPE_HTTPS, 5966 ssl_options, 5967 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 5968 ASSERT_TRUE(test_server.Start()); 5969 5970 // We require that the URL be www.google.com in order to pick up the 5971 // preloaded HSTS entries in the TransportSecurityState. This means that we 5972 // have to use a MockHostResolver in order to direct www.google.com to the 5973 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. 5974 5975 MockHostResolver host_resolver; 5976 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 5977 TestURLRequestContext context(true); 5978 context.set_network_delegate(&network_delegate); 5979 context.set_host_resolver(&host_resolver); 5980 TransportSecurityState transport_security_state; 5981 context.set_transport_security_state(&transport_security_state); 5982 context.Init(); 5983 5984 TestDelegate d; 5985 URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d", 5986 test_server.host_port_pair().port())), 5987 DEFAULT_PRIORITY, 5988 &d, 5989 &context); 5990 5991 r.Start(); 5992 EXPECT_TRUE(r.is_pending()); 5993 5994 base::RunLoop().Run(); 5995 5996 EXPECT_EQ(1, d.response_started_count()); 5997 EXPECT_FALSE(d.received_data_before_response()); 5998 EXPECT_TRUE(d.have_certificate_errors()); 5999 EXPECT_TRUE(d.certificate_errors_are_fatal()); 6000} 6001 6002// This tests that cached HTTPS page loads do not cause any updates to the 6003// TransportSecurityState. 6004TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { 6005 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't 6006 // matter. It just has to be any error. 6007 SpawnedTestServer::SSLOptions ssl_options( 6008 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); 6009 SpawnedTestServer test_server( 6010 SpawnedTestServer::TYPE_HTTPS, 6011 ssl_options, 6012 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 6013 ASSERT_TRUE(test_server.Start()); 6014 6015 // We require that the URL be www.google.com in order to pick up the 6016 // preloaded and dynamic HSTS and public key pin entries in the 6017 // TransportSecurityState. This means that we have to use a 6018 // MockHostResolver in order to direct www.google.com to the testserver. 6019 // By default, MockHostResolver maps all hosts to 127.0.0.1. 6020 6021 MockHostResolver host_resolver; 6022 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 6023 TestURLRequestContext context(true); 6024 context.set_network_delegate(&network_delegate); 6025 context.set_host_resolver(&host_resolver); 6026 TransportSecurityState transport_security_state; 6027 TransportSecurityState::DomainState domain_state; 6028 EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true, 6029 &domain_state)); 6030 context.set_transport_security_state(&transport_security_state); 6031 context.Init(); 6032 6033 TestDelegate d; 6034 URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d", 6035 test_server.host_port_pair().port())), 6036 DEFAULT_PRIORITY, 6037 &d, 6038 &context); 6039 6040 r.Start(); 6041 EXPECT_TRUE(r.is_pending()); 6042 6043 base::RunLoop().Run(); 6044 6045 EXPECT_EQ(1, d.response_started_count()); 6046 EXPECT_FALSE(d.received_data_before_response()); 6047 EXPECT_TRUE(d.have_certificate_errors()); 6048 EXPECT_TRUE(d.certificate_errors_are_fatal()); 6049 6050 // Get a fresh copy of the state, and check that it hasn't been updated. 6051 TransportSecurityState::DomainState new_domain_state; 6052 EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true, 6053 &new_domain_state)); 6054 EXPECT_EQ(new_domain_state.upgrade_mode, domain_state.upgrade_mode); 6055 EXPECT_EQ(new_domain_state.sts_include_subdomains, 6056 domain_state.sts_include_subdomains); 6057 EXPECT_EQ(new_domain_state.pkp_include_subdomains, 6058 domain_state.pkp_include_subdomains); 6059 EXPECT_TRUE(FingerprintsEqual(new_domain_state.static_spki_hashes, 6060 domain_state.static_spki_hashes)); 6061 EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes, 6062 domain_state.dynamic_spki_hashes)); 6063 EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_static_spki_hashes, 6064 domain_state.bad_static_spki_hashes)); 6065} 6066 6067// Make sure HSTS preserves a POST request's method and body. 6068TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { 6069 static const char kData[] = "hello world"; 6070 6071 SpawnedTestServer::SSLOptions ssl_options( 6072 SpawnedTestServer::SSLOptions::CERT_OK); 6073 SpawnedTestServer test_server( 6074 SpawnedTestServer::TYPE_HTTPS, 6075 ssl_options, 6076 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 6077 ASSERT_TRUE(test_server.Start()); 6078 6079 6080 // Per spec, TransportSecurityState expects a domain name, rather than an IP 6081 // address, so a MockHostResolver is needed to redirect www.somewhere.com to 6082 // the SpawnedTestServer. By default, MockHostResolver maps all hosts 6083 // to 127.0.0.1. 6084 MockHostResolver host_resolver; 6085 6086 // Force https for www.somewhere.com. 6087 TransportSecurityState transport_security_state; 6088 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); 6089 bool include_subdomains = false; 6090 transport_security_state.AddHSTS("www.somewhere.com", expiry, 6091 include_subdomains); 6092 6093 TestNetworkDelegate network_delegate; // Must outlive URLRequest. 6094 6095 TestURLRequestContext context(true); 6096 context.set_host_resolver(&host_resolver); 6097 context.set_transport_security_state(&transport_security_state); 6098 context.set_network_delegate(&network_delegate); 6099 context.Init(); 6100 6101 TestDelegate d; 6102 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will 6103 // cause a certificate error. Ignore the error. 6104 d.set_allow_certificate_errors(true); 6105 6106 URLRequest req(GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", 6107 test_server.host_port_pair().port())), 6108 DEFAULT_PRIORITY, 6109 &d, 6110 &context); 6111 req.set_method("POST"); 6112 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); 6113 6114 req.Start(); 6115 base::RunLoop().Run(); 6116 6117 EXPECT_EQ("https", req.url().scheme()); 6118 EXPECT_EQ("POST", req.method()); 6119 EXPECT_EQ(kData, d.data_received()); 6120 6121 LoadTimingInfo load_timing_info; 6122 network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); 6123 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits 6124 TestLoadTimingCacheHitNoNetwork(load_timing_info); 6125} 6126 6127TEST_F(HTTPSRequestTest, SSLv3Fallback) { 6128 SpawnedTestServer::SSLOptions ssl_options( 6129 SpawnedTestServer::SSLOptions::CERT_OK); 6130 ssl_options.tls_intolerant = 6131 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; 6132 SpawnedTestServer test_server( 6133 SpawnedTestServer::TYPE_HTTPS, 6134 ssl_options, 6135 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 6136 ASSERT_TRUE(test_server.Start()); 6137 6138 TestDelegate d; 6139 TestURLRequestContext context(true); 6140 context.Init(); 6141 d.set_allow_certificate_errors(true); 6142 URLRequest r( 6143 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); 6144 r.Start(); 6145 6146 base::RunLoop().Run(); 6147 6148 EXPECT_EQ(1, d.response_started_count()); 6149 EXPECT_NE(0, d.bytes_received()); 6150 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_SSL3), 6151 SSLConnectionStatusToVersion(r.ssl_info().connection_status)); 6152 EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK); 6153} 6154 6155namespace { 6156 6157class SSLClientAuthTestDelegate : public TestDelegate { 6158 public: 6159 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { 6160 } 6161 virtual void OnCertificateRequested( 6162 URLRequest* request, 6163 SSLCertRequestInfo* cert_request_info) OVERRIDE { 6164 on_certificate_requested_count_++; 6165 base::MessageLoop::current()->Quit(); 6166 } 6167 int on_certificate_requested_count() { 6168 return on_certificate_requested_count_; 6169 } 6170 private: 6171 int on_certificate_requested_count_; 6172}; 6173 6174} // namespace 6175 6176// TODO(davidben): Test the rest of the code. Specifically, 6177// - Filtering which certificates to select. 6178// - Sending a certificate back. 6179// - Getting a certificate request in an SSL renegotiation sending the 6180// HTTP request. 6181TEST_F(HTTPSRequestTest, ClientAuthTest) { 6182 SpawnedTestServer::SSLOptions ssl_options; 6183 ssl_options.request_client_certificate = true; 6184 SpawnedTestServer test_server( 6185 SpawnedTestServer::TYPE_HTTPS, 6186 ssl_options, 6187 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 6188 ASSERT_TRUE(test_server.Start()); 6189 6190 SSLClientAuthTestDelegate d; 6191 { 6192 URLRequest r(test_server.GetURL(std::string()), 6193 DEFAULT_PRIORITY, 6194 &d, 6195 &default_context_); 6196 6197 r.Start(); 6198 EXPECT_TRUE(r.is_pending()); 6199 6200 base::RunLoop().Run(); 6201 6202 EXPECT_EQ(1, d.on_certificate_requested_count()); 6203 EXPECT_FALSE(d.received_data_before_response()); 6204 EXPECT_EQ(0, d.bytes_received()); 6205 6206 // Send no certificate. 6207 // TODO(davidben): Get temporary client cert import (with keys) working on 6208 // all platforms so we can test sending a cert as well. 6209 r.ContinueWithCertificate(NULL); 6210 6211 base::RunLoop().Run(); 6212 6213 EXPECT_EQ(1, d.response_started_count()); 6214 EXPECT_FALSE(d.received_data_before_response()); 6215 EXPECT_NE(0, d.bytes_received()); 6216 } 6217} 6218 6219TEST_F(HTTPSRequestTest, ResumeTest) { 6220 // Test that we attempt a session resume when making two connections to the 6221 // same host. 6222 SpawnedTestServer::SSLOptions ssl_options; 6223 ssl_options.record_resume = true; 6224 SpawnedTestServer test_server( 6225 SpawnedTestServer::TYPE_HTTPS, 6226 ssl_options, 6227 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 6228 ASSERT_TRUE(test_server.Start()); 6229 6230 SSLClientSocket::ClearSessionCache(); 6231 6232 { 6233 TestDelegate d; 6234 URLRequest r(test_server.GetURL("ssl-session-cache"), 6235 DEFAULT_PRIORITY, 6236 &d, 6237 &default_context_); 6238 6239 r.Start(); 6240 EXPECT_TRUE(r.is_pending()); 6241 6242 base::RunLoop().Run(); 6243 6244 EXPECT_EQ(1, d.response_started_count()); 6245 } 6246 6247 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> 6248 CloseAllConnections(); 6249 6250 { 6251 TestDelegate d; 6252 URLRequest r(test_server.GetURL("ssl-session-cache"), 6253 DEFAULT_PRIORITY, 6254 &d, 6255 &default_context_); 6256 6257 r.Start(); 6258 EXPECT_TRUE(r.is_pending()); 6259 6260 base::RunLoop().Run(); 6261 6262 // The response will look like; 6263 // insert abc 6264 // lookup abc 6265 // insert xyz 6266 // 6267 // With a newline at the end which makes the split think that there are 6268 // four lines. 6269 6270 EXPECT_EQ(1, d.response_started_count()); 6271 std::vector<std::string> lines; 6272 base::SplitString(d.data_received(), '\n', &lines); 6273 ASSERT_EQ(4u, lines.size()) << d.data_received(); 6274 6275 std::string session_id; 6276 6277 for (size_t i = 0; i < 2; i++) { 6278 std::vector<std::string> parts; 6279 base::SplitString(lines[i], '\t', &parts); 6280 ASSERT_EQ(2u, parts.size()); 6281 if (i == 0) { 6282 EXPECT_EQ("insert", parts[0]); 6283 session_id = parts[1]; 6284 } else { 6285 EXPECT_EQ("lookup", parts[0]); 6286 EXPECT_EQ(session_id, parts[1]); 6287 } 6288 } 6289 } 6290} 6291 6292TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { 6293 // Test that sessions aren't resumed when the value of ssl_session_cache_shard 6294 // differs. 6295 SpawnedTestServer::SSLOptions ssl_options; 6296 ssl_options.record_resume = true; 6297 SpawnedTestServer test_server( 6298 SpawnedTestServer::TYPE_HTTPS, 6299 ssl_options, 6300 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 6301 ASSERT_TRUE(test_server.Start()); 6302 6303 SSLClientSocket::ClearSessionCache(); 6304 6305 { 6306 TestDelegate d; 6307 URLRequest r(test_server.GetURL("ssl-session-cache"), 6308 DEFAULT_PRIORITY, 6309 &d, 6310 &default_context_); 6311 6312 r.Start(); 6313 EXPECT_TRUE(r.is_pending()); 6314 6315 base::RunLoop().Run(); 6316 6317 EXPECT_EQ(1, d.response_started_count()); 6318 } 6319 6320 // Now create a new HttpCache with a different ssl_session_cache_shard value. 6321 HttpNetworkSession::Params params; 6322 params.host_resolver = default_context_.host_resolver(); 6323 params.cert_verifier = default_context_.cert_verifier(); 6324 params.transport_security_state = default_context_.transport_security_state(); 6325 params.proxy_service = default_context_.proxy_service(); 6326 params.ssl_config_service = default_context_.ssl_config_service(); 6327 params.http_auth_handler_factory = 6328 default_context_.http_auth_handler_factory(); 6329 params.network_delegate = &default_network_delegate_; 6330 params.http_server_properties = default_context_.http_server_properties(); 6331 params.ssl_session_cache_shard = "alternate"; 6332 6333 scoped_ptr<net::HttpCache> cache(new net::HttpCache( 6334 new net::HttpNetworkSession(params), 6335 net::HttpCache::DefaultBackend::InMemory(0))); 6336 6337 default_context_.set_http_transaction_factory(cache.get()); 6338 6339 { 6340 TestDelegate d; 6341 URLRequest r(test_server.GetURL("ssl-session-cache"), 6342 DEFAULT_PRIORITY, 6343 &d, 6344 &default_context_); 6345 6346 r.Start(); 6347 EXPECT_TRUE(r.is_pending()); 6348 6349 base::RunLoop().Run(); 6350 6351 // The response will look like; 6352 // insert abc 6353 // insert xyz 6354 // 6355 // With a newline at the end which makes the split think that there are 6356 // three lines. 6357 6358 EXPECT_EQ(1, d.response_started_count()); 6359 std::vector<std::string> lines; 6360 base::SplitString(d.data_received(), '\n', &lines); 6361 ASSERT_EQ(3u, lines.size()); 6362 6363 std::string session_id; 6364 for (size_t i = 0; i < 2; i++) { 6365 std::vector<std::string> parts; 6366 base::SplitString(lines[i], '\t', &parts); 6367 ASSERT_EQ(2u, parts.size()); 6368 EXPECT_EQ("insert", parts[0]); 6369 if (i == 0) { 6370 session_id = parts[1]; 6371 } else { 6372 EXPECT_NE(session_id, parts[1]); 6373 } 6374 } 6375 } 6376} 6377 6378class TestSSLConfigService : public SSLConfigService { 6379 public: 6380 TestSSLConfigService(bool ev_enabled, 6381 bool online_rev_checking, 6382 bool rev_checking_required_local_anchors) 6383 : ev_enabled_(ev_enabled), 6384 online_rev_checking_(online_rev_checking), 6385 rev_checking_required_local_anchors_( 6386 rev_checking_required_local_anchors) {} 6387 6388 // SSLConfigService: 6389 virtual void GetSSLConfig(SSLConfig* config) OVERRIDE { 6390 *config = SSLConfig(); 6391 config->rev_checking_enabled = online_rev_checking_; 6392 config->verify_ev_cert = ev_enabled_; 6393 config->rev_checking_required_local_anchors = 6394 rev_checking_required_local_anchors_; 6395 } 6396 6397 protected: 6398 virtual ~TestSSLConfigService() {} 6399 6400 private: 6401 const bool ev_enabled_; 6402 const bool online_rev_checking_; 6403 const bool rev_checking_required_local_anchors_; 6404}; 6405 6406// This the fingerprint of the "Testing CA" certificate used by the testserver. 6407// See net/data/ssl/certificates/ocsp-test-root.pem. 6408static const SHA1HashValue kOCSPTestCertFingerprint = 6409 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, 6410 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; 6411 6412// This is the SHA256, SPKI hash of the "Testing CA" certificate used by the 6413// testserver. 6414static const SHA256HashValue kOCSPTestCertSPKI = { { 6415 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, 6416 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, 6417 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, 6418 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, 6419} }; 6420 6421// This is the policy OID contained in the certificates that testserver 6422// generates. 6423static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; 6424 6425class HTTPSOCSPTest : public HTTPSRequestTest { 6426 public: 6427 HTTPSOCSPTest() 6428 : context_(true), 6429 ev_test_policy_( 6430 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), 6431 kOCSPTestCertFingerprint, 6432 kOCSPTestCertPolicy)) { 6433 } 6434 6435 virtual void SetUp() OVERRIDE { 6436 SetupContext(&context_); 6437 context_.Init(); 6438 6439 scoped_refptr<net::X509Certificate> root_cert = 6440 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); 6441 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert); 6442 test_root_.reset(new ScopedTestRoot(root_cert.get())); 6443 6444#if defined(USE_NSS) || defined(OS_IOS) 6445 SetURLRequestContextForNSSHttpIO(&context_); 6446 EnsureNSSHttpIOInit(); 6447#endif 6448 } 6449 6450 void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, 6451 CertStatus* out_cert_status) { 6452 // We always overwrite out_cert_status. 6453 *out_cert_status = 0; 6454 SpawnedTestServer test_server( 6455 SpawnedTestServer::TYPE_HTTPS, 6456 ssl_options, 6457 base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); 6458 ASSERT_TRUE(test_server.Start()); 6459 6460 TestDelegate d; 6461 d.set_allow_certificate_errors(true); 6462 URLRequest r( 6463 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context_); 6464 r.Start(); 6465 6466 base::RunLoop().Run(); 6467 6468 EXPECT_EQ(1, d.response_started_count()); 6469 *out_cert_status = r.ssl_info().cert_status; 6470 } 6471 6472 virtual ~HTTPSOCSPTest() { 6473#if defined(USE_NSS) || defined(OS_IOS) 6474 ShutdownNSSHttpIO(); 6475#endif 6476 } 6477 6478 protected: 6479 // SetupContext configures the URLRequestContext that will be used for making 6480 // connetions to testserver. This can be overridden in test subclasses for 6481 // different behaviour. 6482 virtual void SetupContext(URLRequestContext* context) { 6483 context->set_ssl_config_service( 6484 new TestSSLConfigService(true /* check for EV */, 6485 true /* online revocation checking */, 6486 false /* require rev. checking for local 6487 anchors */)); 6488 } 6489 6490 scoped_ptr<ScopedTestRoot> test_root_; 6491 TestURLRequestContext context_; 6492 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_; 6493}; 6494 6495static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { 6496#if defined(OS_WIN) 6497 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't 6498 // have that ability on other platforms. 6499 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; 6500#else 6501 return 0; 6502#endif 6503} 6504 6505// SystemSupportsHardFailRevocationChecking returns true iff the current 6506// operating system supports revocation checking and can distinguish between 6507// situations where a given certificate lacks any revocation information (eg: 6508// no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when 6509// revocation information cannot be obtained (eg: the CRL was unreachable). 6510// If it does not, then tests which rely on 'hard fail' behaviour should be 6511// skipped. 6512static bool SystemSupportsHardFailRevocationChecking() { 6513#if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS) 6514 return true; 6515#else 6516 return false; 6517#endif 6518} 6519 6520// SystemUsesChromiumEVMetadata returns true iff the current operating system 6521// uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then 6522// several tests are effected because our testing EV certificate won't be 6523// recognised as EV. 6524static bool SystemUsesChromiumEVMetadata() { 6525#if defined(USE_OPENSSL) 6526 // http://crbug.com/117478 - OpenSSL does not support EV validation. 6527 return false; 6528#elif defined(OS_MACOSX) && !defined(OS_IOS) 6529 // On OS X, we use the system to tell us whether a certificate is EV or not 6530 // and the system won't recognise our testing root. 6531 return false; 6532#else 6533 return true; 6534#endif 6535} 6536 6537static bool SystemSupportsOCSP() { 6538#if defined(USE_OPENSSL) 6539 // http://crbug.com/117478 - OpenSSL does not support OCSP. 6540 return false; 6541#elif defined(OS_WIN) 6542 return base::win::GetVersion() >= base::win::VERSION_VISTA; 6543#elif defined(OS_ANDROID) 6544 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. 6545 return false; 6546#else 6547 return true; 6548#endif 6549} 6550 6551TEST_F(HTTPSOCSPTest, Valid) { 6552 if (!SystemSupportsOCSP()) { 6553 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6554 return; 6555 } 6556 6557 SpawnedTestServer::SSLOptions ssl_options( 6558 SpawnedTestServer::SSLOptions::CERT_AUTO); 6559 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; 6560 6561 CertStatus cert_status; 6562 DoConnection(ssl_options, &cert_status); 6563 6564 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); 6565 6566 EXPECT_EQ(SystemUsesChromiumEVMetadata(), 6567 static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); 6568 6569 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); 6570} 6571 6572TEST_F(HTTPSOCSPTest, Revoked) { 6573 if (!SystemSupportsOCSP()) { 6574 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6575 return; 6576 } 6577 6578 SpawnedTestServer::SSLOptions ssl_options( 6579 SpawnedTestServer::SSLOptions::CERT_AUTO); 6580 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; 6581 6582 CertStatus cert_status; 6583 DoConnection(ssl_options, &cert_status); 6584 6585#if !(defined(OS_MACOSX) && !defined(OS_IOS)) 6586 // Doesn't pass on OS X yet for reasons that need to be investigated. 6587 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); 6588#endif 6589 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6590 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); 6591} 6592 6593TEST_F(HTTPSOCSPTest, Invalid) { 6594 if (!SystemSupportsOCSP()) { 6595 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6596 return; 6597 } 6598 6599 SpawnedTestServer::SSLOptions ssl_options( 6600 SpawnedTestServer::SSLOptions::CERT_AUTO); 6601 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; 6602 6603 CertStatus cert_status; 6604 DoConnection(ssl_options, &cert_status); 6605 6606 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), 6607 cert_status & CERT_STATUS_ALL_ERRORS); 6608 6609 // Without a positive OCSP response, we shouldn't show the EV status. 6610 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6611 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); 6612} 6613 6614class HTTPSHardFailTest : public HTTPSOCSPTest { 6615 protected: 6616 virtual void SetupContext(URLRequestContext* context) OVERRIDE { 6617 context->set_ssl_config_service( 6618 new TestSSLConfigService(false /* check for EV */, 6619 false /* online revocation checking */, 6620 true /* require rev. checking for local 6621 anchors */)); 6622 } 6623}; 6624 6625 6626TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { 6627 if (!SystemSupportsOCSP()) { 6628 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6629 return; 6630 } 6631 6632 if (!SystemSupportsHardFailRevocationChecking()) { 6633 LOG(WARNING) << "Skipping test because system doesn't support hard fail " 6634 << "revocation checking"; 6635 return; 6636 } 6637 6638 SpawnedTestServer::SSLOptions ssl_options( 6639 SpawnedTestServer::SSLOptions::CERT_AUTO); 6640 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; 6641 6642 CertStatus cert_status; 6643 DoConnection(ssl_options, &cert_status); 6644 6645 EXPECT_EQ(CERT_STATUS_REVOKED, 6646 cert_status & CERT_STATUS_REVOKED); 6647 6648 // Without a positive OCSP response, we shouldn't show the EV status. 6649 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); 6650} 6651 6652class HTTPSEVCRLSetTest : public HTTPSOCSPTest { 6653 protected: 6654 virtual void SetupContext(URLRequestContext* context) OVERRIDE { 6655 context->set_ssl_config_service( 6656 new TestSSLConfigService(true /* check for EV */, 6657 false /* online revocation checking */, 6658 false /* require rev. checking for local 6659 anchors */)); 6660 } 6661}; 6662 6663TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { 6664 if (!SystemSupportsOCSP()) { 6665 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6666 return; 6667 } 6668 6669 SpawnedTestServer::SSLOptions ssl_options( 6670 SpawnedTestServer::SSLOptions::CERT_AUTO); 6671 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; 6672 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); 6673 6674 CertStatus cert_status; 6675 DoConnection(ssl_options, &cert_status); 6676 6677 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), 6678 cert_status & CERT_STATUS_ALL_ERRORS); 6679 6680 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6681 EXPECT_EQ(SystemUsesChromiumEVMetadata(), 6682 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); 6683} 6684 6685TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { 6686 if (!SystemSupportsOCSP()) { 6687 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6688 return; 6689 } 6690 6691 SpawnedTestServer::SSLOptions ssl_options( 6692 SpawnedTestServer::SSLOptions::CERT_AUTO); 6693 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; 6694 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); 6695 6696 CertStatus cert_status; 6697 DoConnection(ssl_options, &cert_status); 6698 6699 // Currently only works for Windows. When using NSS or OS X, it's not 6700 // possible to determine whether the check failed because of actual 6701 // revocation or because there was an OCSP failure. 6702#if defined(OS_WIN) 6703 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); 6704#else 6705 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); 6706#endif 6707 6708 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6709 EXPECT_EQ(SystemUsesChromiumEVMetadata(), 6710 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); 6711} 6712 6713TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { 6714 if (!SystemSupportsOCSP()) { 6715 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6716 return; 6717 } 6718 6719 SpawnedTestServer::SSLOptions ssl_options( 6720 SpawnedTestServer::SSLOptions::CERT_AUTO); 6721 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; 6722 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); 6723 6724 CertStatus cert_status; 6725 DoConnection(ssl_options, &cert_status); 6726 6727 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); 6728 6729 EXPECT_EQ(SystemUsesChromiumEVMetadata(), 6730 static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); 6731 EXPECT_EQ(SystemUsesChromiumEVMetadata(), 6732 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); 6733} 6734 6735TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { 6736 if (!SystemSupportsOCSP()) { 6737 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6738 return; 6739 } 6740 6741 SpawnedTestServer::SSLOptions ssl_options( 6742 SpawnedTestServer::SSLOptions::CERT_AUTO); 6743 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; 6744 SSLConfigService::SetCRLSet( 6745 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); 6746 6747 CertStatus cert_status; 6748 DoConnection(ssl_options, &cert_status); 6749 6750 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), 6751 cert_status & CERT_STATUS_ALL_ERRORS); 6752 6753 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6754 EXPECT_EQ(SystemUsesChromiumEVMetadata(), 6755 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); 6756} 6757 6758TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { 6759 if (!SystemSupportsOCSP()) { 6760 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6761 return; 6762 } 6763 6764 SpawnedTestServer::SSLOptions ssl_options( 6765 SpawnedTestServer::SSLOptions::CERT_AUTO); 6766 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; 6767 SSLConfigService::SetCRLSet( 6768 scoped_refptr<CRLSet>(CRLSet::ForTesting( 6769 false, &kOCSPTestCertSPKI, ""))); 6770 6771 CertStatus cert_status; 6772 DoConnection(ssl_options, &cert_status); 6773 6774 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a 6775 // revocation check for EV. 6776 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); 6777 EXPECT_EQ(SystemUsesChromiumEVMetadata(), 6778 static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); 6779 EXPECT_FALSE( 6780 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); 6781} 6782 6783TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { 6784 if (!SystemSupportsOCSP()) { 6785 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6786 return; 6787 } 6788 6789 SpawnedTestServer::SSLOptions ssl_options( 6790 SpawnedTestServer::SSLOptions::CERT_AUTO); 6791 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; 6792 SSLConfigService::SetCRLSet( 6793 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); 6794 6795 CertStatus cert_status = 0; 6796 DoConnection(ssl_options, &cert_status); 6797 6798 // Even with a fresh CRLSet, we should still do online revocation checks when 6799 // the certificate chain isn't covered by the CRLSet, which it isn't in this 6800 // test. 6801 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), 6802 cert_status & CERT_STATUS_ALL_ERRORS); 6803 6804 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6805 EXPECT_EQ(SystemUsesChromiumEVMetadata(), 6806 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); 6807} 6808 6809TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { 6810 // Test that when EV verification is requested, but online revocation 6811 // checking is disabled, and the leaf certificate is not in fact EV, that 6812 // no revocation checking actually happens. 6813 if (!SystemSupportsOCSP()) { 6814 LOG(WARNING) << "Skipping test because system doesn't support OCSP"; 6815 return; 6816 } 6817 6818 // Unmark the certificate's OID as EV, which should disable revocation 6819 // checking (as per the user preference) 6820 ev_test_policy_.reset(); 6821 6822 SpawnedTestServer::SSLOptions ssl_options( 6823 SpawnedTestServer::SSLOptions::CERT_AUTO); 6824 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; 6825 SSLConfigService::SetCRLSet( 6826 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); 6827 6828 CertStatus cert_status; 6829 DoConnection(ssl_options, &cert_status); 6830 6831 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); 6832 6833 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6834 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); 6835} 6836 6837class HTTPSCRLSetTest : public HTTPSOCSPTest { 6838 protected: 6839 virtual void SetupContext(URLRequestContext* context) OVERRIDE { 6840 context->set_ssl_config_service( 6841 new TestSSLConfigService(false /* check for EV */, 6842 false /* online revocation checking */, 6843 false /* require rev. checking for local 6844 anchors */)); 6845 } 6846}; 6847 6848TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { 6849 SpawnedTestServer::SSLOptions ssl_options( 6850 SpawnedTestServer::SSLOptions::CERT_AUTO); 6851 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; 6852 SSLConfigService::SetCRLSet( 6853 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); 6854 6855 CertStatus cert_status; 6856 DoConnection(ssl_options, &cert_status); 6857 6858 // If we're not trying EV verification then, even if the CRLSet has expired, 6859 // we don't fall back to online revocation checks. 6860 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); 6861 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6862 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); 6863} 6864 6865TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { 6866#if defined(USE_OPENSSL) 6867 LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; 6868 return; 6869#endif 6870 6871 SpawnedTestServer::SSLOptions ssl_options( 6872 SpawnedTestServer::SSLOptions::CERT_AUTO); 6873 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; 6874 ssl_options.cert_serial = 10; 6875 SSLConfigService::SetCRLSet( 6876 scoped_refptr<CRLSet>(CRLSet::ForTesting( 6877 false, &kOCSPTestCertSPKI, "\x0a"))); 6878 6879 CertStatus cert_status = 0; 6880 DoConnection(ssl_options, &cert_status); 6881 6882 // If the certificate is recorded as revoked in the CRLSet, that should be 6883 // reflected without online revocation checking. 6884 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); 6885 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); 6886 EXPECT_FALSE( 6887 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); 6888} 6889#endif // !defined(OS_IOS) 6890 6891#if !defined(DISABLE_FTP_SUPPORT) 6892class URLRequestTestFTP : public URLRequestTest { 6893 public: 6894 URLRequestTestFTP() 6895 : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost, 6896 base::FilePath()) { 6897 } 6898 6899 protected: 6900 SpawnedTestServer test_server_; 6901}; 6902 6903// Make sure an FTP request using an unsafe ports fails. 6904TEST_F(URLRequestTestFTP, UnsafePort) { 6905 ASSERT_TRUE(test_server_.Start()); 6906 6907 URLRequestJobFactoryImpl job_factory; 6908 FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver()); 6909 6910 GURL url("ftp://127.0.0.1:7"); 6911 job_factory.SetProtocolHandler( 6912 "ftp", 6913 new FtpProtocolHandler(&ftp_transaction_factory)); 6914 default_context_.set_job_factory(&job_factory); 6915 6916 TestDelegate d; 6917 { 6918 URLRequest r(url, DEFAULT_PRIORITY, &d, &default_context_); 6919 r.Start(); 6920 EXPECT_TRUE(r.is_pending()); 6921 6922 base::RunLoop().Run(); 6923 6924 EXPECT_FALSE(r.is_pending()); 6925 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); 6926 EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error()); 6927 } 6928} 6929 6930// Flaky, see http://crbug.com/25045. 6931TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) { 6932 ASSERT_TRUE(test_server_.Start()); 6933 6934 TestDelegate d; 6935 { 6936 URLRequest r( 6937 test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, &default_context_); 6938 r.Start(); 6939 EXPECT_TRUE(r.is_pending()); 6940 6941 base::RunLoop().Run(); 6942 6943 EXPECT_FALSE(r.is_pending()); 6944 EXPECT_EQ(1, d.response_started_count()); 6945 EXPECT_FALSE(d.received_data_before_response()); 6946 EXPECT_LT(0, d.bytes_received()); 6947 EXPECT_EQ(test_server_.host_port_pair().host(), 6948 r.GetSocketAddress().host()); 6949 EXPECT_EQ(test_server_.host_port_pair().port(), 6950 r.GetSocketAddress().port()); 6951 } 6952} 6953 6954// Flaky, see http://crbug.com/25045. 6955TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) { 6956 ASSERT_TRUE(test_server_.Start()); 6957 6958 base::FilePath app_path; 6959 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 6960 app_path = app_path.AppendASCII("LICENSE"); 6961 TestDelegate d; 6962 { 6963 URLRequest r(test_server_.GetURL("/LICENSE"), 6964 DEFAULT_PRIORITY, 6965 &d, 6966 &default_context_); 6967 r.Start(); 6968 EXPECT_TRUE(r.is_pending()); 6969 6970 base::RunLoop().Run(); 6971 6972 int64 file_size = 0; 6973 file_util::GetFileSize(app_path, &file_size); 6974 6975 EXPECT_FALSE(r.is_pending()); 6976 EXPECT_EQ(1, d.response_started_count()); 6977 EXPECT_FALSE(d.received_data_before_response()); 6978 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); 6979 EXPECT_EQ(test_server_.host_port_pair().host(), 6980 r.GetSocketAddress().host()); 6981 EXPECT_EQ(test_server_.host_port_pair().port(), 6982 r.GetSocketAddress().port()); 6983 } 6984} 6985 6986// Flaky, see http://crbug.com/25045. 6987TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) { 6988 ASSERT_TRUE(test_server_.Start()); 6989 6990 base::FilePath app_path; 6991 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 6992 app_path = app_path.AppendASCII("LICENSE"); 6993 TestDelegate d; 6994 { 6995 URLRequest r( 6996 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), 6997 DEFAULT_PRIORITY, 6998 &d, 6999 &default_context_); 7000 r.Start(); 7001 EXPECT_TRUE(r.is_pending()); 7002 7003 base::RunLoop().Run(); 7004 7005 int64 file_size = 0; 7006 file_util::GetFileSize(app_path, &file_size); 7007 7008 EXPECT_FALSE(r.is_pending()); 7009 EXPECT_EQ(test_server_.host_port_pair().host(), 7010 r.GetSocketAddress().host()); 7011 EXPECT_EQ(test_server_.host_port_pair().port(), 7012 r.GetSocketAddress().port()); 7013 EXPECT_EQ(1, d.response_started_count()); 7014 EXPECT_FALSE(d.received_data_before_response()); 7015 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); 7016 7017 LoadTimingInfo load_timing_info; 7018 r.GetLoadTimingInfo(&load_timing_info); 7019 TestLoadTimingNoHttpResponse(load_timing_info); 7020 } 7021} 7022 7023// Flaky, see http://crbug.com/25045. 7024TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) { 7025 ASSERT_TRUE(test_server_.Start()); 7026 7027 base::FilePath app_path; 7028 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 7029 app_path = app_path.AppendASCII("LICENSE"); 7030 TestDelegate d; 7031 { 7032 URLRequest r(test_server_.GetURLWithUserAndPassword( 7033 "/LICENSE", "chrome", "wrong_password"), 7034 DEFAULT_PRIORITY, 7035 &d, 7036 &default_context_); 7037 r.Start(); 7038 EXPECT_TRUE(r.is_pending()); 7039 7040 base::RunLoop().Run(); 7041 7042 int64 file_size = 0; 7043 file_util::GetFileSize(app_path, &file_size); 7044 7045 EXPECT_FALSE(r.is_pending()); 7046 EXPECT_EQ(1, d.response_started_count()); 7047 EXPECT_FALSE(d.received_data_before_response()); 7048 EXPECT_EQ(d.bytes_received(), 0); 7049 } 7050} 7051 7052// Flaky, see http://crbug.com/25045. 7053TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) { 7054 ASSERT_TRUE(test_server_.Start()); 7055 7056 base::FilePath app_path; 7057 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 7058 app_path = app_path.AppendASCII("LICENSE"); 7059 TestDelegate d; 7060 // Set correct login credentials. The delegate will be asked for them when 7061 // the initial login with wrong credentials will fail. 7062 d.set_credentials(AuthCredentials(kChrome, kChrome)); 7063 { 7064 URLRequest r(test_server_.GetURLWithUserAndPassword( 7065 "/LICENSE", "chrome", "wrong_password"), 7066 DEFAULT_PRIORITY, 7067 &d, 7068 &default_context_); 7069 r.Start(); 7070 EXPECT_TRUE(r.is_pending()); 7071 7072 base::RunLoop().Run(); 7073 7074 int64 file_size = 0; 7075 file_util::GetFileSize(app_path, &file_size); 7076 7077 EXPECT_FALSE(r.is_pending()); 7078 EXPECT_EQ(1, d.response_started_count()); 7079 EXPECT_FALSE(d.received_data_before_response()); 7080 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); 7081 } 7082} 7083 7084// Flaky, see http://crbug.com/25045. 7085TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) { 7086 ASSERT_TRUE(test_server_.Start()); 7087 7088 base::FilePath app_path; 7089 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 7090 app_path = app_path.AppendASCII("LICENSE"); 7091 TestDelegate d; 7092 { 7093 URLRequest r(test_server_.GetURLWithUserAndPassword( 7094 "/LICENSE", "wrong_user", "chrome"), 7095 DEFAULT_PRIORITY, 7096 &d, 7097 &default_context_); 7098 r.Start(); 7099 EXPECT_TRUE(r.is_pending()); 7100 7101 base::RunLoop().Run(); 7102 7103 int64 file_size = 0; 7104 file_util::GetFileSize(app_path, &file_size); 7105 7106 EXPECT_FALSE(r.is_pending()); 7107 EXPECT_EQ(1, d.response_started_count()); 7108 EXPECT_FALSE(d.received_data_before_response()); 7109 EXPECT_EQ(d.bytes_received(), 0); 7110 } 7111} 7112 7113// Flaky, see http://crbug.com/25045. 7114TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) { 7115 ASSERT_TRUE(test_server_.Start()); 7116 7117 base::FilePath app_path; 7118 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 7119 app_path = app_path.AppendASCII("LICENSE"); 7120 TestDelegate d; 7121 // Set correct login credentials. The delegate will be asked for them when 7122 // the initial login with wrong credentials will fail. 7123 d.set_credentials(AuthCredentials(kChrome, kChrome)); 7124 { 7125 URLRequest r(test_server_.GetURLWithUserAndPassword( 7126 "/LICENSE", "wrong_user", "chrome"), 7127 DEFAULT_PRIORITY, 7128 &d, 7129 &default_context_); 7130 r.Start(); 7131 EXPECT_TRUE(r.is_pending()); 7132 7133 base::RunLoop().Run(); 7134 7135 int64 file_size = 0; 7136 file_util::GetFileSize(app_path, &file_size); 7137 7138 EXPECT_FALSE(r.is_pending()); 7139 EXPECT_EQ(1, d.response_started_count()); 7140 EXPECT_FALSE(d.received_data_before_response()); 7141 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); 7142 } 7143} 7144 7145// Flaky, see http://crbug.com/25045. 7146TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) { 7147 ASSERT_TRUE(test_server_.Start()); 7148 7149 base::FilePath app_path; 7150 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 7151 app_path = app_path.AppendASCII("LICENSE"); 7152 7153 scoped_ptr<TestDelegate> d(new TestDelegate); 7154 { 7155 // Pass correct login identity in the URL. 7156 URLRequest r( 7157 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), 7158 DEFAULT_PRIORITY, 7159 d.get(), 7160 &default_context_); 7161 r.Start(); 7162 EXPECT_TRUE(r.is_pending()); 7163 7164 base::RunLoop().Run(); 7165 7166 int64 file_size = 0; 7167 file_util::GetFileSize(app_path, &file_size); 7168 7169 EXPECT_FALSE(r.is_pending()); 7170 EXPECT_EQ(1, d->response_started_count()); 7171 EXPECT_FALSE(d->received_data_before_response()); 7172 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 7173 } 7174 7175 d.reset(new TestDelegate); 7176 { 7177 // This request should use cached identity from previous request. 7178 URLRequest r(test_server_.GetURL("/LICENSE"), 7179 DEFAULT_PRIORITY, 7180 d.get(), 7181 &default_context_); 7182 r.Start(); 7183 EXPECT_TRUE(r.is_pending()); 7184 7185 base::RunLoop().Run(); 7186 7187 int64 file_size = 0; 7188 file_util::GetFileSize(app_path, &file_size); 7189 7190 EXPECT_FALSE(r.is_pending()); 7191 EXPECT_EQ(1, d->response_started_count()); 7192 EXPECT_FALSE(d->received_data_before_response()); 7193 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 7194 } 7195} 7196 7197// Flaky, see http://crbug.com/25045. 7198TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) { 7199 ASSERT_TRUE(test_server_.Start()); 7200 7201 base::FilePath app_path; 7202 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); 7203 app_path = app_path.AppendASCII("LICENSE"); 7204 7205 scoped_ptr<TestDelegate> d(new TestDelegate); 7206 // Set correct login credentials. The delegate will be asked for them when 7207 // the initial login with wrong credentials will fail. 7208 d->set_credentials(AuthCredentials(kChrome, kChrome)); 7209 { 7210 URLRequest r(test_server_.GetURLWithUserAndPassword( 7211 "/LICENSE", "chrome", "wrong_password"), 7212 DEFAULT_PRIORITY, 7213 d.get(), 7214 &default_context_); 7215 r.Start(); 7216 EXPECT_TRUE(r.is_pending()); 7217 7218 base::RunLoop().Run(); 7219 7220 int64 file_size = 0; 7221 file_util::GetFileSize(app_path, &file_size); 7222 7223 EXPECT_FALSE(r.is_pending()); 7224 EXPECT_EQ(1, d->response_started_count()); 7225 EXPECT_FALSE(d->received_data_before_response()); 7226 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 7227 } 7228 7229 // Use a new delegate without explicit credentials. The cached ones should be 7230 // used. 7231 d.reset(new TestDelegate); 7232 { 7233 // Don't pass wrong credentials in the URL, they would override valid cached 7234 // ones. 7235 URLRequest r(test_server_.GetURL("/LICENSE"), 7236 DEFAULT_PRIORITY, 7237 d.get(), 7238 &default_context_); 7239 r.Start(); 7240 EXPECT_TRUE(r.is_pending()); 7241 7242 base::RunLoop().Run(); 7243 7244 int64 file_size = 0; 7245 file_util::GetFileSize(app_path, &file_size); 7246 7247 EXPECT_FALSE(r.is_pending()); 7248 EXPECT_EQ(1, d->response_started_count()); 7249 EXPECT_FALSE(d->received_data_before_response()); 7250 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 7251 } 7252} 7253#endif // !defined(DISABLE_FTP_SUPPORT) 7254 7255} // namespace net 7256