1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "build/build_config.h"
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/files/scoped_temp_dir.h"
20 #include "base/format_macros.h"
21 #include "base/memory/weak_ptr.h"
22 #include "base/message_loop/message_loop.h"
23 #include "base/message_loop/message_loop_proxy.h"
24 #include "base/path_service.h"
25 #include "base/run_loop.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_piece.h"
28 #include "base/strings/string_split.h"
29 #include "base/strings/string_util.h"
30 #include "base/strings/stringprintf.h"
31 #include "base/strings/utf_string_conversions.h"
32 #include "net/base/capturing_net_log.h"
33 #include "net/base/load_flags.h"
34 #include "net/base/load_timing_info.h"
35 #include "net/base/load_timing_info_test_util.h"
36 #include "net/base/net_errors.h"
37 #include "net/base/net_log.h"
38 #include "net/base/net_log_unittest.h"
39 #include "net/base/net_module.h"
40 #include "net/base/net_util.h"
41 #include "net/base/request_priority.h"
42 #include "net/base/test_data_directory.h"
43 #include "net/base/upload_bytes_element_reader.h"
44 #include "net/base/upload_data_stream.h"
45 #include "net/base/upload_file_element_reader.h"
46 #include "net/cert/ev_root_ca_metadata.h"
47 #include "net/cert/mock_cert_verifier.h"
48 #include "net/cert/test_root_certs.h"
49 #include "net/cookies/cookie_monster.h"
50 #include "net/cookies/cookie_store_test_helpers.h"
51 #include "net/disk_cache/disk_cache.h"
52 #include "net/dns/mock_host_resolver.h"
53 #include "net/ftp/ftp_network_layer.h"
54 #include "net/http/http_byte_range.h"
55 #include "net/http/http_cache.h"
56 #include "net/http/http_network_layer.h"
57 #include "net/http/http_network_session.h"
58 #include "net/http/http_request_headers.h"
59 #include "net/http/http_response_headers.h"
60 #include "net/http/http_util.h"
61 #include "net/ocsp/nss_ocsp.h"
62 #include "net/proxy/proxy_service.h"
63 #include "net/socket/ssl_client_socket.h"
64 #include "net/ssl/ssl_connection_status_flags.h"
65 #include "net/test/cert_test_util.h"
66 #include "net/test/spawned_test_server/spawned_test_server.h"
67 #include "net/url_request/data_protocol_handler.h"
68 #include "net/url_request/static_http_user_agent_settings.h"
69 #include "net/url_request/url_request.h"
70 #include "net/url_request/url_request_http_job.h"
71 #include "net/url_request/url_request_job_factory_impl.h"
72 #include "net/url_request/url_request_redirect_job.h"
73 #include "net/url_request/url_request_test_job.h"
74 #include "net/url_request/url_request_test_util.h"
75 #include "testing/gtest/include/gtest/gtest.h"
76 #include "testing/platform_test.h"
78 #if !defined(DISABLE_FILE_SUPPORT)
79 #include "net/base/filename_util.h"
80 #include "net/url_request/file_protocol_handler.h"
81 #include "net/url_request/url_request_file_dir_job.h"
84 #if !defined(DISABLE_FTP_SUPPORT)
85 #include "net/url_request/ftp_protocol_handler.h"
89 #include "base/win/scoped_com_initializer.h"
90 #include "base/win/scoped_comptr.h"
91 #include "base/win/windows_version.h"
94 using base::ASCIIToUTF16;
101 const base::string16 kChrome(ASCIIToUTF16("chrome"));
102 const base::string16 kSecret(ASCIIToUTF16("secret"));
103 const base::string16 kUser(ASCIIToUTF16("user"));
105 // Tests load timing information in the case a fresh connection was used, with
107 void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info,
108 int connect_timing_flags) {
109 EXPECT_FALSE(load_timing_info.socket_reused);
110 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
112 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
113 EXPECT_FALSE(load_timing_info.request_start.is_null());
115 EXPECT_LE(load_timing_info.request_start,
116 load_timing_info.connect_timing.connect_start);
117 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
118 connect_timing_flags);
119 EXPECT_LE(load_timing_info.connect_timing.connect_end,
120 load_timing_info.send_start);
121 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
122 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
124 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
125 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
128 // Same as above, but with proxy times.
129 void TestLoadTimingNotReusedWithProxy(
130 const LoadTimingInfo& load_timing_info,
131 int connect_timing_flags) {
132 EXPECT_FALSE(load_timing_info.socket_reused);
133 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
135 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
136 EXPECT_FALSE(load_timing_info.request_start.is_null());
138 EXPECT_LE(load_timing_info.request_start,
139 load_timing_info.proxy_resolve_start);
140 EXPECT_LE(load_timing_info.proxy_resolve_start,
141 load_timing_info.proxy_resolve_end);
142 EXPECT_LE(load_timing_info.proxy_resolve_end,
143 load_timing_info.connect_timing.connect_start);
144 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
145 connect_timing_flags);
146 EXPECT_LE(load_timing_info.connect_timing.connect_end,
147 load_timing_info.send_start);
148 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
149 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
152 // Same as above, but with a reused socket and proxy times.
153 void TestLoadTimingReusedWithProxy(
154 const LoadTimingInfo& load_timing_info) {
155 EXPECT_TRUE(load_timing_info.socket_reused);
156 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
158 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
159 EXPECT_FALSE(load_timing_info.request_start.is_null());
161 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
163 EXPECT_LE(load_timing_info.request_start,
164 load_timing_info.proxy_resolve_start);
165 EXPECT_LE(load_timing_info.proxy_resolve_start,
166 load_timing_info.proxy_resolve_end);
167 EXPECT_LE(load_timing_info.proxy_resolve_end,
168 load_timing_info.send_start);
169 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
170 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
173 // Tests load timing information in the case of a cache hit, when no cache
174 // validation request was sent over the wire.
175 base::StringPiece TestNetResourceProvider(int key) {
179 void FillBuffer(char* buffer, size_t len) {
180 static bool called = false;
183 int seed = static_cast<int>(Time::Now().ToInternalValue());
187 for (size_t i = 0; i < len; i++) {
188 buffer[i] = static_cast<char>(rand());
195 void TestLoadTimingCacheHitNoNetwork(
196 const LoadTimingInfo& load_timing_info) {
197 EXPECT_FALSE(load_timing_info.socket_reused);
198 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
200 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
201 EXPECT_FALSE(load_timing_info.request_start.is_null());
203 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
204 EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start);
205 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
206 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
208 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
209 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
212 // Tests load timing in the case that there is no HTTP response. This can be
213 // used to test in the case of errors or non-HTTP requests.
214 void TestLoadTimingNoHttpResponse(
215 const LoadTimingInfo& load_timing_info) {
216 EXPECT_FALSE(load_timing_info.socket_reused);
217 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
219 // Only the request times should be non-null.
220 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
221 EXPECT_FALSE(load_timing_info.request_start.is_null());
223 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
225 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
226 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
227 EXPECT_TRUE(load_timing_info.send_start.is_null());
228 EXPECT_TRUE(load_timing_info.send_end.is_null());
229 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
232 // Do a case-insensitive search through |haystack| for |needle|.
233 bool ContainsString(const std::string& haystack, const char* needle) {
234 std::string::const_iterator it =
235 std::search(haystack.begin(),
238 needle + strlen(needle),
239 base::CaseInsensitiveCompare<char>());
240 return it != haystack.end();
243 UploadDataStream* CreateSimpleUploadData(const char* data) {
244 scoped_ptr<UploadElementReader> reader(
245 new UploadBytesElementReader(data, strlen(data)));
246 return UploadDataStream::CreateWithReader(reader.Pass(), 0);
249 // Verify that the SSLInfo of a successful SSL connection has valid values.
250 void CheckSSLInfo(const SSLInfo& ssl_info) {
251 // -1 means unknown. 0 means no encryption.
252 EXPECT_GT(ssl_info.security_bits, 0);
254 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
255 int cipher_suite = SSLConnectionStatusToCipherSuite(
256 ssl_info.connection_status);
257 EXPECT_NE(0, cipher_suite);
260 void CheckFullRequestHeaders(const HttpRequestHeaders& headers,
261 const GURL& host_url) {
262 std::string sent_value;
264 EXPECT_TRUE(headers.GetHeader("Host", &sent_value));
265 EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value);
267 EXPECT_TRUE(headers.GetHeader("Connection", &sent_value));
268 EXPECT_EQ("keep-alive", sent_value);
271 bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
272 size_t size = a.size();
274 if (size != b.size())
277 for (size_t i = 0; i < size; ++i) {
278 if (!a[i].Equals(b[i]))
284 #endif // !defined(OS_IOS)
286 // A network delegate that allows the user to choose a subset of request stages
287 // to block in. When blocking, the delegate can do one of the following:
288 // * synchronously return a pre-specified error code, or
289 // * asynchronously return that value via an automatically called callback,
291 // * block and wait for the user to do a callback.
292 // Additionally, the user may also specify a redirect URL -- then each request
293 // with the current URL different from the redirect target will be redirected
294 // to that target, in the on-before-URL-request stage, independent of whether
295 // the delegate blocks in ON_BEFORE_URL_REQUEST or not.
296 class BlockingNetworkDelegate : public TestNetworkDelegate {
298 // Stages in which the delegate can block.
301 ON_BEFORE_URL_REQUEST = 1 << 0,
302 ON_BEFORE_SEND_HEADERS = 1 << 1,
303 ON_HEADERS_RECEIVED = 1 << 2,
304 ON_AUTH_REQUIRED = 1 << 3
307 // Behavior during blocked stages. During other stages, just
308 // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
310 SYNCHRONOUS, // No callback, returns specified return values.
311 AUTO_CALLBACK, // |this| posts a task to run the callback using the
312 // specified return codes.
313 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
314 // |auth_retval_| are ignored. In every blocking stage the
315 // message loop is quit.
318 // Creates a delegate which does not block at all.
319 explicit BlockingNetworkDelegate(BlockMode block_mode);
321 // For users to trigger a callback returning |response|.
322 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
323 // Only call if |block_mode_| == USER_CALLBACK.
324 void DoCallback(int response);
325 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
328 void set_retval(int retval) {
329 ASSERT_NE(USER_CALLBACK, block_mode_);
330 ASSERT_NE(ERR_IO_PENDING, retval);
331 ASSERT_NE(OK, retval);
335 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
336 // |auth_credentials_| will be passed with the response.
337 void set_auth_retval(AuthRequiredResponse auth_retval) {
338 ASSERT_NE(USER_CALLBACK, block_mode_);
339 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
340 auth_retval_ = auth_retval;
342 void set_auth_credentials(const AuthCredentials& auth_credentials) {
343 auth_credentials_ = auth_credentials;
346 void set_redirect_url(const GURL& url) {
350 void set_block_on(int block_on) {
351 block_on_ = block_on;
354 // Allows the user to check in which state did we block.
355 Stage stage_blocked_for_callback() const {
356 EXPECT_EQ(USER_CALLBACK, block_mode_);
357 return stage_blocked_for_callback_;
361 void RunCallback(int response, const CompletionCallback& callback);
362 void RunAuthCallback(AuthRequiredResponse response,
363 const AuthCallback& callback);
365 // TestNetworkDelegate implementation.
366 virtual int OnBeforeURLRequest(URLRequest* request,
367 const CompletionCallback& callback,
368 GURL* new_url) OVERRIDE;
370 virtual int OnBeforeSendHeaders(URLRequest* request,
371 const CompletionCallback& callback,
372 HttpRequestHeaders* headers) OVERRIDE;
374 virtual int OnHeadersReceived(
376 const CompletionCallback& callback,
377 const HttpResponseHeaders* original_response_headers,
378 scoped_refptr<HttpResponseHeaders>* override_response_headers,
379 GURL* allowed_unsafe_redirect_url) OVERRIDE;
381 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
383 const AuthChallengeInfo& auth_info,
384 const AuthCallback& callback,
385 AuthCredentials* credentials) OVERRIDE;
387 // Resets the callbacks and |stage_blocked_for_callback_|.
390 // Checks whether we should block in |stage|. If yes, returns an error code
391 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
392 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
394 // Configuration parameters, can be adjusted by public methods:
395 const BlockMode block_mode_;
397 // Values returned on blocking stages when mode is SYNCHRONOUS or
398 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
399 int retval_; // To be returned in non-auth stages.
400 AuthRequiredResponse auth_retval_;
402 GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest.
403 int block_on_; // Bit mask: in which stages to block.
405 // |auth_credentials_| will be copied to |*target_auth_credential_| on
407 AuthCredentials auth_credentials_;
408 AuthCredentials* target_auth_credentials_;
410 // Internal variables, not set by not the user:
411 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
413 Stage stage_blocked_for_callback_;
415 // Callback objects stored during blocking stages.
416 CompletionCallback callback_;
417 AuthCallback auth_callback_;
419 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
421 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
424 BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
425 : block_mode_(block_mode),
427 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
429 target_auth_credentials_(NULL),
430 stage_blocked_for_callback_(NOT_BLOCKED),
431 weak_factory_(this) {
434 void BlockingNetworkDelegate::DoCallback(int response) {
435 ASSERT_EQ(USER_CALLBACK, block_mode_);
436 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
437 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
438 CompletionCallback callback = callback_;
440 RunCallback(response, callback);
443 void BlockingNetworkDelegate::DoAuthCallback(
444 NetworkDelegate::AuthRequiredResponse response) {
445 ASSERT_EQ(USER_CALLBACK, block_mode_);
446 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
447 AuthCallback auth_callback = auth_callback_;
449 RunAuthCallback(response, auth_callback);
452 void BlockingNetworkDelegate::RunCallback(int response,
453 const CompletionCallback& callback) {
454 callback.Run(response);
457 void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
458 const AuthCallback& callback) {
459 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
460 ASSERT_TRUE(target_auth_credentials_ != NULL);
461 *target_auth_credentials_ = auth_credentials_;
463 callback.Run(response);
466 int BlockingNetworkDelegate::OnBeforeURLRequest(
468 const CompletionCallback& callback,
470 if (redirect_url_ == request->url())
471 return OK; // We've already seen this request and redirected elsewhere.
473 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
475 if (!redirect_url_.is_empty())
476 *new_url = redirect_url_;
478 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
481 int BlockingNetworkDelegate::OnBeforeSendHeaders(
483 const CompletionCallback& callback,
484 HttpRequestHeaders* headers) {
485 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
487 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
490 int BlockingNetworkDelegate::OnHeadersReceived(
492 const CompletionCallback& callback,
493 const HttpResponseHeaders* original_response_headers,
494 scoped_refptr<HttpResponseHeaders>* override_response_headers,
495 GURL* allowed_unsafe_redirect_url) {
496 TestNetworkDelegate::OnHeadersReceived(request,
498 original_response_headers,
499 override_response_headers,
500 allowed_unsafe_redirect_url);
502 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
505 NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
507 const AuthChallengeInfo& auth_info,
508 const AuthCallback& callback,
509 AuthCredentials* credentials) {
510 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
512 // Check that the user has provided callback for the previous blocked stage.
513 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
515 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
516 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
519 target_auth_credentials_ = credentials;
521 switch (block_mode_) {
523 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
524 *target_auth_credentials_ = auth_credentials_;
528 base::MessageLoop::current()->PostTask(
530 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
531 weak_factory_.GetWeakPtr(), auth_retval_, callback));
532 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
535 auth_callback_ = callback;
536 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
537 base::MessageLoop::current()->PostTask(FROM_HERE,
538 base::MessageLoop::QuitClosure());
539 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
542 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
545 void BlockingNetworkDelegate::Reset() {
546 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
547 stage_blocked_for_callback_ = NOT_BLOCKED;
549 auth_callback_.Reset();
552 int BlockingNetworkDelegate::MaybeBlockStage(
553 BlockingNetworkDelegate::Stage stage,
554 const CompletionCallback& callback) {
555 // Check that the user has provided callback for the previous blocked stage.
556 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
558 if ((block_on_ & stage) == 0) {
562 switch (block_mode_) {
564 EXPECT_NE(OK, retval_);
568 base::MessageLoop::current()->PostTask(
570 base::Bind(&BlockingNetworkDelegate::RunCallback,
571 weak_factory_.GetWeakPtr(), retval_, callback));
572 return ERR_IO_PENDING;
575 callback_ = callback;
576 stage_blocked_for_callback_ = stage;
577 base::MessageLoop::current()->PostTask(FROM_HERE,
578 base::MessageLoop::QuitClosure());
579 return ERR_IO_PENDING;
585 class TestURLRequestContextWithProxy : public TestURLRequestContext {
587 // Does not own |delegate|.
588 TestURLRequestContextWithProxy(const std::string& proxy,
589 NetworkDelegate* delegate)
590 : TestURLRequestContext(true) {
591 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
592 set_network_delegate(delegate);
595 virtual ~TestURLRequestContextWithProxy() {}
600 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.
601 class URLRequestTest : public PlatformTest {
603 URLRequestTest() : default_context_(true) {
604 default_context_.set_network_delegate(&default_network_delegate_);
605 default_context_.set_net_log(&net_log_);
606 job_factory_.SetProtocolHandler("data", new DataProtocolHandler);
607 #if !defined(DISABLE_FILE_SUPPORT)
608 job_factory_.SetProtocolHandler(
609 "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
611 default_context_.set_job_factory(&job_factory_);
612 default_context_.Init();
614 virtual ~URLRequestTest() {
615 // URLRequestJobs may post clean-up tasks on destruction.
616 base::RunLoop().RunUntilIdle();
619 // Adds the TestJobInterceptor to the default context.
620 TestJobInterceptor* AddTestInterceptor() {
621 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
622 job_factory_.SetProtocolHandler("http", NULL);
623 job_factory_.SetProtocolHandler("http", protocol_handler_);
624 return protocol_handler_;
628 CapturingNetLog net_log_;
629 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
630 URLRequestJobFactoryImpl job_factory_;
631 TestURLRequestContext default_context_;
634 TEST_F(URLRequestTest, AboutBlankTest) {
637 URLRequest r(GURL("about:blank"), DEFAULT_PRIORITY, &d, &default_context_);
640 EXPECT_TRUE(r.is_pending());
642 base::RunLoop().Run();
644 EXPECT_TRUE(!r.is_pending());
645 EXPECT_FALSE(d.received_data_before_response());
646 EXPECT_EQ(d.bytes_received(), 0);
647 EXPECT_EQ("", r.GetSocketAddress().host());
648 EXPECT_EQ(0, r.GetSocketAddress().port());
650 HttpRequestHeaders headers;
651 EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
655 TEST_F(URLRequestTest, DataURLImageTest) {
658 // Use our nice little Chrome logo.
661 "data:image/png;base64,"
662 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
663 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
664 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
665 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
666 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
667 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
668 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
669 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
670 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
671 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
672 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
673 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
674 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
675 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
676 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
677 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
678 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
679 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
685 EXPECT_TRUE(r.is_pending());
687 base::RunLoop().Run();
689 EXPECT_TRUE(!r.is_pending());
690 EXPECT_FALSE(d.received_data_before_response());
691 EXPECT_EQ(d.bytes_received(), 911);
692 EXPECT_EQ("", r.GetSocketAddress().host());
693 EXPECT_EQ(0, r.GetSocketAddress().port());
695 HttpRequestHeaders headers;
696 EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
700 #if !defined(DISABLE_FILE_SUPPORT)
701 TEST_F(URLRequestTest, FileTest) {
702 base::FilePath app_path;
703 PathService::Get(base::FILE_EXE, &app_path);
704 GURL app_url = FilePathToFileURL(app_path);
708 URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_);
711 EXPECT_TRUE(r.is_pending());
713 base::RunLoop().Run();
715 int64 file_size = -1;
716 EXPECT_TRUE(base::GetFileSize(app_path, &file_size));
718 EXPECT_TRUE(!r.is_pending());
719 EXPECT_EQ(1, d.response_started_count());
720 EXPECT_FALSE(d.received_data_before_response());
721 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
722 EXPECT_EQ("", r.GetSocketAddress().host());
723 EXPECT_EQ(0, r.GetSocketAddress().port());
725 HttpRequestHeaders headers;
726 EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
730 TEST_F(URLRequestTest, FileTestCancel) {
731 base::FilePath app_path;
732 PathService::Get(base::FILE_EXE, &app_path);
733 GURL app_url = FilePathToFileURL(app_path);
737 URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_);
740 EXPECT_TRUE(r.is_pending());
743 // Async cancellation should be safe even when URLRequest has been already
745 base::RunLoop().RunUntilIdle();
748 TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
749 const size_t buffer_size = 4000;
750 scoped_ptr<char[]> buffer(new char[buffer_size]);
751 FillBuffer(buffer.get(), buffer_size);
753 base::FilePath temp_path;
754 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
755 GURL temp_url = FilePathToFileURL(temp_path);
756 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
759 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
761 const size_t first_byte_position = 500;
762 const size_t last_byte_position = buffer_size - first_byte_position;
763 const size_t content_length = last_byte_position - first_byte_position + 1;
764 std::string partial_buffer_string(buffer.get() + first_byte_position,
765 buffer.get() + last_byte_position + 1);
769 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
771 HttpRequestHeaders headers;
773 HttpRequestHeaders::kRange,
774 HttpByteRange::Bounded(
775 first_byte_position, last_byte_position).GetHeaderValue());
776 r.SetExtraRequestHeaders(headers);
778 EXPECT_TRUE(r.is_pending());
780 base::RunLoop().Run();
781 EXPECT_TRUE(!r.is_pending());
782 EXPECT_EQ(1, d.response_started_count());
783 EXPECT_FALSE(d.received_data_before_response());
784 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
785 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
786 EXPECT_TRUE(partial_buffer_string == d.data_received());
789 EXPECT_TRUE(base::DeleteFile(temp_path, false));
792 TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
793 const size_t buffer_size = 4000;
794 scoped_ptr<char[]> buffer(new char[buffer_size]);
795 FillBuffer(buffer.get(), buffer_size);
797 base::FilePath temp_path;
798 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
799 GURL temp_url = FilePathToFileURL(temp_path);
800 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
803 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
805 const size_t first_byte_position = 500;
806 const size_t last_byte_position = buffer_size - 1;
807 const size_t content_length = last_byte_position - first_byte_position + 1;
808 std::string partial_buffer_string(buffer.get() + first_byte_position,
809 buffer.get() + last_byte_position + 1);
813 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
815 HttpRequestHeaders headers;
816 headers.SetHeader(HttpRequestHeaders::kRange,
817 HttpByteRange::RightUnbounded(
818 first_byte_position).GetHeaderValue());
819 r.SetExtraRequestHeaders(headers);
821 EXPECT_TRUE(r.is_pending());
823 base::RunLoop().Run();
824 EXPECT_TRUE(!r.is_pending());
825 EXPECT_EQ(1, d.response_started_count());
826 EXPECT_FALSE(d.received_data_before_response());
827 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
828 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
829 EXPECT_TRUE(partial_buffer_string == d.data_received());
832 EXPECT_TRUE(base::DeleteFile(temp_path, false));
835 TEST_F(URLRequestTest, FileTestMultipleRanges) {
836 const size_t buffer_size = 400000;
837 scoped_ptr<char[]> buffer(new char[buffer_size]);
838 FillBuffer(buffer.get(), buffer_size);
840 base::FilePath temp_path;
841 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
842 GURL temp_url = FilePathToFileURL(temp_path);
843 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
846 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
850 URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_);
852 HttpRequestHeaders headers;
853 headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300");
854 r.SetExtraRequestHeaders(headers);
856 EXPECT_TRUE(r.is_pending());
858 base::RunLoop().Run();
859 EXPECT_TRUE(d.request_failed());
862 EXPECT_TRUE(base::DeleteFile(temp_path, false));
865 TEST_F(URLRequestTest, AllowFileURLs) {
866 base::ScopedTempDir temp_dir;
867 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
868 base::FilePath test_file;
869 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file));
870 std::string test_data("monkey");
871 base::WriteFile(test_file, test_data.data(), test_data.size());
872 GURL test_file_url = FilePathToFileURL(test_file);
876 TestNetworkDelegate network_delegate;
877 network_delegate.set_can_access_files(true);
878 default_context_.set_network_delegate(&network_delegate);
879 URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_);
881 base::RunLoop().Run();
882 EXPECT_FALSE(d.request_failed());
883 EXPECT_EQ(test_data, d.data_received());
888 TestNetworkDelegate network_delegate;
889 network_delegate.set_can_access_files(false);
890 default_context_.set_network_delegate(&network_delegate);
891 URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_);
893 base::RunLoop().Run();
894 EXPECT_TRUE(d.request_failed());
895 EXPECT_EQ("", d.data_received());
900 TEST_F(URLRequestTest, FileDirCancelTest) {
901 // Put in mock resource provider.
902 NetModule::SetResourceProvider(TestNetResourceProvider);
906 base::FilePath file_path;
907 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
908 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
909 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
912 FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, &default_context_);
914 EXPECT_TRUE(req.is_pending());
916 d.set_cancel_in_received_data_pending(true);
918 base::RunLoop().Run();
921 // Take out mock resource provider.
922 NetModule::SetResourceProvider(NULL);
925 TEST_F(URLRequestTest, FileDirOutputSanity) {
926 // Verify the general sanity of the the output of the file:
927 // directory lister by checking for the output of a known existing
929 const char sentinel_name[] = "filedir-sentinel";
932 PathService::Get(base::DIR_SOURCE_ROOT, &path);
933 path = path.Append(FILE_PATH_LITERAL("net"));
934 path = path.Append(FILE_PATH_LITERAL("data"));
935 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
939 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_);
941 base::RunLoop().Run();
943 // Generate entry for the sentinel file.
944 base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
945 base::File::Info info;
946 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
947 EXPECT_GT(info.size, 0);
948 std::string sentinel_output = GetDirectoryListingEntry(
949 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
950 std::string(sentinel_name),
955 ASSERT_LT(0, d.bytes_received());
956 ASSERT_FALSE(d.request_failed());
957 ASSERT_TRUE(req.status().is_success());
958 // Check for the entry generated for the "sentinel" file.
959 const std::string& data = d.data_received();
960 ASSERT_NE(data.find(sentinel_output), std::string::npos);
963 TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
964 // There is an implicit redirect when loading a file path that matches a
965 // directory and does not end with a slash. Ensure that following such
966 // redirects does not crash. See http://crbug.com/18686.
969 PathService::Get(base::DIR_SOURCE_ROOT, &path);
970 path = path.Append(FILE_PATH_LITERAL("net"));
971 path = path.Append(FILE_PATH_LITERAL("data"));
972 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
976 FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_);
978 base::RunLoop().Run();
980 ASSERT_EQ(1, d.received_redirect_count());
981 ASSERT_LT(0, d.bytes_received());
982 ASSERT_FALSE(d.request_failed());
983 ASSERT_TRUE(req.status().is_success());
987 // Don't accept the url "file:///" on windows. See http://crbug.com/1474.
988 TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
990 URLRequest req(GURL("file:///"), DEFAULT_PRIORITY, &d, &default_context_);
992 base::RunLoop().Run();
994 ASSERT_EQ(1, d.received_redirect_count());
995 ASSERT_FALSE(req.status().is_success());
997 #endif // defined(OS_WIN)
999 #endif // !defined(DISABLE_FILE_SUPPORT)
1001 TEST_F(URLRequestTest, InvalidUrlTest) {
1004 URLRequest r(GURL("invalid url"), DEFAULT_PRIORITY, &d, &default_context_);
1007 EXPECT_TRUE(r.is_pending());
1009 base::RunLoop().Run();
1010 EXPECT_TRUE(d.request_failed());
1015 TEST_F(URLRequestTest, ResolveShortcutTest) {
1016 base::FilePath app_path;
1017 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1018 app_path = app_path.AppendASCII("net");
1019 app_path = app_path.AppendASCII("data");
1020 app_path = app_path.AppendASCII("url_request_unittest");
1021 app_path = app_path.AppendASCII("with-headers.html");
1023 std::wstring lnk_path = app_path.value() + L".lnk";
1025 base::win::ScopedCOMInitializer com_initializer;
1027 // Temporarily create a shortcut for test
1029 base::win::ScopedComPtr<IShellLink> shell;
1030 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL,
1031 CLSCTX_INPROC_SERVER)));
1032 base::win::ScopedComPtr<IPersistFile> persist;
1033 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive())));
1034 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
1035 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
1036 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
1041 URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)),
1047 EXPECT_TRUE(r.is_pending());
1049 base::RunLoop().Run();
1051 WIN32_FILE_ATTRIBUTE_DATA data;
1052 GetFileAttributesEx(app_path.value().c_str(),
1053 GetFileExInfoStandard, &data);
1054 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
1055 FILE_SHARE_READ, NULL, OPEN_EXISTING,
1056 FILE_ATTRIBUTE_NORMAL, NULL);
1057 EXPECT_NE(INVALID_HANDLE_VALUE, file);
1058 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
1061 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
1063 std::string content(buffer.get(), read_size);
1066 EXPECT_TRUE(!r.is_pending());
1067 EXPECT_EQ(1, d.received_redirect_count());
1068 EXPECT_EQ(content, d.data_received());
1071 // Clean the shortcut
1072 DeleteFile(lnk_path.c_str());
1074 #endif // defined(OS_WIN)
1076 // Custom URLRequestJobs for use with interceptor tests
1077 class RestartTestJob : public URLRequestTestJob {
1079 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1080 : URLRequestTestJob(request, network_delegate, true) {}
1082 virtual void StartAsync() OVERRIDE {
1083 this->NotifyRestartRequired();
1086 virtual ~RestartTestJob() {}
1089 class CancelTestJob : public URLRequestTestJob {
1091 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1092 : URLRequestTestJob(request, network_delegate, true) {}
1094 virtual void StartAsync() OVERRIDE {
1098 virtual ~CancelTestJob() {}
1101 class CancelThenRestartTestJob : public URLRequestTestJob {
1103 explicit CancelThenRestartTestJob(URLRequest* request,
1104 NetworkDelegate* network_delegate)
1105 : URLRequestTestJob(request, network_delegate, true) {
1108 virtual void StartAsync() OVERRIDE {
1110 this->NotifyRestartRequired();
1113 virtual ~CancelThenRestartTestJob() {}
1116 // An Interceptor for use with interceptor tests
1117 class TestInterceptor : URLRequest::Interceptor {
1120 : intercept_main_request_(false), restart_main_request_(false),
1121 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1122 simulate_main_network_error_(false),
1123 intercept_redirect_(false), cancel_redirect_request_(false),
1124 intercept_final_response_(false), cancel_final_request_(false),
1125 did_intercept_main_(false), did_restart_main_(false),
1126 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1127 did_simulate_error_main_(false),
1128 did_intercept_redirect_(false), did_cancel_redirect_(false),
1129 did_intercept_final_(false), did_cancel_final_(false) {
1130 URLRequest::Deprecated::RegisterRequestInterceptor(this);
1133 virtual ~TestInterceptor() {
1134 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
1137 virtual URLRequestJob* MaybeIntercept(
1138 URLRequest* request,
1139 NetworkDelegate* network_delegate) OVERRIDE {
1140 if (restart_main_request_) {
1141 restart_main_request_ = false;
1142 did_restart_main_ = true;
1143 return new RestartTestJob(request, network_delegate);
1145 if (cancel_main_request_) {
1146 cancel_main_request_ = false;
1147 did_cancel_main_ = true;
1148 return new CancelTestJob(request, network_delegate);
1150 if (cancel_then_restart_main_request_) {
1151 cancel_then_restart_main_request_ = false;
1152 did_cancel_then_restart_main_ = true;
1153 return new CancelThenRestartTestJob(request, network_delegate);
1155 if (simulate_main_network_error_) {
1156 simulate_main_network_error_ = false;
1157 did_simulate_error_main_ = true;
1158 // will error since the requeted url is not one of its canned urls
1159 return new URLRequestTestJob(request, network_delegate, true);
1161 if (!intercept_main_request_)
1163 intercept_main_request_ = false;
1164 did_intercept_main_ = true;
1165 URLRequestTestJob* job = new URLRequestTestJob(request,
1170 job->set_load_timing_info(main_request_load_timing_info_);
1174 virtual URLRequestJob* MaybeInterceptRedirect(
1175 URLRequest* request,
1176 NetworkDelegate* network_delegate,
1177 const GURL& location) OVERRIDE {
1178 if (cancel_redirect_request_) {
1179 cancel_redirect_request_ = false;
1180 did_cancel_redirect_ = true;
1181 return new CancelTestJob(request, network_delegate);
1183 if (!intercept_redirect_)
1185 intercept_redirect_ = false;
1186 did_intercept_redirect_ = true;
1187 return new URLRequestTestJob(request,
1194 virtual URLRequestJob* MaybeInterceptResponse(
1195 URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE {
1196 if (cancel_final_request_) {
1197 cancel_final_request_ = false;
1198 did_cancel_final_ = true;
1199 return new CancelTestJob(request, network_delegate);
1201 if (!intercept_final_response_)
1203 intercept_final_response_ = false;
1204 did_intercept_final_ = true;
1205 return new URLRequestTestJob(request,
1212 // Whether to intercept the main request, and if so the response to return and
1213 // the LoadTimingInfo to use.
1214 bool intercept_main_request_;
1215 std::string main_headers_;
1216 std::string main_data_;
1217 LoadTimingInfo main_request_load_timing_info_;
1219 // Other actions we take at MaybeIntercept time
1220 bool restart_main_request_;
1221 bool cancel_main_request_;
1222 bool cancel_then_restart_main_request_;
1223 bool simulate_main_network_error_;
1225 // Whether to intercept redirects, and if so the response to return.
1226 bool intercept_redirect_;
1227 std::string redirect_headers_;
1228 std::string redirect_data_;
1230 // Other actions we can take at MaybeInterceptRedirect time
1231 bool cancel_redirect_request_;
1233 // Whether to intercept final response, and if so the response to return.
1234 bool intercept_final_response_;
1235 std::string final_headers_;
1236 std::string final_data_;
1238 // Other actions we can take at MaybeInterceptResponse time
1239 bool cancel_final_request_;
1241 // If we did something or not
1242 bool did_intercept_main_;
1243 bool did_restart_main_;
1244 bool did_cancel_main_;
1245 bool did_cancel_then_restart_main_;
1246 bool did_simulate_error_main_;
1247 bool did_intercept_redirect_;
1248 bool did_cancel_redirect_;
1249 bool did_intercept_final_;
1250 bool did_cancel_final_;
1252 // Static getters for canned response header and data strings
1254 static std::string ok_data() {
1255 return URLRequestTestJob::test_data_1();
1258 static std::string ok_headers() {
1259 return URLRequestTestJob::test_headers();
1262 static std::string redirect_data() {
1263 return std::string();
1266 static std::string redirect_headers() {
1267 return URLRequestTestJob::test_redirect_headers();
1270 static std::string error_data() {
1271 return std::string("ohhh nooooo mr. bill!");
1274 static std::string error_headers() {
1275 return URLRequestTestJob::test_error_headers();
1279 TEST_F(URLRequestTest, Intercept) {
1280 TestInterceptor interceptor;
1282 // intercept the main request and respond with a simple response
1283 interceptor.intercept_main_request_ = true;
1284 interceptor.main_headers_ = TestInterceptor::ok_headers();
1285 interceptor.main_data_ = TestInterceptor::ok_data();
1288 URLRequest req(GURL("http://test_intercept/foo"),
1292 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1293 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1294 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1295 req.SetUserData(NULL, user_data0);
1296 req.SetUserData(&user_data1, user_data1);
1297 req.SetUserData(&user_data2, user_data2);
1298 req.set_method("GET");
1300 base::RunLoop().Run();
1302 // Make sure we can retrieve our specific user data
1303 EXPECT_EQ(user_data0, req.GetUserData(NULL));
1304 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1305 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1307 // Check the interceptor got called as expected
1308 EXPECT_TRUE(interceptor.did_intercept_main_);
1310 // Check we got one good response
1311 EXPECT_TRUE(req.status().is_success());
1312 EXPECT_EQ(200, req.response_headers()->response_code());
1313 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1314 EXPECT_EQ(1, d.response_started_count());
1315 EXPECT_EQ(0, d.received_redirect_count());
1318 TEST_F(URLRequestTest, InterceptRedirect) {
1319 TestInterceptor interceptor;
1321 // intercept the main request and respond with a redirect
1322 interceptor.intercept_main_request_ = true;
1323 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1324 interceptor.main_data_ = TestInterceptor::redirect_data();
1326 // intercept that redirect and respond a final OK response
1327 interceptor.intercept_redirect_ = true;
1328 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1329 interceptor.redirect_data_ = TestInterceptor::ok_data();
1332 URLRequest req(GURL("http://test_intercept/foo"),
1336 req.set_method("GET");
1338 base::RunLoop().Run();
1340 // Check the interceptor got called as expected
1341 EXPECT_TRUE(interceptor.did_intercept_main_);
1342 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1344 // Check we got one good response
1345 EXPECT_TRUE(req.status().is_success());
1346 if (req.status().is_success()) {
1347 EXPECT_EQ(200, req.response_headers()->response_code());
1349 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1350 EXPECT_EQ(1, d.response_started_count());
1351 EXPECT_EQ(0, d.received_redirect_count());
1354 TEST_F(URLRequestTest, InterceptServerError) {
1355 TestInterceptor interceptor;
1357 // intercept the main request to generate a server error response
1358 interceptor.intercept_main_request_ = true;
1359 interceptor.main_headers_ = TestInterceptor::error_headers();
1360 interceptor.main_data_ = TestInterceptor::error_data();
1362 // intercept that error and respond with an OK response
1363 interceptor.intercept_final_response_ = true;
1364 interceptor.final_headers_ = TestInterceptor::ok_headers();
1365 interceptor.final_data_ = TestInterceptor::ok_data();
1368 URLRequest req(GURL("http://test_intercept/foo"),
1372 req.set_method("GET");
1374 base::RunLoop().Run();
1376 // Check the interceptor got called as expected
1377 EXPECT_TRUE(interceptor.did_intercept_main_);
1378 EXPECT_TRUE(interceptor.did_intercept_final_);
1380 // Check we got one good response
1381 EXPECT_TRUE(req.status().is_success());
1382 EXPECT_EQ(200, req.response_headers()->response_code());
1383 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1384 EXPECT_EQ(1, d.response_started_count());
1385 EXPECT_EQ(0, d.received_redirect_count());
1388 TEST_F(URLRequestTest, InterceptNetworkError) {
1389 TestInterceptor interceptor;
1391 // intercept the main request to simulate a network error
1392 interceptor.simulate_main_network_error_ = true;
1394 // intercept that error and respond with an OK response
1395 interceptor.intercept_final_response_ = true;
1396 interceptor.final_headers_ = TestInterceptor::ok_headers();
1397 interceptor.final_data_ = TestInterceptor::ok_data();
1400 URLRequest req(GURL("http://test_intercept/foo"),
1404 req.set_method("GET");
1406 base::RunLoop().Run();
1408 // Check the interceptor got called as expected
1409 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1410 EXPECT_TRUE(interceptor.did_intercept_final_);
1412 // Check we received one good response
1413 EXPECT_TRUE(req.status().is_success());
1414 EXPECT_EQ(200, req.response_headers()->response_code());
1415 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1416 EXPECT_EQ(1, d.response_started_count());
1417 EXPECT_EQ(0, d.received_redirect_count());
1420 TEST_F(URLRequestTest, InterceptRestartRequired) {
1421 TestInterceptor interceptor;
1423 // restart the main request
1424 interceptor.restart_main_request_ = true;
1426 // then intercept the new main request and respond with an OK response
1427 interceptor.intercept_main_request_ = true;
1428 interceptor.main_headers_ = TestInterceptor::ok_headers();
1429 interceptor.main_data_ = TestInterceptor::ok_data();
1432 URLRequest req(GURL("http://test_intercept/foo"),
1436 req.set_method("GET");
1438 base::RunLoop().Run();
1440 // Check the interceptor got called as expected
1441 EXPECT_TRUE(interceptor.did_restart_main_);
1442 EXPECT_TRUE(interceptor.did_intercept_main_);
1444 // Check we received one good response
1445 EXPECT_TRUE(req.status().is_success());
1446 if (req.status().is_success()) {
1447 EXPECT_EQ(200, req.response_headers()->response_code());
1449 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1450 EXPECT_EQ(1, d.response_started_count());
1451 EXPECT_EQ(0, d.received_redirect_count());
1454 TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1455 TestInterceptor interceptor;
1457 // intercept the main request and cancel from within the restarted job
1458 interceptor.cancel_main_request_ = true;
1460 // setup to intercept final response and override it with an OK response
1461 interceptor.intercept_final_response_ = true;
1462 interceptor.final_headers_ = TestInterceptor::ok_headers();
1463 interceptor.final_data_ = TestInterceptor::ok_data();
1466 URLRequest req(GURL("http://test_intercept/foo"),
1470 req.set_method("GET");
1472 base::RunLoop().Run();
1474 // Check the interceptor got called as expected
1475 EXPECT_TRUE(interceptor.did_cancel_main_);
1476 EXPECT_FALSE(interceptor.did_intercept_final_);
1478 // Check we see a canceled request
1479 EXPECT_FALSE(req.status().is_success());
1480 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1483 TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1484 TestInterceptor interceptor;
1486 // intercept the main request and respond with a redirect
1487 interceptor.intercept_main_request_ = true;
1488 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1489 interceptor.main_data_ = TestInterceptor::redirect_data();
1491 // intercept the redirect and cancel from within that job
1492 interceptor.cancel_redirect_request_ = true;
1494 // setup to intercept final response and override it with an OK response
1495 interceptor.intercept_final_response_ = true;
1496 interceptor.final_headers_ = TestInterceptor::ok_headers();
1497 interceptor.final_data_ = TestInterceptor::ok_data();
1500 URLRequest req(GURL("http://test_intercept/foo"),
1504 req.set_method("GET");
1506 base::RunLoop().Run();
1508 // Check the interceptor got called as expected
1509 EXPECT_TRUE(interceptor.did_intercept_main_);
1510 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1511 EXPECT_FALSE(interceptor.did_intercept_final_);
1513 // Check we see a canceled request
1514 EXPECT_FALSE(req.status().is_success());
1515 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1518 TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1519 TestInterceptor interceptor;
1521 // intercept the main request to simulate a network error
1522 interceptor.simulate_main_network_error_ = true;
1524 // setup to intercept final response and cancel from within that job
1525 interceptor.cancel_final_request_ = true;
1528 URLRequest req(GURL("http://test_intercept/foo"),
1532 req.set_method("GET");
1534 base::RunLoop().Run();
1536 // Check the interceptor got called as expected
1537 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1538 EXPECT_TRUE(interceptor.did_cancel_final_);
1540 // Check we see a canceled request
1541 EXPECT_FALSE(req.status().is_success());
1542 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1545 TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1546 TestInterceptor interceptor;
1548 // intercept the main request and cancel then restart from within that job
1549 interceptor.cancel_then_restart_main_request_ = true;
1551 // setup to intercept final response and override it with an OK response
1552 interceptor.intercept_final_response_ = true;
1553 interceptor.final_headers_ = TestInterceptor::ok_headers();
1554 interceptor.final_data_ = TestInterceptor::ok_data();
1557 URLRequest req(GURL("http://test_intercept/foo"),
1561 req.set_method("GET");
1563 base::RunLoop().Run();
1565 // Check the interceptor got called as expected
1566 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1567 EXPECT_FALSE(interceptor.did_intercept_final_);
1569 // Check we see a canceled request
1570 EXPECT_FALSE(req.status().is_success());
1571 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1574 LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing,
1575 URLRequestContext* context) {
1576 TestInterceptor interceptor;
1577 interceptor.intercept_main_request_ = true;
1578 interceptor.main_request_load_timing_info_ = job_load_timing;
1581 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, context);
1583 base::RunLoop().Run();
1585 LoadTimingInfo resulting_load_timing;
1586 req.GetLoadTimingInfo(&resulting_load_timing);
1588 // None of these should be modified by the URLRequest.
1589 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1590 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1591 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1592 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1593 EXPECT_EQ(job_load_timing.receive_headers_end,
1594 resulting_load_timing.receive_headers_end);
1596 return resulting_load_timing;
1599 // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1600 // reused. |connect_time_flags| is used to indicate if there should be dns
1601 // or SSL times, and |used_proxy| is used for proxy times.
1602 LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1603 int connect_time_flags,
1605 LoadTimingInfo load_timing;
1606 load_timing.socket_log_id = 1;
1609 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1610 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1613 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1614 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1615 connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1616 connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1618 connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1619 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1620 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1621 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1623 connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1625 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1626 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1627 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1631 // Same as above, but in the case of a reused socket.
1632 LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1634 LoadTimingInfo load_timing;
1635 load_timing.socket_log_id = 1;
1636 load_timing.socket_reused = true;
1639 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1640 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1643 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1644 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1645 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1649 // Basic test that the intercept + load timing tests work.
1650 TEST_F(URLRequestTest, InterceptLoadTiming) {
1651 base::TimeTicks now = base::TimeTicks::Now();
1652 LoadTimingInfo job_load_timing =
1653 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1655 LoadTimingInfo load_timing_result =
1656 RunLoadTimingTest(job_load_timing, &default_context_);
1658 // Nothing should have been changed by the URLRequest.
1659 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1660 load_timing_result.proxy_resolve_start);
1661 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1662 load_timing_result.proxy_resolve_end);
1663 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1664 load_timing_result.connect_timing.dns_start);
1665 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1666 load_timing_result.connect_timing.dns_end);
1667 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1668 load_timing_result.connect_timing.connect_start);
1669 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1670 load_timing_result.connect_timing.connect_end);
1671 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1672 load_timing_result.connect_timing.ssl_start);
1673 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1674 load_timing_result.connect_timing.ssl_end);
1676 // Redundant sanity check.
1677 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1680 // Another basic test, with proxy and SSL times, but no DNS times.
1681 TEST_F(URLRequestTest, InterceptLoadTimingProxy) {
1682 base::TimeTicks now = base::TimeTicks::Now();
1683 LoadTimingInfo job_load_timing =
1684 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1686 LoadTimingInfo load_timing_result =
1687 RunLoadTimingTest(job_load_timing, &default_context_);
1689 // Nothing should have been changed by the URLRequest.
1690 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1691 load_timing_result.proxy_resolve_start);
1692 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1693 load_timing_result.proxy_resolve_end);
1694 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1695 load_timing_result.connect_timing.dns_start);
1696 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1697 load_timing_result.connect_timing.dns_end);
1698 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1699 load_timing_result.connect_timing.connect_start);
1700 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1701 load_timing_result.connect_timing.connect_end);
1702 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1703 load_timing_result.connect_timing.ssl_start);
1704 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1705 load_timing_result.connect_timing.ssl_end);
1707 // Redundant sanity check.
1708 TestLoadTimingNotReusedWithProxy(load_timing_result,
1709 CONNECT_TIMING_HAS_SSL_TIMES);
1712 // Make sure that URLRequest correctly adjusts proxy times when they're before
1713 // |request_start|, due to already having a connected socket. This happens in
1714 // the case of reusing a SPDY session. The connected socket is not considered
1715 // reused in this test (May be a preconnect).
1717 // To mix things up from the test above, assumes DNS times but no SSL times.
1718 TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) {
1719 base::TimeTicks now = base::TimeTicks::Now();
1720 LoadTimingInfo job_load_timing =
1721 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1722 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1723 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1724 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1725 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1726 job_load_timing.connect_timing.connect_start =
1727 now - base::TimeDelta::FromDays(2);
1728 job_load_timing.connect_timing.connect_end =
1729 now - base::TimeDelta::FromDays(1);
1731 LoadTimingInfo load_timing_result =
1732 RunLoadTimingTest(job_load_timing, &default_context_);
1734 // Proxy times, connect times, and DNS times should all be replaced with
1736 EXPECT_EQ(load_timing_result.request_start,
1737 load_timing_result.proxy_resolve_start);
1738 EXPECT_EQ(load_timing_result.request_start,
1739 load_timing_result.proxy_resolve_end);
1740 EXPECT_EQ(load_timing_result.request_start,
1741 load_timing_result.connect_timing.dns_start);
1742 EXPECT_EQ(load_timing_result.request_start,
1743 load_timing_result.connect_timing.dns_end);
1744 EXPECT_EQ(load_timing_result.request_start,
1745 load_timing_result.connect_timing.connect_start);
1746 EXPECT_EQ(load_timing_result.request_start,
1747 load_timing_result.connect_timing.connect_end);
1749 // Other times should have been left null.
1750 TestLoadTimingNotReusedWithProxy(load_timing_result,
1751 CONNECT_TIMING_HAS_DNS_TIMES);
1754 // Same as above, but in the reused case.
1755 TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) {
1756 base::TimeTicks now = base::TimeTicks::Now();
1757 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1758 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1759 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1761 LoadTimingInfo load_timing_result =
1762 RunLoadTimingTest(job_load_timing, &default_context_);
1764 // Proxy times and connect times should all be replaced with request_start.
1765 EXPECT_EQ(load_timing_result.request_start,
1766 load_timing_result.proxy_resolve_start);
1767 EXPECT_EQ(load_timing_result.request_start,
1768 load_timing_result.proxy_resolve_end);
1770 // Other times should have been left null.
1771 TestLoadTimingReusedWithProxy(load_timing_result);
1774 // Make sure that URLRequest correctly adjusts connect times when they're before
1775 // |request_start|, due to reusing a connected socket. The connected socket is
1776 // not considered reused in this test (May be a preconnect).
1778 // To mix things up, the request has SSL times, but no DNS times.
1779 TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) {
1780 base::TimeTicks now = base::TimeTicks::Now();
1781 LoadTimingInfo job_load_timing =
1782 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1783 job_load_timing.connect_timing.connect_start =
1784 now - base::TimeDelta::FromDays(1);
1785 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1786 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1787 job_load_timing.connect_timing.connect_end =
1788 now - base::TimeDelta::FromDays(4);
1790 LoadTimingInfo load_timing_result =
1791 RunLoadTimingTest(job_load_timing, &default_context_);
1793 // Connect times, and SSL times should be replaced with request_start.
1794 EXPECT_EQ(load_timing_result.request_start,
1795 load_timing_result.connect_timing.connect_start);
1796 EXPECT_EQ(load_timing_result.request_start,
1797 load_timing_result.connect_timing.ssl_start);
1798 EXPECT_EQ(load_timing_result.request_start,
1799 load_timing_result.connect_timing.ssl_end);
1800 EXPECT_EQ(load_timing_result.request_start,
1801 load_timing_result.connect_timing.connect_end);
1803 // Other times should have been left null.
1804 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1807 // Make sure that URLRequest correctly adjusts connect times when they're before
1808 // |request_start|, due to reusing a connected socket in the case that there
1809 // are also proxy times. The connected socket is not considered reused in this
1810 // test (May be a preconnect).
1812 // In this test, there are no SSL or DNS times.
1813 TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) {
1814 base::TimeTicks now = base::TimeTicks::Now();
1815 LoadTimingInfo job_load_timing =
1816 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1817 job_load_timing.connect_timing.connect_start =
1818 now - base::TimeDelta::FromDays(1);
1819 job_load_timing.connect_timing.connect_end =
1820 now - base::TimeDelta::FromDays(2);
1822 LoadTimingInfo load_timing_result =
1823 RunLoadTimingTest(job_load_timing, &default_context_);
1825 // Connect times should be replaced with proxy_resolve_end.
1826 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1827 load_timing_result.connect_timing.connect_start);
1828 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1829 load_timing_result.connect_timing.connect_end);
1831 // Other times should have been left null.
1832 TestLoadTimingNotReusedWithProxy(load_timing_result,
1833 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
1836 // Check that two different URL requests have different identifiers.
1837 TEST_F(URLRequestTest, Identifiers) {
1839 TestURLRequestContext context;
1841 GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
1842 TestURLRequest other_req(
1843 GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
1845 ASSERT_NE(req.identifier(), other_req.identifier());
1848 // Check that a failure to connect to the proxy is reported to the network
1850 TEST_F(URLRequestTest, NetworkDelegateProxyError) {
1851 MockHostResolver host_resolver;
1852 host_resolver.rules()->AddSimulatedFailure("*");
1854 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
1855 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
1858 URLRequest req(GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context);
1859 req.set_method("GET");
1862 base::RunLoop().Run();
1864 // Check we see a failed request.
1865 EXPECT_FALSE(req.status().is_success());
1866 // The proxy server is not set before failure.
1867 EXPECT_TRUE(req.proxy_server().IsEmpty());
1868 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
1869 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error());
1871 EXPECT_EQ(1, network_delegate.error_count());
1872 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
1873 EXPECT_EQ(1, network_delegate.completed_requests());
1876 // Make sure that NetworkDelegate::NotifyCompleted is called if
1877 // content is empty.
1878 TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
1880 URLRequest req(GURL("data:,"), DEFAULT_PRIORITY, &d, &default_context_);
1882 base::RunLoop().Run();
1883 EXPECT_EQ("", d.data_received());
1884 EXPECT_EQ(1, default_network_delegate_.completed_requests());
1887 // Make sure that SetPriority actually sets the URLRequest's priority
1888 // correctly, both before and after start.
1889 TEST_F(URLRequestTest, SetPriorityBasic) {
1891 URLRequest req(GURL("http://test_intercept/foo"),
1895 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1897 req.SetPriority(LOW);
1898 EXPECT_EQ(LOW, req.priority());
1901 EXPECT_EQ(LOW, req.priority());
1903 req.SetPriority(MEDIUM);
1904 EXPECT_EQ(MEDIUM, req.priority());
1907 // Make sure that URLRequest calls SetPriority on a job before calling
1909 TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
1911 URLRequest req(GURL("http://test_intercept/foo"),
1915 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
1917 scoped_refptr<URLRequestTestJob> job =
1918 new URLRequestTestJob(&req, &default_network_delegate_);
1919 AddTestInterceptor()->set_main_intercept_job(job.get());
1920 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
1922 req.SetPriority(LOW);
1925 EXPECT_EQ(LOW, job->priority());
1928 // Make sure that URLRequest passes on its priority updates to its
1930 TEST_F(URLRequestTest, SetJobPriority) {
1932 URLRequest req(GURL("http://test_intercept/foo"),
1937 scoped_refptr<URLRequestTestJob> job =
1938 new URLRequestTestJob(&req, &default_network_delegate_);
1939 AddTestInterceptor()->set_main_intercept_job(job.get());
1941 req.SetPriority(LOW);
1943 EXPECT_EQ(LOW, job->priority());
1945 req.SetPriority(MEDIUM);
1946 EXPECT_EQ(MEDIUM, req.priority());
1947 EXPECT_EQ(MEDIUM, job->priority());
1950 // Setting the IGNORE_LIMITS load flag should be okay if the priority
1951 // is MAXIMUM_PRIORITY.
1952 TEST_F(URLRequestTest, PriorityIgnoreLimits) {
1954 URLRequest req(GURL("http://test_intercept/foo"),
1958 EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1960 scoped_refptr<URLRequestTestJob> job =
1961 new URLRequestTestJob(&req, &default_network_delegate_);
1962 AddTestInterceptor()->set_main_intercept_job(job.get());
1964 req.SetLoadFlags(LOAD_IGNORE_LIMITS);
1965 EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1967 req.SetPriority(MAXIMUM_PRIORITY);
1968 EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1971 EXPECT_EQ(MAXIMUM_PRIORITY, req.priority());
1972 EXPECT_EQ(MAXIMUM_PRIORITY, job->priority());
1975 // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
1976 #if !defined(OS_IOS)
1977 // A subclass of SpawnedTestServer that uses a statically-configured hostname.
1978 // This is to work around mysterious failures in chrome_frame_net_tests. See:
1979 // http://crbug.com/114369
1980 // TODO(erikwright): remove or update as needed; see http://crbug.com/334634.
1981 class LocalHttpTestServer : public SpawnedTestServer {
1983 explicit LocalHttpTestServer(const base::FilePath& document_root)
1984 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1985 ScopedCustomUrlRequestTestHttpHost::value(),
1987 LocalHttpTestServer()
1988 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
1989 ScopedCustomUrlRequestTestHttpHost::value(),
1990 base::FilePath()) {}
1993 TEST_F(URLRequestTest, DelayedCookieCallback) {
1994 LocalHttpTestServer test_server;
1995 ASSERT_TRUE(test_server.Start());
1997 TestURLRequestContext context;
1998 scoped_refptr<DelayedCookieMonster> delayed_cm =
1999 new DelayedCookieMonster();
2000 scoped_refptr<CookieStore> cookie_store = delayed_cm;
2001 context.set_cookie_store(delayed_cm.get());
2005 TestNetworkDelegate network_delegate;
2006 context.set_network_delegate(&network_delegate);
2008 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2013 base::RunLoop().Run();
2014 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2015 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2016 EXPECT_EQ(1, network_delegate.set_cookie_count());
2019 // Verify that the cookie is set.
2021 TestNetworkDelegate network_delegate;
2022 context.set_network_delegate(&network_delegate);
2024 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2029 base::RunLoop().Run();
2031 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2032 != std::string::npos);
2033 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2034 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2038 TEST_F(URLRequestTest, DoNotSendCookies) {
2039 LocalHttpTestServer test_server;
2040 ASSERT_TRUE(test_server.Start());
2044 TestNetworkDelegate network_delegate;
2045 default_context_.set_network_delegate(&network_delegate);
2047 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2052 base::RunLoop().Run();
2053 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2054 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2057 // Verify that the cookie is set.
2059 TestNetworkDelegate network_delegate;
2060 default_context_.set_network_delegate(&network_delegate);
2062 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2067 base::RunLoop().Run();
2069 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2070 != std::string::npos);
2071 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2072 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2075 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2077 TestNetworkDelegate network_delegate;
2078 default_context_.set_network_delegate(&network_delegate);
2080 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2084 req.SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES);
2086 base::RunLoop().Run();
2088 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2089 == std::string::npos);
2091 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2092 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2093 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2097 TEST_F(URLRequestTest, DoNotSaveCookies) {
2098 LocalHttpTestServer test_server;
2099 ASSERT_TRUE(test_server.Start());
2103 TestNetworkDelegate network_delegate;
2104 default_context_.set_network_delegate(&network_delegate);
2106 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2111 base::RunLoop().Run();
2113 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2114 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2115 EXPECT_EQ(1, network_delegate.set_cookie_count());
2118 // Try to set-up another cookie and update the previous cookie.
2120 TestNetworkDelegate network_delegate;
2121 default_context_.set_network_delegate(&network_delegate);
2124 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2128 req.SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES);
2131 base::RunLoop().Run();
2133 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2134 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2135 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2136 EXPECT_EQ(0, network_delegate.set_cookie_count());
2139 // Verify the cookies weren't saved or updated.
2141 TestNetworkDelegate network_delegate;
2142 default_context_.set_network_delegate(&network_delegate);
2144 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2149 base::RunLoop().Run();
2151 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2152 == std::string::npos);
2153 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2154 != std::string::npos);
2156 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2157 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2158 EXPECT_EQ(0, network_delegate.set_cookie_count());
2162 TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
2163 LocalHttpTestServer test_server;
2164 ASSERT_TRUE(test_server.Start());
2168 TestNetworkDelegate network_delegate;
2169 default_context_.set_network_delegate(&network_delegate);
2171 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2176 base::RunLoop().Run();
2178 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2179 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2182 // Verify that the cookie is set.
2184 TestNetworkDelegate network_delegate;
2185 default_context_.set_network_delegate(&network_delegate);
2187 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2192 base::RunLoop().Run();
2194 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2195 != std::string::npos);
2197 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2198 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2201 // Verify that the cookie isn't sent.
2203 TestNetworkDelegate network_delegate;
2204 default_context_.set_network_delegate(&network_delegate);
2206 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2207 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2212 base::RunLoop().Run();
2214 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2215 == std::string::npos);
2217 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2218 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2222 TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
2223 LocalHttpTestServer test_server;
2224 ASSERT_TRUE(test_server.Start());
2228 TestNetworkDelegate network_delegate;
2229 default_context_.set_network_delegate(&network_delegate);
2231 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2236 base::RunLoop().Run();
2238 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2239 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2242 // Try to set-up another cookie and update the previous cookie.
2244 TestNetworkDelegate network_delegate;
2245 default_context_.set_network_delegate(&network_delegate);
2247 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2249 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2255 base::RunLoop().Run();
2257 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2258 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2261 // Verify the cookies weren't saved or updated.
2263 TestNetworkDelegate network_delegate;
2264 default_context_.set_network_delegate(&network_delegate);
2266 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2271 base::RunLoop().Run();
2273 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2274 == std::string::npos);
2275 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2276 != std::string::npos);
2278 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2279 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2283 TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2284 LocalHttpTestServer test_server;
2285 ASSERT_TRUE(test_server.Start());
2287 // Set up an empty cookie.
2289 TestNetworkDelegate network_delegate;
2290 default_context_.set_network_delegate(&network_delegate);
2292 URLRequest req(test_server.GetURL("set-cookie"),
2297 base::RunLoop().Run();
2299 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2300 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2301 EXPECT_EQ(0, network_delegate.set_cookie_count());
2305 TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2306 LocalHttpTestServer test_server;
2307 ASSERT_TRUE(test_server.Start());
2311 TestNetworkDelegate network_delegate;
2312 default_context_.set_network_delegate(&network_delegate);
2314 URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"),
2319 base::RunLoop().Run();
2321 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2322 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2325 // Verify that the cookie is set.
2327 TestNetworkDelegate network_delegate;
2328 default_context_.set_network_delegate(&network_delegate);
2330 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2335 base::RunLoop().Run();
2337 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2338 != std::string::npos);
2340 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2341 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2344 // Verify that the cookie isn't sent.
2346 TestNetworkDelegate network_delegate;
2347 default_context_.set_network_delegate(&network_delegate);
2349 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2350 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2355 base::RunLoop().Run();
2357 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2358 == std::string::npos);
2360 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2361 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2365 TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2366 LocalHttpTestServer test_server;
2367 ASSERT_TRUE(test_server.Start());
2371 TestNetworkDelegate network_delegate;
2372 default_context_.set_network_delegate(&network_delegate);
2374 URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"),
2379 base::RunLoop().Run();
2381 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2382 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2385 // Try to set-up another cookie and update the previous cookie.
2387 TestNetworkDelegate network_delegate;
2388 default_context_.set_network_delegate(&network_delegate);
2390 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2392 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2398 base::RunLoop().Run();
2400 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2401 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2404 // Verify the cookies weren't saved or updated.
2406 TestNetworkDelegate network_delegate;
2407 default_context_.set_network_delegate(&network_delegate);
2409 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2414 base::RunLoop().Run();
2416 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2417 == std::string::npos);
2418 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2419 != std::string::npos);
2421 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2422 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2426 // FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2427 // value for the |fixed_date| argument given to the constructor.
2428 class FixedDateNetworkDelegate : public TestNetworkDelegate {
2430 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2431 : fixed_date_(fixed_date) {}
2432 virtual ~FixedDateNetworkDelegate() {}
2434 // NetworkDelegate implementation
2435 virtual int OnHeadersReceived(
2436 URLRequest* request,
2437 const CompletionCallback& callback,
2438 const HttpResponseHeaders* original_response_headers,
2439 scoped_refptr<HttpResponseHeaders>* override_response_headers,
2440 GURL* allowed_unsafe_redirect_url) OVERRIDE;
2443 std::string fixed_date_;
2445 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2448 int FixedDateNetworkDelegate::OnHeadersReceived(
2449 URLRequest* request,
2450 const CompletionCallback& callback,
2451 const HttpResponseHeaders* original_response_headers,
2452 scoped_refptr<HttpResponseHeaders>* override_response_headers,
2453 GURL* allowed_unsafe_redirect_url) {
2454 HttpResponseHeaders* new_response_headers =
2455 new HttpResponseHeaders(original_response_headers->raw_headers());
2457 new_response_headers->RemoveHeader("Date");
2458 new_response_headers->AddHeader("Date: " + fixed_date_);
2460 *override_response_headers = new_response_headers;
2461 return TestNetworkDelegate::OnHeadersReceived(request,
2463 original_response_headers,
2464 override_response_headers,
2465 allowed_unsafe_redirect_url);
2468 // Test that cookie expiration times are adjusted for server/client clock
2469 // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2470 // headers by defaulting to GMT. (crbug.com/135131)
2471 TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2472 LocalHttpTestServer test_server;
2473 ASSERT_TRUE(test_server.Start());
2475 // Set up an expired cookie.
2477 TestNetworkDelegate network_delegate;
2478 default_context_.set_network_delegate(&network_delegate);
2482 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2487 base::RunLoop().Run();
2489 // Verify that the cookie is not set.
2491 TestNetworkDelegate network_delegate;
2492 default_context_.set_network_delegate(&network_delegate);
2494 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2499 base::RunLoop().Run();
2501 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2503 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2505 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2506 default_context_.set_network_delegate(&network_delegate);
2510 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2515 base::RunLoop().Run();
2517 // Verify that the cookie is set.
2519 TestNetworkDelegate network_delegate;
2520 default_context_.set_network_delegate(&network_delegate);
2522 URLRequest req(test_server.GetURL("echoheader?Cookie"),
2527 base::RunLoop().Run();
2529 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2534 // Check that it is impossible to change the referrer in the extra headers of
2536 TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2537 LocalHttpTestServer test_server;
2538 ASSERT_TRUE(test_server.Start());
2540 // If extra headers contain referer and the request contains a referer,
2541 // only the latter shall be respected.
2544 URLRequest req(test_server.GetURL("echoheader?Referer"),
2548 req.SetReferrer("http://foo.com/");
2550 HttpRequestHeaders headers;
2551 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2552 req.SetExtraRequestHeaders(headers);
2555 base::RunLoop().Run();
2557 EXPECT_EQ("http://foo.com/", d.data_received());
2560 // If extra headers contain a referer but the request does not, no referer
2561 // shall be sent in the header.
2564 URLRequest req(test_server.GetURL("echoheader?Referer"),
2569 HttpRequestHeaders headers;
2570 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2571 req.SetExtraRequestHeaders(headers);
2572 req.SetLoadFlags(LOAD_VALIDATE_CACHE);
2575 base::RunLoop().Run();
2577 EXPECT_EQ("None", d.data_received());
2581 class URLRequestTestHTTP : public URLRequestTest {
2583 URLRequestTestHTTP()
2584 : test_server_(base::FilePath(FILE_PATH_LITERAL(
2585 "net/data/url_request_unittest"))) {
2589 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2590 // |request_method| is the method to use for the initial request.
2591 // |redirect_method| is the method that is expected to be used for the second
2592 // request, after redirection.
2593 // If |include_data| is true, data is uploaded with the request. The
2594 // response body is expected to match it exactly, if and only if
2595 // |request_method| == |redirect_method|.
2596 void HTTPRedirectMethodTest(const GURL& redirect_url,
2597 const std::string& request_method,
2598 const std::string& redirect_method,
2599 bool include_data) {
2600 static const char kData[] = "hello world";
2602 URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
2603 req.set_method(request_method);
2605 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
2606 HttpRequestHeaders headers;
2607 headers.SetHeader(HttpRequestHeaders::kContentLength,
2608 base::UintToString(arraysize(kData) - 1));
2609 req.SetExtraRequestHeaders(headers);
2612 base::RunLoop().Run();
2613 EXPECT_EQ(redirect_method, req.method());
2614 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
2615 EXPECT_EQ(OK, req.status().error());
2617 if (request_method == redirect_method) {
2618 EXPECT_EQ(kData, d.data_received());
2620 EXPECT_NE(kData, d.data_received());
2624 LOG(WARNING) << "Request method was: " << request_method;
2627 void HTTPUploadDataOperationTest(const std::string& method) {
2628 const int kMsgSize = 20000; // multiple of 10
2629 const int kIterations = 50;
2630 char* uploadBytes = new char[kMsgSize+1];
2631 char* ptr = uploadBytes;
2633 for (int idx = 0; idx < kMsgSize/10; idx++) {
2634 memcpy(ptr, "----------", 10);
2636 if (idx % 100 == 0) {
2643 uploadBytes[kMsgSize] = '\0';
2645 for (int i = 0; i < kIterations; ++i) {
2648 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
2649 r.set_method(method.c_str());
2651 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes)));
2654 EXPECT_TRUE(r.is_pending());
2656 base::RunLoop().Run();
2658 ASSERT_EQ(1, d.response_started_count())
2659 << "request failed: " << r.status().status()
2660 << ", os error: " << r.status().error();
2662 EXPECT_FALSE(d.received_data_before_response());
2663 EXPECT_EQ(uploadBytes, d.data_received());
2665 delete[] uploadBytes;
2668 void AddChunksToUpload(URLRequest* r) {
2669 r->AppendChunkToUpload("a", 1, false);
2670 r->AppendChunkToUpload("bcd", 3, false);
2671 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2672 r->AppendChunkToUpload("\r\n\r\n", 4, false);
2673 r->AppendChunkToUpload("0", 1, false);
2674 r->AppendChunkToUpload("2323", 4, true);
2677 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
2678 // This should match the chunks sent by AddChunksToUpload().
2679 const std::string expected_data =
2680 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2682 ASSERT_EQ(1, d->response_started_count())
2683 << "request failed: " << r->status().status()
2684 << ", os error: " << r->status().error();
2686 EXPECT_FALSE(d->received_data_before_response());
2688 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2689 EXPECT_EQ(expected_data, d->data_received());
2692 bool DoManyCookiesRequest(int num_cookies) {
2694 URLRequest r(test_server_.GetURL("set-many-cookies?" +
2695 base::IntToString(num_cookies)),
2701 EXPECT_TRUE(r.is_pending());
2703 base::RunLoop().Run();
2705 bool is_success = r.status().is_success();
2708 EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG);
2709 // The test server appears to be unable to handle subsequent requests
2710 // after this error is triggered. Force it to restart.
2711 EXPECT_TRUE(test_server_.Stop());
2712 EXPECT_TRUE(test_server_.Start());
2718 LocalHttpTestServer test_server_;
2721 // In this unit test, we're using the HTTPTestServer as a proxy server and
2722 // issuing a CONNECT request with the magic host name "www.redirect.com".
2723 // The HTTPTestServer will return a 302 response, which we should not
2725 TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
2726 ASSERT_TRUE(test_server_.Start());
2728 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
2729 TestURLRequestContextWithProxy context(
2730 test_server_.host_port_pair().ToString(), &network_delegate);
2735 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context);
2737 EXPECT_TRUE(r.is_pending());
2739 base::RunLoop().Run();
2741 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2742 // The proxy server is not set before failure.
2743 EXPECT_TRUE(r.proxy_server().IsEmpty());
2744 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
2745 EXPECT_EQ(1, d.response_started_count());
2746 // We should not have followed the redirect.
2747 EXPECT_EQ(0, d.received_redirect_count());
2751 // This is the same as the previous test, but checks that the network delegate
2752 // registers the error.
2753 TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
2754 ASSERT_TRUE(test_server_.Start());
2756 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
2757 TestURLRequestContextWithProxy context(
2758 test_server_.host_port_pair().ToString(), &network_delegate);
2763 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context);
2765 EXPECT_TRUE(r.is_pending());
2767 base::RunLoop().Run();
2769 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2770 // The proxy server is not set before failure.
2771 EXPECT_TRUE(r.proxy_server().IsEmpty());
2772 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
2773 EXPECT_EQ(1, d.response_started_count());
2774 // We should not have followed the redirect.
2775 EXPECT_EQ(0, d.received_redirect_count());
2777 EXPECT_EQ(1, network_delegate.error_count());
2778 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
2782 // Tests that we can block and asynchronously return OK in various stages.
2783 TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
2784 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
2785 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2786 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2787 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2789 static const size_t blocking_stages_length = arraysize(blocking_stages);
2791 ASSERT_TRUE(test_server_.Start());
2794 BlockingNetworkDelegate network_delegate(
2795 BlockingNetworkDelegate::USER_CALLBACK);
2796 network_delegate.set_block_on(
2797 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
2798 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
2799 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
2801 TestURLRequestContext context(true);
2802 context.set_network_delegate(&network_delegate);
2807 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, &context);
2810 for (size_t i = 0; i < blocking_stages_length; ++i) {
2811 base::RunLoop().Run();
2812 EXPECT_EQ(blocking_stages[i],
2813 network_delegate.stage_blocked_for_callback());
2814 network_delegate.DoCallback(OK);
2816 base::RunLoop().Run();
2817 EXPECT_EQ(200, r.GetResponseCode());
2818 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2819 EXPECT_EQ(1, network_delegate.created_requests());
2820 EXPECT_EQ(0, network_delegate.destroyed_requests());
2822 EXPECT_EQ(1, network_delegate.destroyed_requests());
2825 // Tests that the network delegate can block and cancel a request.
2826 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
2827 ASSERT_TRUE(test_server_.Start());
2830 BlockingNetworkDelegate network_delegate(
2831 BlockingNetworkDelegate::AUTO_CALLBACK);
2832 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2833 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
2835 TestURLRequestContextWithProxy context(
2836 test_server_.host_port_pair().ToString(), &network_delegate);
2840 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
2843 base::RunLoop().Run();
2845 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2846 // The proxy server is not set before cancellation.
2847 EXPECT_TRUE(r.proxy_server().IsEmpty());
2848 EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error());
2849 EXPECT_EQ(1, network_delegate.created_requests());
2850 EXPECT_EQ(0, network_delegate.destroyed_requests());
2852 EXPECT_EQ(1, network_delegate.destroyed_requests());
2855 // Helper function for NetworkDelegateCancelRequestAsynchronously and
2856 // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2857 // delegate operating in |block_mode| and a request for |url|. It blocks the
2858 // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2859 void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
2860 BlockingNetworkDelegate::Stage stage,
2863 BlockingNetworkDelegate network_delegate(block_mode);
2864 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
2865 network_delegate.set_block_on(stage);
2867 TestURLRequestContext context(true);
2868 context.set_network_delegate(&network_delegate);
2872 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
2875 base::RunLoop().Run();
2877 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
2878 // The proxy server is not set before cancellation.
2879 EXPECT_TRUE(r.proxy_server().IsEmpty());
2880 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error());
2881 EXPECT_EQ(1, network_delegate.created_requests());
2882 EXPECT_EQ(0, network_delegate.destroyed_requests());
2884 EXPECT_EQ(1, network_delegate.destroyed_requests());
2887 // The following 3 tests check that the network delegate can cancel a request
2888 // synchronously in various stages of the request.
2889 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
2890 ASSERT_TRUE(test_server_.Start());
2891 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2892 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2893 test_server_.GetURL(std::string()));
2896 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
2897 ASSERT_TRUE(test_server_.Start());
2898 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2899 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2900 test_server_.GetURL(std::string()));
2903 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
2904 ASSERT_TRUE(test_server_.Start());
2905 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
2906 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2907 test_server_.GetURL(std::string()));
2910 // The following 3 tests check that the network delegate can cancel a request
2911 // asynchronously in various stages of the request.
2912 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
2913 ASSERT_TRUE(test_server_.Start());
2914 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2915 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
2916 test_server_.GetURL(std::string()));
2919 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
2920 ASSERT_TRUE(test_server_.Start());
2921 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2922 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
2923 test_server_.GetURL(std::string()));
2926 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
2927 ASSERT_TRUE(test_server_.Start());
2928 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
2929 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
2930 test_server_.GetURL(std::string()));
2933 // Tests that the network delegate can block and redirect a request to a new
2935 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
2936 ASSERT_TRUE(test_server_.Start());
2939 BlockingNetworkDelegate network_delegate(
2940 BlockingNetworkDelegate::AUTO_CALLBACK);
2941 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
2942 GURL redirect_url(test_server_.GetURL("simple.html"));
2943 network_delegate.set_redirect_url(redirect_url);
2945 TestURLRequestContextWithProxy context(
2946 test_server_.host_port_pair().ToString(), &network_delegate);
2949 GURL original_url(test_server_.GetURL("empty.html"));
2950 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
2952 // Quit after hitting the redirect, so can check the headers.
2953 d.set_quit_on_redirect(true);
2955 base::RunLoop().Run();
2957 // Check headers from URLRequestJob.
2958 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2959 EXPECT_EQ(307, r.GetResponseCode());
2960 EXPECT_EQ(307, r.response_headers()->response_code());
2961 std::string location;
2962 ASSERT_TRUE(r.response_headers()->EnumerateHeader(NULL, "Location",
2964 EXPECT_EQ(redirect_url, GURL(location));
2966 // Let the request finish.
2967 r.FollowDeferredRedirect();
2968 base::RunLoop().Run();
2969 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
2970 EXPECT_TRUE(r.proxy_server().Equals(test_server_.host_port_pair()));
2972 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
2974 network_delegate.last_observed_proxy().Equals(
2975 test_server_.host_port_pair()));
2977 EXPECT_EQ(0, r.status().error());
2978 EXPECT_EQ(redirect_url, r.url());
2979 EXPECT_EQ(original_url, r.original_url());
2980 EXPECT_EQ(2U, r.url_chain().size());
2981 EXPECT_EQ(1, network_delegate.created_requests());
2982 EXPECT_EQ(0, network_delegate.destroyed_requests());
2984 EXPECT_EQ(1, network_delegate.destroyed_requests());
2987 // Tests that the network delegate can block and redirect a request to a new
2988 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2989 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
2990 ASSERT_TRUE(test_server_.Start());
2993 BlockingNetworkDelegate network_delegate(
2994 BlockingNetworkDelegate::SYNCHRONOUS);
2995 GURL redirect_url(test_server_.GetURL("simple.html"));
2996 network_delegate.set_redirect_url(redirect_url);
2998 TestURLRequestContextWithProxy context(
2999 test_server_.host_port_pair().ToString(), &network_delegate);
3002 GURL original_url(test_server_.GetURL("empty.html"));
3003 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
3005 // Quit after hitting the redirect, so can check the headers.
3006 d.set_quit_on_redirect(true);
3008 base::RunLoop().Run();
3010 // Check headers from URLRequestJob.
3011 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3012 EXPECT_EQ(307, r.GetResponseCode());
3013 EXPECT_EQ(307, r.response_headers()->response_code());
3014 std::string location;
3015 ASSERT_TRUE(r.response_headers()->EnumerateHeader(NULL, "Location",
3017 EXPECT_EQ(redirect_url, GURL(location));
3019 // Let the request finish.
3020 r.FollowDeferredRedirect();
3021 base::RunLoop().Run();
3023 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3024 EXPECT_TRUE(r.proxy_server().Equals(test_server_.host_port_pair()));
3026 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
3028 network_delegate.last_observed_proxy().Equals(
3029 test_server_.host_port_pair()));
3030 EXPECT_EQ(0, r.status().error());
3031 EXPECT_EQ(redirect_url, r.url());
3032 EXPECT_EQ(original_url, r.original_url());
3033 EXPECT_EQ(2U, r.url_chain().size());
3034 EXPECT_EQ(1, network_delegate.created_requests());
3035 EXPECT_EQ(0, network_delegate.destroyed_requests());
3037 EXPECT_EQ(1, network_delegate.destroyed_requests());
3040 // Tests that redirects caused by the network delegate preserve POST data.
3041 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
3042 ASSERT_TRUE(test_server_.Start());
3044 const char kData[] = "hello world";
3047 BlockingNetworkDelegate network_delegate(
3048 BlockingNetworkDelegate::AUTO_CALLBACK);
3049 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3050 GURL redirect_url(test_server_.GetURL("echo"));
3051 network_delegate.set_redirect_url(redirect_url);
3053 TestURLRequestContext context(true);
3054 context.set_network_delegate(&network_delegate);
3058 GURL original_url(test_server_.GetURL("empty.html"));
3059 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
3060 r.set_method("POST");
3061 r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
3062 HttpRequestHeaders headers;
3063 headers.SetHeader(HttpRequestHeaders::kContentLength,
3064 base::UintToString(arraysize(kData) - 1));
3065 r.SetExtraRequestHeaders(headers);
3067 // Quit after hitting the redirect, so can check the headers.
3068 d.set_quit_on_redirect(true);
3070 base::RunLoop().Run();
3072 // Check headers from URLRequestJob.
3073 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3074 EXPECT_EQ(307, r.GetResponseCode());
3075 EXPECT_EQ(307, r.response_headers()->response_code());
3076 std::string location;
3077 ASSERT_TRUE(r.response_headers()->EnumerateHeader(NULL, "Location",
3079 EXPECT_EQ(redirect_url, GURL(location));
3081 // Let the request finish.
3082 r.FollowDeferredRedirect();
3083 base::RunLoop().Run();
3085 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3086 EXPECT_EQ(0, r.status().error());
3087 EXPECT_EQ(redirect_url, r.url());
3088 EXPECT_EQ(original_url, r.original_url());
3089 EXPECT_EQ(2U, r.url_chain().size());
3090 EXPECT_EQ(1, network_delegate.created_requests());
3091 EXPECT_EQ(0, network_delegate.destroyed_requests());
3092 EXPECT_EQ("POST", r.method());
3093 EXPECT_EQ(kData, d.data_received());
3095 EXPECT_EQ(1, network_delegate.destroyed_requests());
3098 // Tests that the network delegate can block and redirect a request to a new
3099 // URL during OnHeadersReceived.
3100 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) {
3101 ASSERT_TRUE(test_server_.Start());
3104 BlockingNetworkDelegate network_delegate(
3105 BlockingNetworkDelegate::AUTO_CALLBACK);
3106 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3107 GURL redirect_url(test_server_.GetURL("simple.html"));
3108 network_delegate.set_redirect_on_headers_received_url(redirect_url);
3110 TestURLRequestContextWithProxy context(
3111 test_server_.host_port_pair().ToString(), &network_delegate);
3114 GURL original_url(test_server_.GetURL("empty.html"));
3115 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context);
3118 base::RunLoop().Run();
3120 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3121 EXPECT_TRUE(r.proxy_server().Equals(test_server_.host_port_pair()));
3123 2, network_delegate.observed_before_proxy_headers_sent_callbacks());
3125 network_delegate.last_observed_proxy().Equals(
3126 test_server_.host_port_pair()));
3127 EXPECT_EQ(OK, r.status().error());
3128 EXPECT_EQ(redirect_url, r.url());
3129 EXPECT_EQ(original_url, r.original_url());
3130 EXPECT_EQ(2U, r.url_chain().size());
3131 EXPECT_EQ(2, network_delegate.created_requests());
3132 EXPECT_EQ(0, network_delegate.destroyed_requests());
3134 EXPECT_EQ(1, network_delegate.destroyed_requests());
3137 // Tests that the network delegate can synchronously complete OnAuthRequired
3138 // by taking no action. This indicates that the NetworkDelegate does not want to
3139 // handle the challenge, and is passing the buck along to the
3140 // URLRequest::Delegate.
3141 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
3142 ASSERT_TRUE(test_server_.Start());
3145 BlockingNetworkDelegate network_delegate(
3146 BlockingNetworkDelegate::SYNCHRONOUS);
3148 TestURLRequestContext context(true);
3149 context.set_network_delegate(&network_delegate);
3152 d.set_credentials(AuthCredentials(kUser, kSecret));
3155 GURL url(test_server_.GetURL("auth-basic"));
3156 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3159 base::RunLoop().Run();
3161 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3162 EXPECT_EQ(0, r.status().error());
3163 EXPECT_EQ(200, r.GetResponseCode());
3164 EXPECT_TRUE(d.auth_required_called());
3165 EXPECT_EQ(1, network_delegate.created_requests());
3166 EXPECT_EQ(0, network_delegate.destroyed_requests());
3168 EXPECT_EQ(1, network_delegate.destroyed_requests());
3171 TEST_F(URLRequestTestHTTP,
3172 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) {
3173 ASSERT_TRUE(test_server_.Start());
3176 BlockingNetworkDelegate network_delegate(
3177 BlockingNetworkDelegate::SYNCHRONOUS);
3179 TestURLRequestContext context(true);
3180 context.set_network_delegate(&network_delegate);
3183 d.set_credentials(AuthCredentials(kUser, kSecret));
3186 GURL url(test_server_.GetURL("auth-basic"));
3187 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3191 HttpRequestHeaders headers;
3192 EXPECT_TRUE(r.GetFullRequestHeaders(&headers));
3193 EXPECT_FALSE(headers.HasHeader("Authorization"));
3196 base::RunLoop().Run();
3198 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3199 EXPECT_EQ(0, r.status().error());
3200 EXPECT_EQ(200, r.GetResponseCode());
3201 EXPECT_TRUE(d.auth_required_called());
3202 EXPECT_EQ(1, network_delegate.created_requests());
3203 EXPECT_EQ(0, network_delegate.destroyed_requests());
3205 EXPECT_EQ(1, network_delegate.destroyed_requests());
3208 // Tests that the network delegate can synchronously complete OnAuthRequired
3209 // by setting credentials.
3210 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
3211 ASSERT_TRUE(test_server_.Start());
3214 BlockingNetworkDelegate network_delegate(
3215 BlockingNetworkDelegate::SYNCHRONOUS);
3216 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3217 network_delegate.set_auth_retval(
3218 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3220 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3222 TestURLRequestContext context(true);
3223 context.set_network_delegate(&network_delegate);
3227 GURL url(test_server_.GetURL("auth-basic"));
3228 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3230 base::RunLoop().Run();
3232 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3233 EXPECT_EQ(0, r.status().error());
3234 EXPECT_EQ(200, r.GetResponseCode());
3235 EXPECT_FALSE(d.auth_required_called());
3236 EXPECT_EQ(1, network_delegate.created_requests());
3237 EXPECT_EQ(0, network_delegate.destroyed_requests());
3239 EXPECT_EQ(1, network_delegate.destroyed_requests());
3242 // Same as above, but also tests that GetFullRequestHeaders returns the proper
3243 // headers (for the first or second request) when called at the proper times.
3244 TEST_F(URLRequestTestHTTP,
3245 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) {
3246 ASSERT_TRUE(test_server_.Start());
3249 BlockingNetworkDelegate network_delegate(
3250 BlockingNetworkDelegate::SYNCHRONOUS);
3251 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3252 network_delegate.set_auth_retval(
3253 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3255 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3257 TestURLRequestContext context(true);
3258 context.set_network_delegate(&network_delegate);
3262 GURL url(test_server_.GetURL("auth-basic"));
3263 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3265 base::RunLoop().Run();
3267 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3268 EXPECT_EQ(0, r.status().error());
3269 EXPECT_EQ(200, r.GetResponseCode());
3270 EXPECT_FALSE(d.auth_required_called());
3271 EXPECT_EQ(1, network_delegate.created_requests());
3272 EXPECT_EQ(0, network_delegate.destroyed_requests());
3275 HttpRequestHeaders headers;
3276 EXPECT_TRUE(r.GetFullRequestHeaders(&headers));
3277 EXPECT_TRUE(headers.HasHeader("Authorization"));
3280 EXPECT_EQ(1, network_delegate.destroyed_requests());
3283 // Tests that the network delegate can synchronously complete OnAuthRequired
3284 // by cancelling authentication.
3285 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
3286 ASSERT_TRUE(test_server_.Start());
3289 BlockingNetworkDelegate network_delegate(
3290 BlockingNetworkDelegate::SYNCHRONOUS);
3291 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3292 network_delegate.set_auth_retval(
3293 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3295 TestURLRequestContext context(true);
3296 context.set_network_delegate(&network_delegate);
3300 GURL url(test_server_.GetURL("auth-basic"));
3301 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3303 base::RunLoop().Run();
3305 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3306 EXPECT_EQ(OK, r.status().error());
3307 EXPECT_EQ(401, r.GetResponseCode());
3308 EXPECT_FALSE(d.auth_required_called());
3309 EXPECT_EQ(1, network_delegate.created_requests());
3310 EXPECT_EQ(0, network_delegate.destroyed_requests());
3312 EXPECT_EQ(1, network_delegate.destroyed_requests());
3315 // Tests that the network delegate can asynchronously complete OnAuthRequired
3316 // by taking no action. This indicates that the NetworkDelegate does not want
3317 // to handle the challenge, and is passing the buck along to the
3318 // URLRequest::Delegate.
3319 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
3320 ASSERT_TRUE(test_server_.Start());
3323 BlockingNetworkDelegate network_delegate(
3324 BlockingNetworkDelegate::AUTO_CALLBACK);
3325 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3327 TestURLRequestContext context(true);
3328 context.set_network_delegate(&network_delegate);
3331 d.set_credentials(AuthCredentials(kUser, kSecret));
3334 GURL url(test_server_.GetURL("auth-basic"));
3335 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3337 base::RunLoop().Run();
3339 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3340 EXPECT_EQ(0, r.status().error());
3341 EXPECT_EQ(200, r.GetResponseCode());
3342 EXPECT_TRUE(d.auth_required_called());
3343 EXPECT_EQ(1, network_delegate.created_requests());
3344 EXPECT_EQ(0, network_delegate.destroyed_requests());
3346 EXPECT_EQ(1, network_delegate.destroyed_requests());
3349 // Tests that the network delegate can asynchronously complete OnAuthRequired
3350 // by setting credentials.
3351 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
3352 ASSERT_TRUE(test_server_.Start());
3355 BlockingNetworkDelegate network_delegate(
3356 BlockingNetworkDelegate::AUTO_CALLBACK);
3357 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3358 network_delegate.set_auth_retval(
3359 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3361 AuthCredentials auth_credentials(kUser, kSecret);
3362 network_delegate.set_auth_credentials(auth_credentials);
3364 TestURLRequestContext context(true);
3365 context.set_network_delegate(&network_delegate);
3369 GURL url(test_server_.GetURL("auth-basic"));
3370 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3372 base::RunLoop().Run();
3374 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3375 EXPECT_EQ(0, r.status().error());
3377 EXPECT_EQ(200, r.GetResponseCode());
3378 EXPECT_FALSE(d.auth_required_called());
3379 EXPECT_EQ(1, network_delegate.created_requests());
3380 EXPECT_EQ(0, network_delegate.destroyed_requests());
3382 EXPECT_EQ(1, network_delegate.destroyed_requests());
3385 // Tests that the network delegate can asynchronously complete OnAuthRequired
3386 // by cancelling authentication.
3387 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
3388 ASSERT_TRUE(test_server_.Start());
3391 BlockingNetworkDelegate network_delegate(
3392 BlockingNetworkDelegate::AUTO_CALLBACK);
3393 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3394 network_delegate.set_auth_retval(
3395 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3397 TestURLRequestContext context(true);
3398 context.set_network_delegate(&network_delegate);
3402 GURL url(test_server_.GetURL("auth-basic"));
3403 URLRequest r(url, DEFAULT_PRIORITY, &d, &context);
3405 base::RunLoop().Run();
3407 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
3408 EXPECT_EQ(OK, r.status().error());
3409 EXPECT_EQ(401, r.GetResponseCode());
3410 EXPECT_FALSE(d.auth_required_called());
3411 EXPECT_EQ(1, network_delegate.created_requests());
3412 EXPECT_EQ(0, network_delegate.destroyed_requests());
3414 EXPECT_EQ(1, network_delegate.destroyed_requests());
3417 // Tests that we can handle when a network request was canceled while we were
3418 // waiting for the network delegate.
3419 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3420 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
3421 ASSERT_TRUE(test_server_.Start());
3424 BlockingNetworkDelegate network_delegate(
3425 BlockingNetworkDelegate::USER_CALLBACK);
3426 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3428 TestURLRequestContext context(true);
3429 context.set_network_delegate(&network_delegate);
3434 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
3437 base::RunLoop().Run();
3438 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3439 network_delegate.stage_blocked_for_callback());
3440 EXPECT_EQ(0, network_delegate.completed_requests());
3441 // Cancel before callback.
3443 // Ensure that network delegate is notified.
3444 EXPECT_EQ(1, network_delegate.completed_requests());
3445 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3446 EXPECT_EQ(ERR_ABORTED, r.status().error());
3447 EXPECT_EQ(1, network_delegate.created_requests());
3448 EXPECT_EQ(0, network_delegate.destroyed_requests());
3450 EXPECT_EQ(1, network_delegate.destroyed_requests());
3453 // Tests that we can handle when a network request was canceled while we were
3454 // waiting for the network delegate.
3455 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3456 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3457 ASSERT_TRUE(test_server_.Start());
3460 BlockingNetworkDelegate network_delegate(
3461 BlockingNetworkDelegate::USER_CALLBACK);
3462 network_delegate.set_block_on(
3463 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
3465 TestURLRequestContext context(true);
3466 context.set_network_delegate(&network_delegate);
3471 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
3474 base::RunLoop().Run();
3475 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3476 network_delegate.stage_blocked_for_callback());
3477 EXPECT_EQ(0, network_delegate.completed_requests());
3478 // Cancel before callback.
3480 // Ensure that network delegate is notified.
3481 EXPECT_EQ(1, network_delegate.completed_requests());
3482 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3483 EXPECT_EQ(ERR_ABORTED, r.status().error());
3484 EXPECT_EQ(1, network_delegate.created_requests());
3485 EXPECT_EQ(0, network_delegate.destroyed_requests());
3487 EXPECT_EQ(1, network_delegate.destroyed_requests());
3490 // Tests that we can handle when a network request was canceled while we were
3491 // waiting for the network delegate.
3492 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3493 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3494 ASSERT_TRUE(test_server_.Start());
3497 BlockingNetworkDelegate network_delegate(
3498 BlockingNetworkDelegate::USER_CALLBACK);
3499 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3501 TestURLRequestContext context(true);
3502 context.set_network_delegate(&network_delegate);
3507 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context);
3510 base::RunLoop().Run();
3511 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3512 network_delegate.stage_blocked_for_callback());
3513 EXPECT_EQ(0, network_delegate.completed_requests());
3514 // Cancel before callback.
3516 // Ensure that network delegate is notified.
3517 EXPECT_EQ(1, network_delegate.completed_requests());
3518 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3519 EXPECT_EQ(ERR_ABORTED, r.status().error());
3520 EXPECT_EQ(1, network_delegate.created_requests());
3521 EXPECT_EQ(0, network_delegate.destroyed_requests());
3523 EXPECT_EQ(1, network_delegate.destroyed_requests());
3526 // Tests that we can handle when a network request was canceled while we were
3527 // waiting for the network delegate.
3528 // Part 4: Request is cancelled while waiting for OnAuthRequired callback.
3529 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
3530 ASSERT_TRUE(test_server_.Start());
3533 BlockingNetworkDelegate network_delegate(
3534 BlockingNetworkDelegate::USER_CALLBACK);
3535 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3537 TestURLRequestContext context(true);
3538 context.set_network_delegate(&network_delegate);
3543 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, &context);
3546 base::RunLoop().Run();
3547 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3548 network_delegate.stage_blocked_for_callback());
3549 EXPECT_EQ(0, network_delegate.completed_requests());
3550 // Cancel before callback.
3552 // Ensure that network delegate is notified.
3553 EXPECT_EQ(1, network_delegate.completed_requests());
3554 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
3555 EXPECT_EQ(ERR_ABORTED, r.status().error());
3556 EXPECT_EQ(1, network_delegate.created_requests());
3557 EXPECT_EQ(0, network_delegate.destroyed_requests());
3559 EXPECT_EQ(1, network_delegate.destroyed_requests());
3562 // In this unit test, we're using the HTTPTestServer as a proxy server and
3563 // issuing a CONNECT request with the magic host name "www.server-auth.com".
3564 // The HTTPTestServer will return a 401 response, which we should balk at.
3565 TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
3566 ASSERT_TRUE(test_server_.Start());
3568 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
3569 TestURLRequestContextWithProxy context(
3570 test_server_.host_port_pair().ToString(), &network_delegate);
3575 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, &context);
3578 EXPECT_TRUE(r.is_pending());
3580 base::RunLoop().Run();
3582 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
3583 // The proxy server is not set before failure.
3584 EXPECT_TRUE(r.proxy_server().IsEmpty());
3585 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error());
3589 TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
3590 ASSERT_TRUE(test_server_.Start());
3594 URLRequest r(test_server_.GetURL(std::string()),
3600 EXPECT_TRUE(r.is_pending());
3602 base::RunLoop().Run();
3604 EXPECT_EQ(1, d.response_started_count());
3605 EXPECT_FALSE(d.received_data_before_response());
3606 EXPECT_NE(0, d.bytes_received());
3607 EXPECT_EQ(test_server_.host_port_pair().host(),
3608 r.GetSocketAddress().host());
3609 EXPECT_EQ(test_server_.host_port_pair().port(),
3610 r.GetSocketAddress().port());
3612 // TODO(eroman): Add back the NetLog tests...
3616 // This test has the server send a large number of cookies to the client.
3617 // To ensure that no number of cookies causes a crash, a galloping binary
3618 // search is used to estimate that maximum number of cookies that are accepted
3619 // by the browser. Beyond the maximum number, the request will fail with
3620 // ERR_RESPONSE_HEADERS_TOO_BIG.
3622 // http://crbug.com/177916
3623 #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3625 #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3626 #endif // defined(OS_WIN)
3627 TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
3628 ASSERT_TRUE(test_server_.Start());
3630 int lower_bound = 0;
3631 int upper_bound = 1;
3633 // Double the number of cookies until the response header limits are
3635 while (DoManyCookiesRequest(upper_bound)) {
3636 lower_bound = upper_bound;
3638 ASSERT_LT(upper_bound, 1000000);
3641 int tolerance = upper_bound * 0.005;
3645 // Perform a binary search to find the highest possible number of cookies,
3646 // within the desired tolerance.
3647 while (upper_bound - lower_bound >= tolerance) {
3648 int num_cookies = (lower_bound + upper_bound) / 2;
3650 if (DoManyCookiesRequest(num_cookies))
3651 lower_bound = num_cookies;
3653 upper_bound = num_cookies;
3655 // Success: the test did not crash.
3658 TEST_F(URLRequestTestHTTP, GetTest) {
3659 ASSERT_TRUE(test_server_.Start());
3663 URLRequest r(test_server_.GetURL(std::string()),
3669 EXPECT_TRUE(r.is_pending());
3671 base::RunLoop().Run();
3673 EXPECT_EQ(1, d.response_started_count());
3674 EXPECT_FALSE(d.received_data_before_response());
3675 EXPECT_NE(0, d.bytes_received());
3676 EXPECT_EQ(test_server_.host_port_pair().host(),
3677 r.GetSocketAddress().host());
3678 EXPECT_EQ(test_server_.host_port_pair().port(),
3679 r.GetSocketAddress().port());
3683 TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) {
3684 ASSERT_TRUE(test_server_.Start());
3688 GURL test_url(test_server_.GetURL(std::string()));
3689 URLRequest r(test_url, DEFAULT_PRIORITY, &d, &default_context_);
3691 HttpRequestHeaders headers;
3692 EXPECT_FALSE(r.GetFullRequestHeaders(&headers));
3695 EXPECT_TRUE(r.is_pending());
3697 base::RunLoop().Run();
3699 EXPECT_EQ(1, d.response_started_count());
3700 EXPECT_FALSE(d.received_data_before_response());
3701 EXPECT_NE(0, d.bytes_received());
3702 EXPECT_EQ(test_server_.host_port_pair().host(),
3703 r.GetSocketAddress().host());
3704 EXPECT_EQ(test_server_.host_port_pair().port(),
3705 r.GetSocketAddress().port());
3707 EXPECT_TRUE(d.have_full_request_headers());
3708 CheckFullRequestHeaders(d.full_request_headers(), test_url);
3712 TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3713 ASSERT_TRUE(test_server_.Start());
3717 URLRequest r(test_server_.GetURL(std::string()),
3723 EXPECT_TRUE(r.is_pending());
3725 base::RunLoop().Run();
3727 LoadTimingInfo load_timing_info;
3728 r.GetLoadTimingInfo(&load_timing_info);
3729 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3731 EXPECT_EQ(1, d.response_started_count());
3732 EXPECT_FALSE(d.received_data_before_response());
3733 EXPECT_NE(0, d.bytes_received());
3734 EXPECT_EQ(test_server_.host_port_pair().host(),
3735 r.GetSocketAddress().host());
3736 EXPECT_EQ(test_server_.host_port_pair().port(),
3737 r.GetSocketAddress().port());
3741 TEST_F(URLRequestTestHTTP, GetZippedTest) {
3742 ASSERT_TRUE(test_server_.Start());
3744 // Parameter that specifies the Content-Length field in the response:
3745 // C - Compressed length.
3746 // U - Uncompressed length.
3747 // L - Large length (larger than both C & U).
3748 // M - Medium length (between C & U).
3749 // S - Small length (smaller than both C & U).
3750 const char test_parameters[] = "CULMS";
3751 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
3752 // C & U should be OK.
3753 // L & M are larger than the data sent, and show an error.
3754 // S has too little data, but we seem to accept it.
3755 const bool test_expect_success[num_tests] =
3756 { true, true, false, false, true };
3758 for (int i = 0; i < num_tests ; i++) {
3761 std::string test_file =
3762 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
3763 test_parameters[i]);
3765 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
3766 TestURLRequestContext context(true);
3767 context.set_network_delegate(&network_delegate);
3771 test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, &context);
3773 EXPECT_TRUE(r.is_pending());
3775 base::RunLoop().Run();
3777 EXPECT_EQ(1, d.response_started_count());
3778 EXPECT_FALSE(d.received_data_before_response());
3779 VLOG(1) << " Received " << d.bytes_received() << " bytes"
3780 << " status = " << r.status().status()
3781 << " error = " << r.status().error();
3782 if (test_expect_success[i]) {
3783 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status())
3784 << " Parameter = \"" << test_file << "\"";
3786 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
3787 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error())
3788 << " Parameter = \"" << test_file << "\"";
3794 TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) {
3795 ASSERT_TRUE(test_server_.Start());
3797 SpawnedTestServer https_test_server(
3798 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost,
3799 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
3800 ASSERT_TRUE(https_test_server.Start());
3802 // An https server is sent a request with an https referer,
3803 // and responds with a redirect to an http url. The http
3804 // server should not be sent the referer.
3805 GURL http_destination = test_server_.GetURL(std::string());
3808 https_test_server.GetURL("server-redirect?" + http_destination.spec()),
3812 req.SetReferrer("https://www.referrer.com/");
3814 base::RunLoop().Run();
3816 EXPECT_EQ(1, d.response_started_count());
3817 EXPECT_EQ(1, d.received_redirect_count());
3818 EXPECT_EQ(http_destination, req.url());
3819 EXPECT_EQ(std::string(), req.referrer());
3822 TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
3823 ASSERT_TRUE(test_server_.Start());
3825 GURL destination_url = test_server_.GetURL(std::string());
3827 test_server_.GetURL("server-redirect?" + destination_url.spec());
3829 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
3831 base::RunLoop().Run();
3833 EXPECT_EQ(1, d.response_started_count());
3834 EXPECT_EQ(1, d.received_redirect_count());
3835 EXPECT_EQ(destination_url, req.url());
3836 EXPECT_EQ(original_url, req.original_url());
3837 ASSERT_EQ(2U, req.url_chain().size());
3838 EXPECT_EQ(original_url, req.url_chain()[0]);
3839 EXPECT_EQ(destination_url, req.url_chain()[1]);
3841 LoadTimingInfo load_timing_info_before_redirect;
3842 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
3843 &load_timing_info_before_redirect));
3844 TestLoadTimingNotReused(load_timing_info_before_redirect,
3845 CONNECT_TIMING_HAS_DNS_TIMES);
3847 LoadTimingInfo load_timing_info;
3848 req.GetLoadTimingInfo(&load_timing_info);
3849 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
3851 // Check that a new socket was used on redirect, since the server does not
3852 // supposed keep-alive sockets, and that the times before the redirect are
3853 // before the ones recorded for the second request.
3854 EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
3855 load_timing_info.socket_log_id);
3856 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
3857 load_timing_info.connect_timing.connect_start);
3860 TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
3861 ASSERT_TRUE(test_server_.Start());
3863 GURL destination_url = test_server_.GetURL(std::string());
3864 GURL middle_redirect_url =
3865 test_server_.GetURL("server-redirect?" + destination_url.spec());
3866 GURL original_url = test_server_.GetURL(
3867 "server-redirect?" + middle_redirect_url.spec());
3869 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
3871 base::RunLoop().Run();
3873 EXPECT_EQ(1, d.response_started_count());
3874 EXPECT_EQ(2, d.received_redirect_count());
3875 EXPECT_EQ(destination_url, req.url());
3876 EXPECT_EQ(original_url, req.original_url());
3877 ASSERT_EQ(3U, req.url_chain().size());
3878 EXPECT_EQ(original_url, req.url_chain()[0]);
3879 EXPECT_EQ(middle_redirect_url, req.url_chain()[1]);
3880 EXPECT_EQ(destination_url, req.url_chain()[2]);
3883 // First and second pieces of information logged by delegates to URLRequests.
3884 const char kFirstDelegateInfo[] = "Wonderful delegate";
3885 const char kSecondDelegateInfo[] = "Exciting delegate";
3887 // Logs delegate information to a URLRequest. The first string is logged
3888 // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is
3889 // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then
3890 // another asynchronous call is used to clear the delegate information
3891 // before calling a callback. The object then deletes itself.
3892 class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> {
3894 typedef base::Callback<void()> Callback;
3896 // Each time delegate information is added to the URLRequest, the resulting
3897 // load state is checked. The expected load state after each request is
3898 // passed in as an argument.
3899 static void Run(URLRequest* url_request,
3900 LoadState expected_first_load_state,
3901 LoadState expected_second_load_state,
3902 LoadState expected_third_load_state,
3903 const Callback& callback) {
3904 AsyncDelegateLogger* logger = new AsyncDelegateLogger(
3906 expected_first_load_state,
3907 expected_second_load_state,
3908 expected_third_load_state,
3913 // Checks that the log entries, starting with log_position, contain the
3914 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
3915 // recorded. Returns the index of entry after the expected number of
3916 // events this logged, or entries.size() if there aren't enough entries.
3917 static size_t CheckDelegateInfo(
3918 const CapturingNetLog::CapturedEntryList& entries, size_t log_position) {
3919 // There should be 4 DELEGATE_INFO events: Two begins and two ends.
3920 if (log_position + 3 >= entries.size()) {
3921 ADD_FAILURE() << "Not enough log entries";
3922 return entries.size();
3924 std::string delegate_info;
3925 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3926 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3927 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3929 EXPECT_EQ(kFirstDelegateInfo, delegate_info);
3932 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3933 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3936 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3937 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
3938 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
3940 EXPECT_EQ(kSecondDelegateInfo, delegate_info);
3943 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
3944 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
3946 return log_position + 1;
3949 // Find delegate request begin and end messages for OnBeforeNetworkStart.
3950 // Returns the position of the end message.
3951 static size_t ExpectBeforeNetworkEvents(
3952 const CapturingNetLog::CapturedEntryList& entries,
3953 size_t log_position) {
3955 ExpectLogContainsSomewhereAfter(entries,
3957 NetLog::TYPE_URL_REQUEST_DELEGATE,
3958 NetLog::PHASE_BEGIN);
3959 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
3960 entries[log_position + 1].type);
3961 EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
3962 return log_position + 1;
3966 friend class base::RefCounted<AsyncDelegateLogger>;
3968 AsyncDelegateLogger(URLRequest* url_request,
3969 LoadState expected_first_load_state,
3970 LoadState expected_second_load_state,
3971 LoadState expected_third_load_state,
3972 const Callback& callback)
3973 : url_request_(url_request),
3974 expected_first_load_state_(expected_first_load_state),
3975 expected_second_load_state_(expected_second_load_state),
3976 expected_third_load_state_(expected_third_load_state),
3977 callback_(callback) {
3980 ~AsyncDelegateLogger() {}
3983 url_request_->LogBlockedBy(kFirstDelegateInfo);
3984 LoadStateWithParam load_state = url_request_->GetLoadState();
3985 EXPECT_EQ(expected_first_load_state_, load_state.state);
3986 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param);
3987 base::MessageLoop::current()->PostTask(
3989 base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this));
3992 void LogSecondDelegate() {
3993 url_request_->LogAndReportBlockedBy(kSecondDelegateInfo);
3994 LoadStateWithParam load_state = url_request_->GetLoadState();
3995 EXPECT_EQ(expected_second_load_state_, load_state.state);
3996 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) {
3997 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
3999 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
4001 base::MessageLoop::current()->PostTask(
4003 base::Bind(&AsyncDelegateLogger::LogComplete, this));
4006 void LogComplete() {
4007 url_request_->LogUnblocked();
4008 LoadStateWithParam load_state = url_request_->GetLoadState();
4009 EXPECT_EQ(expected_third_load_state_, load_state.state);
4010 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE)
4011 EXPECT_EQ(base::string16(), load_state.param);
4015 URLRequest* url_request_;
4016 const int expected_first_load_state_;
4017 const int expected_second_load_state_;
4018 const int expected_third_load_state_;
4019 const Callback callback_;
4021 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger);
4024 // NetworkDelegate that logs delegate information before a request is started,
4025 // before headers are sent, when headers are read, and when auth information
4026 // is requested. Uses AsyncDelegateLogger.
4027 class AsyncLoggingNetworkDelegate : public TestNetworkDelegate {
4029 AsyncLoggingNetworkDelegate() {}
4030 virtual ~AsyncLoggingNetworkDelegate() {}
4032 // NetworkDelegate implementation.
4033 virtual int OnBeforeURLRequest(URLRequest* request,
4034 const CompletionCallback& callback,
4035 GURL* new_url) OVERRIDE {
4036 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
4037 return RunCallbackAsynchronously(request, callback);
4040 virtual int OnBeforeSendHeaders(URLRequest* request,
4041 const CompletionCallback& callback,
4042 HttpRequestHeaders* headers) OVERRIDE {
4043 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
4044 return RunCallbackAsynchronously(request, callback);
4047 virtual int OnHeadersReceived(
4048 URLRequest* request,
4049 const CompletionCallback& callback,
4050 const HttpResponseHeaders* original_response_headers,
4051 scoped_refptr<HttpResponseHeaders>* override_response_headers,
4052 GURL* allowed_unsafe_redirect_url) OVERRIDE {
4053 TestNetworkDelegate::OnHeadersReceived(request,
4055 original_response_headers,
4056 override_response_headers,
4057 allowed_unsafe_redirect_url);
4058 return RunCallbackAsynchronously(request, callback);
4061 virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired(
4062 URLRequest* request,
4063 const AuthChallengeInfo& auth_info,
4064 const AuthCallback& callback,
4065 AuthCredentials* credentials) OVERRIDE {
4066 AsyncDelegateLogger::Run(
4068 LOAD_STATE_WAITING_FOR_DELEGATE,
4069 LOAD_STATE_WAITING_FOR_DELEGATE,
4070 LOAD_STATE_WAITING_FOR_DELEGATE,
4071 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume,
4072 callback, credentials));
4073 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
4077 static int RunCallbackAsynchronously(
4078 URLRequest* request,
4079 const CompletionCallback& callback) {
4080 AsyncDelegateLogger::Run(
4082 LOAD_STATE_WAITING_FOR_DELEGATE,
4083 LOAD_STATE_WAITING_FOR_DELEGATE,
4084 LOAD_STATE_WAITING_FOR_DELEGATE,
4085 base::Bind(callback, OK));
4086 return ERR_IO_PENDING;
4089 static void SetAuthAndResume(const AuthCallback& callback,
4090 AuthCredentials* credentials) {
4091 *credentials = AuthCredentials(kUser, kSecret);
4092 callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
4095 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate);
4098 // URLRequest::Delegate that logs delegate information when the headers
4099 // are received, when each read completes, and during redirects. Uses
4100 // AsyncDelegateLogger. Can optionally cancel a request in any phase.
4102 // Inherits from TestDelegate to reuse the TestDelegate code to handle
4103 // advancing to the next step in most cases, as well as cancellation.
4104 class AsyncLoggingUrlRequestDelegate : public TestDelegate {
4108 CANCEL_ON_RECEIVED_REDIRECT,
4109 CANCEL_ON_RESPONSE_STARTED,
4110 CANCEL_ON_READ_COMPLETED
4113 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage)
4114 : cancel_stage_(cancel_stage) {
4115 if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT)
4116 set_cancel_in_received_redirect(true);
4117 else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED)
4118 set_cancel_in_response_started(true);
4119 else if (cancel_stage == CANCEL_ON_READ_COMPLETED)
4120 set_cancel_in_received_data(true);
4122 virtual ~AsyncLoggingUrlRequestDelegate() {}
4124 // URLRequest::Delegate implementation:
4125 void virtual OnReceivedRedirect(URLRequest* request,
4126 const RedirectInfo& redirect_info,
4127 bool* defer_redirect) OVERRIDE {
4128 *defer_redirect = true;
4129 AsyncDelegateLogger::Run(
4131 LOAD_STATE_WAITING_FOR_DELEGATE,
4132 LOAD_STATE_WAITING_FOR_DELEGATE,
4133 LOAD_STATE_WAITING_FOR_DELEGATE,
4135 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete,
4136 base::Unretained(this), request, redirect_info));
4139 virtual void OnResponseStarted(URLRequest* request) OVERRIDE {
4140 AsyncDelegateLogger::Run(
4142 LOAD_STATE_WAITING_FOR_DELEGATE,
4143 LOAD_STATE_WAITING_FOR_DELEGATE,
4144 LOAD_STATE_WAITING_FOR_DELEGATE,
4146 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete,
4147 base::Unretained(this), request));
4150 virtual void OnReadCompleted(URLRequest* request,
4151 int bytes_read) OVERRIDE {
4152 AsyncDelegateLogger::Run(
4158 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete,
4159 base::Unretained(this), request, bytes_read));
4163 void OnReceivedRedirectLoggingComplete(URLRequest* request,
4164 const RedirectInfo& redirect_info) {
4165 bool defer_redirect = false;
4166 TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect);
4167 // FollowDeferredRedirect should not be called after cancellation.
4168 if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT)
4170 if (!defer_redirect)
4171 request->FollowDeferredRedirect();
4174 void OnResponseStartedLoggingComplete(URLRequest* request) {
4175 // The parent class continues the request.
4176 TestDelegate::OnResponseStarted(request);
4179 void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) {
4180 // The parent class continues the request.
4181 TestDelegate::OnReadCompleted(request, bytes_read);
4184 const CancelStage cancel_stage_;
4186 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate);
4189 // Tests handling of delegate info before a request starts.
4190 TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) {
4191 ASSERT_TRUE(test_server_.Start());
4193 TestDelegate request_delegate;
4194 TestURLRequestContext context(true);
4195 context.set_network_delegate(NULL);
4196 context.set_net_log(&net_log_);
4200 URLRequest r(test_server_.GetURL("empty.html"),
4204 LoadStateWithParam load_state = r.GetLoadState();
4205 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4206 EXPECT_EQ(base::string16(), load_state.param);
4208 AsyncDelegateLogger::Run(
4210 LOAD_STATE_WAITING_FOR_DELEGATE,
4211 LOAD_STATE_WAITING_FOR_DELEGATE,
4213 base::Bind(&URLRequest::Start, base::Unretained(&r)));
4215 base::RunLoop().Run();
4217 EXPECT_EQ(200, r.GetResponseCode());
4218 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4221 CapturingNetLog::CapturedEntryList entries;
4222 net_log_.GetEntries(&entries);
4223 size_t log_position = ExpectLogContainsSomewhereAfter(
4226 NetLog::TYPE_DELEGATE_INFO,
4227 NetLog::PHASE_BEGIN);
4229 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position);
4231 // Nothing else should add any delegate info to the request.
4232 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4233 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4236 // Tests handling of delegate info from a network delegate.
4237 TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) {
4238 ASSERT_TRUE(test_server_.Start());
4240 TestDelegate request_delegate;
4241 AsyncLoggingNetworkDelegate network_delegate;
4242 TestURLRequestContext context(true);
4243 context.set_network_delegate(&network_delegate);
4244 context.set_net_log(&net_log_);
4248 URLRequest r(test_server_.GetURL("simple.html"),
4252 LoadStateWithParam load_state = r.GetLoadState();
4253 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4254 EXPECT_EQ(base::string16(), load_state.param);
4257 base::RunLoop().Run();
4259 EXPECT_EQ(200, r.GetResponseCode());
4260 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4261 EXPECT_EQ(1, network_delegate.created_requests());
4262 EXPECT_EQ(0, network_delegate.destroyed_requests());
4264 EXPECT_EQ(1, network_delegate.destroyed_requests());
4266 size_t log_position = 0;
4267 CapturingNetLog::CapturedEntryList entries;
4268 net_log_.GetEntries(&entries);
4269 for (size_t i = 0; i < 3; ++i) {
4270 log_position = ExpectLogContainsSomewhereAfter(
4273 NetLog::TYPE_URL_REQUEST_DELEGATE,
4274 NetLog::PHASE_BEGIN);
4276 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4279 ASSERT_LT(log_position, entries.size());
4280 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4281 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4284 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4285 entries, log_position + 1);
4289 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4290 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4293 // Tests handling of delegate info from a network delegate in the case of an
4295 TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4296 ASSERT_TRUE(test_server_.Start());
4298 TestDelegate request_delegate;
4299 AsyncLoggingNetworkDelegate network_delegate;
4300 TestURLRequestContext context(true);
4301 context.set_network_delegate(&network_delegate);
4302 context.set_net_log(&net_log_);
4306 URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
4310 LoadStateWithParam load_state = r.GetLoadState();
4311 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4312 EXPECT_EQ(base::string16(), load_state.param);
4315 base::RunLoop().Run();
4317 EXPECT_EQ(200, r.GetResponseCode());
4318 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4319 EXPECT_EQ(2, network_delegate.created_requests());
4320 EXPECT_EQ(0, network_delegate.destroyed_requests());
4322 EXPECT_EQ(1, network_delegate.destroyed_requests());
4324 size_t log_position = 0;
4325 CapturingNetLog::CapturedEntryList entries;
4326 net_log_.GetEntries(&entries);
4327 // The NetworkDelegate logged information in OnBeforeURLRequest,
4328 // OnBeforeSendHeaders, and OnHeadersReceived.
4329 for (size_t i = 0; i < 3; ++i) {
4330 log_position = ExpectLogContainsSomewhereAfter(
4333 NetLog::TYPE_URL_REQUEST_DELEGATE,
4334 NetLog::PHASE_BEGIN);
4336 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4339 ASSERT_LT(log_position, entries.size());
4340 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4341 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4344 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4345 entries, log_position + 1);
4349 // The URLRequest::Delegate then gets informed about the redirect.
4350 log_position = ExpectLogContainsSomewhereAfter(
4353 NetLog::TYPE_URL_REQUEST_DELEGATE,
4354 NetLog::PHASE_BEGIN);
4356 // The NetworkDelegate logged information in the same three events as before.
4357 for (size_t i = 0; i < 3; ++i) {
4358 log_position = ExpectLogContainsSomewhereAfter(
4361 NetLog::TYPE_URL_REQUEST_DELEGATE,
4362 NetLog::PHASE_BEGIN);
4364 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4367 ASSERT_LT(log_position, entries.size());
4368 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4369 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4372 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4373 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4376 // Tests handling of delegate info from a network delegate in the case of HTTP
4378 TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) {
4379 ASSERT_TRUE(test_server_.Start());
4381 TestDelegate request_delegate;
4382 AsyncLoggingNetworkDelegate network_delegate;
4383 TestURLRequestContext context(true);
4384 context.set_network_delegate(&network_delegate);
4385 context.set_net_log(&net_log_);
4389 URLRequest r(test_server_.GetURL("auth-basic"),
4393 LoadStateWithParam load_state = r.GetLoadState();
4394 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4395 EXPECT_EQ(base::string16(), load_state.param);
4398 base::RunLoop().Run();
4400 EXPECT_EQ(200, r.GetResponseCode());
4401 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4402 EXPECT_EQ(1, network_delegate.created_requests());
4403 EXPECT_EQ(0, network_delegate.destroyed_requests());
4405 EXPECT_EQ(1, network_delegate.destroyed_requests());
4407 size_t log_position = 0;
4408 CapturingNetLog::CapturedEntryList entries;
4409 net_log_.GetEntries(&entries);
4410 // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4411 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4412 // OnBeforeURLRequest and OnBeforeSendHeaders.
4413 for (size_t i = 0; i < 6; ++i) {
4414 log_position = ExpectLogContainsSomewhereAfter(
4417 NetLog::TYPE_URL_REQUEST_DELEGATE,
4418 NetLog::PHASE_BEGIN);
4420 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4423 ASSERT_LT(log_position, entries.size());
4424 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4425 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4428 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4429 entries, log_position + 1);
4433 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4434 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4437 // Tests handling of delegate info from a URLRequest::Delegate.
4438 TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4439 ASSERT_TRUE(test_server_.Start());
4441 AsyncLoggingUrlRequestDelegate request_delegate(
4442 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4443 TestURLRequestContext context(true);
4444 context.set_network_delegate(NULL);
4445 context.set_net_log(&net_log_);
4449 // A chunked response with delays between chunks is used to make sure that
4450 // attempts by the URLRequest delegate to log information while reading the
4451 // body are ignored. Since they are ignored, this test is robust against
4452 // the possibility of multiple reads being combined in the unlikely event
4454 URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"),
4458 LoadStateWithParam load_state = r.GetLoadState();
4460 base::RunLoop().Run();
4462 EXPECT_EQ(200, r.GetResponseCode());
4463 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4466 CapturingNetLog::CapturedEntryList entries;
4467 net_log_.GetEntries(&entries);
4469 size_t log_position = 0;
4471 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4472 entries, log_position);
4474 // The delegate info should only have been logged on header complete. Other
4475 // times it should silently be ignored.
4477 ExpectLogContainsSomewhereAfter(entries,
4479 NetLog::TYPE_URL_REQUEST_DELEGATE,
4480 NetLog::PHASE_BEGIN);
4482 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4485 ASSERT_LT(log_position, entries.size());
4486 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4487 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4489 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4490 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4491 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4492 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4495 // Tests handling of delegate info from a URLRequest::Delegate in the case of
4496 // an HTTP redirect.
4497 TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) {
4498 ASSERT_TRUE(test_server_.Start());
4500 AsyncLoggingUrlRequestDelegate request_delegate(
4501 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4502 TestURLRequestContext context(true);
4503 context.set_network_delegate(NULL);
4504 context.set_net_log(&net_log_);
4508 URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
4512 LoadStateWithParam load_state = r.GetLoadState();
4514 base::RunLoop().Run();
4516 EXPECT_EQ(200, r.GetResponseCode());
4517 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4520 CapturingNetLog::CapturedEntryList entries;
4521 net_log_.GetEntries(&entries);
4523 // Delegate info should only have been logged in OnReceivedRedirect and
4524 // OnResponseStarted.
4525 size_t log_position = 0;
4526 for (int i = 0; i < 2; ++i) {
4528 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4529 entries, log_position) + 1;
4532 log_position = ExpectLogContainsSomewhereAfter(
4535 NetLog::TYPE_URL_REQUEST_DELEGATE,
4536 NetLog::PHASE_BEGIN);
4538 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4541 ASSERT_LT(log_position, entries.size());
4542 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4543 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4546 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4547 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4548 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4549 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4552 // Tests handling of delegate info from a URLRequest::Delegate in the case of
4553 // an HTTP redirect, with cancellation at various points.
4554 TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) {
4555 ASSERT_TRUE(test_server_.Start());
4557 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = {
4558 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT,
4559 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED,
4560 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED,
4563 for (size_t test_case = 0; test_case < arraysize(kCancelStages);
4565 AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]);
4566 TestURLRequestContext context(true);
4567 CapturingNetLog net_log;
4568 context.set_network_delegate(NULL);
4569 context.set_net_log(&net_log);
4573 URLRequest r(test_server_.GetURL("server-redirect?simple.html"),
4577 LoadStateWithParam load_state = r.GetLoadState();
4579 base::RunLoop().Run();
4580 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4583 CapturingNetLog::CapturedEntryList entries;
4584 net_log.GetEntries(&entries);
4586 // Delegate info is always logged in both OnReceivedRedirect and
4587 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4588 // OnResponseStarted delegate call is after cancellation, but logging is
4589 // still currently supported in that call.
4590 size_t log_position = 0;
4591 for (int i = 0; i < 2; ++i) {
4593 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4594 entries, log_position) + 1;
4597 log_position = ExpectLogContainsSomewhereAfter(
4600 NetLog::TYPE_URL_REQUEST_DELEGATE,
4601 NetLog::PHASE_BEGIN);
4603 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4606 ASSERT_LT(log_position, entries.size());
4607 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4608 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4611 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4612 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4613 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4614 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4620 const char kExtraHeader[] = "Allow-Snafu";
4621 const char kExtraValue[] = "fubar";
4623 class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
4624 virtual void OnReceivedRedirect(URLRequest* request,
4625 const RedirectInfo& redirect_info,
4626 bool* defer_redirect) OVERRIDE {
4627 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
4628 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
4634 TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
4635 ASSERT_TRUE(test_server_.Start());
4637 GURL destination_url = test_server_.GetURL(
4638 "echoheader?" + std::string(kExtraHeader));
4639 GURL original_url = test_server_.GetURL(
4640 "server-redirect?" + destination_url.spec());
4641 RedirectWithAdditionalHeadersDelegate d;
4642 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
4644 base::RunLoop().Run();
4647 const HttpRequestHeaders& headers = req.extra_request_headers();
4648 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
4649 EXPECT_EQ(kExtraValue, value);
4650 EXPECT_FALSE(req.is_pending());
4651 EXPECT_FALSE(req.is_redirecting());
4652 EXPECT_EQ(kExtraValue, d.data_received());
4657 const char kExtraHeaderToRemove[] = "To-Be-Removed";
4659 class RedirectWithHeaderRemovalDelegate : public TestDelegate {
4660 virtual void OnReceivedRedirect(URLRequest* request,
4661 const RedirectInfo& redirect_info,
4662 bool* defer_redirect) OVERRIDE {
4663 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
4664 request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
4670 TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
4671 ASSERT_TRUE(test_server_.Start());
4673 GURL destination_url = test_server_.GetURL(
4674 "echoheader?" + std::string(kExtraHeaderToRemove));
4675 GURL original_url = test_server_.GetURL(
4676 "server-redirect?" + destination_url.spec());
4677 RedirectWithHeaderRemovalDelegate d;
4678 URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_);
4679 req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
4681 base::RunLoop().Run();
4684 const HttpRequestHeaders& headers = req.extra_request_headers();
4685 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
4686 EXPECT_FALSE(req.is_pending());
4687 EXPECT_FALSE(req.is_redirecting());
4688 EXPECT_EQ("None", d.data_received());
4691 TEST_F(URLRequestTestHTTP, CancelTest) {
4694 URLRequest r(GURL("http://www.google.com/"),
4700 EXPECT_TRUE(r.is_pending());
4704 base::RunLoop().Run();
4706 // We expect to receive OnResponseStarted even though the request has been
4708 EXPECT_EQ(1, d.response_started_count());
4709 EXPECT_EQ(0, d.bytes_received());
4710 EXPECT_FALSE(d.received_data_before_response());
4714 TEST_F(URLRequestTestHTTP, CancelTest2) {
4715 ASSERT_TRUE(test_server_.Start());
4719 URLRequest r(test_server_.GetURL(std::string()),
4724 d.set_cancel_in_response_started(true);
4727 EXPECT_TRUE(r.is_pending());
4729 base::RunLoop().Run();
4731 EXPECT_EQ(1, d.response_started_count());
4732 EXPECT_EQ(0, d.bytes_received());
4733 EXPECT_FALSE(d.received_data_before_response());
4734 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4738 TEST_F(URLRequestTestHTTP, CancelTest3) {
4739 ASSERT_TRUE(test_server_.Start());
4743 URLRequest r(test_server_.GetURL(std::string()),
4748 d.set_cancel_in_received_data(true);
4751 EXPECT_TRUE(r.is_pending());
4753 base::RunLoop().Run();
4755 EXPECT_EQ(1, d.response_started_count());
4756 // There is no guarantee about how much data was received
4757 // before the cancel was issued. It could have been 0 bytes,
4758 // or it could have been all the bytes.
4759 // EXPECT_EQ(0, d.bytes_received());
4760 EXPECT_FALSE(d.received_data_before_response());
4761 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4765 TEST_F(URLRequestTestHTTP, CancelTest4) {
4766 ASSERT_TRUE(test_server_.Start());
4770 URLRequest r(test_server_.GetURL(std::string()),
4776 EXPECT_TRUE(r.is_pending());
4778 // The request will be implicitly canceled when it is destroyed. The
4779 // test delegate must not post a quit message when this happens because
4780 // this test doesn't actually have a message loop. The quit message would
4781 // get put on this thread's message queue and the next test would exit
4782 // early, causing problems.
4783 d.set_quit_on_complete(false);
4785 // expect things to just cleanup properly.
4787 // we won't actually get a received reponse here because we've never run the
4789 EXPECT_FALSE(d.received_data_before_response());
4790 EXPECT_EQ(0, d.bytes_received());
4793 TEST_F(URLRequestTestHTTP, CancelTest5) {
4794 ASSERT_TRUE(test_server_.Start());
4799 URLRequest r(test_server_.GetURL("cachetime"),
4804 base::RunLoop().Run();
4805 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
4808 // cancel read from cache (see bug 990242)
4811 URLRequest r(test_server_.GetURL("cachetime"),
4817 base::RunLoop().Run();
4819 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status());
4820 EXPECT_EQ(1, d.response_started_count());
4821 EXPECT_EQ(0, d.bytes_received());
4822 EXPECT_FALSE(d.received_data_before_response());
4826 TEST_F(URLRequestTestHTTP, PostTest) {
4827 ASSERT_TRUE(test_server_.Start());
4828 HTTPUploadDataOperationTest("POST");
4831 TEST_F(URLRequestTestHTTP, PutTest) {
4832 ASSERT_TRUE(test_server_.Start());
4833 HTTPUploadDataOperationTest("PUT");
4836 TEST_F(URLRequestTestHTTP, PostEmptyTest) {
4837 ASSERT_TRUE(test_server_.Start());
4842 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4843 r.set_method("POST");
4846 EXPECT_TRUE(r.is_pending());
4848 base::RunLoop().Run();
4850 ASSERT_EQ(1, d.response_started_count())
4851 << "request failed: " << r.status().status()
4852 << ", error: " << r.status().error();
4854 EXPECT_FALSE(d.received_data_before_response());
4855 EXPECT_TRUE(d.data_received().empty());
4859 TEST_F(URLRequestTestHTTP, PostFileTest) {
4860 ASSERT_TRUE(test_server_.Start());
4865 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4866 r.set_method("POST");
4869 PathService::Get(base::DIR_EXE, &dir);
4870 base::SetCurrentDirectory(dir);
4872 ScopedVector<UploadElementReader> element_readers;
4874 base::FilePath path;
4875 PathService::Get(base::DIR_SOURCE_ROOT, &path);
4876 path = path.Append(FILE_PATH_LITERAL("net"));
4877 path = path.Append(FILE_PATH_LITERAL("data"));
4878 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
4879 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
4880 element_readers.push_back(
4881 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
4886 r.set_upload(make_scoped_ptr(
4887 new UploadDataStream(element_readers.Pass(), 0)));
4890 EXPECT_TRUE(r.is_pending());
4892 base::RunLoop().Run();
4895 ASSERT_EQ(true, base::GetFileSize(path, &size));
4896 scoped_ptr<char[]> buf(new char[size]);
4898 ASSERT_EQ(size, base::ReadFile(path, buf.get(), size));
4900 ASSERT_EQ(1, d.response_started_count())
4901 << "request failed: " << r.status().status()
4902 << ", error: " << r.status().error();
4904 EXPECT_FALSE(d.received_data_before_response());
4906 EXPECT_EQ(size, d.bytes_received());
4907 EXPECT_EQ(std::string(&buf[0], size), d.data_received());
4911 TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) {
4912 ASSERT_TRUE(test_server_.Start());
4916 URLRequest r(test_server_.GetURL("echo"), DEFAULT_PRIORITY,
4917 &d, &default_context_);
4918 r.set_method("POST");
4920 ScopedVector<UploadElementReader> element_readers;
4922 element_readers.push_back(new UploadFileElementReader(
4923 base::MessageLoopProxy::current().get(),
4924 base::FilePath(FILE_PATH_LITERAL(
4925 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
4929 r.set_upload(make_scoped_ptr(
4930 new UploadDataStream(element_readers.Pass(), 0)));
4933 EXPECT_TRUE(r.is_pending());
4935 base::RunLoop().Run();
4937 EXPECT_TRUE(d.request_failed());
4938 EXPECT_FALSE(d.received_data_before_response());
4939 EXPECT_EQ(0, d.bytes_received());
4940 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
4941 EXPECT_EQ(ERR_FILE_NOT_FOUND, r.status().error());
4945 TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
4946 ASSERT_TRUE(test_server_.Start());
4951 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4952 r.EnableChunkedUpload();
4953 r.set_method("POST");
4954 AddChunksToUpload(&r);
4956 EXPECT_TRUE(r.is_pending());
4958 base::RunLoop().Run();
4960 VerifyReceivedDataMatchesChunks(&r, &d);
4964 TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
4965 ASSERT_TRUE(test_server_.Start());
4970 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4971 r.EnableChunkedUpload();
4972 r.set_method("POST");
4974 EXPECT_TRUE(r.is_pending());
4975 AddChunksToUpload(&r);
4976 base::RunLoop().Run();
4978 VerifyReceivedDataMatchesChunks(&r, &d);
4982 TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
4983 ASSERT_TRUE(test_server_.Start());
4988 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_);
4989 r.EnableChunkedUpload();
4990 r.set_method("POST");
4992 EXPECT_TRUE(r.is_pending());
4994 base::RunLoop().RunUntilIdle();
4995 AddChunksToUpload(&r);
4996 base::RunLoop().Run();
4998 VerifyReceivedDataMatchesChunks(&r, &d);
5002 TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
5003 ASSERT_TRUE(test_server_.Start());
5006 URLRequest req(test_server_.GetURL("files/with-headers.html"),
5011 base::RunLoop().Run();
5013 const HttpResponseHeaders* headers = req.response_headers();
5015 // Simple sanity check that response_info() accesses the same data.
5016 EXPECT_EQ(headers, req.response_info().headers.get());
5019 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
5020 EXPECT_EQ("private", header);
5023 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
5024 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
5026 // The response has two "X-Multiple-Entries" headers.
5027 // This verfies our output has them concatenated together.
5029 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
5030 EXPECT_EQ("a, b", header);
5033 TEST_F(URLRequestTestHTTP, ProcessSTS) {
5034 SpawnedTestServer::SSLOptions ssl_options;
5035 SpawnedTestServer https_test_server(
5036 SpawnedTestServer::TYPE_HTTPS,
5038 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5039 ASSERT_TRUE(https_test_server.Start());
5042 URLRequest request(https_test_server.GetURL("files/hsts-headers.html"),
5047 base::RunLoop().Run();
5049 TransportSecurityState* security_state =
5050 default_context_.transport_security_state();
5051 TransportSecurityState::DomainState domain_state;
5052 EXPECT_TRUE(security_state->GetDynamicDomainState(
5053 SpawnedTestServer::kLocalhost, &domain_state));
5054 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5055 domain_state.sts.upgrade_mode);
5056 EXPECT_TRUE(domain_state.sts.include_subdomains);
5057 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5058 #if defined(OS_ANDROID)
5059 // Android's CertVerifyProc does not (yet) handle pins.
5061 EXPECT_FALSE(domain_state.HasPublicKeyPins());
5065 // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
5066 // reject HPKP headers, and a test setting only HPKP headers will fail (no
5067 // DomainState present because header rejected).
5068 #if defined(OS_ANDROID)
5069 #define MAYBE_ProcessPKP DISABLED_ProcessPKP
5071 #define MAYBE_ProcessPKP ProcessPKP
5074 // Tests that enabling HPKP on a domain does not affect the HSTS
5075 // validity/expiration.
5076 TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) {
5077 SpawnedTestServer::SSLOptions ssl_options;
5078 SpawnedTestServer https_test_server(
5079 SpawnedTestServer::TYPE_HTTPS,
5081 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5082 ASSERT_TRUE(https_test_server.Start());
5085 URLRequest request(https_test_server.GetURL("files/hpkp-headers.html"),
5090 base::RunLoop().Run();
5092 TransportSecurityState* security_state =
5093 default_context_.transport_security_state();
5094 TransportSecurityState::DomainState domain_state;
5095 EXPECT_TRUE(security_state->GetDynamicDomainState(
5096 SpawnedTestServer::kLocalhost, &domain_state));
5097 EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT,
5098 domain_state.sts.upgrade_mode);
5099 EXPECT_FALSE(domain_state.sts.include_subdomains);
5100 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5101 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5102 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5105 TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
5106 SpawnedTestServer::SSLOptions ssl_options;
5107 SpawnedTestServer https_test_server(
5108 SpawnedTestServer::TYPE_HTTPS,
5110 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5111 ASSERT_TRUE(https_test_server.Start());
5115 https_test_server.GetURL("files/hsts-multiple-headers.html"),
5120 base::RunLoop().Run();
5122 // We should have set parameters from the first header, not the second.
5123 TransportSecurityState* security_state =
5124 default_context_.transport_security_state();
5125 TransportSecurityState::DomainState domain_state;
5126 EXPECT_TRUE(security_state->GetDynamicDomainState(
5127 SpawnedTestServer::kLocalhost, &domain_state));
5128 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5129 domain_state.sts.upgrade_mode);
5130 EXPECT_FALSE(domain_state.sts.include_subdomains);
5131 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5134 TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
5135 SpawnedTestServer::SSLOptions ssl_options;
5136 SpawnedTestServer https_test_server(
5137 SpawnedTestServer::TYPE_HTTPS,
5139 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5140 ASSERT_TRUE(https_test_server.Start());
5144 https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
5149 base::RunLoop().Run();
5151 // We should have set parameters from the first header, not the second.
5152 TransportSecurityState* security_state =
5153 default_context_.transport_security_state();
5154 TransportSecurityState::DomainState domain_state;
5155 EXPECT_TRUE(security_state->GetDynamicDomainState(
5156 SpawnedTestServer::kLocalhost, &domain_state));
5157 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5158 domain_state.sts.upgrade_mode);
5159 #if defined(OS_ANDROID)
5160 // Android's CertVerifyProc does not (yet) handle pins.
5162 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5164 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5166 // Even though there is an HSTS header asserting includeSubdomains, it is
5167 // the *second* such header, and we MUST process only the first.
5168 EXPECT_FALSE(domain_state.sts.include_subdomains);
5169 // includeSubdomains does not occur in the test HPKP header.
5170 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5173 // Tests that when multiple HPKP headers are present, asserting different
5174 // policies, that only the first such policy is processed.
5175 TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) {
5176 SpawnedTestServer::SSLOptions ssl_options;
5177 SpawnedTestServer https_test_server(
5178 SpawnedTestServer::TYPE_HTTPS,
5180 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5181 ASSERT_TRUE(https_test_server.Start());
5185 https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"),
5190 base::RunLoop().Run();
5192 TransportSecurityState* security_state =
5193 default_context_.transport_security_state();
5194 TransportSecurityState::DomainState domain_state;
5195 EXPECT_TRUE(security_state->GetDynamicDomainState(
5196 SpawnedTestServer::kLocalhost, &domain_state));
5197 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5198 domain_state.sts.upgrade_mode);
5199 #if defined(OS_ANDROID)
5200 // Android's CertVerifyProc does not (yet) handle pins.
5202 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5204 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5206 EXPECT_TRUE(domain_state.sts.include_subdomains);
5207 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5210 TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
5211 ASSERT_TRUE(test_server_.Start());
5214 URLRequest req(test_server_.GetURL("files/content-type-normalization.html"),
5219 base::RunLoop().Run();
5221 std::string mime_type;
5222 req.GetMimeType(&mime_type);
5223 EXPECT_EQ("text/html", mime_type);
5225 std::string charset;
5226 req.GetCharset(&charset);
5227 EXPECT_EQ("utf-8", charset);
5231 TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) {
5232 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5233 GURL data_url("data:,foo");
5234 DataProtocolHandler data_protocol_handler;
5235 EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url));
5237 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5238 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url));
5241 #if !defined(DISABLE_FILE_SUPPORT)
5242 TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) {
5243 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5244 GURL file_url("file:///foo.txt");
5245 FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current());
5246 EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url));
5248 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5249 EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url));
5252 TEST_F(URLRequestTestHTTP, RestrictFileRedirects) {
5253 ASSERT_TRUE(test_server_.Start());
5256 URLRequest req(test_server_.GetURL("files/redirect-to-file.html"),
5261 base::RunLoop().Run();
5263 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5264 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
5266 #endif // !defined(DISABLE_FILE_SUPPORT)
5268 TEST_F(URLRequestTestHTTP, RestrictDataRedirects) {
5269 ASSERT_TRUE(test_server_.Start());
5272 URLRequest req(test_server_.GetURL("files/redirect-to-data.html"),
5277 base::MessageLoop::current()->Run();
5279 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5280 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error());
5283 TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
5284 ASSERT_TRUE(test_server_.Start());
5287 URLRequest req(test_server_.GetURL("files/redirect-to-invalid-url.html"),
5292 base::RunLoop().Run();
5294 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
5295 EXPECT_EQ(ERR_INVALID_URL, req.status().error());
5298 // Make sure redirects are cached, despite not reading their bodies.
5299 TEST_F(URLRequestTestHTTP, CacheRedirect) {
5300 ASSERT_TRUE(test_server_.Start());
5302 test_server_.GetURL("files/redirect302-to-echo-cacheable");
5306 URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
5308 base::RunLoop().Run();
5309 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5310 EXPECT_EQ(1, d.received_redirect_count());
5311 EXPECT_EQ(test_server_.GetURL("echo"), req.url());
5316 d.set_quit_on_redirect(true);
5317 URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_);
5319 base::RunLoop().Run();
5321 EXPECT_EQ(1, d.received_redirect_count());
5322 EXPECT_EQ(0, d.response_started_count());
5323 EXPECT_TRUE(req.was_cached());
5325 req.FollowDeferredRedirect();
5326 base::RunLoop().Run();
5327 EXPECT_EQ(1, d.received_redirect_count());
5328 EXPECT_EQ(1, d.response_started_count());
5329 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5330 EXPECT_EQ(test_server_.GetURL("echo"), req.url());
5334 // Make sure a request isn't cached when a NetworkDelegate forces a redirect
5335 // when the headers are read, since the body won't have been read.
5336 TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) {
5337 ASSERT_TRUE(test_server_.Start());
5338 // URL that is normally cached.
5339 GURL initial_url = test_server_.GetURL("cachetime");
5342 // Set up the TestNetworkDelegate tp force a redirect.
5343 GURL redirect_to_url = test_server_.GetURL("echo");
5344 default_network_delegate_.set_redirect_on_headers_received_url(
5348 URLRequest req(initial_url, DEFAULT_PRIORITY, &d, &default_context_);
5350 base::RunLoop().Run();
5351 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5352 EXPECT_EQ(1, d.received_redirect_count());
5353 EXPECT_EQ(redirect_to_url, req.url());
5358 URLRequest req(initial_url, DEFAULT_PRIORITY, &d, &default_context_);
5360 base::RunLoop().Run();
5362 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5363 EXPECT_FALSE(req.was_cached());
5364 EXPECT_EQ(0, d.received_redirect_count());
5365 EXPECT_EQ(initial_url, req.url());
5369 // Tests that redirection to an unsafe URL is allowed when it has been marked as
5371 TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) {
5372 ASSERT_TRUE(test_server_.Start());
5374 GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url");
5375 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5376 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5380 URLRequest r(test_server_.GetURL("whatever"),
5386 base::RunLoop().Run();
5388 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5390 EXPECT_EQ(2U, r.url_chain().size());
5391 EXPECT_EQ(OK, r.status().error());
5392 EXPECT_EQ(unsafe_url, r.url());
5393 EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received());
5397 // Tests that a redirect to a different unsafe URL is blocked, even after adding
5398 // some other URL to the whitelist.
5399 TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) {
5400 ASSERT_TRUE(test_server_.Start());
5402 GURL unsafe_url("data:text/html,something");
5403 GURL different_unsafe_url("data:text/html,something-else");
5404 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5405 default_network_delegate_.set_allowed_unsafe_redirect_url(
5406 different_unsafe_url);
5410 URLRequest r(test_server_.GetURL("whatever"),
5416 base::RunLoop().Run();
5418 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
5419 EXPECT_EQ(ERR_UNSAFE_REDIRECT, r.status().error());
5423 // Redirects from an URL with fragment to an unsafe URL with fragment should
5424 // be allowed, and the reference fragment of the target URL should be preserved.
5425 TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) {
5426 ASSERT_TRUE(test_server_.Start());
5428 GURL original_url(test_server_.GetURL("original#fragment1"));
5429 GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5430 GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5432 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5433 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5437 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
5440 base::RunLoop().Run();
5442 EXPECT_EQ(2U, r.url_chain().size());
5443 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5444 EXPECT_EQ(OK, r.status().error());
5445 EXPECT_EQ(original_url, r.original_url());
5446 EXPECT_EQ(expected_url, r.url());
5450 // When a delegate has specified a safe redirect URL, but it does not match the
5451 // redirect target, then do not prevent the reference fragment from being added.
5452 TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) {
5453 ASSERT_TRUE(test_server_.Start());
5455 GURL original_url(test_server_.GetURL("original#expected-fragment"));
5456 GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url");
5457 GURL redirect_url(test_server_.GetURL("target"));
5458 GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment"));
5460 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5461 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5465 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
5468 base::RunLoop().Run();
5470 EXPECT_EQ(2U, r.url_chain().size());
5471 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5472 EXPECT_EQ(OK, r.status().error());
5473 EXPECT_EQ(original_url, r.original_url());
5474 EXPECT_EQ(expected_redirect_url, r.url());
5478 // When a delegate has specified a safe redirect URL, assume that the redirect
5479 // URL should not be changed. In particular, the reference fragment should not
5481 TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) {
5482 ASSERT_TRUE(test_server_.Start());
5484 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5485 GURL redirect_url("data:text/html,expect-no-reference-fragment");
5487 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5488 default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url);
5492 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
5495 base::RunLoop().Run();
5497 EXPECT_EQ(2U, r.url_chain().size());
5498 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5499 EXPECT_EQ(OK, r.status().error());
5500 EXPECT_EQ(original_url, r.original_url());
5501 EXPECT_EQ(redirect_url, r.url());
5505 // When a URLRequestRedirectJob is created, the redirection must be followed and
5506 // the reference fragment of the target URL must not be modified.
5507 TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) {
5508 ASSERT_TRUE(test_server_.Start());
5510 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5511 GURL redirect_url(test_server_.GetURL("echo"));
5514 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
5516 URLRequestRedirectJob* job = new URLRequestRedirectJob(
5517 &r, &default_network_delegate_, redirect_url,
5518 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
5519 AddTestInterceptor()->set_main_intercept_job(job);
5522 base::RunLoop().Run();
5524 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
5525 EXPECT_EQ(OK, r.status().error());
5526 EXPECT_EQ(original_url, r.original_url());
5527 EXPECT_EQ(redirect_url, r.url());
5530 TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
5531 ASSERT_TRUE(test_server_.Start());
5534 URLRequest req(test_server_.GetURL("echoheader?Referer"),
5538 req.SetReferrer("http://user:pass@foo.com/");
5540 base::RunLoop().Run();
5542 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
5545 TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
5546 ASSERT_TRUE(test_server_.Start());
5549 URLRequest req(test_server_.GetURL("echoheader?Referer"),
5553 req.SetReferrer("http://foo.com/test#fragment");
5555 base::RunLoop().Run();
5557 EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
5560 TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
5561 ASSERT_TRUE(test_server_.Start());
5564 URLRequest req(test_server_.GetURL("echoheader?Referer"),
5568 req.SetReferrer("http://foo.com/test#fragment");
5569 req.SetReferrer("");
5571 base::RunLoop().Run();
5573 EXPECT_EQ(std::string("None"), d.data_received());
5576 // Defer network start and then resume, checking that the request was a success
5577 // and bytes were received.
5578 TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5579 ASSERT_TRUE(test_server_.Start());
5583 d.set_quit_on_network_start(true);
5584 GURL test_url(test_server_.GetURL("echo"));
5585 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5588 base::RunLoop().Run();
5590 EXPECT_EQ(1, d.received_before_network_start_count());
5591 EXPECT_EQ(0, d.response_started_count());
5593 req.ResumeNetworkStart();
5594 base::RunLoop().Run();
5596 EXPECT_EQ(1, d.response_started_count());
5597 EXPECT_NE(0, d.bytes_received());
5598 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5602 // Check that OnBeforeNetworkStart is only called once even if there is a
5604 TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5605 ASSERT_TRUE(test_server_.Start());
5609 d.set_quit_on_redirect(true);
5610 d.set_quit_on_network_start(true);
5611 URLRequest req(test_server_.GetURL("server-redirect?echo"),
5617 base::RunLoop().Run();
5619 EXPECT_EQ(1, d.received_before_network_start_count());
5620 EXPECT_EQ(0, d.response_started_count());
5621 EXPECT_EQ(0, d.received_redirect_count());
5623 req.ResumeNetworkStart();
5624 base::RunLoop().Run();
5626 EXPECT_EQ(1, d.received_redirect_count());
5627 req.FollowDeferredRedirect();
5628 base::RunLoop().Run();
5630 // Check that the redirect's new network transaction does not get propagated
5631 // to a second OnBeforeNetworkStart() notification.
5632 EXPECT_EQ(1, d.received_before_network_start_count());
5634 EXPECT_EQ(1, d.response_started_count());
5635 EXPECT_NE(0, d.bytes_received());
5636 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5640 // Cancel the request after learning that the request would use the network.
5641 TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5642 ASSERT_TRUE(test_server_.Start());
5646 d.set_quit_on_network_start(true);
5647 GURL test_url(test_server_.GetURL("echo"));
5648 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5651 base::RunLoop().Run();
5653 EXPECT_EQ(1, d.received_before_network_start_count());
5654 EXPECT_EQ(0, d.response_started_count());
5657 base::RunLoop().Run();
5659 EXPECT_EQ(1, d.response_started_count());
5660 EXPECT_EQ(0, d.bytes_received());
5661 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5665 TEST_F(URLRequestTestHTTP, CancelRedirect) {
5666 ASSERT_TRUE(test_server_.Start());
5670 d.set_cancel_in_received_redirect(true);
5671 URLRequest req(test_server_.GetURL("files/redirect-test.html"),
5676 base::RunLoop().Run();
5678 EXPECT_EQ(1, d.response_started_count());
5679 EXPECT_EQ(0, d.bytes_received());
5680 EXPECT_FALSE(d.received_data_before_response());
5681 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5685 TEST_F(URLRequestTestHTTP, DeferredRedirect) {
5686 ASSERT_TRUE(test_server_.Start());
5690 d.set_quit_on_redirect(true);
5691 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
5692 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5695 base::RunLoop().Run();
5697 EXPECT_EQ(1, d.received_redirect_count());
5699 req.FollowDeferredRedirect();
5700 base::RunLoop().Run();
5702 EXPECT_EQ(1, d.response_started_count());
5703 EXPECT_FALSE(d.received_data_before_response());
5704 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5706 base::FilePath path;
5707 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5708 path = path.Append(FILE_PATH_LITERAL("net"));
5709 path = path.Append(FILE_PATH_LITERAL("data"));
5710 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5711 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5713 std::string contents;
5714 EXPECT_TRUE(base::ReadFileToString(path, &contents));
5715 EXPECT_EQ(contents, d.data_received());
5719 TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) {
5720 ASSERT_TRUE(test_server_.Start());
5724 d.set_quit_on_redirect(true);
5725 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
5726 URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_);
5728 EXPECT_FALSE(d.have_full_request_headers());
5731 base::RunLoop().Run();
5733 EXPECT_EQ(1, d.received_redirect_count());
5734 EXPECT_TRUE(d.have_full_request_headers());
5735 CheckFullRequestHeaders(d.full_request_headers(), test_url);
5736 d.ClearFullRequestHeaders();
5738 req.FollowDeferredRedirect();
5739 base::RunLoop().Run();
5741 GURL target_url(test_server_.GetURL("files/with-headers.html"));
5742 EXPECT_EQ(1, d.response_started_count());
5743 EXPECT_TRUE(d.have_full_request_headers());
5744 CheckFullRequestHeaders(d.full_request_headers(), target_url);
5745 EXPECT_FALSE(d.received_data_before_response());
5746 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status());
5748 base::FilePath path;
5749 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5750 path = path.Append(FILE_PATH_LITERAL("net"));
5751 path = path.Append(FILE_PATH_LITERAL("data"));
5752 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5753 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5755 std::string contents;
5756 EXPECT_TRUE(base::ReadFileToString(path, &contents));
5757 EXPECT_EQ(contents, d.data_received());
5761 TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
5762 ASSERT_TRUE(test_server_.Start());
5766 d.set_quit_on_redirect(true);
5767 URLRequest req(test_server_.GetURL("files/redirect-test.html"),
5772 base::RunLoop().Run();
5774 EXPECT_EQ(1, d.received_redirect_count());
5777 base::RunLoop().Run();
5779 EXPECT_EQ(1, d.response_started_count());
5780 EXPECT_EQ(0, d.bytes_received());
5781 EXPECT_FALSE(d.received_data_before_response());
5782 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
5786 TEST_F(URLRequestTestHTTP, VaryHeader) {
5787 ASSERT_TRUE(test_server_.Start());
5789 // Populate the cache.
5792 URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5796 HttpRequestHeaders headers;
5797 headers.SetHeader("foo", "1");
5798 req.SetExtraRequestHeaders(headers);
5800 base::RunLoop().Run();
5802 LoadTimingInfo load_timing_info;
5803 req.GetLoadTimingInfo(&load_timing_info);
5804 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5807 // Expect a cache hit.
5810 URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5814 HttpRequestHeaders headers;
5815 headers.SetHeader("foo", "1");
5816 req.SetExtraRequestHeaders(headers);
5818 base::RunLoop().Run();
5820 EXPECT_TRUE(req.was_cached());
5822 LoadTimingInfo load_timing_info;
5823 req.GetLoadTimingInfo(&load_timing_info);
5824 TestLoadTimingCacheHitNoNetwork(load_timing_info);
5827 // Expect a cache miss.
5830 URLRequest req(test_server_.GetURL("echoheadercache?foo"),
5834 HttpRequestHeaders headers;
5835 headers.SetHeader("foo", "2");
5836 req.SetExtraRequestHeaders(headers);
5838 base::RunLoop().Run();
5840 EXPECT_FALSE(req.was_cached());
5842 LoadTimingInfo load_timing_info;
5843 req.GetLoadTimingInfo(&load_timing_info);
5844 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5848 TEST_F(URLRequestTestHTTP, BasicAuth) {
5849 ASSERT_TRUE(test_server_.Start());
5851 // populate the cache
5854 d.set_credentials(AuthCredentials(kUser, kSecret));
5856 URLRequest r(test_server_.GetURL("auth-basic"),
5862 base::RunLoop().Run();
5864 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5867 // repeat request with end-to-end validation. since auth-basic results in a
5868 // cachable page, we expect this test to result in a 304. in which case, the
5869 // response should be fetched from the cache.
5872 d.set_credentials(AuthCredentials(kUser, kSecret));
5874 URLRequest r(test_server_.GetURL("auth-basic"),
5878 r.SetLoadFlags(LOAD_VALIDATE_CACHE);
5881 base::RunLoop().Run();
5883 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5885 // Should be the same cached document.
5886 EXPECT_TRUE(r.was_cached());
5890 // Check that Set-Cookie headers in 401 responses are respected.
5891 // http://crbug.com/6450
5892 TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
5893 ASSERT_TRUE(test_server_.Start());
5895 GURL url_requiring_auth =
5896 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
5898 // Request a page that will give a 401 containing a Set-Cookie header.
5899 // Verify that when the transaction is restarted, it includes the new cookie.
5901 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
5902 TestURLRequestContext context(true);
5903 context.set_network_delegate(&network_delegate);
5907 d.set_credentials(AuthCredentials(kUser, kSecret));
5909 URLRequest r(url_requiring_auth, DEFAULT_PRIORITY, &d, &context);
5912 base::RunLoop().Run();
5914 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5916 // Make sure we sent the cookie in the restarted transaction.
5917 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5918 != std::string::npos);
5921 // Same test as above, except this time the restart is initiated earlier
5922 // (without user intervention since identity is embedded in the URL).
5924 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
5925 TestURLRequestContext context(true);
5926 context.set_network_delegate(&network_delegate);
5931 GURL::Replacements replacements;
5932 std::string username("user2");
5933 std::string password("secret");
5934 replacements.SetUsernameStr(username);
5935 replacements.SetPasswordStr(password);
5936 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
5938 URLRequest r(url_with_identity, DEFAULT_PRIORITY, &d, &context);
5941 base::RunLoop().Run();
5943 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
5945 // Make sure we sent the cookie in the restarted transaction.
5946 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
5947 != std::string::npos);
5951 // Tests that load timing works as expected with auth and the cache.
5952 TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
5953 ASSERT_TRUE(test_server_.Start());
5955 // populate the cache
5958 d.set_credentials(AuthCredentials(kUser, kSecret));
5960 URLRequest r(test_server_.GetURL("auth-basic"),
5966 base::RunLoop().Run();
5968 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
5970 LoadTimingInfo load_timing_info_before_auth;
5971 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
5972 &load_timing_info_before_auth));
5973 TestLoadTimingNotReused(load_timing_info_before_auth,
5974 CONNECT_TIMING_HAS_DNS_TIMES);
5976 LoadTimingInfo load_timing_info;
5977 r.GetLoadTimingInfo(&load_timing_info);
5978 // The test server does not support keep alive sockets, so the second
5979 // request with auth should use a new socket.
5980 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
5981 EXPECT_NE(load_timing_info_before_auth.socket_log_id,
5982 load_timing_info.socket_log_id);
5983 EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
5984 load_timing_info.connect_timing.connect_start);
5987 // Repeat request with end-to-end validation. Since auth-basic results in a
5988 // cachable page, we expect this test to result in a 304. In which case, the
5989 // response should be fetched from the cache.
5992 d.set_credentials(AuthCredentials(kUser, kSecret));
5994 URLRequest r(test_server_.GetURL("auth-basic"),
5998 r.SetLoadFlags(LOAD_VALIDATE_CACHE);
6001 base::RunLoop().Run();
6003 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6005 // Should be the same cached document.
6006 EXPECT_TRUE(r.was_cached());
6008 // Since there was a request that went over the wire, the load timing
6009 // information should include connection times.
6010 LoadTimingInfo load_timing_info;
6011 r.GetLoadTimingInfo(&load_timing_info);
6012 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
6016 // In this test, we do a POST which the server will 302 redirect.
6017 // The subsequent transaction should use GET, and should not send the
6018 // Content-Type header.
6019 // http://code.google.com/p/chromium/issues/detail?id=843
6020 TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
6021 ASSERT_TRUE(test_server_.Start());
6023 const char kData[] = "hello world";
6026 URLRequest req(test_server_.GetURL("files/redirect-to-echoall"),
6030 req.set_method("POST");
6031 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
6033 // Set headers (some of which are specific to the POST).
6034 HttpRequestHeaders headers;
6035 headers.AddHeadersFromString(
6036 "Content-Type: multipart/form-data; "
6037 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
6038 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
6039 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
6040 "Accept-Language: en-US,en\r\n"
6041 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
6042 "Content-Length: 11\r\n"
6043 "Origin: http://localhost:1337/");
6044 req.SetExtraRequestHeaders(headers);
6046 base::RunLoop().Run();
6048 std::string mime_type;
6049 req.GetMimeType(&mime_type);
6050 EXPECT_EQ("text/html", mime_type);
6052 const std::string& data = d.data_received();
6054 // Check that the post-specific headers were stripped:
6055 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
6056 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
6057 EXPECT_FALSE(ContainsString(data, "Origin:"));
6059 // These extra request headers should not have been stripped.
6060 EXPECT_TRUE(ContainsString(data, "Accept:"));
6061 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
6062 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
6065 // The following tests check that we handle mutating the request method for
6066 // HTTP redirects as expected.
6067 // See http://crbug.com/56373 and http://crbug.com/102130.
6069 TEST_F(URLRequestTestHTTP, Redirect301Tests) {
6070 ASSERT_TRUE(test_server_.Start());
6072 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
6074 HTTPRedirectMethodTest(url, "POST", "GET", true);
6075 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6076 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6079 TEST_F(URLRequestTestHTTP, Redirect302Tests) {
6080 ASSERT_TRUE(test_server_.Start());
6082 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
6084 HTTPRedirectMethodTest(url, "POST", "GET", true);
6085 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6086 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6089 TEST_F(URLRequestTestHTTP, Redirect303Tests) {
6090 ASSERT_TRUE(test_server_.Start());
6092 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
6094 HTTPRedirectMethodTest(url, "POST", "GET", true);
6095 HTTPRedirectMethodTest(url, "PUT", "GET", true);
6096 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6099 TEST_F(URLRequestTestHTTP, Redirect307Tests) {
6100 ASSERT_TRUE(test_server_.Start());
6102 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
6104 HTTPRedirectMethodTest(url, "POST", "POST", true);
6105 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6106 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6109 TEST_F(URLRequestTestHTTP, Redirect308Tests) {
6110 ASSERT_TRUE(test_server_.Start());
6112 const GURL url = test_server_.GetURL("files/redirect308-to-echo");
6114 HTTPRedirectMethodTest(url, "POST", "POST", true);
6115 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6116 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6119 // Make sure that 308 responses without bodies are not treated as redirects.
6120 // Certain legacy apis that pre-date the response code expect this behavior
6121 // (Like Google Drive).
6122 TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) {
6123 ASSERT_TRUE(test_server_.Start());
6126 const GURL url = test_server_.GetURL("files/308-without-location-header");
6128 URLRequest request(url, DEFAULT_PRIORITY, &d, &default_context_);
6131 base::RunLoop().Run();
6132 EXPECT_EQ(URLRequestStatus::SUCCESS, request.status().status());
6133 EXPECT_EQ(OK, request.status().error());
6134 EXPECT_EQ(0, d.received_redirect_count());
6135 EXPECT_EQ(308, request.response_headers()->response_code());
6136 EXPECT_EQ("This is not a redirect.", d.data_received());
6139 TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) {
6140 ASSERT_TRUE(test_server_.Start());
6142 GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment"));
6143 GURL expected_url(test_server_.GetURL("echo#fragment"));
6147 URLRequest r(original_url, DEFAULT_PRIORITY, &d, &default_context_);
6150 base::RunLoop().Run();
6152 EXPECT_EQ(2U, r.url_chain().size());
6153 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
6154 EXPECT_EQ(OK, r.status().error());
6155 EXPECT_EQ(original_url, r.original_url());
6156 EXPECT_EQ(expected_url, r.url());
6160 TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) {
6161 ASSERT_TRUE(test_server_.Start());
6163 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6164 GURL first_party_url("http://example.com");
6168 URLRequest r(url, DEFAULT_PRIORITY, &d, &default_context_);
6169 r.set_first_party_for_cookies(first_party_url);
6172 base::RunLoop().Run();
6174 EXPECT_EQ(2U, r.url_chain().size());
6175 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
6176 EXPECT_EQ(OK, r.status().error());
6177 EXPECT_EQ(first_party_url, r.first_party_for_cookies());
6181 TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) {
6182 ASSERT_TRUE(test_server_.Start());
6184 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6185 GURL original_first_party_url("http://example.com");
6186 GURL expected_first_party_url(test_server_.GetURL("echo"));
6190 URLRequest r(url, DEFAULT_PRIORITY, &d, &default_context_);
6191 r.set_first_party_for_cookies(original_first_party_url);
6192 r.set_first_party_url_policy(
6193 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
6196 base::RunLoop().Run();
6198 EXPECT_EQ(2U, r.url_chain().size());
6199 EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status());
6200 EXPECT_EQ(OK, r.status().error());
6201 EXPECT_EQ(expected_first_party_url, r.first_party_for_cookies());
6205 TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
6206 ASSERT_TRUE(test_server_.Start());
6208 const char kData[] = "hello world";
6211 URLRequest req(test_server_.GetURL("empty.html"),
6215 req.set_method("POST");
6216 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
6217 HttpRequestHeaders headers;
6218 headers.SetHeader(HttpRequestHeaders::kContentLength,
6219 base::UintToString(arraysize(kData) - 1));
6220 req.SetExtraRequestHeaders(headers);
6222 URLRequestRedirectJob* job = new URLRequestRedirectJob(
6223 &req, &default_network_delegate_, test_server_.GetURL("echo"),
6224 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
6225 AddTestInterceptor()->set_main_intercept_job(job);
6228 base::RunLoop().Run();
6229 EXPECT_EQ("GET", req.method());
6232 TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
6233 ASSERT_TRUE(test_server_.Start());
6235 const char kData[] = "hello world";
6238 URLRequest req(test_server_.GetURL("empty.html"),
6242 req.set_method("POST");
6243 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
6244 HttpRequestHeaders headers;
6245 headers.SetHeader(HttpRequestHeaders::kContentLength,
6246 base::UintToString(arraysize(kData) - 1));
6247 req.SetExtraRequestHeaders(headers);
6249 URLRequestRedirectJob* job = new URLRequestRedirectJob(
6250 &req, &default_network_delegate_, test_server_.GetURL("echo"),
6251 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT,
6252 "Very Good Reason");
6253 AddTestInterceptor()->set_main_intercept_job(job);
6256 base::RunLoop().Run();
6257 EXPECT_EQ("POST", req.method());
6258 EXPECT_EQ(kData, d.data_received());
6261 // Check that default A-L header is sent.
6262 TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
6263 ASSERT_TRUE(test_server_.Start());
6265 StaticHttpUserAgentSettings settings("en", std::string());
6266 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
6267 TestURLRequestContext context(true);
6268 context.set_network_delegate(&network_delegate);
6269 context.set_http_user_agent_settings(&settings);
6273 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
6278 base::RunLoop().Run();
6279 EXPECT_EQ("en", d.data_received());
6282 // Check that an empty A-L header is not sent. http://crbug.com/77365.
6283 TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
6284 ASSERT_TRUE(test_server_.Start());
6286 std::string empty_string; // Avoid most vexing parse on line below.
6287 StaticHttpUserAgentSettings settings(empty_string, empty_string);
6288 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
6289 TestURLRequestContext context(true);
6290 context.set_network_delegate(&network_delegate);
6292 // We override the language after initialization because empty entries
6293 // get overridden by Init().
6294 context.set_http_user_agent_settings(&settings);
6297 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
6302 base::RunLoop().Run();
6303 EXPECT_EQ("None", d.data_received());
6306 // Check that if request overrides the A-L header, the default is not appended.
6307 // See http://crbug.com/20894
6308 TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
6309 ASSERT_TRUE(test_server_.Start());
6312 URLRequest req(test_server_.GetURL("echoheader?Accept-Language"),
6316 HttpRequestHeaders headers;
6317 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
6318 req.SetExtraRequestHeaders(headers);
6320 base::RunLoop().Run();
6321 EXPECT_EQ(std::string("ru"), d.data_received());
6324 // Check that default A-E header is sent.
6325 TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
6326 ASSERT_TRUE(test_server_.Start());
6329 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
6333 HttpRequestHeaders headers;
6334 req.SetExtraRequestHeaders(headers);
6336 base::RunLoop().Run();
6337 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
6340 // Check that if request overrides the A-E header, the default is not appended.
6341 // See http://crbug.com/47381
6342 TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
6343 ASSERT_TRUE(test_server_.Start());
6346 URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"),
6350 HttpRequestHeaders headers;
6351 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
6352 req.SetExtraRequestHeaders(headers);
6354 base::RunLoop().Run();
6355 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
6356 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
6359 // Check that setting the A-C header sends the proper header.
6360 TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
6361 ASSERT_TRUE(test_server_.Start());
6364 URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"),
6368 HttpRequestHeaders headers;
6369 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
6370 req.SetExtraRequestHeaders(headers);
6372 base::RunLoop().Run();
6373 EXPECT_EQ(std::string("koi-8r"), d.data_received());
6376 // Check that default User-Agent header is sent.
6377 TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
6378 ASSERT_TRUE(test_server_.Start());
6381 URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
6386 base::RunLoop().Run();
6387 EXPECT_EQ(req.context()->http_user_agent_settings()->GetUserAgent(),
6391 // Check that if request overrides the User-Agent header,
6392 // the default is not appended.
6393 TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
6394 ASSERT_TRUE(test_server_.Start());
6397 URLRequest req(test_server_.GetURL("echoheader?User-Agent"),
6401 HttpRequestHeaders headers;
6402 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
6403 req.SetExtraRequestHeaders(headers);
6405 base::RunLoop().Run();
6406 EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
6409 // Check that a NULL HttpUserAgentSettings causes the corresponding empty
6410 // User-Agent header to be sent but does not send the Accept-Language and
6411 // Accept-Charset headers.
6412 TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
6413 ASSERT_TRUE(test_server_.Start());
6415 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
6416 TestURLRequestContext context(true);
6417 context.set_network_delegate(&network_delegate);
6419 // We override the HttpUserAgentSettings after initialization because empty
6420 // entries get overridden by Init().
6421 context.set_http_user_agent_settings(NULL);
6424 const char* request;
6425 const char* expected_response;
6426 } tests[] = { { "echoheader?Accept-Language", "None" },
6427 { "echoheader?Accept-Charset", "None" },
6428 { "echoheader?User-Agent", "" } };
6430 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) {
6433 test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, &context);
6435 base::RunLoop().Run();
6436 EXPECT_EQ(tests[i].expected_response, d.data_received())
6437 << " Request = \"" << tests[i].request << "\"";
6441 // Make sure that URLRequest passes on its priority updates to
6442 // newly-created jobs after the first one.
6443 TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
6444 ASSERT_TRUE(test_server_.Start());
6447 URLRequest req(test_server_.GetURL("empty.html"),
6451 EXPECT_EQ(DEFAULT_PRIORITY, req.priority());
6453 scoped_refptr<URLRequestRedirectJob> redirect_job =
6454 new URLRequestRedirectJob(
6455 &req, &default_network_delegate_, test_server_.GetURL("echo"),
6456 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
6457 AddTestInterceptor()->set_main_intercept_job(redirect_job.get());
6459 req.SetPriority(LOW);
6461 EXPECT_TRUE(req.is_pending());
6463 scoped_refptr<URLRequestTestJob> job =
6464 new URLRequestTestJob(&req, &default_network_delegate_);
6465 AddTestInterceptor()->set_main_intercept_job(job.get());
6467 // Should trigger |job| to be started.
6468 base::RunLoop().Run();
6469 EXPECT_EQ(LOW, job->priority());
6472 // Check that creating a network request while entering/exiting suspend mode
6473 // fails as it should. This is the only case where an HttpTransactionFactory
6474 // does not return an HttpTransaction.
6475 TEST_F(URLRequestTestHTTP, NetworkSuspendTest) {
6476 // Create a new HttpNetworkLayer that thinks it's suspended.
6477 HttpNetworkSession::Params params;
6478 params.host_resolver = default_context_.host_resolver();
6479 params.cert_verifier = default_context_.cert_verifier();
6480 params.transport_security_state = default_context_.transport_security_state();
6481 params.proxy_service = default_context_.proxy_service();
6482 params.ssl_config_service = default_context_.ssl_config_service();
6483 params.http_auth_handler_factory =
6484 default_context_.http_auth_handler_factory();
6485 params.network_delegate = &default_network_delegate_;
6486 params.http_server_properties = default_context_.http_server_properties();
6487 scoped_ptr<HttpNetworkLayer> network_layer(
6488 new HttpNetworkLayer(new HttpNetworkSession(params)));
6489 network_layer->OnSuspend();
6491 HttpCache http_cache(network_layer.release(), default_context_.net_log(),
6492 HttpCache::DefaultBackend::InMemory(0));
6494 TestURLRequestContext context(true);
6495 context.set_http_transaction_factory(&http_cache);
6499 URLRequest req(GURL("http://127.0.0.1/"),
6504 base::RunLoop().Run();
6506 EXPECT_TRUE(d.request_failed());
6507 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
6508 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req.status().error());
6511 // Check that creating a network request while entering/exiting suspend mode
6512 // fails as it should in the case there is no cache. This is the only case
6513 // where an HttpTransactionFactory does not return an HttpTransaction.
6514 TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) {
6515 // Create a new HttpNetworkLayer that thinks it's suspended.
6516 HttpNetworkSession::Params params;
6517 params.host_resolver = default_context_.host_resolver();
6518 params.cert_verifier = default_context_.cert_verifier();
6519 params.transport_security_state = default_context_.transport_security_state();
6520 params.proxy_service = default_context_.proxy_service();
6521 params.ssl_config_service = default_context_.ssl_config_service();
6522 params.http_auth_handler_factory =
6523 default_context_.http_auth_handler_factory();
6524 params.network_delegate = &default_network_delegate_;
6525 params.http_server_properties = default_context_.http_server_properties();
6526 HttpNetworkLayer network_layer(new HttpNetworkSession(params));
6527 network_layer.OnSuspend();
6529 TestURLRequestContext context(true);
6530 context.set_http_transaction_factory(&network_layer);
6534 URLRequest req(GURL("http://127.0.0.1/"),
6539 base::RunLoop().Run();
6541 EXPECT_TRUE(d.request_failed());
6542 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status());
6543 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req.status().error());
6546 class HTTPSRequestTest : public testing::Test {
6548 HTTPSRequestTest() : default_context_(true) {
6549 default_context_.set_network_delegate(&default_network_delegate_);
6550 default_context_.Init();
6552 virtual ~HTTPSRequestTest() {}
6555 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
6556 TestURLRequestContext default_context_;
6559 TEST_F(HTTPSRequestTest, HTTPSGetTest) {
6560 SpawnedTestServer test_server(
6561 SpawnedTestServer::TYPE_HTTPS,
6562 SpawnedTestServer::kLocalhost,
6563 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6564 ASSERT_TRUE(test_server.Start());
6568 URLRequest r(test_server.GetURL(std::string()),
6573 EXPECT_TRUE(r.is_pending());
6575 base::RunLoop().Run();
6577 EXPECT_EQ(1, d.response_started_count());
6578 EXPECT_FALSE(d.received_data_before_response());
6579 EXPECT_NE(0, d.bytes_received());
6580 CheckSSLInfo(r.ssl_info());
6581 EXPECT_EQ(test_server.host_port_pair().host(),
6582 r.GetSocketAddress().host());
6583 EXPECT_EQ(test_server.host_port_pair().port(),
6584 r.GetSocketAddress().port());
6588 TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
6589 SpawnedTestServer::SSLOptions ssl_options(
6590 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6591 SpawnedTestServer test_server(
6592 SpawnedTestServer::TYPE_HTTPS,
6594 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6595 ASSERT_TRUE(test_server.Start());
6597 bool err_allowed = true;
6598 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6601 d.set_allow_certificate_errors(err_allowed);
6602 URLRequest r(test_server.GetURL(std::string()),
6608 EXPECT_TRUE(r.is_pending());
6610 base::RunLoop().Run();
6612 EXPECT_EQ(1, d.response_started_count());
6613 EXPECT_FALSE(d.received_data_before_response());
6614 EXPECT_TRUE(d.have_certificate_errors());
6616 EXPECT_NE(0, d.bytes_received());
6617 CheckSSLInfo(r.ssl_info());
6619 EXPECT_EQ(0, d.bytes_received());
6625 TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
6626 SpawnedTestServer::SSLOptions ssl_options(
6627 SpawnedTestServer::SSLOptions::CERT_EXPIRED);
6628 SpawnedTestServer test_server(
6629 SpawnedTestServer::TYPE_HTTPS,
6631 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6632 ASSERT_TRUE(test_server.Start());
6634 // Iterate from false to true, just so that we do the opposite of the
6635 // previous test in order to increase test coverage.
6636 bool err_allowed = false;
6637 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
6640 d.set_allow_certificate_errors(err_allowed);
6641 URLRequest r(test_server.GetURL(std::string()),
6647 EXPECT_TRUE(r.is_pending());
6649 base::RunLoop().Run();
6651 EXPECT_EQ(1, d.response_started_count());
6652 EXPECT_FALSE(d.received_data_before_response());
6653 EXPECT_TRUE(d.have_certificate_errors());
6655 EXPECT_NE(0, d.bytes_received());
6656 CheckSSLInfo(r.ssl_info());
6658 EXPECT_EQ(0, d.bytes_received());
6664 // This tests that a load of www.google.com with a certificate error sets
6665 // the |certificate_errors_are_fatal| flag correctly. This flag will cause
6666 // the interstitial to be fatal.
6667 TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
6668 SpawnedTestServer::SSLOptions ssl_options(
6669 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6670 SpawnedTestServer test_server(
6671 SpawnedTestServer::TYPE_HTTPS,
6673 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6674 ASSERT_TRUE(test_server.Start());
6676 // We require that the URL be www.google.com in order to pick up the
6677 // preloaded HSTS entries in the TransportSecurityState. This means that we
6678 // have to use a MockHostResolver in order to direct www.google.com to the
6679 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
6681 MockHostResolver host_resolver;
6682 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6683 TestURLRequestContext context(true);
6684 context.set_network_delegate(&network_delegate);
6685 context.set_host_resolver(&host_resolver);
6686 TransportSecurityState transport_security_state;
6687 context.set_transport_security_state(&transport_security_state);
6691 URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
6692 test_server.host_port_pair().port())),
6698 EXPECT_TRUE(r.is_pending());
6700 base::RunLoop().Run();
6702 EXPECT_EQ(1, d.response_started_count());
6703 EXPECT_FALSE(d.received_data_before_response());
6704 EXPECT_TRUE(d.have_certificate_errors());
6705 EXPECT_TRUE(d.certificate_errors_are_fatal());
6708 // This tests that cached HTTPS page loads do not cause any updates to the
6709 // TransportSecurityState.
6710 TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
6711 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
6712 // matter. It just has to be any error.
6713 SpawnedTestServer::SSLOptions ssl_options(
6714 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
6715 SpawnedTestServer test_server(
6716 SpawnedTestServer::TYPE_HTTPS,
6718 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6719 ASSERT_TRUE(test_server.Start());
6721 // We require that the URL be www.google.com in order to pick up the static
6722 // and dynamic STS and PKP entries in the TransportSecurityState. This means
6723 // that we have to use a MockHostResolver in order to direct www.google.com to
6724 // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
6726 MockHostResolver host_resolver;
6727 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6728 TestURLRequestContext context(true);
6729 context.set_network_delegate(&network_delegate);
6730 context.set_host_resolver(&host_resolver);
6731 TransportSecurityState transport_security_state;
6733 TransportSecurityState::DomainState static_domain_state;
6734 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
6735 "www.google.com", true, &static_domain_state));
6736 context.set_transport_security_state(&transport_security_state);
6739 TransportSecurityState::DomainState dynamic_domain_state;
6740 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
6741 "www.google.com", &dynamic_domain_state));
6744 URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d",
6745 test_server.host_port_pair().port())),
6751 EXPECT_TRUE(r.is_pending());
6753 base::RunLoop().Run();
6755 EXPECT_EQ(1, d.response_started_count());
6756 EXPECT_FALSE(d.received_data_before_response());
6757 EXPECT_TRUE(d.have_certificate_errors());
6758 EXPECT_TRUE(d.certificate_errors_are_fatal());
6760 // Get a fresh copy of the states, and check that they haven't changed.
6761 TransportSecurityState::DomainState new_static_domain_state;
6762 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
6763 "www.google.com", true, &new_static_domain_state));
6764 TransportSecurityState::DomainState new_dynamic_domain_state;
6765 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
6766 "www.google.com", &new_dynamic_domain_state));
6768 EXPECT_EQ(new_static_domain_state.sts.upgrade_mode,
6769 static_domain_state.sts.upgrade_mode);
6770 EXPECT_EQ(new_static_domain_state.sts.include_subdomains,
6771 static_domain_state.sts.include_subdomains);
6772 EXPECT_EQ(new_static_domain_state.pkp.include_subdomains,
6773 static_domain_state.pkp.include_subdomains);
6774 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes,
6775 static_domain_state.pkp.spki_hashes));
6776 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes,
6777 static_domain_state.pkp.bad_spki_hashes));
6780 // Make sure HSTS preserves a POST request's method and body.
6781 TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
6782 static const char kData[] = "hello world";
6784 SpawnedTestServer::SSLOptions ssl_options(
6785 SpawnedTestServer::SSLOptions::CERT_OK);
6786 SpawnedTestServer test_server(
6787 SpawnedTestServer::TYPE_HTTPS,
6789 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6790 ASSERT_TRUE(test_server.Start());
6793 // Per spec, TransportSecurityState expects a domain name, rather than an IP
6794 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
6795 // the SpawnedTestServer. By default, MockHostResolver maps all hosts
6797 MockHostResolver host_resolver;
6799 // Force https for www.somewhere.com.
6800 TransportSecurityState transport_security_state;
6801 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
6802 bool include_subdomains = false;
6803 transport_security_state.AddHSTS("www.somewhere.com", expiry,
6804 include_subdomains);
6806 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6808 TestURLRequestContext context(true);
6809 context.set_host_resolver(&host_resolver);
6810 context.set_transport_security_state(&transport_security_state);
6811 context.set_network_delegate(&network_delegate);
6815 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
6816 // cause a certificate error. Ignore the error.
6817 d.set_allow_certificate_errors(true);
6819 URLRequest req(GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
6820 test_server.host_port_pair().port())),
6824 req.set_method("POST");
6825 req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData)));
6828 base::RunLoop().Run();
6830 EXPECT_EQ("https", req.url().scheme());
6831 EXPECT_EQ("POST", req.method());
6832 EXPECT_EQ(kData, d.data_received());
6834 LoadTimingInfo load_timing_info;
6835 network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info);
6836 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
6837 TestLoadTimingCacheHitNoNetwork(load_timing_info);
6842 class SSLClientAuthTestDelegate : public TestDelegate {
6844 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
6846 virtual void OnCertificateRequested(
6847 URLRequest* request,
6848 SSLCertRequestInfo* cert_request_info) OVERRIDE {
6849 on_certificate_requested_count_++;
6850 base::MessageLoop::current()->Quit();
6852 int on_certificate_requested_count() {
6853 return on_certificate_requested_count_;
6856 int on_certificate_requested_count_;
6861 // TODO(davidben): Test the rest of the code. Specifically,
6862 // - Filtering which certificates to select.
6863 // - Sending a certificate back.
6864 // - Getting a certificate request in an SSL renegotiation sending the
6866 TEST_F(HTTPSRequestTest, ClientAuthTest) {
6867 SpawnedTestServer::SSLOptions ssl_options;
6868 ssl_options.request_client_certificate = true;
6869 SpawnedTestServer test_server(
6870 SpawnedTestServer::TYPE_HTTPS,
6872 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6873 ASSERT_TRUE(test_server.Start());
6875 SSLClientAuthTestDelegate d;
6877 URLRequest r(test_server.GetURL(std::string()),
6883 EXPECT_TRUE(r.is_pending());
6885 base::RunLoop().Run();
6887 EXPECT_EQ(1, d.on_certificate_requested_count());
6888 EXPECT_FALSE(d.received_data_before_response());
6889 EXPECT_EQ(0, d.bytes_received());
6891 // Send no certificate.
6892 // TODO(davidben): Get temporary client cert import (with keys) working on
6893 // all platforms so we can test sending a cert as well.
6894 r.ContinueWithCertificate(NULL);
6896 base::RunLoop().Run();
6898 EXPECT_EQ(1, d.response_started_count());
6899 EXPECT_FALSE(d.received_data_before_response());
6900 EXPECT_NE(0, d.bytes_received());
6904 TEST_F(HTTPSRequestTest, ResumeTest) {
6905 // Test that we attempt a session resume when making two connections to the
6907 SpawnedTestServer::SSLOptions ssl_options;
6908 ssl_options.record_resume = true;
6909 SpawnedTestServer test_server(
6910 SpawnedTestServer::TYPE_HTTPS,
6912 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6913 ASSERT_TRUE(test_server.Start());
6915 SSLClientSocket::ClearSessionCache();
6919 URLRequest r(test_server.GetURL("ssl-session-cache"),
6925 EXPECT_TRUE(r.is_pending());
6927 base::RunLoop().Run();
6929 EXPECT_EQ(1, d.response_started_count());
6932 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
6933 CloseAllConnections();
6937 URLRequest r(test_server.GetURL("ssl-session-cache"),
6943 EXPECT_TRUE(r.is_pending());
6945 base::RunLoop().Run();
6947 // The response will look like;
6952 // With a newline at the end which makes the split think that there are
6955 EXPECT_EQ(1, d.response_started_count());
6956 std::vector<std::string> lines;
6957 base::SplitString(d.data_received(), '\n', &lines);
6958 ASSERT_EQ(4u, lines.size()) << d.data_received();
6960 std::string session_id;
6962 for (size_t i = 0; i < 2; i++) {
6963 std::vector<std::string> parts;
6964 base::SplitString(lines[i], '\t', &parts);
6965 ASSERT_EQ(2u, parts.size());
6967 EXPECT_EQ("insert", parts[0]);
6968 session_id = parts[1];
6970 EXPECT_EQ("lookup", parts[0]);
6971 EXPECT_EQ(session_id, parts[1]);
6977 TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
6978 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
6980 SpawnedTestServer::SSLOptions ssl_options;
6981 ssl_options.record_resume = true;
6982 SpawnedTestServer test_server(
6983 SpawnedTestServer::TYPE_HTTPS,
6985 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6986 ASSERT_TRUE(test_server.Start());
6988 SSLClientSocket::ClearSessionCache();
6992 URLRequest r(test_server.GetURL("ssl-session-cache"),
6998 EXPECT_TRUE(r.is_pending());
7000 base::RunLoop().Run();
7002 EXPECT_EQ(1, d.response_started_count());
7005 // Now create a new HttpCache with a different ssl_session_cache_shard value.
7006 HttpNetworkSession::Params params;
7007 params.host_resolver = default_context_.host_resolver();
7008 params.cert_verifier = default_context_.cert_verifier();
7009 params.transport_security_state = default_context_.transport_security_state();
7010 params.proxy_service = default_context_.proxy_service();
7011 params.ssl_config_service = default_context_.ssl_config_service();
7012 params.http_auth_handler_factory =
7013 default_context_.http_auth_handler_factory();
7014 params.network_delegate = &default_network_delegate_;
7015 params.http_server_properties = default_context_.http_server_properties();
7016 params.ssl_session_cache_shard = "alternate";
7018 scoped_ptr<HttpCache> cache(new HttpCache(
7019 new HttpNetworkSession(params),
7020 HttpCache::DefaultBackend::InMemory(0)));
7022 default_context_.set_http_transaction_factory(cache.get());
7026 URLRequest r(test_server.GetURL("ssl-session-cache"),
7032 EXPECT_TRUE(r.is_pending());
7034 base::RunLoop().Run();
7036 // The response will look like;
7040 // With a newline at the end which makes the split think that there are
7043 EXPECT_EQ(1, d.response_started_count());
7044 std::vector<std::string> lines;
7045 base::SplitString(d.data_received(), '\n', &lines);
7046 ASSERT_EQ(3u, lines.size());
7048 std::string session_id;
7049 for (size_t i = 0; i < 2; i++) {
7050 std::vector<std::string> parts;
7051 base::SplitString(lines[i], '\t', &parts);
7052 ASSERT_EQ(2u, parts.size());
7053 EXPECT_EQ("insert", parts[0]);
7055 session_id = parts[1];
7057 EXPECT_NE(session_id, parts[1]);
7063 class HTTPSFallbackTest : public testing::Test {
7065 HTTPSFallbackTest() : context_(true) {
7067 delegate_.set_allow_certificate_errors(true);
7069 virtual ~HTTPSFallbackTest() {}
7072 void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) {
7074 SpawnedTestServer test_server(
7075 SpawnedTestServer::TYPE_HTTPS,
7077 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7078 ASSERT_TRUE(test_server.Start());
7080 request_.reset(new URLRequest(
7081 test_server.GetURL(std::string()), DEFAULT_PRIORITY,
7082 &delegate_, &context_));
7085 base::RunLoop().Run();
7088 void ExpectConnection(int version) {
7089 EXPECT_EQ(1, delegate_.response_started_count());
7090 EXPECT_NE(0, delegate_.bytes_received());
7091 EXPECT_EQ(version, SSLConnectionStatusToVersion(
7092 request_->ssl_info().connection_status));
7093 EXPECT_TRUE(request_->ssl_info().connection_status &
7094 SSL_CONNECTION_VERSION_FALLBACK);
7097 void ExpectFailure(int error) {
7098 EXPECT_EQ(1, delegate_.response_started_count());
7099 EXPECT_FALSE(request_->status().is_success());
7100 EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status());
7101 EXPECT_EQ(error, request_->status().error());
7105 TestDelegate delegate_;
7106 TestURLRequestContext context_;
7107 scoped_ptr<URLRequest> request_;
7110 // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
7112 TEST_F(HTTPSFallbackTest, TLSv1Fallback) {
7113 SpawnedTestServer::SSLOptions ssl_options(
7114 SpawnedTestServer::SSLOptions::CERT_OK);
7115 ssl_options.tls_intolerant =
7116 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7118 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7119 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7122 // This test is disabled on Android because the remote test server doesn't cause
7124 #if !defined(OS_ANDROID)
7125 // Tests fallback to TLS 1.0 on connection reset.
7126 TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) {
7127 SpawnedTestServer::SSLOptions ssl_options(
7128 SpawnedTestServer::SSLOptions::CERT_OK);
7129 ssl_options.tls_intolerant =
7130 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7131 ssl_options.tls_intolerance_type =
7132 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7134 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7135 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7137 #endif // !OS_ANDROID
7139 // Tests that we don't fallback on handshake failure with servers that implement
7140 // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
7141 TEST_F(HTTPSFallbackTest, FallbackSCSV) {
7142 SpawnedTestServer::SSLOptions ssl_options(
7143 SpawnedTestServer::SSLOptions::CERT_OK);
7144 // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
7145 // a version fallback.
7146 ssl_options.tls_intolerant =
7147 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7148 // Have the server process TLS_FALLBACK_SCSV so that version fallback
7149 // connections are rejected.
7150 ssl_options.fallback_scsv_enabled = true;
7152 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7154 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
7155 // intolerance. If the fallback SCSV is processed when the original error
7156 // that caused the fallback should be returned, which should be
7157 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
7158 ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH);
7161 // Tests that we don't fallback on connection closed with servers that implement
7162 // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
7163 TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) {
7164 SpawnedTestServer::SSLOptions ssl_options(
7165 SpawnedTestServer::SSLOptions::CERT_OK);
7166 // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
7167 // a version fallback.
7168 ssl_options.tls_intolerant =
7169 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7170 ssl_options.tls_intolerance_type =
7171 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
7172 // Have the server process TLS_FALLBACK_SCSV so that version fallback
7173 // connections are rejected.
7174 ssl_options.fallback_scsv_enabled = true;
7176 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7178 // The original error should be replayed on rejected fallback.
7179 ExpectFailure(ERR_CONNECTION_CLOSED);
7182 // Tests that the SSLv3 fallback triggers on alert.
7183 TEST_F(HTTPSFallbackTest, SSLv3Fallback) {
7184 SpawnedTestServer::SSLOptions ssl_options(
7185 SpawnedTestServer::SSLOptions::CERT_OK);
7186 ssl_options.tls_intolerant =
7187 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7189 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7190 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
7193 // Tests that the SSLv3 fallback triggers on closed connections.
7194 TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) {
7195 SpawnedTestServer::SSLOptions ssl_options(
7196 SpawnedTestServer::SSLOptions::CERT_OK);
7197 ssl_options.tls_intolerant =
7198 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7199 ssl_options.tls_intolerance_type =
7200 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
7202 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7203 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
7206 // This test is disabled on Android because the remote test server doesn't cause
7208 #if !defined(OS_ANDROID)
7209 // Tests that a reset connection does not fallback down to SSL3.
7210 TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) {
7211 SpawnedTestServer::SSLOptions ssl_options(
7212 SpawnedTestServer::SSLOptions::CERT_OK);
7213 ssl_options.tls_intolerant =
7214 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
7215 ssl_options.tls_intolerance_type =
7216 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7218 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7219 ExpectFailure(ERR_CONNECTION_RESET);
7221 #endif // !OS_ANDROID
7223 class HTTPSSessionTest : public testing::Test {
7225 HTTPSSessionTest() : default_context_(true) {
7226 cert_verifier_.set_default_result(OK);
7228 default_context_.set_network_delegate(&default_network_delegate_);
7229 default_context_.set_cert_verifier(&cert_verifier_);
7230 default_context_.Init();
7232 virtual ~HTTPSSessionTest() {}
7235 MockCertVerifier cert_verifier_;
7236 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
7237 TestURLRequestContext default_context_;
7240 // Tests that session resumption is not attempted if an invalid certificate
7242 TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) {
7243 SpawnedTestServer::SSLOptions ssl_options;
7244 ssl_options.record_resume = true;
7245 SpawnedTestServer test_server(
7246 SpawnedTestServer::TYPE_HTTPS,
7248 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7249 ASSERT_TRUE(test_server.Start());
7251 SSLClientSocket::ClearSessionCache();
7253 // Simulate the certificate being expired and attempt a connection.
7254 cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID);
7257 URLRequest r(test_server.GetURL("ssl-session-cache"),
7263 EXPECT_TRUE(r.is_pending());
7265 base::RunLoop().Run();
7267 EXPECT_EQ(1, d.response_started_count());
7270 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
7271 CloseAllConnections();
7273 // Now change the certificate to be acceptable (so that the response is
7274 // loaded), and ensure that no session id is presented to the peer.
7275 cert_verifier_.set_default_result(OK);
7278 URLRequest r(test_server.GetURL("ssl-session-cache"),
7284 EXPECT_TRUE(r.is_pending());
7286 base::RunLoop().Run();
7288 // The response will look like;
7292 // With a newline at the end which makes the split think that there are
7295 // If a session was presented (eg: a bug), then the response would look
7301 EXPECT_EQ(1, d.response_started_count());
7302 std::vector<std::string> lines;
7303 base::SplitString(d.data_received(), '\n', &lines);
7304 ASSERT_EQ(3u, lines.size()) << d.data_received();
7306 std::string session_id;
7307 for (size_t i = 0; i < 2; i++) {
7308 std::vector<std::string> parts;
7309 base::SplitString(lines[i], '\t', &parts);
7310 ASSERT_EQ(2u, parts.size());
7311 EXPECT_EQ("insert", parts[0]);
7313 session_id = parts[1];
7315 EXPECT_NE(session_id, parts[1]);
7321 class TestSSLConfigService : public SSLConfigService {
7323 TestSSLConfigService(bool ev_enabled,
7324 bool online_rev_checking,
7325 bool rev_checking_required_local_anchors)
7326 : ev_enabled_(ev_enabled),
7327 online_rev_checking_(online_rev_checking),
7328 rev_checking_required_local_anchors_(
7329 rev_checking_required_local_anchors) {}
7331 // SSLConfigService:
7332 virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
7333 *config = SSLConfig();
7334 config->rev_checking_enabled = online_rev_checking_;
7335 config->verify_ev_cert = ev_enabled_;
7336 config->rev_checking_required_local_anchors =
7337 rev_checking_required_local_anchors_;
7341 virtual ~TestSSLConfigService() {}
7344 const bool ev_enabled_;
7345 const bool online_rev_checking_;
7346 const bool rev_checking_required_local_anchors_;
7349 // This the fingerprint of the "Testing CA" certificate used by the testserver.
7350 // See net/data/ssl/certificates/ocsp-test-root.pem.
7351 static const SHA1HashValue kOCSPTestCertFingerprint =
7352 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
7353 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
7355 // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
7357 static const SHA256HashValue kOCSPTestCertSPKI = { {
7358 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
7359 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
7360 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
7361 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
7364 // This is the policy OID contained in the certificates that testserver
7366 static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
7368 class HTTPSOCSPTest : public HTTPSRequestTest {
7373 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
7374 kOCSPTestCertFingerprint,
7375 kOCSPTestCertPolicy)) {
7378 virtual void SetUp() OVERRIDE {
7379 SetupContext(&context_);
7382 scoped_refptr<X509Certificate> root_cert =
7383 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
7384 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert);
7385 test_root_.reset(new ScopedTestRoot(root_cert.get()));
7387 #if defined(USE_NSS) || defined(OS_IOS)
7388 SetURLRequestContextForNSSHttpIO(&context_);
7389 EnsureNSSHttpIOInit();
7393 void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options,
7394 CertStatus* out_cert_status) {
7395 // We always overwrite out_cert_status.
7396 *out_cert_status = 0;
7397 SpawnedTestServer test_server(
7398 SpawnedTestServer::TYPE_HTTPS,
7400 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7401 ASSERT_TRUE(test_server.Start());
7404 d.set_allow_certificate_errors(true);
7406 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context_);
7409 base::RunLoop().Run();
7411 EXPECT_EQ(1, d.response_started_count());
7412 *out_cert_status = r.ssl_info().cert_status;
7415 virtual ~HTTPSOCSPTest() {
7416 #if defined(USE_NSS) || defined(OS_IOS)
7417 ShutdownNSSHttpIO();
7422 // SetupContext configures the URLRequestContext that will be used for making
7423 // connetions to testserver. This can be overridden in test subclasses for
7424 // different behaviour.
7425 virtual void SetupContext(URLRequestContext* context) {
7426 context->set_ssl_config_service(
7427 new TestSSLConfigService(true /* check for EV */,
7428 true /* online revocation checking */,
7429 false /* require rev. checking for local
7433 scoped_ptr<ScopedTestRoot> test_root_;
7434 TestURLRequestContext context_;
7435 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
7438 static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
7440 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
7441 // have that ability on other platforms.
7442 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
7448 // SystemSupportsHardFailRevocationChecking returns true iff the current
7449 // operating system supports revocation checking and can distinguish between
7450 // situations where a given certificate lacks any revocation information (eg:
7451 // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
7452 // revocation information cannot be obtained (eg: the CRL was unreachable).
7453 // If it does not, then tests which rely on 'hard fail' behaviour should be
7455 static bool SystemSupportsHardFailRevocationChecking() {
7456 #if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS)
7463 // SystemUsesChromiumEVMetadata returns true iff the current operating system
7464 // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
7465 // several tests are effected because our testing EV certificate won't be
7466 // recognised as EV.
7467 static bool SystemUsesChromiumEVMetadata() {
7468 #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID)
7469 // http://crbug.com/117478 - OpenSSL does not support EV validation.
7471 #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
7472 // On OS X and Android, we use the system to tell us whether a certificate is
7473 // EV or not and the system won't recognise our testing root.
7480 static bool SystemSupportsOCSP() {
7481 #if defined(USE_OPENSSL)
7482 // http://crbug.com/117478 - OpenSSL does not support OCSP.
7484 #elif defined(OS_WIN)
7485 return base::win::GetVersion() >= base::win::VERSION_VISTA;
7486 #elif defined(OS_ANDROID)
7487 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
7494 TEST_F(HTTPSOCSPTest, Valid) {
7495 if (!SystemSupportsOCSP()) {
7496 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7500 SpawnedTestServer::SSLOptions ssl_options(
7501 SpawnedTestServer::SSLOptions::CERT_AUTO);
7502 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7504 CertStatus cert_status;
7505 DoConnection(ssl_options, &cert_status);
7507 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7509 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7510 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7512 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7515 TEST_F(HTTPSOCSPTest, Revoked) {
7516 if (!SystemSupportsOCSP()) {
7517 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7521 SpawnedTestServer::SSLOptions ssl_options(
7522 SpawnedTestServer::SSLOptions::CERT_AUTO);
7523 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7525 CertStatus cert_status;
7526 DoConnection(ssl_options, &cert_status);
7528 #if !(defined(OS_MACOSX) && !defined(OS_IOS))
7529 // Doesn't pass on OS X yet for reasons that need to be investigated.
7530 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7532 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7533 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7536 TEST_F(HTTPSOCSPTest, Invalid) {
7537 if (!SystemSupportsOCSP()) {
7538 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7542 SpawnedTestServer::SSLOptions ssl_options(
7543 SpawnedTestServer::SSLOptions::CERT_AUTO);
7544 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7546 CertStatus cert_status;
7547 DoConnection(ssl_options, &cert_status);
7549 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7550 cert_status & CERT_STATUS_ALL_ERRORS);
7552 // Without a positive OCSP response, we shouldn't show the EV status.
7553 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7554 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7557 class HTTPSHardFailTest : public HTTPSOCSPTest {
7559 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7560 context->set_ssl_config_service(
7561 new TestSSLConfigService(false /* check for EV */,
7562 false /* online revocation checking */,
7563 true /* require rev. checking for local
7569 TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) {
7570 if (!SystemSupportsOCSP()) {
7571 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7575 if (!SystemSupportsHardFailRevocationChecking()) {
7576 LOG(WARNING) << "Skipping test because system doesn't support hard fail "
7577 << "revocation checking";
7581 SpawnedTestServer::SSLOptions ssl_options(
7582 SpawnedTestServer::SSLOptions::CERT_AUTO);
7583 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7585 CertStatus cert_status;
7586 DoConnection(ssl_options, &cert_status);
7588 EXPECT_EQ(CERT_STATUS_REVOKED,
7589 cert_status & CERT_STATUS_REVOKED);
7591 // Without a positive OCSP response, we shouldn't show the EV status.
7592 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7595 class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
7597 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7598 context->set_ssl_config_service(
7599 new TestSSLConfigService(true /* check for EV */,
7600 false /* online revocation checking */,
7601 false /* require rev. checking for local
7606 TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
7607 if (!SystemSupportsOCSP()) {
7608 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7612 SpawnedTestServer::SSLOptions ssl_options(
7613 SpawnedTestServer::SSLOptions::CERT_AUTO);
7614 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7615 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7617 CertStatus cert_status;
7618 DoConnection(ssl_options, &cert_status);
7620 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7621 cert_status & CERT_STATUS_ALL_ERRORS);
7623 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7624 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7625 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7628 TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) {
7629 if (!SystemSupportsOCSP()) {
7630 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7634 SpawnedTestServer::SSLOptions ssl_options(
7635 SpawnedTestServer::SSLOptions::CERT_AUTO);
7636 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7637 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7639 CertStatus cert_status;
7640 DoConnection(ssl_options, &cert_status);
7642 // Currently only works for Windows. When using NSS or OS X, it's not
7643 // possible to determine whether the check failed because of actual
7644 // revocation or because there was an OCSP failure.
7646 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7648 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7651 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7652 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7653 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7656 TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
7657 if (!SystemSupportsOCSP()) {
7658 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7662 SpawnedTestServer::SSLOptions ssl_options(
7663 SpawnedTestServer::SSLOptions::CERT_AUTO);
7664 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7665 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
7667 CertStatus cert_status;
7668 DoConnection(ssl_options, &cert_status);
7670 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7672 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7673 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7674 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7675 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7678 TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
7679 if (!SystemSupportsOCSP()) {
7680 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7684 SpawnedTestServer::SSLOptions ssl_options(
7685 SpawnedTestServer::SSLOptions::CERT_AUTO);
7686 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7687 SSLConfigService::SetCRLSet(
7688 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7690 CertStatus cert_status;
7691 DoConnection(ssl_options, &cert_status);
7693 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7694 cert_status & CERT_STATUS_ALL_ERRORS);
7696 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7697 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7698 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7701 TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) {
7702 if (!SystemSupportsOCSP()) {
7703 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7707 SpawnedTestServer::SSLOptions ssl_options(
7708 SpawnedTestServer::SSLOptions::CERT_AUTO);
7709 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7710 SSLConfigService::SetCRLSet(
7711 scoped_refptr<CRLSet>(CRLSet::ForTesting(
7712 false, &kOCSPTestCertSPKI, "")));
7714 CertStatus cert_status;
7715 DoConnection(ssl_options, &cert_status);
7717 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
7718 // revocation check for EV.
7719 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7720 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7721 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
7723 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7726 TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) {
7727 if (!SystemSupportsOCSP()) {
7728 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7732 SpawnedTestServer::SSLOptions ssl_options(
7733 SpawnedTestServer::SSLOptions::CERT_AUTO);
7734 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7735 SSLConfigService::SetCRLSet(
7736 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
7738 CertStatus cert_status = 0;
7739 DoConnection(ssl_options, &cert_status);
7741 // Even with a fresh CRLSet, we should still do online revocation checks when
7742 // the certificate chain isn't covered by the CRLSet, which it isn't in this
7744 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7745 cert_status & CERT_STATUS_ALL_ERRORS);
7747 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7748 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7749 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7752 TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
7753 // Test that when EV verification is requested, but online revocation
7754 // checking is disabled, and the leaf certificate is not in fact EV, that
7755 // no revocation checking actually happens.
7756 if (!SystemSupportsOCSP()) {
7757 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
7761 // Unmark the certificate's OID as EV, which should disable revocation
7762 // checking (as per the user preference)
7763 ev_test_policy_.reset();
7765 SpawnedTestServer::SSLOptions ssl_options(
7766 SpawnedTestServer::SSLOptions::CERT_AUTO);
7767 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
7768 SSLConfigService::SetCRLSet(
7769 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7771 CertStatus cert_status;
7772 DoConnection(ssl_options, &cert_status);
7774 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7776 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7777 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7780 class HTTPSCRLSetTest : public HTTPSOCSPTest {
7782 virtual void SetupContext(URLRequestContext* context) OVERRIDE {
7783 context->set_ssl_config_service(
7784 new TestSSLConfigService(false /* check for EV */,
7785 false /* online revocation checking */,
7786 false /* require rev. checking for local
7791 TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
7792 SpawnedTestServer::SSLOptions ssl_options(
7793 SpawnedTestServer::SSLOptions::CERT_AUTO);
7794 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
7795 SSLConfigService::SetCRLSet(
7796 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
7798 CertStatus cert_status;
7799 DoConnection(ssl_options, &cert_status);
7801 // If we're not trying EV verification then, even if the CRLSet has expired,
7802 // we don't fall back to online revocation checks.
7803 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
7804 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7805 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
7808 TEST_F(HTTPSCRLSetTest, CRLSetRevoked) {
7809 #if defined(USE_OPENSSL)
7810 LOG(WARNING) << "Skipping test because system doesn't support CRLSets";
7814 SpawnedTestServer::SSLOptions ssl_options(
7815 SpawnedTestServer::SSLOptions::CERT_AUTO);
7816 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
7817 ssl_options.cert_serial = 10;
7818 SSLConfigService::SetCRLSet(
7819 scoped_refptr<CRLSet>(CRLSet::ForTesting(
7820 false, &kOCSPTestCertSPKI, "\x0a")));
7822 CertStatus cert_status = 0;
7823 DoConnection(ssl_options, &cert_status);
7825 // If the certificate is recorded as revoked in the CRLSet, that should be
7826 // reflected without online revocation checking.
7827 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
7828 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
7830 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
7832 #endif // !defined(OS_IOS)
7834 #if !defined(DISABLE_FTP_SUPPORT)
7835 class URLRequestTestFTP : public URLRequestTest {
7838 : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost,
7843 SpawnedTestServer test_server_;
7846 // Make sure an FTP request using an unsafe ports fails.
7847 TEST_F(URLRequestTestFTP, UnsafePort) {
7848 ASSERT_TRUE(test_server_.Start());
7850 URLRequestJobFactoryImpl job_factory;
7851 FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver());
7853 GURL url("ftp://127.0.0.1:7");
7854 job_factory.SetProtocolHandler(
7856 new FtpProtocolHandler(&ftp_transaction_factory));
7857 default_context_.set_job_factory(&job_factory);
7861 URLRequest r(url, DEFAULT_PRIORITY, &d, &default_context_);
7863 EXPECT_TRUE(r.is_pending());
7865 base::RunLoop().Run();
7867 EXPECT_FALSE(r.is_pending());
7868 EXPECT_EQ(URLRequestStatus::FAILED, r.status().status());
7869 EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error());
7873 // Flaky, see http://crbug.com/25045.
7874 TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
7875 ASSERT_TRUE(test_server_.Start());
7880 test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, &default_context_);
7882 EXPECT_TRUE(r.is_pending());
7884 base::RunLoop().Run();
7886 EXPECT_FALSE(r.is_pending());
7887 EXPECT_EQ(1, d.response_started_count());
7888 EXPECT_FALSE(d.received_data_before_response());
7889 EXPECT_LT(0, d.bytes_received());
7890 EXPECT_EQ(test_server_.host_port_pair().host(),
7891 r.GetSocketAddress().host());
7892 EXPECT_EQ(test_server_.host_port_pair().port(),
7893 r.GetSocketAddress().port());
7897 // Flaky, see http://crbug.com/25045.
7898 TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
7899 ASSERT_TRUE(test_server_.Start());
7901 base::FilePath app_path;
7902 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7903 app_path = app_path.AppendASCII("LICENSE");
7906 URLRequest r(test_server_.GetURL("/LICENSE"),
7911 EXPECT_TRUE(r.is_pending());
7913 base::RunLoop().Run();
7915 int64 file_size = 0;
7916 base::GetFileSize(app_path, &file_size);
7918 EXPECT_FALSE(r.is_pending());
7919 EXPECT_EQ(1, d.response_started_count());
7920 EXPECT_FALSE(d.received_data_before_response());
7921 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7922 EXPECT_EQ(test_server_.host_port_pair().host(),
7923 r.GetSocketAddress().host());
7924 EXPECT_EQ(test_server_.host_port_pair().port(),
7925 r.GetSocketAddress().port());
7929 // Flaky, see http://crbug.com/25045.
7930 TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
7931 ASSERT_TRUE(test_server_.Start());
7933 base::FilePath app_path;
7934 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7935 app_path = app_path.AppendASCII("LICENSE");
7939 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
7944 EXPECT_TRUE(r.is_pending());
7946 base::RunLoop().Run();
7948 int64 file_size = 0;
7949 base::GetFileSize(app_path, &file_size);
7951 EXPECT_FALSE(r.is_pending());
7952 EXPECT_EQ(test_server_.host_port_pair().host(),
7953 r.GetSocketAddress().host());
7954 EXPECT_EQ(test_server_.host_port_pair().port(),
7955 r.GetSocketAddress().port());
7956 EXPECT_EQ(1, d.response_started_count());
7957 EXPECT_FALSE(d.received_data_before_response());
7958 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
7960 LoadTimingInfo load_timing_info;
7961 r.GetLoadTimingInfo(&load_timing_info);
7962 TestLoadTimingNoHttpResponse(load_timing_info);
7966 // Flaky, see http://crbug.com/25045.
7967 TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
7968 ASSERT_TRUE(test_server_.Start());
7970 base::FilePath app_path;
7971 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
7972 app_path = app_path.AppendASCII("LICENSE");
7975 URLRequest r(test_server_.GetURLWithUserAndPassword(
7976 "/LICENSE", "chrome", "wrong_password"),
7981 EXPECT_TRUE(r.is_pending());
7983 base::RunLoop().Run();
7985 int64 file_size = 0;
7986 base::GetFileSize(app_path, &file_size);
7988 EXPECT_FALSE(r.is_pending());
7989 EXPECT_EQ(1, d.response_started_count());
7990 EXPECT_FALSE(d.received_data_before_response());
7991 EXPECT_EQ(d.bytes_received(), 0);
7995 // Flaky, see http://crbug.com/25045.
7996 TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
7997 ASSERT_TRUE(test_server_.Start());
7999 base::FilePath app_path;
8000 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8001 app_path = app_path.AppendASCII("LICENSE");
8003 // Set correct login credentials. The delegate will be asked for them when
8004 // the initial login with wrong credentials will fail.
8005 d.set_credentials(AuthCredentials(kChrome, kChrome));
8007 URLRequest r(test_server_.GetURLWithUserAndPassword(
8008 "/LICENSE", "chrome", "wrong_password"),
8013 EXPECT_TRUE(r.is_pending());
8015 base::RunLoop().Run();
8017 int64 file_size = 0;
8018 base::GetFileSize(app_path, &file_size);
8020 EXPECT_FALSE(r.is_pending());
8021 EXPECT_EQ(1, d.response_started_count());
8022 EXPECT_FALSE(d.received_data_before_response());
8023 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8027 // Flaky, see http://crbug.com/25045.
8028 TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
8029 ASSERT_TRUE(test_server_.Start());
8031 base::FilePath app_path;
8032 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8033 app_path = app_path.AppendASCII("LICENSE");
8036 URLRequest r(test_server_.GetURLWithUserAndPassword(
8037 "/LICENSE", "wrong_user", "chrome"),
8042 EXPECT_TRUE(r.is_pending());
8044 base::RunLoop().Run();
8046 int64 file_size = 0;
8047 base::GetFileSize(app_path, &file_size);
8049 EXPECT_FALSE(r.is_pending());
8050 EXPECT_EQ(1, d.response_started_count());
8051 EXPECT_FALSE(d.received_data_before_response());
8052 EXPECT_EQ(d.bytes_received(), 0);
8056 // Flaky, see http://crbug.com/25045.
8057 TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
8058 ASSERT_TRUE(test_server_.Start());
8060 base::FilePath app_path;
8061 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8062 app_path = app_path.AppendASCII("LICENSE");
8064 // Set correct login credentials. The delegate will be asked for them when
8065 // the initial login with wrong credentials will fail.
8066 d.set_credentials(AuthCredentials(kChrome, kChrome));
8068 URLRequest r(test_server_.GetURLWithUserAndPassword(
8069 "/LICENSE", "wrong_user", "chrome"),
8074 EXPECT_TRUE(r.is_pending());
8076 base::RunLoop().Run();
8078 int64 file_size = 0;
8079 base::GetFileSize(app_path, &file_size);
8081 EXPECT_FALSE(r.is_pending());
8082 EXPECT_EQ(1, d.response_started_count());
8083 EXPECT_FALSE(d.received_data_before_response());
8084 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8088 // Flaky, see http://crbug.com/25045.
8089 TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
8090 ASSERT_TRUE(test_server_.Start());
8092 base::FilePath app_path;
8093 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8094 app_path = app_path.AppendASCII("LICENSE");
8096 scoped_ptr<TestDelegate> d(new TestDelegate);
8098 // Pass correct login identity in the URL.
8100 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
8105 EXPECT_TRUE(r.is_pending());
8107 base::RunLoop().Run();
8109 int64 file_size = 0;
8110 base::GetFileSize(app_path, &file_size);
8112 EXPECT_FALSE(r.is_pending());
8113 EXPECT_EQ(1, d->response_started_count());
8114 EXPECT_FALSE(d->received_data_before_response());
8115 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8118 d.reset(new TestDelegate);
8120 // This request should use cached identity from previous request.
8121 URLRequest r(test_server_.GetURL("/LICENSE"),
8126 EXPECT_TRUE(r.is_pending());
8128 base::RunLoop().Run();
8130 int64 file_size = 0;
8131 base::GetFileSize(app_path, &file_size);
8133 EXPECT_FALSE(r.is_pending());
8134 EXPECT_EQ(1, d->response_started_count());
8135 EXPECT_FALSE(d->received_data_before_response());
8136 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8140 // Flaky, see http://crbug.com/25045.
8141 TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
8142 ASSERT_TRUE(test_server_.Start());
8144 base::FilePath app_path;
8145 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8146 app_path = app_path.AppendASCII("LICENSE");
8148 scoped_ptr<TestDelegate> d(new TestDelegate);
8149 // Set correct login credentials. The delegate will be asked for them when
8150 // the initial login with wrong credentials will fail.
8151 d->set_credentials(AuthCredentials(kChrome, kChrome));
8153 URLRequest r(test_server_.GetURLWithUserAndPassword(
8154 "/LICENSE", "chrome", "wrong_password"),
8159 EXPECT_TRUE(r.is_pending());
8161 base::RunLoop().Run();
8163 int64 file_size = 0;
8164 base::GetFileSize(app_path, &file_size);
8166 EXPECT_FALSE(r.is_pending());
8167 EXPECT_EQ(1, d->response_started_count());
8168 EXPECT_FALSE(d->received_data_before_response());
8169 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8172 // Use a new delegate without explicit credentials. The cached ones should be
8174 d.reset(new TestDelegate);
8176 // Don't pass wrong credentials in the URL, they would override valid cached
8178 URLRequest r(test_server_.GetURL("/LICENSE"),
8183 EXPECT_TRUE(r.is_pending());
8185 base::RunLoop().Run();
8187 int64 file_size = 0;
8188 base::GetFileSize(app_path, &file_size);
8190 EXPECT_FALSE(r.is_pending());
8191 EXPECT_EQ(1, d->response_started_count());
8192 EXPECT_FALSE(d->received_data_before_response());
8193 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8196 #endif // !defined(DISABLE_FTP_SUPPORT)