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"
16 #include "base/basictypes.h"
17 #include "base/bind.h"
18 #include "base/compiler_specific.h"
19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h"
21 #include "base/files/scoped_temp_dir.h"
22 #include "base/format_macros.h"
23 #include "base/json/json_reader.h"
24 #include "base/location.h"
25 #include "base/memory/scoped_ptr.h"
26 #include "base/memory/weak_ptr.h"
27 #include "base/message_loop/message_loop.h"
28 #include "base/path_service.h"
29 #include "base/run_loop.h"
30 #include "base/single_thread_task_runner.h"
31 #include "base/strings/string_number_conversions.h"
32 #include "base/strings/string_piece.h"
33 #include "base/strings/string_split.h"
34 #include "base/strings/string_util.h"
35 #include "base/strings/stringprintf.h"
36 #include "base/strings/utf_string_conversions.h"
37 #include "base/test/histogram_tester.h"
38 #include "base/thread_task_runner_handle.h"
39 #include "base/values.h"
40 #include "net/base/chunked_upload_data_stream.h"
41 #include "net/base/elements_upload_data_stream.h"
42 #include "net/base/external_estimate_provider.h"
43 #include "net/base/load_flags.h"
44 #include "net/base/load_timing_info.h"
45 #include "net/base/load_timing_info_test_util.h"
46 #include "net/base/net_errors.h"
47 #include "net/base/net_module.h"
48 #include "net/base/net_util.h"
49 #include "net/base/network_quality_estimator.h"
50 #include "net/base/request_priority.h"
51 #include "net/base/test_data_directory.h"
52 #include "net/base/upload_bytes_element_reader.h"
53 #include "net/base/upload_data_stream.h"
54 #include "net/base/upload_file_element_reader.h"
55 #include "net/cert/ev_root_ca_metadata.h"
56 #include "net/cert/mock_cert_verifier.h"
57 #include "net/cert/test_root_certs.h"
58 #include "net/cert_net/nss_ocsp.h"
59 #include "net/cookies/cookie_monster.h"
60 #include "net/cookies/cookie_store_test_helpers.h"
61 #include "net/disk_cache/disk_cache.h"
62 #include "net/dns/mock_host_resolver.h"
63 #include "net/http/http_byte_range.h"
64 #include "net/http/http_cache.h"
65 #include "net/http/http_network_layer.h"
66 #include "net/http/http_network_session.h"
67 #include "net/http/http_request_headers.h"
68 #include "net/http/http_response_headers.h"
69 #include "net/http/http_util.h"
70 #include "net/log/net_log.h"
71 #include "net/log/test_net_log.h"
72 #include "net/log/test_net_log_entry.h"
73 #include "net/log/test_net_log_util.h"
74 #include "net/proxy/proxy_service.h"
75 #include "net/socket/ssl_client_socket.h"
76 #include "net/ssl/ssl_cipher_suite_names.h"
77 #include "net/ssl/ssl_connection_status_flags.h"
78 #include "net/test/cert_test_util.h"
79 #include "net/test/spawned_test_server/spawned_test_server.h"
80 #include "net/url_request/data_protocol_handler.h"
81 #include "net/url_request/static_http_user_agent_settings.h"
82 #include "net/url_request/url_request.h"
83 #include "net/url_request/url_request_http_job.h"
84 #include "net/url_request/url_request_intercepting_job_factory.h"
85 #include "net/url_request/url_request_interceptor.h"
86 #include "net/url_request/url_request_job_factory_impl.h"
87 #include "net/url_request/url_request_redirect_job.h"
88 #include "net/url_request/url_request_test_job.h"
89 #include "net/url_request/url_request_test_util.h"
90 #include "testing/gtest/include/gtest/gtest.h"
91 #include "testing/platform_test.h"
93 #if !defined(DISABLE_FILE_SUPPORT)
94 #include "net/base/filename_util.h"
95 #include "net/url_request/file_protocol_handler.h"
96 #include "net/url_request/url_request_file_dir_job.h"
99 #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID)
100 #include "net/ftp/ftp_network_layer.h"
101 #include "net/url_request/ftp_protocol_handler.h"
105 #include "base/win/scoped_com_initializer.h"
106 #include "base/win/scoped_comptr.h"
107 #include "base/win/windows_version.h"
110 using base::ASCIIToUTF16
;
118 const base::string16
kChrome(ASCIIToUTF16("chrome"));
119 const base::string16
kSecret(ASCIIToUTF16("secret"));
120 const base::string16
kUser(ASCIIToUTF16("user"));
122 const base::FilePath::CharType kTestFilePath
[] =
123 FILE_PATH_LITERAL("net/data/url_request_unittest");
125 #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID)
126 // Test file used in most FTP tests.
127 const char kFtpTestFile
[] = "BullRunSpeech.txt";
130 // Tests load timing information in the case a fresh connection was used, with
132 void TestLoadTimingNotReused(const LoadTimingInfo
& load_timing_info
,
133 int connect_timing_flags
) {
134 EXPECT_FALSE(load_timing_info
.socket_reused
);
135 EXPECT_NE(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
137 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
138 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
140 EXPECT_LE(load_timing_info
.request_start
,
141 load_timing_info
.connect_timing
.connect_start
);
142 ExpectConnectTimingHasTimes(load_timing_info
.connect_timing
,
143 connect_timing_flags
);
144 EXPECT_LE(load_timing_info
.connect_timing
.connect_end
,
145 load_timing_info
.send_start
);
146 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
147 EXPECT_LE(load_timing_info
.send_end
, load_timing_info
.receive_headers_end
);
149 EXPECT_TRUE(load_timing_info
.proxy_resolve_start
.is_null());
150 EXPECT_TRUE(load_timing_info
.proxy_resolve_end
.is_null());
153 // Same as above, but with proxy times.
154 void TestLoadTimingNotReusedWithProxy(
155 const LoadTimingInfo
& load_timing_info
,
156 int connect_timing_flags
) {
157 EXPECT_FALSE(load_timing_info
.socket_reused
);
158 EXPECT_NE(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
160 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
161 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
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
.connect_timing
.connect_start
);
169 ExpectConnectTimingHasTimes(load_timing_info
.connect_timing
,
170 connect_timing_flags
);
171 EXPECT_LE(load_timing_info
.connect_timing
.connect_end
,
172 load_timing_info
.send_start
);
173 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
174 EXPECT_LE(load_timing_info
.send_end
, load_timing_info
.receive_headers_end
);
177 // Same as above, but with a reused socket and proxy times.
178 void TestLoadTimingReusedWithProxy(
179 const LoadTimingInfo
& load_timing_info
) {
180 EXPECT_TRUE(load_timing_info
.socket_reused
);
181 EXPECT_NE(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
183 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
184 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
186 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
188 EXPECT_LE(load_timing_info
.request_start
,
189 load_timing_info
.proxy_resolve_start
);
190 EXPECT_LE(load_timing_info
.proxy_resolve_start
,
191 load_timing_info
.proxy_resolve_end
);
192 EXPECT_LE(load_timing_info
.proxy_resolve_end
,
193 load_timing_info
.send_start
);
194 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
195 EXPECT_LE(load_timing_info
.send_end
, load_timing_info
.receive_headers_end
);
198 #if !defined(DISABLE_FILE_SUPPORT)
199 // Tests load timing information in the case of a cache hit, when no cache
200 // validation request was sent over the wire.
201 base::StringPiece
TestNetResourceProvider(int key
) {
205 void FillBuffer(char* buffer
, size_t len
) {
206 static bool called
= false;
209 int seed
= static_cast<int>(Time::Now().ToInternalValue());
213 for (size_t i
= 0; i
< len
; i
++) {
214 buffer
[i
] = static_cast<char>(rand());
222 void TestLoadTimingCacheHitNoNetwork(
223 const LoadTimingInfo
& load_timing_info
) {
224 EXPECT_FALSE(load_timing_info
.socket_reused
);
225 EXPECT_EQ(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
227 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
228 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
230 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
231 EXPECT_LE(load_timing_info
.request_start
, load_timing_info
.send_start
);
232 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
233 EXPECT_LE(load_timing_info
.send_end
, load_timing_info
.receive_headers_end
);
235 EXPECT_TRUE(load_timing_info
.proxy_resolve_start
.is_null());
236 EXPECT_TRUE(load_timing_info
.proxy_resolve_end
.is_null());
239 #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID)
240 // Tests load timing in the case that there is no HTTP response. This can be
241 // used to test in the case of errors or non-HTTP requests.
242 void TestLoadTimingNoHttpResponse(
243 const LoadTimingInfo
& load_timing_info
) {
244 EXPECT_FALSE(load_timing_info
.socket_reused
);
245 EXPECT_EQ(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
247 // Only the request times should be non-null.
248 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
249 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
251 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
253 EXPECT_TRUE(load_timing_info
.proxy_resolve_start
.is_null());
254 EXPECT_TRUE(load_timing_info
.proxy_resolve_end
.is_null());
255 EXPECT_TRUE(load_timing_info
.send_start
.is_null());
256 EXPECT_TRUE(load_timing_info
.send_end
.is_null());
257 EXPECT_TRUE(load_timing_info
.receive_headers_end
.is_null());
261 // Do a case-insensitive search through |haystack| for |needle|.
262 bool ContainsString(const std::string
& haystack
, const char* needle
) {
263 std::string::const_iterator it
= std::search(
264 haystack
.begin(), haystack
.end(), needle
, needle
+ strlen(needle
),
265 base::CaseInsensitiveCompareASCII
<char>());
266 return it
!= haystack
.end();
269 scoped_ptr
<UploadDataStream
> CreateSimpleUploadData(const char* data
) {
270 scoped_ptr
<UploadElementReader
> reader(
271 new UploadBytesElementReader(data
, strlen(data
)));
272 return ElementsUploadDataStream::CreateWithReader(reader
.Pass(), 0);
275 // Verify that the SSLInfo of a successful SSL connection has valid values.
276 void CheckSSLInfo(const SSLInfo
& ssl_info
) {
277 // -1 means unknown. 0 means no encryption.
278 EXPECT_GT(ssl_info
.security_bits
, 0);
280 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
281 uint16 cipher_suite
= SSLConnectionStatusToCipherSuite(
282 ssl_info
.connection_status
);
283 EXPECT_NE(0U, cipher_suite
);
286 void CheckFullRequestHeaders(const HttpRequestHeaders
& headers
,
287 const GURL
& host_url
) {
288 std::string sent_value
;
290 EXPECT_TRUE(headers
.GetHeader("Host", &sent_value
));
291 EXPECT_EQ(GetHostAndOptionalPort(host_url
), sent_value
);
293 EXPECT_TRUE(headers
.GetHeader("Connection", &sent_value
));
294 EXPECT_EQ("keep-alive", sent_value
);
297 bool FingerprintsEqual(const HashValueVector
& a
, const HashValueVector
& b
) {
298 size_t size
= a
.size();
300 if (size
!= b
.size())
303 for (size_t i
= 0; i
< size
; ++i
) {
304 if (!a
[i
].Equals(b
[i
]))
310 #endif // !defined(OS_IOS)
312 // A network delegate that allows the user to choose a subset of request stages
313 // to block in. When blocking, the delegate can do one of the following:
314 // * synchronously return a pre-specified error code, or
315 // * asynchronously return that value via an automatically called callback,
317 // * block and wait for the user to do a callback.
318 // Additionally, the user may also specify a redirect URL -- then each request
319 // with the current URL different from the redirect target will be redirected
320 // to that target, in the on-before-URL-request stage, independent of whether
321 // the delegate blocks in ON_BEFORE_URL_REQUEST or not.
322 class BlockingNetworkDelegate
: public TestNetworkDelegate
{
324 // Stages in which the delegate can block.
327 ON_BEFORE_URL_REQUEST
= 1 << 0,
328 ON_BEFORE_SEND_HEADERS
= 1 << 1,
329 ON_HEADERS_RECEIVED
= 1 << 2,
330 ON_AUTH_REQUIRED
= 1 << 3
333 // Behavior during blocked stages. During other stages, just
334 // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
336 SYNCHRONOUS
, // No callback, returns specified return values.
337 AUTO_CALLBACK
, // |this| posts a task to run the callback using the
338 // specified return codes.
339 USER_CALLBACK
, // User takes care of doing a callback. |retval_| and
340 // |auth_retval_| are ignored. In every blocking stage the
341 // message loop is quit.
344 // Creates a delegate which does not block at all.
345 explicit BlockingNetworkDelegate(BlockMode block_mode
);
347 // For users to trigger a callback returning |response|.
348 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
349 // Only call if |block_mode_| == USER_CALLBACK.
350 void DoCallback(int response
);
351 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response
);
354 void set_retval(int retval
) {
355 ASSERT_NE(USER_CALLBACK
, block_mode_
);
356 ASSERT_NE(ERR_IO_PENDING
, retval
);
357 ASSERT_NE(OK
, retval
);
361 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
362 // |auth_credentials_| will be passed with the response.
363 void set_auth_retval(AuthRequiredResponse auth_retval
) {
364 ASSERT_NE(USER_CALLBACK
, block_mode_
);
365 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING
, auth_retval
);
366 auth_retval_
= auth_retval
;
368 void set_auth_credentials(const AuthCredentials
& auth_credentials
) {
369 auth_credentials_
= auth_credentials
;
372 void set_redirect_url(const GURL
& url
) {
376 void set_block_on(int block_on
) {
377 block_on_
= block_on
;
380 // Allows the user to check in which state did we block.
381 Stage
stage_blocked_for_callback() const {
382 EXPECT_EQ(USER_CALLBACK
, block_mode_
);
383 return stage_blocked_for_callback_
;
387 void RunCallback(int response
, const CompletionCallback
& callback
);
388 void RunAuthCallback(AuthRequiredResponse response
,
389 const AuthCallback
& callback
);
391 // TestNetworkDelegate implementation.
392 int OnBeforeURLRequest(URLRequest
* request
,
393 const CompletionCallback
& callback
,
394 GURL
* new_url
) override
;
396 int OnBeforeSendHeaders(URLRequest
* request
,
397 const CompletionCallback
& callback
,
398 HttpRequestHeaders
* headers
) override
;
400 int OnHeadersReceived(
402 const CompletionCallback
& callback
,
403 const HttpResponseHeaders
* original_response_headers
,
404 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
405 GURL
* allowed_unsafe_redirect_url
) override
;
407 NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
409 const AuthChallengeInfo
& auth_info
,
410 const AuthCallback
& callback
,
411 AuthCredentials
* credentials
) override
;
413 // Resets the callbacks and |stage_blocked_for_callback_|.
416 // Checks whether we should block in |stage|. If yes, returns an error code
417 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
418 int MaybeBlockStage(Stage stage
, const CompletionCallback
& callback
);
420 // Configuration parameters, can be adjusted by public methods:
421 const BlockMode block_mode_
;
423 // Values returned on blocking stages when mode is SYNCHRONOUS or
424 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
425 int retval_
; // To be returned in non-auth stages.
426 AuthRequiredResponse auth_retval_
;
428 GURL redirect_url_
; // Used if non-empty during OnBeforeURLRequest.
429 int block_on_
; // Bit mask: in which stages to block.
431 // |auth_credentials_| will be copied to |*target_auth_credential_| on
433 AuthCredentials auth_credentials_
;
434 AuthCredentials
* target_auth_credentials_
;
436 // Internal variables, not set by not the user:
437 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
439 Stage stage_blocked_for_callback_
;
441 // Callback objects stored during blocking stages.
442 CompletionCallback callback_
;
443 AuthCallback auth_callback_
;
445 base::WeakPtrFactory
<BlockingNetworkDelegate
> weak_factory_
;
447 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate
);
450 BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode
)
451 : block_mode_(block_mode
),
453 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION
),
455 target_auth_credentials_(NULL
),
456 stage_blocked_for_callback_(NOT_BLOCKED
),
457 weak_factory_(this) {
460 void BlockingNetworkDelegate::DoCallback(int response
) {
461 ASSERT_EQ(USER_CALLBACK
, block_mode_
);
462 ASSERT_NE(NOT_BLOCKED
, stage_blocked_for_callback_
);
463 ASSERT_NE(ON_AUTH_REQUIRED
, stage_blocked_for_callback_
);
464 CompletionCallback callback
= callback_
;
466 RunCallback(response
, callback
);
469 void BlockingNetworkDelegate::DoAuthCallback(
470 NetworkDelegate::AuthRequiredResponse response
) {
471 ASSERT_EQ(USER_CALLBACK
, block_mode_
);
472 ASSERT_EQ(ON_AUTH_REQUIRED
, stage_blocked_for_callback_
);
473 AuthCallback auth_callback
= auth_callback_
;
475 RunAuthCallback(response
, auth_callback
);
478 void BlockingNetworkDelegate::RunCallback(int response
,
479 const CompletionCallback
& callback
) {
480 callback
.Run(response
);
483 void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response
,
484 const AuthCallback
& callback
) {
485 if (auth_retval_
== AUTH_REQUIRED_RESPONSE_SET_AUTH
) {
486 ASSERT_TRUE(target_auth_credentials_
!= NULL
);
487 *target_auth_credentials_
= auth_credentials_
;
489 callback
.Run(response
);
492 int BlockingNetworkDelegate::OnBeforeURLRequest(
494 const CompletionCallback
& callback
,
496 if (redirect_url_
== request
->url())
497 return OK
; // We've already seen this request and redirected elsewhere.
499 TestNetworkDelegate::OnBeforeURLRequest(request
, callback
, new_url
);
501 if (!redirect_url_
.is_empty())
502 *new_url
= redirect_url_
;
504 return MaybeBlockStage(ON_BEFORE_URL_REQUEST
, callback
);
507 int BlockingNetworkDelegate::OnBeforeSendHeaders(
509 const CompletionCallback
& callback
,
510 HttpRequestHeaders
* headers
) {
511 TestNetworkDelegate::OnBeforeSendHeaders(request
, callback
, headers
);
513 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS
, callback
);
516 int BlockingNetworkDelegate::OnHeadersReceived(
518 const CompletionCallback
& callback
,
519 const HttpResponseHeaders
* original_response_headers
,
520 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
521 GURL
* allowed_unsafe_redirect_url
) {
522 TestNetworkDelegate::OnHeadersReceived(request
,
524 original_response_headers
,
525 override_response_headers
,
526 allowed_unsafe_redirect_url
);
528 return MaybeBlockStage(ON_HEADERS_RECEIVED
, callback
);
531 NetworkDelegate::AuthRequiredResponse
BlockingNetworkDelegate::OnAuthRequired(
533 const AuthChallengeInfo
& auth_info
,
534 const AuthCallback
& callback
,
535 AuthCredentials
* credentials
) {
536 TestNetworkDelegate::OnAuthRequired(request
, auth_info
, callback
,
538 // Check that the user has provided callback for the previous blocked stage.
539 EXPECT_EQ(NOT_BLOCKED
, stage_blocked_for_callback_
);
541 if ((block_on_
& ON_AUTH_REQUIRED
) == 0) {
542 return AUTH_REQUIRED_RESPONSE_NO_ACTION
;
545 target_auth_credentials_
= credentials
;
547 switch (block_mode_
) {
549 if (auth_retval_
== AUTH_REQUIRED_RESPONSE_SET_AUTH
)
550 *target_auth_credentials_
= auth_credentials_
;
554 base::ThreadTaskRunnerHandle::Get()->PostTask(
556 base::Bind(&BlockingNetworkDelegate::RunAuthCallback
,
557 weak_factory_
.GetWeakPtr(), auth_retval_
, callback
));
558 return AUTH_REQUIRED_RESPONSE_IO_PENDING
;
561 auth_callback_
= callback
;
562 stage_blocked_for_callback_
= ON_AUTH_REQUIRED
;
563 base::ThreadTaskRunnerHandle::Get()->PostTask(
564 FROM_HERE
, base::MessageLoop::QuitClosure());
565 return AUTH_REQUIRED_RESPONSE_IO_PENDING
;
568 return AUTH_REQUIRED_RESPONSE_NO_ACTION
; // Dummy value.
571 void BlockingNetworkDelegate::Reset() {
572 EXPECT_NE(NOT_BLOCKED
, stage_blocked_for_callback_
);
573 stage_blocked_for_callback_
= NOT_BLOCKED
;
575 auth_callback_
.Reset();
578 int BlockingNetworkDelegate::MaybeBlockStage(
579 BlockingNetworkDelegate::Stage stage
,
580 const CompletionCallback
& callback
) {
581 // Check that the user has provided callback for the previous blocked stage.
582 EXPECT_EQ(NOT_BLOCKED
, stage_blocked_for_callback_
);
584 if ((block_on_
& stage
) == 0) {
588 switch (block_mode_
) {
590 EXPECT_NE(OK
, retval_
);
594 base::ThreadTaskRunnerHandle::Get()->PostTask(
595 FROM_HERE
, base::Bind(&BlockingNetworkDelegate::RunCallback
,
596 weak_factory_
.GetWeakPtr(), retval_
, callback
));
597 return ERR_IO_PENDING
;
600 callback_
= callback
;
601 stage_blocked_for_callback_
= stage
;
602 base::ThreadTaskRunnerHandle::Get()->PostTask(
603 FROM_HERE
, base::MessageLoop::QuitClosure());
604 return ERR_IO_PENDING
;
610 class TestURLRequestContextWithProxy
: public TestURLRequestContext
{
612 // Does not own |delegate|.
613 TestURLRequestContextWithProxy(const std::string
& proxy
,
614 NetworkDelegate
* delegate
)
615 : TestURLRequestContext(true) {
616 context_storage_
.set_proxy_service(ProxyService::CreateFixed(proxy
));
617 set_network_delegate(delegate
);
620 ~TestURLRequestContextWithProxy() override
{}
623 // A mock ReportSender that just remembers the latest report
624 // URI and report to be sent.
625 class MockCertificateReportSender
626 : public TransportSecurityState::ReportSender
{
628 MockCertificateReportSender() {}
629 ~MockCertificateReportSender() override
{}
631 void Send(const GURL
& report_uri
, const std::string
& report
) override
{
632 latest_report_uri_
= report_uri
;
633 latest_report_
= report
;
636 const GURL
& latest_report_uri() { return latest_report_uri_
; }
637 const std::string
& latest_report() { return latest_report_
; }
640 GURL latest_report_uri_
;
641 std::string latest_report_
;
646 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.
647 class URLRequestTest
: public PlatformTest
{
649 URLRequestTest() : default_context_(true) {
650 default_context_
.set_network_delegate(&default_network_delegate_
);
651 default_context_
.set_net_log(&net_log_
);
652 job_factory_impl_
= new URLRequestJobFactoryImpl();
653 job_factory_
.reset(job_factory_impl_
);
656 ~URLRequestTest() override
{
657 // URLRequestJobs may post clean-up tasks on destruction.
658 base::RunLoop().RunUntilIdle();
661 void SetUp() override
{
663 default_context_
.set_job_factory(job_factory_
.get());
664 default_context_
.Init();
665 PlatformTest::SetUp();
668 virtual void SetUpFactory() {
669 job_factory_impl_
->SetProtocolHandler(
670 "data", make_scoped_ptr(new DataProtocolHandler
));
671 #if !defined(DISABLE_FILE_SUPPORT)
672 job_factory_impl_
->SetProtocolHandler(
673 "file", make_scoped_ptr(new FileProtocolHandler(
674 base::ThreadTaskRunnerHandle::Get())));
678 TestNetworkDelegate
* default_network_delegate() {
679 return &default_network_delegate_
;
682 const TestURLRequestContext
& default_context() const {
683 return default_context_
;
687 // Adds the TestJobInterceptor to the default context.
688 TestJobInterceptor
* AddTestInterceptor() {
689 TestJobInterceptor
* protocol_handler_
= new TestJobInterceptor();
690 job_factory_impl_
->SetProtocolHandler("http", nullptr);
691 job_factory_impl_
->SetProtocolHandler("http",
692 make_scoped_ptr(protocol_handler_
));
693 return protocol_handler_
;
698 TestNetworkDelegate default_network_delegate_
; // Must outlive URLRequest.
699 URLRequestJobFactoryImpl
* job_factory_impl_
;
700 scoped_ptr
<URLRequestJobFactory
> job_factory_
;
701 TestURLRequestContext default_context_
;
704 TEST_F(URLRequestTest
, AboutBlankTest
) {
707 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
708 GURL("about:blank"), DEFAULT_PRIORITY
, &d
));
711 EXPECT_TRUE(r
->is_pending());
713 base::RunLoop().Run();
715 EXPECT_TRUE(!r
->is_pending());
716 EXPECT_FALSE(d
.received_data_before_response());
717 EXPECT_EQ(d
.bytes_received(), 0);
718 EXPECT_EQ("", r
->GetSocketAddress().host());
719 EXPECT_EQ(0, r
->GetSocketAddress().port());
721 HttpRequestHeaders headers
;
722 EXPECT_FALSE(r
->GetFullRequestHeaders(&headers
));
726 TEST_F(URLRequestTest
, DataURLImageTest
) {
729 // Use our nice little Chrome logo.
730 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
732 "data:image/png;base64,"
733 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB"
734 "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ"
735 "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI"
736 "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz"
737 "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck"
738 "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0"
739 "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H"
740 "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk"
741 "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6"
742 "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ"
743 "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ"
744 "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz"
745 "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC"
746 "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40"
747 "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4"
748 "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O"
749 "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm"
750 "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV"
751 "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
752 DEFAULT_PRIORITY
, &d
));
755 EXPECT_TRUE(r
->is_pending());
757 base::RunLoop().Run();
759 EXPECT_TRUE(!r
->is_pending());
760 EXPECT_FALSE(d
.received_data_before_response());
761 EXPECT_EQ(d
.bytes_received(), 911);
762 EXPECT_EQ("", r
->GetSocketAddress().host());
763 EXPECT_EQ(0, r
->GetSocketAddress().port());
765 HttpRequestHeaders headers
;
766 EXPECT_FALSE(r
->GetFullRequestHeaders(&headers
));
770 #if !defined(DISABLE_FILE_SUPPORT)
771 TEST_F(URLRequestTest
, FileTest
) {
772 base::FilePath app_path
;
773 PathService::Get(base::FILE_EXE
, &app_path
);
774 GURL app_url
= FilePathToFileURL(app_path
);
778 scoped_ptr
<URLRequest
> r(
779 default_context_
.CreateRequest(app_url
, DEFAULT_PRIORITY
, &d
));
782 EXPECT_TRUE(r
->is_pending());
784 base::RunLoop().Run();
786 int64 file_size
= -1;
787 EXPECT_TRUE(base::GetFileSize(app_path
, &file_size
));
789 EXPECT_TRUE(!r
->is_pending());
790 EXPECT_EQ(1, d
.response_started_count());
791 EXPECT_FALSE(d
.received_data_before_response());
792 EXPECT_EQ(d
.bytes_received(), static_cast<int>(file_size
));
793 EXPECT_EQ("", r
->GetSocketAddress().host());
794 EXPECT_EQ(0, r
->GetSocketAddress().port());
796 HttpRequestHeaders headers
;
797 EXPECT_FALSE(r
->GetFullRequestHeaders(&headers
));
801 TEST_F(URLRequestTest
, FileTestCancel
) {
802 base::FilePath app_path
;
803 PathService::Get(base::FILE_EXE
, &app_path
);
804 GURL app_url
= FilePathToFileURL(app_path
);
808 scoped_ptr
<URLRequest
> r(
809 default_context_
.CreateRequest(app_url
, DEFAULT_PRIORITY
, &d
));
812 EXPECT_TRUE(r
->is_pending());
815 // Async cancellation should be safe even when URLRequest has been already
817 base::RunLoop().RunUntilIdle();
820 TEST_F(URLRequestTest
, FileTestFullSpecifiedRange
) {
821 const size_t buffer_size
= 4000;
822 scoped_ptr
<char[]> buffer(new char[buffer_size
]);
823 FillBuffer(buffer
.get(), buffer_size
);
825 base::FilePath temp_path
;
826 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path
));
827 GURL temp_url
= FilePathToFileURL(temp_path
);
828 EXPECT_TRUE(base::WriteFile(temp_path
, buffer
.get(), buffer_size
));
831 EXPECT_TRUE(base::GetFileSize(temp_path
, &file_size
));
833 const size_t first_byte_position
= 500;
834 const size_t last_byte_position
= buffer_size
- first_byte_position
;
835 const size_t content_length
= last_byte_position
- first_byte_position
+ 1;
836 std::string
partial_buffer_string(buffer
.get() + first_byte_position
,
837 buffer
.get() + last_byte_position
+ 1);
841 scoped_ptr
<URLRequest
> r(
842 default_context_
.CreateRequest(temp_url
, DEFAULT_PRIORITY
, &d
));
844 HttpRequestHeaders headers
;
846 HttpRequestHeaders::kRange
,
847 HttpByteRange::Bounded(
848 first_byte_position
, last_byte_position
).GetHeaderValue());
849 r
->SetExtraRequestHeaders(headers
);
851 EXPECT_TRUE(r
->is_pending());
853 base::RunLoop().Run();
854 EXPECT_TRUE(!r
->is_pending());
855 EXPECT_EQ(1, d
.response_started_count());
856 EXPECT_FALSE(d
.received_data_before_response());
857 EXPECT_EQ(static_cast<int>(content_length
), d
.bytes_received());
858 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
859 EXPECT_TRUE(partial_buffer_string
== d
.data_received());
862 EXPECT_TRUE(base::DeleteFile(temp_path
, false));
865 TEST_F(URLRequestTest
, FileTestHalfSpecifiedRange
) {
866 const size_t buffer_size
= 4000;
867 scoped_ptr
<char[]> buffer(new char[buffer_size
]);
868 FillBuffer(buffer
.get(), buffer_size
);
870 base::FilePath temp_path
;
871 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path
));
872 GURL temp_url
= FilePathToFileURL(temp_path
);
873 EXPECT_TRUE(base::WriteFile(temp_path
, buffer
.get(), buffer_size
));
876 EXPECT_TRUE(base::GetFileSize(temp_path
, &file_size
));
878 const size_t first_byte_position
= 500;
879 const size_t last_byte_position
= buffer_size
- 1;
880 const size_t content_length
= last_byte_position
- first_byte_position
+ 1;
881 std::string
partial_buffer_string(buffer
.get() + first_byte_position
,
882 buffer
.get() + last_byte_position
+ 1);
886 scoped_ptr
<URLRequest
> r(
887 default_context_
.CreateRequest(temp_url
, DEFAULT_PRIORITY
, &d
));
889 HttpRequestHeaders headers
;
890 headers
.SetHeader(HttpRequestHeaders::kRange
,
891 HttpByteRange::RightUnbounded(
892 first_byte_position
).GetHeaderValue());
893 r
->SetExtraRequestHeaders(headers
);
895 EXPECT_TRUE(r
->is_pending());
897 base::RunLoop().Run();
898 EXPECT_TRUE(!r
->is_pending());
899 EXPECT_EQ(1, d
.response_started_count());
900 EXPECT_FALSE(d
.received_data_before_response());
901 EXPECT_EQ(static_cast<int>(content_length
), d
.bytes_received());
902 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
903 EXPECT_TRUE(partial_buffer_string
== d
.data_received());
906 EXPECT_TRUE(base::DeleteFile(temp_path
, false));
909 TEST_F(URLRequestTest
, FileTestMultipleRanges
) {
910 const size_t buffer_size
= 400000;
911 scoped_ptr
<char[]> buffer(new char[buffer_size
]);
912 FillBuffer(buffer
.get(), buffer_size
);
914 base::FilePath temp_path
;
915 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path
));
916 GURL temp_url
= FilePathToFileURL(temp_path
);
917 EXPECT_TRUE(base::WriteFile(temp_path
, buffer
.get(), buffer_size
));
920 EXPECT_TRUE(base::GetFileSize(temp_path
, &file_size
));
924 scoped_ptr
<URLRequest
> r(
925 default_context_
.CreateRequest(temp_url
, DEFAULT_PRIORITY
, &d
));
927 HttpRequestHeaders headers
;
928 headers
.SetHeader(HttpRequestHeaders::kRange
, "bytes=0-0,10-200,200-300");
929 r
->SetExtraRequestHeaders(headers
);
931 EXPECT_TRUE(r
->is_pending());
933 base::RunLoop().Run();
934 EXPECT_TRUE(d
.request_failed());
937 EXPECT_TRUE(base::DeleteFile(temp_path
, false));
940 TEST_F(URLRequestTest
, AllowFileURLs
) {
941 base::ScopedTempDir temp_dir
;
942 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
943 base::FilePath test_file
;
944 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir
.path(), &test_file
));
945 std::string
test_data("monkey");
946 base::WriteFile(test_file
, test_data
.data(), test_data
.size());
947 GURL test_file_url
= FilePathToFileURL(test_file
);
951 TestNetworkDelegate network_delegate
;
952 network_delegate
.set_can_access_files(true);
953 default_context_
.set_network_delegate(&network_delegate
);
954 scoped_ptr
<URLRequest
> r(
955 default_context_
.CreateRequest(test_file_url
, DEFAULT_PRIORITY
, &d
));
957 base::RunLoop().Run();
958 EXPECT_FALSE(d
.request_failed());
959 EXPECT_EQ(test_data
, d
.data_received());
964 TestNetworkDelegate network_delegate
;
965 network_delegate
.set_can_access_files(false);
966 default_context_
.set_network_delegate(&network_delegate
);
967 scoped_ptr
<URLRequest
> r(
968 default_context_
.CreateRequest(test_file_url
, DEFAULT_PRIORITY
, &d
));
970 base::RunLoop().Run();
971 EXPECT_TRUE(d
.request_failed());
972 EXPECT_EQ("", d
.data_received());
977 TEST_F(URLRequestTest
, FileDirCancelTest
) {
978 // Put in mock resource provider.
979 NetModule::SetResourceProvider(TestNetResourceProvider
);
983 base::FilePath file_path
;
984 PathService::Get(base::DIR_SOURCE_ROOT
, &file_path
);
985 file_path
= file_path
.Append(FILE_PATH_LITERAL("net"));
986 file_path
= file_path
.Append(FILE_PATH_LITERAL("data"));
988 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
989 FilePathToFileURL(file_path
), DEFAULT_PRIORITY
, &d
));
991 EXPECT_TRUE(req
->is_pending());
993 d
.set_cancel_in_received_data_pending(true);
995 base::RunLoop().Run();
998 // Take out mock resource provider.
999 NetModule::SetResourceProvider(NULL
);
1002 TEST_F(URLRequestTest
, FileDirOutputSanity
) {
1003 // Verify the general sanity of the the output of the file:
1004 // directory lister by checking for the output of a known existing
1006 const char sentinel_name
[] = "filedir-sentinel";
1008 base::FilePath path
;
1009 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
1010 path
= path
.Append(kTestFilePath
);
1013 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
1014 FilePathToFileURL(path
), DEFAULT_PRIORITY
, &d
));
1016 base::RunLoop().Run();
1018 // Generate entry for the sentinel file.
1019 base::FilePath sentinel_path
= path
.AppendASCII(sentinel_name
);
1020 base::File::Info info
;
1021 EXPECT_TRUE(base::GetFileInfo(sentinel_path
, &info
));
1022 EXPECT_GT(info
.size
, 0);
1023 std::string sentinel_output
= GetDirectoryListingEntry(
1024 base::string16(sentinel_name
, sentinel_name
+ strlen(sentinel_name
)),
1025 std::string(sentinel_name
),
1028 info
.last_modified
);
1030 ASSERT_LT(0, d
.bytes_received());
1031 ASSERT_FALSE(d
.request_failed());
1032 ASSERT_TRUE(req
->status().is_success());
1033 // Check for the entry generated for the "sentinel" file.
1034 const std::string
& data
= d
.data_received();
1035 ASSERT_NE(data
.find(sentinel_output
), std::string::npos
);
1038 TEST_F(URLRequestTest
, FileDirRedirectNoCrash
) {
1039 // There is an implicit redirect when loading a file path that matches a
1040 // directory and does not end with a slash. Ensure that following such
1041 // redirects does not crash. See http://crbug.com/18686.
1043 base::FilePath path
;
1044 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
1045 path
= path
.Append(kTestFilePath
);
1048 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
1049 FilePathToFileURL(path
), DEFAULT_PRIORITY
, &d
));
1051 base::RunLoop().Run();
1053 ASSERT_EQ(1, d
.received_redirect_count());
1054 ASSERT_LT(0, d
.bytes_received());
1055 ASSERT_FALSE(d
.request_failed());
1056 ASSERT_TRUE(req
->status().is_success());
1060 // Don't accept the url "file:///" on windows. See http://crbug.com/1474.
1061 TEST_F(URLRequestTest
, FileDirRedirectSingleSlash
) {
1063 scoped_ptr
<URLRequest
> req(
1064 default_context_
.CreateRequest(GURL("file:///"), DEFAULT_PRIORITY
, &d
));
1066 base::RunLoop().Run();
1068 ASSERT_EQ(1, d
.received_redirect_count());
1069 ASSERT_FALSE(req
->status().is_success());
1071 #endif // defined(OS_WIN)
1073 #endif // !defined(DISABLE_FILE_SUPPORT)
1075 TEST_F(URLRequestTest
, InvalidUrlTest
) {
1078 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
1079 GURL("invalid url"), DEFAULT_PRIORITY
, &d
));
1082 EXPECT_TRUE(r
->is_pending());
1084 base::RunLoop().Run();
1085 EXPECT_TRUE(d
.request_failed());
1089 TEST_F(URLRequestTest
, InvalidReferrerTest
) {
1090 TestURLRequestContext context
;
1091 TestNetworkDelegate network_delegate
;
1092 network_delegate
.set_cancel_request_with_policy_violating_referrer(true);
1093 context
.set_network_delegate(&network_delegate
);
1095 scoped_ptr
<URLRequest
> req(
1096 context
.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY
, &d
));
1097 req
->SetReferrer("https://somewhere.com/");
1100 base::RunLoop().Run();
1101 EXPECT_TRUE(d
.request_failed());
1105 TEST_F(URLRequestTest
, ResolveShortcutTest
) {
1106 base::FilePath app_path
;
1107 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
1108 app_path
= app_path
.Append(kTestFilePath
);
1109 app_path
= app_path
.AppendASCII("with-headers.html");
1111 std::wstring lnk_path
= app_path
.value() + L
".lnk";
1113 base::win::ScopedCOMInitializer com_initializer
;
1115 // Temporarily create a shortcut for test
1117 base::win::ScopedComPtr
<IShellLink
> shell
;
1118 ASSERT_TRUE(SUCCEEDED(shell
.CreateInstance(CLSID_ShellLink
, NULL
,
1119 CLSCTX_INPROC_SERVER
)));
1120 base::win::ScopedComPtr
<IPersistFile
> persist
;
1121 ASSERT_TRUE(SUCCEEDED(shell
.QueryInterface(persist
.Receive())));
1122 EXPECT_TRUE(SUCCEEDED(shell
->SetPath(app_path
.value().c_str())));
1123 EXPECT_TRUE(SUCCEEDED(shell
->SetDescription(L
"ResolveShortcutTest")));
1124 EXPECT_TRUE(SUCCEEDED(persist
->Save(lnk_path
.c_str(), TRUE
)));
1129 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
1130 FilePathToFileURL(base::FilePath(lnk_path
)), DEFAULT_PRIORITY
, &d
));
1133 EXPECT_TRUE(r
->is_pending());
1135 base::RunLoop().Run();
1137 WIN32_FILE_ATTRIBUTE_DATA data
;
1138 GetFileAttributesEx(app_path
.value().c_str(),
1139 GetFileExInfoStandard
, &data
);
1140 HANDLE file
= CreateFile(app_path
.value().c_str(), GENERIC_READ
,
1141 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
1142 FILE_ATTRIBUTE_NORMAL
, NULL
);
1143 EXPECT_NE(INVALID_HANDLE_VALUE
, file
);
1144 scoped_ptr
<char[]> buffer(new char[data
.nFileSizeLow
]);
1147 result
= ReadFile(file
, buffer
.get(), data
.nFileSizeLow
,
1149 std::string
content(buffer
.get(), read_size
);
1152 EXPECT_TRUE(!r
->is_pending());
1153 EXPECT_EQ(1, d
.received_redirect_count());
1154 EXPECT_EQ(content
, d
.data_received());
1157 // Clean the shortcut
1158 DeleteFile(lnk_path
.c_str());
1160 #endif // defined(OS_WIN)
1162 // Custom URLRequestJobs for use with interceptor tests
1163 class RestartTestJob
: public URLRequestTestJob
{
1165 RestartTestJob(URLRequest
* request
, NetworkDelegate
* network_delegate
)
1166 : URLRequestTestJob(request
, network_delegate
, true) {}
1168 void StartAsync() override
{ this->NotifyRestartRequired(); }
1170 ~RestartTestJob() override
{}
1173 class CancelTestJob
: public URLRequestTestJob
{
1175 explicit CancelTestJob(URLRequest
* request
, NetworkDelegate
* network_delegate
)
1176 : URLRequestTestJob(request
, network_delegate
, true) {}
1178 void StartAsync() override
{ request_
->Cancel(); }
1180 ~CancelTestJob() override
{}
1183 class CancelThenRestartTestJob
: public URLRequestTestJob
{
1185 explicit CancelThenRestartTestJob(URLRequest
* request
,
1186 NetworkDelegate
* network_delegate
)
1187 : URLRequestTestJob(request
, network_delegate
, true) {
1190 void StartAsync() override
{
1192 this->NotifyRestartRequired();
1195 ~CancelThenRestartTestJob() override
{}
1198 // An Interceptor for use with interceptor tests.
1199 class MockURLRequestInterceptor
: public URLRequestInterceptor
{
1201 // Static getters for canned response header and data strings.
1202 static std::string
ok_data() {
1203 return URLRequestTestJob::test_data_1();
1206 static std::string
ok_headers() {
1207 return URLRequestTestJob::test_headers();
1210 static std::string
redirect_data() {
1211 return std::string();
1214 static std::string
redirect_headers() {
1215 return URLRequestTestJob::test_redirect_headers();
1218 static std::string
error_data() {
1219 return std::string("ohhh nooooo mr. bill!");
1222 static std::string
error_headers() {
1223 return URLRequestTestJob::test_error_headers();
1226 MockURLRequestInterceptor()
1227 : intercept_main_request_(false), restart_main_request_(false),
1228 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1229 simulate_main_network_error_(false),
1230 intercept_redirect_(false), cancel_redirect_request_(false),
1231 intercept_final_response_(false), cancel_final_request_(false),
1232 use_url_request_http_job_(false),
1233 did_intercept_main_(false), did_restart_main_(false),
1234 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1235 did_simulate_error_main_(false),
1236 did_intercept_redirect_(false), did_cancel_redirect_(false),
1237 did_intercept_final_(false), did_cancel_final_(false) {
1240 ~MockURLRequestInterceptor() override
{
1243 // URLRequestInterceptor implementation:
1244 URLRequestJob
* MaybeInterceptRequest(
1245 URLRequest
* request
,
1246 NetworkDelegate
* network_delegate
) const override
{
1247 if (restart_main_request_
) {
1248 restart_main_request_
= false;
1249 did_restart_main_
= true;
1250 return new RestartTestJob(request
, network_delegate
);
1252 if (cancel_main_request_
) {
1253 cancel_main_request_
= false;
1254 did_cancel_main_
= true;
1255 return new CancelTestJob(request
, network_delegate
);
1257 if (cancel_then_restart_main_request_
) {
1258 cancel_then_restart_main_request_
= false;
1259 did_cancel_then_restart_main_
= true;
1260 return new CancelThenRestartTestJob(request
, network_delegate
);
1262 if (simulate_main_network_error_
) {
1263 simulate_main_network_error_
= false;
1264 did_simulate_error_main_
= true;
1265 if (use_url_request_http_job_
) {
1266 return URLRequestHttpJob::Factory(request
, network_delegate
, "http");
1268 // This job will result in error since the requested URL is not one of the
1269 // URLs supported by these tests.
1270 return new URLRequestTestJob(request
, network_delegate
, true);
1272 if (!intercept_main_request_
)
1274 intercept_main_request_
= false;
1275 did_intercept_main_
= true;
1276 URLRequestTestJob
* job
= new URLRequestTestJob(request
,
1281 job
->set_load_timing_info(main_request_load_timing_info_
);
1285 URLRequestJob
* MaybeInterceptRedirect(URLRequest
* request
,
1286 NetworkDelegate
* network_delegate
,
1287 const GURL
& location
) const override
{
1288 if (cancel_redirect_request_
) {
1289 cancel_redirect_request_
= false;
1290 did_cancel_redirect_
= true;
1291 return new CancelTestJob(request
, network_delegate
);
1293 if (!intercept_redirect_
)
1295 intercept_redirect_
= false;
1296 did_intercept_redirect_
= true;
1297 if (use_url_request_http_job_
) {
1298 return URLRequestHttpJob::Factory(request
, network_delegate
, "http");
1300 return new URLRequestTestJob(request
,
1307 URLRequestJob
* MaybeInterceptResponse(
1308 URLRequest
* request
,
1309 NetworkDelegate
* network_delegate
) const override
{
1310 if (cancel_final_request_
) {
1311 cancel_final_request_
= false;
1312 did_cancel_final_
= true;
1313 return new CancelTestJob(request
, network_delegate
);
1315 if (!intercept_final_response_
)
1317 intercept_final_response_
= false;
1318 did_intercept_final_
= true;
1319 if (use_url_request_http_job_
) {
1320 return URLRequestHttpJob::Factory(request
, network_delegate
, "http");
1322 return new URLRequestTestJob(request
,
1329 void set_intercept_main_request(bool intercept_main_request
) {
1330 intercept_main_request_
= intercept_main_request
;
1333 void set_main_headers(const std::string
& main_headers
) {
1334 main_headers_
= main_headers
;
1337 void set_main_data(const std::string
& main_data
) {
1338 main_data_
= main_data
;
1341 void set_main_request_load_timing_info(
1342 const LoadTimingInfo
& main_request_load_timing_info
) {
1343 main_request_load_timing_info_
= main_request_load_timing_info
;
1346 void set_restart_main_request(bool restart_main_request
) {
1347 restart_main_request_
= restart_main_request
;
1350 void set_cancel_main_request(bool cancel_main_request
) {
1351 cancel_main_request_
= cancel_main_request
;
1354 void set_cancel_then_restart_main_request(
1355 bool cancel_then_restart_main_request
) {
1356 cancel_then_restart_main_request_
= cancel_then_restart_main_request
;
1359 void set_simulate_main_network_error(bool simulate_main_network_error
) {
1360 simulate_main_network_error_
= simulate_main_network_error
;
1363 void set_intercept_redirect(bool intercept_redirect
) {
1364 intercept_redirect_
= intercept_redirect
;
1367 void set_redirect_headers(const std::string
& redirect_headers
) {
1368 redirect_headers_
= redirect_headers
;
1371 void set_redirect_data(const std::string
& redirect_data
) {
1372 redirect_data_
= redirect_data
;
1375 void set_cancel_redirect_request(bool cancel_redirect_request
) {
1376 cancel_redirect_request_
= cancel_redirect_request
;
1379 void set_intercept_final_response(bool intercept_final_response
) {
1380 intercept_final_response_
= intercept_final_response
;
1383 void set_final_headers(const std::string
& final_headers
) {
1384 final_headers_
= final_headers
;
1387 void set_final_data(const std::string
& final_data
) {
1388 final_data_
= final_data
;
1391 void set_cancel_final_request(bool cancel_final_request
) {
1392 cancel_final_request_
= cancel_final_request
;
1395 void set_use_url_request_http_job(bool use_url_request_http_job
) {
1396 use_url_request_http_job_
= use_url_request_http_job
;
1399 bool did_intercept_main() const {
1400 return did_intercept_main_
;
1403 bool did_restart_main() const {
1404 return did_restart_main_
;
1407 bool did_cancel_main() const {
1408 return did_cancel_main_
;
1411 bool did_cancel_then_restart_main() const {
1412 return did_cancel_then_restart_main_
;
1415 bool did_simulate_error_main() const {
1416 return did_simulate_error_main_
;
1419 bool did_intercept_redirect() const {
1420 return did_intercept_redirect_
;
1423 bool did_cancel_redirect() const {
1424 return did_cancel_redirect_
;
1427 bool did_intercept_final() const {
1428 return did_intercept_final_
;
1431 bool did_cancel_final() const {
1432 return did_cancel_final_
;
1436 // Indicate whether to intercept the main request, and if so specify the
1437 // response to return and the LoadTimingInfo to use.
1438 mutable bool intercept_main_request_
;
1439 mutable std::string main_headers_
;
1440 mutable std::string main_data_
;
1441 mutable LoadTimingInfo main_request_load_timing_info_
;
1443 // These indicate actions that can be taken within MaybeInterceptRequest.
1444 mutable bool restart_main_request_
;
1445 mutable bool cancel_main_request_
;
1446 mutable bool cancel_then_restart_main_request_
;
1447 mutable bool simulate_main_network_error_
;
1449 // Indicate whether to intercept redirects, and if so specify the response to
1451 mutable bool intercept_redirect_
;
1452 mutable std::string redirect_headers_
;
1453 mutable std::string redirect_data_
;
1455 // Cancel the request within MaybeInterceptRedirect.
1456 mutable bool cancel_redirect_request_
;
1458 // Indicate whether to intercept the final response, and if so specify the
1459 // response to return.
1460 mutable bool intercept_final_response_
;
1461 mutable std::string final_headers_
;
1462 mutable std::string final_data_
;
1464 // Cancel the final request within MaybeInterceptResponse.
1465 mutable bool cancel_final_request_
;
1467 // Instruct the interceptor to use a real URLRequestHTTPJob.
1468 mutable bool use_url_request_http_job_
;
1470 // These indicate if the interceptor did something or not.
1471 mutable bool did_intercept_main_
;
1472 mutable bool did_restart_main_
;
1473 mutable bool did_cancel_main_
;
1474 mutable bool did_cancel_then_restart_main_
;
1475 mutable bool did_simulate_error_main_
;
1476 mutable bool did_intercept_redirect_
;
1477 mutable bool did_cancel_redirect_
;
1478 mutable bool did_intercept_final_
;
1479 mutable bool did_cancel_final_
;
1482 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.
1483 class URLRequestInterceptorTest
: public URLRequestTest
{
1485 URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL
) {
1488 ~URLRequestInterceptorTest() override
{
1489 // URLRequestJobs may post clean-up tasks on destruction.
1490 base::RunLoop().RunUntilIdle();
1493 void SetUpFactory() override
{
1494 interceptor_
= new MockURLRequestInterceptor();
1495 job_factory_
.reset(new URLRequestInterceptingJobFactory(
1496 job_factory_
.Pass(), make_scoped_ptr(interceptor_
)));
1499 MockURLRequestInterceptor
* interceptor() const {
1500 return interceptor_
;
1504 MockURLRequestInterceptor
* interceptor_
;
1507 TEST_F(URLRequestInterceptorTest
, Intercept
) {
1508 // Intercept the main request and respond with a simple response.
1509 interceptor()->set_intercept_main_request(true);
1510 interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers());
1511 interceptor()->set_main_data(MockURLRequestInterceptor::ok_data());
1513 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1514 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1515 base::SupportsUserData::Data
* user_data0
= new base::SupportsUserData::Data();
1516 base::SupportsUserData::Data
* user_data1
= new base::SupportsUserData::Data();
1517 base::SupportsUserData::Data
* user_data2
= new base::SupportsUserData::Data();
1518 req
->SetUserData(nullptr, user_data0
);
1519 req
->SetUserData(&user_data1
, user_data1
);
1520 req
->SetUserData(&user_data2
, user_data2
);
1521 req
->set_method("GET");
1523 base::RunLoop().Run();
1525 // Make sure we can retrieve our specific user data.
1526 EXPECT_EQ(user_data0
, req
->GetUserData(nullptr));
1527 EXPECT_EQ(user_data1
, req
->GetUserData(&user_data1
));
1528 EXPECT_EQ(user_data2
, req
->GetUserData(&user_data2
));
1530 // Check that we got one good response.
1531 EXPECT_TRUE(req
->status().is_success());
1532 EXPECT_EQ(200, req
->response_headers()->response_code());
1533 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d
.data_received());
1534 EXPECT_EQ(1, d
.response_started_count());
1535 EXPECT_EQ(0, d
.received_redirect_count());
1538 TEST_F(URLRequestInterceptorTest
, InterceptRedirect
) {
1539 // Intercept the main request and respond with a redirect.
1540 interceptor()->set_intercept_main_request(true);
1541 interceptor()->set_main_headers(
1542 MockURLRequestInterceptor::redirect_headers());
1543 interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data());
1545 // Intercept that redirect and respond with a final OK response.
1546 interceptor()->set_intercept_redirect(true);
1547 interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers());
1548 interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data());
1551 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1552 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1553 req
->set_method("GET");
1555 base::RunLoop().Run();
1557 // Check that the interceptor got called as expected.
1558 EXPECT_TRUE(interceptor()->did_intercept_main());
1559 EXPECT_TRUE(interceptor()->did_intercept_redirect());
1561 // Check that we got one good response.
1562 EXPECT_TRUE(req
->status().is_success());
1563 if (req
->status().is_success())
1564 EXPECT_EQ(200, req
->response_headers()->response_code());
1566 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d
.data_received());
1567 EXPECT_EQ(1, d
.response_started_count());
1568 EXPECT_EQ(0, d
.received_redirect_count());
1571 TEST_F(URLRequestInterceptorTest
, InterceptServerError
) {
1572 // Intercept the main request to generate a server error response.
1573 interceptor()->set_intercept_main_request(true);
1574 interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers());
1575 interceptor()->set_main_data(MockURLRequestInterceptor::error_data());
1577 // Intercept that error and respond with an OK response.
1578 interceptor()->set_intercept_final_response(true);
1579 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1580 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1583 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1584 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1585 req
->set_method("GET");
1587 base::RunLoop().Run();
1589 // Check that the interceptor got called as expected.
1590 EXPECT_TRUE(interceptor()->did_intercept_main());
1591 EXPECT_TRUE(interceptor()->did_intercept_final());
1593 // Check that we got one good response.
1594 EXPECT_TRUE(req
->status().is_success());
1595 EXPECT_EQ(200, req
->response_headers()->response_code());
1596 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d
.data_received());
1597 EXPECT_EQ(1, d
.response_started_count());
1598 EXPECT_EQ(0, d
.received_redirect_count());
1601 TEST_F(URLRequestInterceptorTest
, InterceptNetworkError
) {
1602 // Intercept the main request to simulate a network error.
1603 interceptor()->set_simulate_main_network_error(true);
1605 // Intercept that error and respond with an OK response.
1606 interceptor()->set_intercept_final_response(true);
1607 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1608 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1611 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1612 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1613 req
->set_method("GET");
1615 base::RunLoop().Run();
1617 // Check that the interceptor got called as expected.
1618 EXPECT_TRUE(interceptor()->did_simulate_error_main());
1619 EXPECT_TRUE(interceptor()->did_intercept_final());
1621 // Check that we received one good response.
1622 EXPECT_TRUE(req
->status().is_success());
1623 EXPECT_EQ(200, req
->response_headers()->response_code());
1624 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d
.data_received());
1625 EXPECT_EQ(1, d
.response_started_count());
1626 EXPECT_EQ(0, d
.received_redirect_count());
1629 TEST_F(URLRequestInterceptorTest
, InterceptRestartRequired
) {
1630 // Restart the main request.
1631 interceptor()->set_restart_main_request(true);
1633 // then intercept the new main request and respond with an OK response
1634 interceptor()->set_intercept_main_request(true);
1635 interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers());
1636 interceptor()->set_main_data(MockURLRequestInterceptor::ok_data());
1639 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1640 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1641 req
->set_method("GET");
1643 base::RunLoop().Run();
1645 // Check that the interceptor got called as expected.
1646 EXPECT_TRUE(interceptor()->did_restart_main());
1647 EXPECT_TRUE(interceptor()->did_intercept_main());
1649 // Check that we received one good response.
1650 EXPECT_TRUE(req
->status().is_success());
1651 if (req
->status().is_success())
1652 EXPECT_EQ(200, req
->response_headers()->response_code());
1654 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d
.data_received());
1655 EXPECT_EQ(1, d
.response_started_count());
1656 EXPECT_EQ(0, d
.received_redirect_count());
1659 TEST_F(URLRequestInterceptorTest
, InterceptRespectsCancelMain
) {
1660 // Intercept the main request and cancel from within the restarted job.
1661 interceptor()->set_cancel_main_request(true);
1663 // Set up to intercept the final response and override it with an OK response.
1664 interceptor()->set_intercept_final_response(true);
1665 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1666 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1669 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1670 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1671 req
->set_method("GET");
1673 base::RunLoop().Run();
1675 // Check that the interceptor got called as expected.
1676 EXPECT_TRUE(interceptor()->did_cancel_main());
1677 EXPECT_FALSE(interceptor()->did_intercept_final());
1679 // Check that we see a canceled request.
1680 EXPECT_FALSE(req
->status().is_success());
1681 EXPECT_EQ(URLRequestStatus::CANCELED
, req
->status().status());
1684 TEST_F(URLRequestInterceptorTest
, InterceptRespectsCancelRedirect
) {
1685 // Intercept the main request and respond with a redirect.
1686 interceptor()->set_intercept_main_request(true);
1687 interceptor()->set_main_headers(
1688 MockURLRequestInterceptor::redirect_headers());
1689 interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data());
1691 // Intercept the redirect and cancel from within that job.
1692 interceptor()->set_cancel_redirect_request(true);
1694 // Set up to intercept the final response and override it with an OK response.
1695 interceptor()->set_intercept_final_response(true);
1696 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1697 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1700 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1701 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1702 req
->set_method("GET");
1704 base::RunLoop().Run();
1706 // Check that the interceptor got called as expected.
1707 EXPECT_TRUE(interceptor()->did_intercept_main());
1708 EXPECT_TRUE(interceptor()->did_cancel_redirect());
1709 EXPECT_FALSE(interceptor()->did_intercept_final());
1711 // Check that we see a canceled request.
1712 EXPECT_FALSE(req
->status().is_success());
1713 EXPECT_EQ(URLRequestStatus::CANCELED
, req
->status().status());
1716 TEST_F(URLRequestInterceptorTest
, InterceptRespectsCancelFinal
) {
1717 // Intercept the main request to simulate a network error.
1718 interceptor()->set_simulate_main_network_error(true);
1720 // Set up to intercept final the response and cancel from within that job.
1721 interceptor()->set_cancel_final_request(true);
1724 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1725 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1726 req
->set_method("GET");
1728 base::RunLoop().Run();
1730 // Check that the interceptor got called as expected.
1731 EXPECT_TRUE(interceptor()->did_simulate_error_main());
1732 EXPECT_TRUE(interceptor()->did_cancel_final());
1734 // Check that we see a canceled request.
1735 EXPECT_FALSE(req
->status().is_success());
1736 EXPECT_EQ(URLRequestStatus::CANCELED
, req
->status().status());
1739 TEST_F(URLRequestInterceptorTest
, InterceptRespectsCancelInRestart
) {
1740 // Intercept the main request and cancel then restart from within that job.
1741 interceptor()->set_cancel_then_restart_main_request(true);
1743 // Set up to intercept the final response and override it with an OK response.
1744 interceptor()->set_intercept_final_response(true);
1745 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1746 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1749 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
1750 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1751 req
->set_method("GET");
1753 base::RunLoop().Run();
1755 // Check that the interceptor got called as expected.
1756 EXPECT_TRUE(interceptor()->did_cancel_then_restart_main());
1757 EXPECT_FALSE(interceptor()->did_intercept_final());
1759 // Check that we see a canceled request.
1760 EXPECT_FALSE(req
->status().is_success());
1761 EXPECT_EQ(URLRequestStatus::CANCELED
, req
->status().status());
1764 // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1765 // reused. |connect_time_flags| is used to indicate if there should be dns
1766 // or SSL times, and |used_proxy| is used for proxy times.
1767 LoadTimingInfo
NormalLoadTimingInfo(base::TimeTicks now
,
1768 int connect_time_flags
,
1770 LoadTimingInfo load_timing
;
1771 load_timing
.socket_log_id
= 1;
1774 load_timing
.proxy_resolve_start
= now
+ base::TimeDelta::FromDays(1);
1775 load_timing
.proxy_resolve_end
= now
+ base::TimeDelta::FromDays(2);
1778 LoadTimingInfo::ConnectTiming
& connect_timing
= load_timing
.connect_timing
;
1779 if (connect_time_flags
& CONNECT_TIMING_HAS_DNS_TIMES
) {
1780 connect_timing
.dns_start
= now
+ base::TimeDelta::FromDays(3);
1781 connect_timing
.dns_end
= now
+ base::TimeDelta::FromDays(4);
1783 connect_timing
.connect_start
= now
+ base::TimeDelta::FromDays(5);
1784 if (connect_time_flags
& CONNECT_TIMING_HAS_SSL_TIMES
) {
1785 connect_timing
.ssl_start
= now
+ base::TimeDelta::FromDays(6);
1786 connect_timing
.ssl_end
= now
+ base::TimeDelta::FromDays(7);
1788 connect_timing
.connect_end
= now
+ base::TimeDelta::FromDays(8);
1790 load_timing
.send_start
= now
+ base::TimeDelta::FromDays(9);
1791 load_timing
.send_end
= now
+ base::TimeDelta::FromDays(10);
1792 load_timing
.receive_headers_end
= now
+ base::TimeDelta::FromDays(11);
1796 // Same as above, but in the case of a reused socket.
1797 LoadTimingInfo
NormalLoadTimingInfoReused(base::TimeTicks now
,
1799 LoadTimingInfo load_timing
;
1800 load_timing
.socket_log_id
= 1;
1801 load_timing
.socket_reused
= true;
1804 load_timing
.proxy_resolve_start
= now
+ base::TimeDelta::FromDays(1);
1805 load_timing
.proxy_resolve_end
= now
+ base::TimeDelta::FromDays(2);
1808 load_timing
.send_start
= now
+ base::TimeDelta::FromDays(9);
1809 load_timing
.send_end
= now
+ base::TimeDelta::FromDays(10);
1810 load_timing
.receive_headers_end
= now
+ base::TimeDelta::FromDays(11);
1814 LoadTimingInfo
RunURLRequestInterceptorLoadTimingTest(
1815 const LoadTimingInfo
& job_load_timing
,
1816 const URLRequestContext
& context
,
1817 MockURLRequestInterceptor
* interceptor
) {
1818 interceptor
->set_intercept_main_request(true);
1819 interceptor
->set_main_request_load_timing_info(job_load_timing
);
1821 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
1822 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
1824 base::RunLoop().Run();
1826 LoadTimingInfo resulting_load_timing
;
1827 req
->GetLoadTimingInfo(&resulting_load_timing
);
1829 // None of these should be modified by the URLRequest.
1830 EXPECT_EQ(job_load_timing
.socket_reused
, resulting_load_timing
.socket_reused
);
1831 EXPECT_EQ(job_load_timing
.socket_log_id
, resulting_load_timing
.socket_log_id
);
1832 EXPECT_EQ(job_load_timing
.send_start
, resulting_load_timing
.send_start
);
1833 EXPECT_EQ(job_load_timing
.send_end
, resulting_load_timing
.send_end
);
1834 EXPECT_EQ(job_load_timing
.receive_headers_end
,
1835 resulting_load_timing
.receive_headers_end
);
1837 return resulting_load_timing
;
1840 // Basic test that the intercept + load timing tests work.
1841 TEST_F(URLRequestInterceptorTest
, InterceptLoadTiming
) {
1842 base::TimeTicks now
= base::TimeTicks::Now();
1843 LoadTimingInfo job_load_timing
=
1844 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_DNS_TIMES
, false);
1846 LoadTimingInfo load_timing_result
=
1847 RunURLRequestInterceptorLoadTimingTest(
1848 job_load_timing
, default_context(), interceptor());
1850 // Nothing should have been changed by the URLRequest.
1851 EXPECT_EQ(job_load_timing
.proxy_resolve_start
,
1852 load_timing_result
.proxy_resolve_start
);
1853 EXPECT_EQ(job_load_timing
.proxy_resolve_end
,
1854 load_timing_result
.proxy_resolve_end
);
1855 EXPECT_EQ(job_load_timing
.connect_timing
.dns_start
,
1856 load_timing_result
.connect_timing
.dns_start
);
1857 EXPECT_EQ(job_load_timing
.connect_timing
.dns_end
,
1858 load_timing_result
.connect_timing
.dns_end
);
1859 EXPECT_EQ(job_load_timing
.connect_timing
.connect_start
,
1860 load_timing_result
.connect_timing
.connect_start
);
1861 EXPECT_EQ(job_load_timing
.connect_timing
.connect_end
,
1862 load_timing_result
.connect_timing
.connect_end
);
1863 EXPECT_EQ(job_load_timing
.connect_timing
.ssl_start
,
1864 load_timing_result
.connect_timing
.ssl_start
);
1865 EXPECT_EQ(job_load_timing
.connect_timing
.ssl_end
,
1866 load_timing_result
.connect_timing
.ssl_end
);
1868 // Redundant sanity check.
1869 TestLoadTimingNotReused(load_timing_result
, CONNECT_TIMING_HAS_DNS_TIMES
);
1872 // Another basic test, with proxy and SSL times, but no DNS times.
1873 TEST_F(URLRequestInterceptorTest
, InterceptLoadTimingProxy
) {
1874 base::TimeTicks now
= base::TimeTicks::Now();
1875 LoadTimingInfo job_load_timing
=
1876 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_SSL_TIMES
, true);
1878 LoadTimingInfo load_timing_result
=
1879 RunURLRequestInterceptorLoadTimingTest(
1880 job_load_timing
, default_context(), interceptor());
1882 // Nothing should have been changed by the URLRequest.
1883 EXPECT_EQ(job_load_timing
.proxy_resolve_start
,
1884 load_timing_result
.proxy_resolve_start
);
1885 EXPECT_EQ(job_load_timing
.proxy_resolve_end
,
1886 load_timing_result
.proxy_resolve_end
);
1887 EXPECT_EQ(job_load_timing
.connect_timing
.dns_start
,
1888 load_timing_result
.connect_timing
.dns_start
);
1889 EXPECT_EQ(job_load_timing
.connect_timing
.dns_end
,
1890 load_timing_result
.connect_timing
.dns_end
);
1891 EXPECT_EQ(job_load_timing
.connect_timing
.connect_start
,
1892 load_timing_result
.connect_timing
.connect_start
);
1893 EXPECT_EQ(job_load_timing
.connect_timing
.connect_end
,
1894 load_timing_result
.connect_timing
.connect_end
);
1895 EXPECT_EQ(job_load_timing
.connect_timing
.ssl_start
,
1896 load_timing_result
.connect_timing
.ssl_start
);
1897 EXPECT_EQ(job_load_timing
.connect_timing
.ssl_end
,
1898 load_timing_result
.connect_timing
.ssl_end
);
1900 // Redundant sanity check.
1901 TestLoadTimingNotReusedWithProxy(load_timing_result
,
1902 CONNECT_TIMING_HAS_SSL_TIMES
);
1905 // Make sure that URLRequest correctly adjusts proxy times when they're before
1906 // |request_start|, due to already having a connected socket. This happens in
1907 // the case of reusing a SPDY session. The connected socket is not considered
1908 // reused in this test (May be a preconnect).
1910 // To mix things up from the test above, assumes DNS times but no SSL times.
1911 TEST_F(URLRequestInterceptorTest
, InterceptLoadTimingEarlyProxyResolution
) {
1912 base::TimeTicks now
= base::TimeTicks::Now();
1913 LoadTimingInfo job_load_timing
=
1914 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_DNS_TIMES
, true);
1915 job_load_timing
.proxy_resolve_start
= now
- base::TimeDelta::FromDays(6);
1916 job_load_timing
.proxy_resolve_end
= now
- base::TimeDelta::FromDays(5);
1917 job_load_timing
.connect_timing
.dns_start
= now
- base::TimeDelta::FromDays(4);
1918 job_load_timing
.connect_timing
.dns_end
= now
- base::TimeDelta::FromDays(3);
1919 job_load_timing
.connect_timing
.connect_start
=
1920 now
- base::TimeDelta::FromDays(2);
1921 job_load_timing
.connect_timing
.connect_end
=
1922 now
- base::TimeDelta::FromDays(1);
1924 LoadTimingInfo load_timing_result
=
1925 RunURLRequestInterceptorLoadTimingTest(
1926 job_load_timing
, default_context(), interceptor());
1928 // Proxy times, connect times, and DNS times should all be replaced with
1930 EXPECT_EQ(load_timing_result
.request_start
,
1931 load_timing_result
.proxy_resolve_start
);
1932 EXPECT_EQ(load_timing_result
.request_start
,
1933 load_timing_result
.proxy_resolve_end
);
1934 EXPECT_EQ(load_timing_result
.request_start
,
1935 load_timing_result
.connect_timing
.dns_start
);
1936 EXPECT_EQ(load_timing_result
.request_start
,
1937 load_timing_result
.connect_timing
.dns_end
);
1938 EXPECT_EQ(load_timing_result
.request_start
,
1939 load_timing_result
.connect_timing
.connect_start
);
1940 EXPECT_EQ(load_timing_result
.request_start
,
1941 load_timing_result
.connect_timing
.connect_end
);
1943 // Other times should have been left null.
1944 TestLoadTimingNotReusedWithProxy(load_timing_result
,
1945 CONNECT_TIMING_HAS_DNS_TIMES
);
1948 // Same as above, but in the reused case.
1949 TEST_F(URLRequestInterceptorTest
,
1950 InterceptLoadTimingEarlyProxyResolutionReused
) {
1951 base::TimeTicks now
= base::TimeTicks::Now();
1952 LoadTimingInfo job_load_timing
= NormalLoadTimingInfoReused(now
, true);
1953 job_load_timing
.proxy_resolve_start
= now
- base::TimeDelta::FromDays(4);
1954 job_load_timing
.proxy_resolve_end
= now
- base::TimeDelta::FromDays(3);
1956 LoadTimingInfo load_timing_result
=
1957 RunURLRequestInterceptorLoadTimingTest(
1958 job_load_timing
, default_context(), interceptor());
1960 // Proxy times and connect times should all be replaced with request_start.
1961 EXPECT_EQ(load_timing_result
.request_start
,
1962 load_timing_result
.proxy_resolve_start
);
1963 EXPECT_EQ(load_timing_result
.request_start
,
1964 load_timing_result
.proxy_resolve_end
);
1966 // Other times should have been left null.
1967 TestLoadTimingReusedWithProxy(load_timing_result
);
1970 // Make sure that URLRequest correctly adjusts connect times when they're before
1971 // |request_start|, due to reusing a connected socket. The connected socket is
1972 // not considered reused in this test (May be a preconnect).
1974 // To mix things up, the request has SSL times, but no DNS times.
1975 TEST_F(URLRequestInterceptorTest
, InterceptLoadTimingEarlyConnect
) {
1976 base::TimeTicks now
= base::TimeTicks::Now();
1977 LoadTimingInfo job_load_timing
=
1978 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_SSL_TIMES
, false);
1979 job_load_timing
.connect_timing
.connect_start
=
1980 now
- base::TimeDelta::FromDays(1);
1981 job_load_timing
.connect_timing
.ssl_start
= now
- base::TimeDelta::FromDays(2);
1982 job_load_timing
.connect_timing
.ssl_end
= now
- base::TimeDelta::FromDays(3);
1983 job_load_timing
.connect_timing
.connect_end
=
1984 now
- base::TimeDelta::FromDays(4);
1986 LoadTimingInfo load_timing_result
=
1987 RunURLRequestInterceptorLoadTimingTest(
1988 job_load_timing
, default_context(), interceptor());
1990 // Connect times, and SSL times should be replaced with request_start.
1991 EXPECT_EQ(load_timing_result
.request_start
,
1992 load_timing_result
.connect_timing
.connect_start
);
1993 EXPECT_EQ(load_timing_result
.request_start
,
1994 load_timing_result
.connect_timing
.ssl_start
);
1995 EXPECT_EQ(load_timing_result
.request_start
,
1996 load_timing_result
.connect_timing
.ssl_end
);
1997 EXPECT_EQ(load_timing_result
.request_start
,
1998 load_timing_result
.connect_timing
.connect_end
);
2000 // Other times should have been left null.
2001 TestLoadTimingNotReused(load_timing_result
, CONNECT_TIMING_HAS_SSL_TIMES
);
2004 // Make sure that URLRequest correctly adjusts connect times when they're before
2005 // |request_start|, due to reusing a connected socket in the case that there
2006 // are also proxy times. The connected socket is not considered reused in this
2007 // test (May be a preconnect).
2009 // In this test, there are no SSL or DNS times.
2010 TEST_F(URLRequestInterceptorTest
, InterceptLoadTimingEarlyConnectWithProxy
) {
2011 base::TimeTicks now
= base::TimeTicks::Now();
2012 LoadTimingInfo job_load_timing
=
2013 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY
, true);
2014 job_load_timing
.connect_timing
.connect_start
=
2015 now
- base::TimeDelta::FromDays(1);
2016 job_load_timing
.connect_timing
.connect_end
=
2017 now
- base::TimeDelta::FromDays(2);
2019 LoadTimingInfo load_timing_result
=
2020 RunURLRequestInterceptorLoadTimingTest(
2021 job_load_timing
, default_context(), interceptor());
2023 // Connect times should be replaced with proxy_resolve_end.
2024 EXPECT_EQ(load_timing_result
.proxy_resolve_end
,
2025 load_timing_result
.connect_timing
.connect_start
);
2026 EXPECT_EQ(load_timing_result
.proxy_resolve_end
,
2027 load_timing_result
.connect_timing
.connect_end
);
2029 // Other times should have been left null.
2030 TestLoadTimingNotReusedWithProxy(load_timing_result
,
2031 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY
);
2034 // Check that two different URL requests have different identifiers.
2035 TEST_F(URLRequestTest
, Identifiers
) {
2037 TestURLRequestContext context
;
2038 scoped_ptr
<URLRequest
> req(
2039 context
.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY
, &d
));
2040 scoped_ptr
<URLRequest
> other_req(
2041 context
.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY
, &d
));
2043 ASSERT_NE(req
->identifier(), other_req
->identifier());
2046 // Check that a failure to connect to the proxy is reported to the network
2048 TEST_F(URLRequestTest
, NetworkDelegateProxyError
) {
2049 MockHostResolver host_resolver
;
2050 host_resolver
.rules()->AddSimulatedFailure("*");
2052 TestNetworkDelegate network_delegate
; // Must outlive URLRequests.
2053 TestURLRequestContextWithProxy
context("myproxy:70", &network_delegate
);
2056 scoped_ptr
<URLRequest
> req(
2057 context
.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY
, &d
));
2058 req
->set_method("GET");
2061 base::RunLoop().Run();
2063 // Check we see a failed request.
2064 EXPECT_FALSE(req
->status().is_success());
2065 // The proxy server is not set before failure.
2066 EXPECT_TRUE(req
->proxy_server().IsEmpty());
2067 EXPECT_EQ(URLRequestStatus::FAILED
, req
->status().status());
2068 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED
, req
->status().error());
2070 EXPECT_EQ(1, network_delegate
.error_count());
2071 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED
, network_delegate
.last_error());
2072 EXPECT_EQ(1, network_delegate
.completed_requests());
2075 // Make sure that NetworkDelegate::NotifyCompleted is called if
2076 // content is empty.
2077 TEST_F(URLRequestTest
, RequestCompletionForEmptyResponse
) {
2079 scoped_ptr
<URLRequest
> req(
2080 default_context_
.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY
, &d
));
2082 base::RunLoop().Run();
2083 EXPECT_EQ("", d
.data_received());
2084 EXPECT_EQ(1, default_network_delegate_
.completed_requests());
2087 // Make sure that SetPriority actually sets the URLRequest's priority
2088 // correctly, both before and after start.
2089 TEST_F(URLRequestTest
, SetPriorityBasic
) {
2091 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2092 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
2093 EXPECT_EQ(DEFAULT_PRIORITY
, req
->priority());
2095 req
->SetPriority(LOW
);
2096 EXPECT_EQ(LOW
, req
->priority());
2099 EXPECT_EQ(LOW
, req
->priority());
2101 req
->SetPriority(MEDIUM
);
2102 EXPECT_EQ(MEDIUM
, req
->priority());
2105 // Make sure that URLRequest calls SetPriority on a job before calling
2107 TEST_F(URLRequestTest
, SetJobPriorityBeforeJobStart
) {
2109 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2110 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
2111 EXPECT_EQ(DEFAULT_PRIORITY
, req
->priority());
2113 scoped_refptr
<URLRequestTestJob
> job
=
2114 new URLRequestTestJob(req
.get(), &default_network_delegate_
);
2115 AddTestInterceptor()->set_main_intercept_job(job
.get());
2116 EXPECT_EQ(DEFAULT_PRIORITY
, job
->priority());
2118 req
->SetPriority(LOW
);
2121 EXPECT_EQ(LOW
, job
->priority());
2124 // Make sure that URLRequest passes on its priority updates to its
2126 TEST_F(URLRequestTest
, SetJobPriority
) {
2128 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2129 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
2131 scoped_refptr
<URLRequestTestJob
> job
=
2132 new URLRequestTestJob(req
.get(), &default_network_delegate_
);
2133 AddTestInterceptor()->set_main_intercept_job(job
.get());
2135 req
->SetPriority(LOW
);
2137 EXPECT_EQ(LOW
, job
->priority());
2139 req
->SetPriority(MEDIUM
);
2140 EXPECT_EQ(MEDIUM
, req
->priority());
2141 EXPECT_EQ(MEDIUM
, job
->priority());
2144 // Setting the IGNORE_LIMITS load flag should be okay if the priority
2145 // is MAXIMUM_PRIORITY.
2146 TEST_F(URLRequestTest
, PriorityIgnoreLimits
) {
2148 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2149 GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY
, &d
));
2150 EXPECT_EQ(MAXIMUM_PRIORITY
, req
->priority());
2152 scoped_refptr
<URLRequestTestJob
> job
=
2153 new URLRequestTestJob(req
.get(), &default_network_delegate_
);
2154 AddTestInterceptor()->set_main_intercept_job(job
.get());
2156 req
->SetLoadFlags(LOAD_IGNORE_LIMITS
);
2157 EXPECT_EQ(MAXIMUM_PRIORITY
, req
->priority());
2159 req
->SetPriority(MAXIMUM_PRIORITY
);
2160 EXPECT_EQ(MAXIMUM_PRIORITY
, req
->priority());
2163 EXPECT_EQ(MAXIMUM_PRIORITY
, req
->priority());
2164 EXPECT_EQ(MAXIMUM_PRIORITY
, job
->priority());
2167 // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
2168 #if !defined(OS_IOS)
2171 // Less verbose way of running a simple testserver for the tests below.
2172 class LocalHttpTestServer
: public SpawnedTestServer
{
2174 explicit LocalHttpTestServer(const base::FilePath
& document_root
)
2175 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
2176 SpawnedTestServer::kLocalhost
,
2178 LocalHttpTestServer()
2179 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
2180 SpawnedTestServer::kLocalhost
,
2181 base::FilePath()) {}
2186 TEST_F(URLRequestTest
, DelayedCookieCallback
) {
2187 LocalHttpTestServer test_server
;
2188 ASSERT_TRUE(test_server
.Start());
2190 TestURLRequestContext context
;
2191 scoped_refptr
<DelayedCookieMonster
> delayed_cm
=
2192 new DelayedCookieMonster();
2193 scoped_refptr
<CookieStore
> cookie_store
= delayed_cm
;
2194 context
.set_cookie_store(delayed_cm
.get());
2198 TestNetworkDelegate network_delegate
;
2199 context
.set_network_delegate(&network_delegate
);
2201 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
2202 test_server
.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY
,
2205 base::RunLoop().Run();
2206 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2207 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2208 EXPECT_EQ(1, network_delegate
.set_cookie_count());
2211 // Verify that the cookie is set.
2213 TestNetworkDelegate network_delegate
;
2214 context
.set_network_delegate(&network_delegate
);
2216 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
2217 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2219 base::RunLoop().Run();
2221 EXPECT_TRUE(d
.data_received().find("CookieToNotSend=1")
2222 != std::string::npos
);
2223 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2224 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2228 TEST_F(URLRequestTest
, DoNotSendCookies
) {
2229 LocalHttpTestServer test_server
;
2230 ASSERT_TRUE(test_server
.Start());
2234 TestNetworkDelegate network_delegate
;
2235 default_context_
.set_network_delegate(&network_delegate
);
2237 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2238 test_server
.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY
,
2241 base::RunLoop().Run();
2242 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2243 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2246 // Verify that the cookie is set.
2248 TestNetworkDelegate network_delegate
;
2249 default_context_
.set_network_delegate(&network_delegate
);
2251 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2252 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2254 base::RunLoop().Run();
2256 EXPECT_TRUE(d
.data_received().find("CookieToNotSend=1")
2257 != std::string::npos
);
2258 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2259 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2262 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2264 TestNetworkDelegate network_delegate
;
2265 default_context_
.set_network_delegate(&network_delegate
);
2267 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2268 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2269 req
->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES
);
2271 base::RunLoop().Run();
2273 EXPECT_TRUE(d
.data_received().find("Cookie: CookieToNotSend=1")
2274 == std::string::npos
);
2276 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2277 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2278 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2282 TEST_F(URLRequestTest
, DoNotSaveCookies
) {
2283 LocalHttpTestServer test_server
;
2284 ASSERT_TRUE(test_server
.Start());
2288 TestNetworkDelegate network_delegate
;
2289 default_context_
.set_network_delegate(&network_delegate
);
2291 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2292 test_server
.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY
,
2295 base::RunLoop().Run();
2297 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2298 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2299 EXPECT_EQ(1, network_delegate
.set_cookie_count());
2302 // Try to set-up another cookie and update the previous cookie.
2304 TestNetworkDelegate network_delegate
;
2305 default_context_
.set_network_delegate(&network_delegate
);
2307 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2308 test_server
.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2309 DEFAULT_PRIORITY
, &d
));
2310 req
->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES
);
2313 base::RunLoop().Run();
2315 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2316 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2317 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2318 EXPECT_EQ(0, network_delegate
.set_cookie_count());
2321 // Verify the cookies weren't saved or updated.
2323 TestNetworkDelegate network_delegate
;
2324 default_context_
.set_network_delegate(&network_delegate
);
2326 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2327 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2329 base::RunLoop().Run();
2331 EXPECT_TRUE(d
.data_received().find("CookieToNotSave=1")
2332 == std::string::npos
);
2333 EXPECT_TRUE(d
.data_received().find("CookieToNotUpdate=2")
2334 != std::string::npos
);
2336 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2337 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2338 EXPECT_EQ(0, network_delegate
.set_cookie_count());
2342 TEST_F(URLRequestTest
, DoNotSendCookies_ViaPolicy
) {
2343 LocalHttpTestServer test_server
;
2344 ASSERT_TRUE(test_server
.Start());
2348 TestNetworkDelegate network_delegate
;
2349 default_context_
.set_network_delegate(&network_delegate
);
2351 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2352 test_server
.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY
,
2355 base::RunLoop().Run();
2357 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2358 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2361 // Verify that the cookie is set.
2363 TestNetworkDelegate network_delegate
;
2364 default_context_
.set_network_delegate(&network_delegate
);
2366 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2367 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2369 base::RunLoop().Run();
2371 EXPECT_TRUE(d
.data_received().find("CookieToNotSend=1")
2372 != std::string::npos
);
2374 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2375 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2378 // Verify that the cookie isn't sent.
2380 TestNetworkDelegate network_delegate
;
2381 default_context_
.set_network_delegate(&network_delegate
);
2383 network_delegate
.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES
);
2384 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2385 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2387 base::RunLoop().Run();
2389 EXPECT_TRUE(d
.data_received().find("Cookie: CookieToNotSend=1")
2390 == std::string::npos
);
2392 EXPECT_EQ(1, network_delegate
.blocked_get_cookies_count());
2393 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2397 TEST_F(URLRequestTest
, DoNotSaveCookies_ViaPolicy
) {
2398 LocalHttpTestServer test_server
;
2399 ASSERT_TRUE(test_server
.Start());
2403 TestNetworkDelegate network_delegate
;
2404 default_context_
.set_network_delegate(&network_delegate
);
2406 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2407 test_server
.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY
,
2410 base::RunLoop().Run();
2412 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2413 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2416 // Try to set-up another cookie and update the previous cookie.
2418 TestNetworkDelegate network_delegate
;
2419 default_context_
.set_network_delegate(&network_delegate
);
2421 network_delegate
.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE
);
2422 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2423 test_server
.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2424 DEFAULT_PRIORITY
, &d
));
2427 base::RunLoop().Run();
2429 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2430 EXPECT_EQ(2, network_delegate
.blocked_set_cookie_count());
2433 // Verify the cookies weren't saved or updated.
2435 TestNetworkDelegate network_delegate
;
2436 default_context_
.set_network_delegate(&network_delegate
);
2438 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2439 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2441 base::RunLoop().Run();
2443 EXPECT_TRUE(d
.data_received().find("CookieToNotSave=1")
2444 == std::string::npos
);
2445 EXPECT_TRUE(d
.data_received().find("CookieToNotUpdate=2")
2446 != std::string::npos
);
2448 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2449 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2453 TEST_F(URLRequestTest
, DoNotSaveEmptyCookies
) {
2454 LocalHttpTestServer test_server
;
2455 ASSERT_TRUE(test_server
.Start());
2457 // Set up an empty cookie.
2459 TestNetworkDelegate network_delegate
;
2460 default_context_
.set_network_delegate(&network_delegate
);
2462 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2463 test_server
.GetURL("set-cookie"), DEFAULT_PRIORITY
, &d
));
2465 base::RunLoop().Run();
2467 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2468 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2469 EXPECT_EQ(0, network_delegate
.set_cookie_count());
2473 TEST_F(URLRequestTest
, DoNotSendCookies_ViaPolicy_Async
) {
2474 LocalHttpTestServer test_server
;
2475 ASSERT_TRUE(test_server
.Start());
2479 TestNetworkDelegate network_delegate
;
2480 default_context_
.set_network_delegate(&network_delegate
);
2482 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2483 test_server
.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY
,
2486 base::RunLoop().Run();
2488 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2489 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2492 // Verify that the cookie is set.
2494 TestNetworkDelegate network_delegate
;
2495 default_context_
.set_network_delegate(&network_delegate
);
2497 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2498 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2500 base::RunLoop().Run();
2502 EXPECT_TRUE(d
.data_received().find("CookieToNotSend=1")
2503 != std::string::npos
);
2505 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2506 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2509 // Verify that the cookie isn't sent.
2511 TestNetworkDelegate network_delegate
;
2512 default_context_
.set_network_delegate(&network_delegate
);
2514 network_delegate
.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES
);
2515 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2516 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2518 base::RunLoop().Run();
2520 EXPECT_TRUE(d
.data_received().find("Cookie: CookieToNotSend=1")
2521 == std::string::npos
);
2523 EXPECT_EQ(1, network_delegate
.blocked_get_cookies_count());
2524 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2528 TEST_F(URLRequestTest
, DoNotSaveCookies_ViaPolicy_Async
) {
2529 LocalHttpTestServer test_server
;
2530 ASSERT_TRUE(test_server
.Start());
2534 TestNetworkDelegate network_delegate
;
2535 default_context_
.set_network_delegate(&network_delegate
);
2537 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2538 test_server
.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY
,
2541 base::RunLoop().Run();
2543 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2544 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2547 // Try to set-up another cookie and update the previous cookie.
2549 TestNetworkDelegate network_delegate
;
2550 default_context_
.set_network_delegate(&network_delegate
);
2552 network_delegate
.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE
);
2553 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2554 test_server
.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2555 DEFAULT_PRIORITY
, &d
));
2558 base::RunLoop().Run();
2560 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2561 EXPECT_EQ(2, network_delegate
.blocked_set_cookie_count());
2564 // Verify the cookies weren't saved or updated.
2566 TestNetworkDelegate network_delegate
;
2567 default_context_
.set_network_delegate(&network_delegate
);
2569 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2570 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2572 base::RunLoop().Run();
2574 EXPECT_TRUE(d
.data_received().find("CookieToNotSave=1")
2575 == std::string::npos
);
2576 EXPECT_TRUE(d
.data_received().find("CookieToNotUpdate=2")
2577 != std::string::npos
);
2579 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2580 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2584 TEST_F(URLRequestTest
, FirstPartyOnlyCookiesEnabled
) {
2585 LocalHttpTestServer test_server
;
2586 ASSERT_TRUE(test_server
.Start());
2588 // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where
2589 // LocalHttpTestServer points).
2591 TestNetworkDelegate network_delegate
;
2592 network_delegate
.set_first_party_only_cookies_enabled(true);
2593 default_context_
.set_network_delegate(&network_delegate
);
2596 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2598 "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"),
2599 DEFAULT_PRIORITY
, &d
));
2601 base::RunLoop().Run();
2602 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2603 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2604 EXPECT_EQ(1, network_delegate
.set_cookie_count());
2607 // Verify that the cookie is sent for first-party requests.
2609 TestNetworkDelegate network_delegate
;
2610 network_delegate
.set_first_party_only_cookies_enabled(true);
2611 default_context_
.set_network_delegate(&network_delegate
);
2613 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2614 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2615 req
->set_first_party_for_cookies(test_server
.GetURL(""));
2617 base::RunLoop().Run();
2619 EXPECT_TRUE(d
.data_received().find("FirstPartyCookieToSet=1") !=
2621 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2622 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2625 // Verify that the cookie is not-sent for non-first-party requests.
2627 TestNetworkDelegate network_delegate
;
2628 network_delegate
.set_first_party_only_cookies_enabled(true);
2629 default_context_
.set_network_delegate(&network_delegate
);
2631 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2632 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2633 req
->set_first_party_for_cookies(GURL("http://third-party.test/"));
2635 base::RunLoop().Run();
2637 EXPECT_TRUE(d
.data_received().find("FirstPartyCookieToSet=1") ==
2639 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2640 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2644 TEST_F(URLRequestTest
, FirstPartyOnlyCookiesDisabled
) {
2645 LocalHttpTestServer test_server
;
2646 ASSERT_TRUE(test_server
.Start());
2648 // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where
2649 // LocalHttpTestServer points).
2651 TestNetworkDelegate network_delegate
;
2652 network_delegate
.set_first_party_only_cookies_enabled(false);
2653 default_context_
.set_network_delegate(&network_delegate
);
2656 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2658 "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"),
2659 DEFAULT_PRIORITY
, &d
));
2661 base::RunLoop().Run();
2662 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2663 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2664 EXPECT_EQ(1, network_delegate
.set_cookie_count());
2667 // Verify that the cookie is sent for first-party requests.
2669 TestNetworkDelegate network_delegate
;
2670 network_delegate
.set_first_party_only_cookies_enabled(false);
2671 default_context_
.set_network_delegate(&network_delegate
);
2673 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2674 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2675 req
->set_first_party_for_cookies(test_server
.GetURL(""));
2677 base::RunLoop().Run();
2679 EXPECT_TRUE(d
.data_received().find("FirstPartyCookieToSet=1") !=
2681 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2682 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2685 // Verify that the cookie is also sent for non-first-party requests.
2687 TestNetworkDelegate network_delegate
;
2688 network_delegate
.set_first_party_only_cookies_enabled(false);
2689 default_context_
.set_network_delegate(&network_delegate
);
2691 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2692 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2693 req
->set_first_party_for_cookies(GURL("http://third-party.test/"));
2695 base::RunLoop().Run();
2697 EXPECT_TRUE(d
.data_received().find("FirstPartyCookieToSet=1") !=
2699 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2700 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2704 // FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2705 // value for the |fixed_date| argument given to the constructor.
2706 class FixedDateNetworkDelegate
: public TestNetworkDelegate
{
2708 explicit FixedDateNetworkDelegate(const std::string
& fixed_date
)
2709 : fixed_date_(fixed_date
) {}
2710 ~FixedDateNetworkDelegate() override
{}
2712 // NetworkDelegate implementation
2713 int OnHeadersReceived(
2714 URLRequest
* request
,
2715 const CompletionCallback
& callback
,
2716 const HttpResponseHeaders
* original_response_headers
,
2717 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
2718 GURL
* allowed_unsafe_redirect_url
) override
;
2721 std::string fixed_date_
;
2723 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate
);
2726 int FixedDateNetworkDelegate::OnHeadersReceived(
2727 URLRequest
* request
,
2728 const CompletionCallback
& callback
,
2729 const HttpResponseHeaders
* original_response_headers
,
2730 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
2731 GURL
* allowed_unsafe_redirect_url
) {
2732 HttpResponseHeaders
* new_response_headers
=
2733 new HttpResponseHeaders(original_response_headers
->raw_headers());
2735 new_response_headers
->RemoveHeader("Date");
2736 new_response_headers
->AddHeader("Date: " + fixed_date_
);
2738 *override_response_headers
= new_response_headers
;
2739 return TestNetworkDelegate::OnHeadersReceived(request
,
2741 original_response_headers
,
2742 override_response_headers
,
2743 allowed_unsafe_redirect_url
);
2746 // Test that cookie expiration times are adjusted for server/client clock
2747 // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2748 // headers by defaulting to GMT. (crbug.com/135131)
2749 TEST_F(URLRequestTest
, AcceptClockSkewCookieWithWrongDateTimezone
) {
2750 LocalHttpTestServer test_server
;
2751 ASSERT_TRUE(test_server
.Start());
2753 // Set up an expired cookie.
2755 TestNetworkDelegate network_delegate
;
2756 default_context_
.set_network_delegate(&network_delegate
);
2758 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2760 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2761 DEFAULT_PRIORITY
, &d
));
2763 base::RunLoop().Run();
2765 // Verify that the cookie is not set.
2767 TestNetworkDelegate network_delegate
;
2768 default_context_
.set_network_delegate(&network_delegate
);
2770 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2771 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2773 base::RunLoop().Run();
2775 EXPECT_TRUE(d
.data_received().find("StillGood=1") == std::string::npos
);
2777 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2779 FixedDateNetworkDelegate
network_delegate("18-Apr-1977 22:49:13 UTC");
2780 default_context_
.set_network_delegate(&network_delegate
);
2782 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2784 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2785 DEFAULT_PRIORITY
, &d
));
2787 base::RunLoop().Run();
2789 // Verify that the cookie is set.
2791 TestNetworkDelegate network_delegate
;
2792 default_context_
.set_network_delegate(&network_delegate
);
2794 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2795 test_server
.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY
, &d
));
2797 base::RunLoop().Run();
2799 EXPECT_TRUE(d
.data_received().find("StillGood=1") != std::string::npos
);
2804 // Check that it is impossible to change the referrer in the extra headers of
2806 TEST_F(URLRequestTest
, DoNotOverrideReferrer
) {
2807 LocalHttpTestServer test_server
;
2808 ASSERT_TRUE(test_server
.Start());
2810 // If extra headers contain referer and the request contains a referer,
2811 // only the latter shall be respected.
2814 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2815 test_server
.GetURL("echoheader?Referer"), DEFAULT_PRIORITY
, &d
));
2816 req
->SetReferrer("http://foo.com/");
2818 HttpRequestHeaders headers
;
2819 headers
.SetHeader(HttpRequestHeaders::kReferer
, "http://bar.com/");
2820 req
->SetExtraRequestHeaders(headers
);
2823 base::RunLoop().Run();
2825 EXPECT_EQ("http://foo.com/", d
.data_received());
2828 // If extra headers contain a referer but the request does not, no referer
2829 // shall be sent in the header.
2832 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
2833 test_server
.GetURL("echoheader?Referer"), DEFAULT_PRIORITY
, &d
));
2835 HttpRequestHeaders headers
;
2836 headers
.SetHeader(HttpRequestHeaders::kReferer
, "http://bar.com/");
2837 req
->SetExtraRequestHeaders(headers
);
2838 req
->SetLoadFlags(LOAD_VALIDATE_CACHE
);
2841 base::RunLoop().Run();
2843 EXPECT_EQ("None", d
.data_received());
2847 class URLRequestTestHTTP
: public URLRequestTest
{
2849 URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath
)) {}
2852 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2853 // |request_method| is the method to use for the initial request.
2854 // |redirect_method| is the method that is expected to be used for the second
2855 // request, after redirection.
2856 // If |include_data| is true, data is uploaded with the request. The
2857 // response body is expected to match it exactly, if and only if
2858 // |request_method| == |redirect_method|.
2859 void HTTPRedirectMethodTest(const GURL
& redirect_url
,
2860 const std::string
& request_method
,
2861 const std::string
& redirect_method
,
2862 bool include_data
) {
2863 static const char kData
[] = "hello world";
2865 scoped_ptr
<URLRequest
> req(
2866 default_context_
.CreateRequest(redirect_url
, DEFAULT_PRIORITY
, &d
));
2867 req
->set_method(request_method
);
2869 req
->set_upload(CreateSimpleUploadData(kData
));
2870 HttpRequestHeaders headers
;
2871 headers
.SetHeader(HttpRequestHeaders::kContentLength
,
2872 base::SizeTToString(arraysize(kData
) - 1));
2873 req
->SetExtraRequestHeaders(headers
);
2876 base::RunLoop().Run();
2877 EXPECT_EQ(redirect_method
, req
->method());
2878 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
2879 EXPECT_EQ(OK
, req
->status().error());
2881 if (request_method
== redirect_method
) {
2882 EXPECT_EQ(kData
, d
.data_received());
2884 EXPECT_NE(kData
, d
.data_received());
2888 LOG(WARNING
) << "Request method was: " << request_method
;
2891 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2892 // |request_method| is the method to use for the initial request.
2893 // |redirect_method| is the method that is expected to be used for the second
2894 // request, after redirection.
2895 // |origin_value| is the expected value for the Origin header after
2896 // redirection. If empty, expects that there will be no Origin header.
2897 void HTTPRedirectOriginHeaderTest(const GURL
& redirect_url
,
2898 const std::string
& request_method
,
2899 const std::string
& redirect_method
,
2900 const std::string
& origin_value
) {
2902 scoped_ptr
<URLRequest
> req(
2903 default_context_
.CreateRequest(redirect_url
, DEFAULT_PRIORITY
, &d
));
2904 req
->set_method(request_method
);
2905 req
->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin
,
2906 redirect_url
.GetOrigin().spec(), false);
2909 base::RunLoop().Run();
2911 EXPECT_EQ(redirect_method
, req
->method());
2912 // Note that there is no check for request success here because, for
2913 // purposes of testing, the request very well may fail. For example, if the
2914 // test redirects to an HTTPS server from an HTTP origin, thus it is cross
2915 // origin, there is not an HTTPS server in this unit test framework, so the
2916 // request would fail. However, that's fine, as long as the request headers
2917 // are in order and pass the checks below.
2918 if (origin_value
.empty()) {
2920 req
->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin
));
2922 std::string origin_header
;
2923 EXPECT_TRUE(req
->extra_request_headers().GetHeader(
2924 HttpRequestHeaders::kOrigin
, &origin_header
));
2925 EXPECT_EQ(origin_value
, origin_header
);
2929 void HTTPUploadDataOperationTest(const std::string
& method
) {
2930 const int kMsgSize
= 20000; // multiple of 10
2931 const int kIterations
= 50;
2932 char* uploadBytes
= new char[kMsgSize
+1];
2933 char* ptr
= uploadBytes
;
2935 for (int idx
= 0; idx
< kMsgSize
/10; idx
++) {
2936 memcpy(ptr
, "----------", 10);
2938 if (idx
% 100 == 0) {
2945 uploadBytes
[kMsgSize
] = '\0';
2947 for (int i
= 0; i
< kIterations
; ++i
) {
2949 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
2950 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
));
2951 r
->set_method(method
.c_str());
2953 r
->set_upload(CreateSimpleUploadData(uploadBytes
));
2956 EXPECT_TRUE(r
->is_pending());
2958 base::RunLoop().Run();
2960 ASSERT_EQ(1, d
.response_started_count())
2961 << "request failed: " << r
->status().status()
2962 << ", os error: " << r
->status().error();
2964 EXPECT_FALSE(d
.received_data_before_response());
2965 EXPECT_EQ(uploadBytes
, d
.data_received());
2967 delete[] uploadBytes
;
2970 void AddChunksToUpload(URLRequest
* r
) {
2971 r
->AppendChunkToUpload("a", 1, false);
2972 r
->AppendChunkToUpload("bcd", 3, false);
2973 r
->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2974 r
->AppendChunkToUpload("\r\n\r\n", 4, false);
2975 r
->AppendChunkToUpload("0", 1, false);
2976 r
->AppendChunkToUpload("2323", 4, true);
2979 void VerifyReceivedDataMatchesChunks(URLRequest
* r
, TestDelegate
* d
) {
2980 // This should match the chunks sent by AddChunksToUpload().
2981 const std::string expected_data
=
2982 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2984 ASSERT_EQ(1, d
->response_started_count())
2985 << "request failed: " << r
->status().status()
2986 << ", os error: " << r
->status().error();
2988 EXPECT_FALSE(d
->received_data_before_response());
2990 EXPECT_EQ(expected_data
.size(), static_cast<size_t>(d
->bytes_received()));
2991 EXPECT_EQ(expected_data
, d
->data_received());
2994 bool DoManyCookiesRequest(int num_cookies
) {
2996 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
2997 test_server_
.GetURL("set-many-cookies?" +
2998 base::IntToString(num_cookies
)),
2999 DEFAULT_PRIORITY
, &d
));
3002 EXPECT_TRUE(r
->is_pending());
3004 base::RunLoop().Run();
3006 bool is_success
= r
->status().is_success();
3009 EXPECT_TRUE(r
->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG
);
3010 // The test server appears to be unable to handle subsequent requests
3011 // after this error is triggered. Force it to restart.
3012 EXPECT_TRUE(test_server_
.Stop());
3013 EXPECT_TRUE(test_server_
.Start());
3019 LocalHttpTestServer
* test_server() {
3020 return &test_server_
;
3024 LocalHttpTestServer test_server_
;
3027 // In this unit test, we're using the HTTPTestServer as a proxy server and
3028 // issuing a CONNECT request with the magic host name "www.redirect.com".
3029 // The HTTPTestServer will return a 302 response, which we should not
3031 TEST_F(URLRequestTestHTTP
, ProxyTunnelRedirectTest
) {
3032 ASSERT_TRUE(test_server_
.Start());
3034 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
3035 TestURLRequestContextWithProxy
context(
3036 test_server_
.host_port_pair().ToString(), &network_delegate
);
3040 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3041 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY
, &d
));
3043 EXPECT_TRUE(r
->is_pending());
3045 base::RunLoop().Run();
3047 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
3048 // The proxy server is not set before failure.
3049 EXPECT_TRUE(r
->proxy_server().IsEmpty());
3050 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED
, r
->status().error());
3051 EXPECT_EQ(1, d
.response_started_count());
3052 // We should not have followed the redirect.
3053 EXPECT_EQ(0, d
.received_redirect_count());
3057 // This is the same as the previous test, but checks that the network delegate
3058 // registers the error.
3059 TEST_F(URLRequestTestHTTP
, NetworkDelegateTunnelConnectionFailed
) {
3060 ASSERT_TRUE(test_server_
.Start());
3062 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
3063 TestURLRequestContextWithProxy
context(
3064 test_server_
.host_port_pair().ToString(), &network_delegate
);
3068 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3069 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY
, &d
));
3071 EXPECT_TRUE(r
->is_pending());
3073 base::RunLoop().Run();
3075 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
3076 // The proxy server is not set before failure.
3077 EXPECT_TRUE(r
->proxy_server().IsEmpty());
3078 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED
, r
->status().error());
3079 EXPECT_EQ(1, d
.response_started_count());
3080 // We should not have followed the redirect.
3081 EXPECT_EQ(0, d
.received_redirect_count());
3083 EXPECT_EQ(1, network_delegate
.error_count());
3084 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED
, network_delegate
.last_error());
3088 // Tests that we can block and asynchronously return OK in various stages.
3089 TEST_F(URLRequestTestHTTP
, NetworkDelegateBlockAsynchronously
) {
3090 static const BlockingNetworkDelegate::Stage blocking_stages
[] = {
3091 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
,
3092 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
,
3093 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
3095 static const size_t blocking_stages_length
= arraysize(blocking_stages
);
3097 ASSERT_TRUE(test_server_
.Start());
3100 BlockingNetworkDelegate
network_delegate(
3101 BlockingNetworkDelegate::USER_CALLBACK
);
3102 network_delegate
.set_block_on(
3103 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
|
3104 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
|
3105 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
);
3107 TestURLRequestContext
context(true);
3108 context
.set_network_delegate(&network_delegate
);
3112 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3113 test_server_
.GetURL("empty.html"), DEFAULT_PRIORITY
, &d
));
3116 for (size_t i
= 0; i
< blocking_stages_length
; ++i
) {
3117 base::RunLoop().Run();
3118 EXPECT_EQ(blocking_stages
[i
],
3119 network_delegate
.stage_blocked_for_callback());
3120 network_delegate
.DoCallback(OK
);
3122 base::RunLoop().Run();
3123 EXPECT_EQ(200, r
->GetResponseCode());
3124 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3125 EXPECT_EQ(1, network_delegate
.created_requests());
3126 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3128 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3131 // Tests that the network delegate can block and cancel a request.
3132 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequest
) {
3133 ASSERT_TRUE(test_server_
.Start());
3136 BlockingNetworkDelegate
network_delegate(
3137 BlockingNetworkDelegate::AUTO_CALLBACK
);
3138 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
);
3139 network_delegate
.set_retval(ERR_EMPTY_RESPONSE
);
3141 TestURLRequestContextWithProxy
context(
3142 test_server_
.host_port_pair().ToString(), &network_delegate
);
3145 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3146 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
3149 base::RunLoop().Run();
3151 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
3152 // The proxy server is not set before cancellation.
3153 EXPECT_TRUE(r
->proxy_server().IsEmpty());
3154 EXPECT_EQ(ERR_EMPTY_RESPONSE
, r
->status().error());
3155 EXPECT_EQ(1, network_delegate
.created_requests());
3156 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3158 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3161 // Helper function for NetworkDelegateCancelRequestAsynchronously and
3162 // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
3163 // delegate operating in |block_mode| and a request for |url|. It blocks the
3164 // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
3165 void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode
,
3166 BlockingNetworkDelegate::Stage stage
,
3169 BlockingNetworkDelegate
network_delegate(block_mode
);
3170 network_delegate
.set_retval(ERR_BLOCKED_BY_CLIENT
);
3171 network_delegate
.set_block_on(stage
);
3173 TestURLRequestContext
context(true);
3174 context
.set_network_delegate(&network_delegate
);
3178 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3181 base::RunLoop().Run();
3183 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
3184 // The proxy server is not set before cancellation.
3185 EXPECT_TRUE(r
->proxy_server().IsEmpty());
3186 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT
, r
->status().error());
3187 EXPECT_EQ(1, network_delegate
.created_requests());
3188 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3190 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3193 // The following 3 tests check that the network delegate can cancel a request
3194 // synchronously in various stages of the request.
3195 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestSynchronously1
) {
3196 ASSERT_TRUE(test_server_
.Start());
3197 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS
,
3198 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
,
3199 test_server_
.GetURL(std::string()));
3202 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestSynchronously2
) {
3203 ASSERT_TRUE(test_server_
.Start());
3204 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS
,
3205 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
,
3206 test_server_
.GetURL(std::string()));
3209 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestSynchronously3
) {
3210 ASSERT_TRUE(test_server_
.Start());
3211 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS
,
3212 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
,
3213 test_server_
.GetURL(std::string()));
3216 // The following 3 tests check that the network delegate can cancel a request
3217 // asynchronously in various stages of the request.
3218 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestAsynchronously1
) {
3219 ASSERT_TRUE(test_server_
.Start());
3220 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK
,
3221 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
,
3222 test_server_
.GetURL(std::string()));
3225 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestAsynchronously2
) {
3226 ASSERT_TRUE(test_server_
.Start());
3227 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK
,
3228 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
,
3229 test_server_
.GetURL(std::string()));
3232 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestAsynchronously3
) {
3233 ASSERT_TRUE(test_server_
.Start());
3234 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK
,
3235 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
,
3236 test_server_
.GetURL(std::string()));
3239 // Tests that the network delegate can block and redirect a request to a new
3241 TEST_F(URLRequestTestHTTP
, NetworkDelegateRedirectRequest
) {
3242 ASSERT_TRUE(test_server_
.Start());
3245 BlockingNetworkDelegate
network_delegate(
3246 BlockingNetworkDelegate::AUTO_CALLBACK
);
3247 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
);
3248 GURL
redirect_url(test_server_
.GetURL("simple.html"));
3249 network_delegate
.set_redirect_url(redirect_url
);
3251 TestURLRequestContextWithProxy
context(
3252 test_server_
.host_port_pair().ToString(), &network_delegate
);
3255 GURL
original_url(test_server_
.GetURL("empty.html"));
3256 scoped_ptr
<URLRequest
> r(
3257 context
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
3259 // Quit after hitting the redirect, so can check the headers.
3260 d
.set_quit_on_redirect(true);
3262 base::RunLoop().Run();
3264 // Check headers from URLRequestJob.
3265 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3266 EXPECT_EQ(307, r
->GetResponseCode());
3267 EXPECT_EQ(307, r
->response_headers()->response_code());
3268 std::string location
;
3269 ASSERT_TRUE(r
->response_headers()->EnumerateHeader(NULL
, "Location",
3271 EXPECT_EQ(redirect_url
, GURL(location
));
3273 // Let the request finish.
3274 r
->FollowDeferredRedirect();
3275 base::RunLoop().Run();
3276 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3277 EXPECT_TRUE(r
->proxy_server().Equals(test_server_
.host_port_pair()));
3279 1, network_delegate
.observed_before_proxy_headers_sent_callbacks());
3281 network_delegate
.last_observed_proxy().Equals(
3282 test_server_
.host_port_pair()));
3284 EXPECT_EQ(0, r
->status().error());
3285 EXPECT_EQ(redirect_url
, r
->url());
3286 EXPECT_EQ(original_url
, r
->original_url());
3287 EXPECT_EQ(2U, r
->url_chain().size());
3288 EXPECT_EQ(1, network_delegate
.created_requests());
3289 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3291 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3294 // Tests that the network delegate can block and redirect a request to a new
3295 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
3296 TEST_F(URLRequestTestHTTP
, NetworkDelegateRedirectRequestSynchronously
) {
3297 ASSERT_TRUE(test_server_
.Start());
3300 BlockingNetworkDelegate
network_delegate(
3301 BlockingNetworkDelegate::SYNCHRONOUS
);
3302 GURL
redirect_url(test_server_
.GetURL("simple.html"));
3303 network_delegate
.set_redirect_url(redirect_url
);
3305 TestURLRequestContextWithProxy
context(
3306 test_server_
.host_port_pair().ToString(), &network_delegate
);
3309 GURL
original_url(test_server_
.GetURL("empty.html"));
3310 scoped_ptr
<URLRequest
> r(
3311 context
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
3313 // Quit after hitting the redirect, so can check the headers.
3314 d
.set_quit_on_redirect(true);
3316 base::RunLoop().Run();
3318 // Check headers from URLRequestJob.
3319 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3320 EXPECT_EQ(307, r
->GetResponseCode());
3321 EXPECT_EQ(307, r
->response_headers()->response_code());
3322 std::string location
;
3323 ASSERT_TRUE(r
->response_headers()->EnumerateHeader(NULL
, "Location",
3325 EXPECT_EQ(redirect_url
, GURL(location
));
3327 // Let the request finish.
3328 r
->FollowDeferredRedirect();
3329 base::RunLoop().Run();
3331 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3332 EXPECT_TRUE(r
->proxy_server().Equals(test_server_
.host_port_pair()));
3334 1, network_delegate
.observed_before_proxy_headers_sent_callbacks());
3336 network_delegate
.last_observed_proxy().Equals(
3337 test_server_
.host_port_pair()));
3338 EXPECT_EQ(0, r
->status().error());
3339 EXPECT_EQ(redirect_url
, r
->url());
3340 EXPECT_EQ(original_url
, r
->original_url());
3341 EXPECT_EQ(2U, r
->url_chain().size());
3342 EXPECT_EQ(1, network_delegate
.created_requests());
3343 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3345 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3348 // Tests that redirects caused by the network delegate preserve POST data.
3349 TEST_F(URLRequestTestHTTP
, NetworkDelegateRedirectRequestPost
) {
3350 ASSERT_TRUE(test_server_
.Start());
3352 const char kData
[] = "hello world";
3355 BlockingNetworkDelegate
network_delegate(
3356 BlockingNetworkDelegate::AUTO_CALLBACK
);
3357 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
);
3358 GURL
redirect_url(test_server_
.GetURL("echo"));
3359 network_delegate
.set_redirect_url(redirect_url
);
3361 TestURLRequestContext
context(true);
3362 context
.set_network_delegate(&network_delegate
);
3366 GURL
original_url(test_server_
.GetURL("empty.html"));
3367 scoped_ptr
<URLRequest
> r(
3368 context
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
3369 r
->set_method("POST");
3370 r
->set_upload(CreateSimpleUploadData(kData
));
3371 HttpRequestHeaders headers
;
3372 headers
.SetHeader(HttpRequestHeaders::kContentLength
,
3373 base::SizeTToString(arraysize(kData
) - 1));
3374 r
->SetExtraRequestHeaders(headers
);
3376 // Quit after hitting the redirect, so can check the headers.
3377 d
.set_quit_on_redirect(true);
3379 base::RunLoop().Run();
3381 // Check headers from URLRequestJob.
3382 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3383 EXPECT_EQ(307, r
->GetResponseCode());
3384 EXPECT_EQ(307, r
->response_headers()->response_code());
3385 std::string location
;
3386 ASSERT_TRUE(r
->response_headers()->EnumerateHeader(NULL
, "Location",
3388 EXPECT_EQ(redirect_url
, GURL(location
));
3390 // Let the request finish.
3391 r
->FollowDeferredRedirect();
3392 base::RunLoop().Run();
3394 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3395 EXPECT_EQ(0, r
->status().error());
3396 EXPECT_EQ(redirect_url
, r
->url());
3397 EXPECT_EQ(original_url
, r
->original_url());
3398 EXPECT_EQ(2U, r
->url_chain().size());
3399 EXPECT_EQ(1, network_delegate
.created_requests());
3400 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3401 EXPECT_EQ("POST", r
->method());
3402 EXPECT_EQ(kData
, d
.data_received());
3404 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3407 // Tests that the network delegate can block and redirect a request to a new
3408 // URL during OnHeadersReceived.
3409 TEST_F(URLRequestTestHTTP
, NetworkDelegateRedirectRequestOnHeadersReceived
) {
3410 ASSERT_TRUE(test_server_
.Start());
3413 BlockingNetworkDelegate
network_delegate(
3414 BlockingNetworkDelegate::AUTO_CALLBACK
);
3415 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED
);
3416 GURL
redirect_url(test_server_
.GetURL("simple.html"));
3417 network_delegate
.set_redirect_on_headers_received_url(redirect_url
);
3419 TestURLRequestContextWithProxy
context(
3420 test_server_
.host_port_pair().ToString(), &network_delegate
);
3423 GURL
original_url(test_server_
.GetURL("empty.html"));
3424 scoped_ptr
<URLRequest
> r(
3425 context
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
3428 base::RunLoop().Run();
3430 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3431 EXPECT_TRUE(r
->proxy_server().Equals(test_server_
.host_port_pair()));
3433 2, network_delegate
.observed_before_proxy_headers_sent_callbacks());
3435 network_delegate
.last_observed_proxy().Equals(
3436 test_server_
.host_port_pair()));
3438 EXPECT_EQ(OK
, r
->status().error());
3439 EXPECT_EQ(redirect_url
, r
->url());
3440 EXPECT_EQ(original_url
, r
->original_url());
3441 EXPECT_EQ(2U, r
->url_chain().size());
3442 EXPECT_EQ(2, network_delegate
.created_requests());
3443 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3445 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3448 // Tests that the network delegate can synchronously complete OnAuthRequired
3449 // by taking no action. This indicates that the NetworkDelegate does not want to
3450 // handle the challenge, and is passing the buck along to the
3451 // URLRequest::Delegate.
3452 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredSyncNoAction
) {
3453 ASSERT_TRUE(test_server_
.Start());
3456 BlockingNetworkDelegate
network_delegate(
3457 BlockingNetworkDelegate::SYNCHRONOUS
);
3459 TestURLRequestContext
context(true);
3460 context
.set_network_delegate(&network_delegate
);
3463 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
3466 GURL
url(test_server_
.GetURL("auth-basic"));
3467 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3470 base::RunLoop().Run();
3472 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3473 EXPECT_EQ(0, r
->status().error());
3474 EXPECT_EQ(200, r
->GetResponseCode());
3475 EXPECT_TRUE(d
.auth_required_called());
3476 EXPECT_EQ(1, network_delegate
.created_requests());
3477 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3479 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3482 TEST_F(URLRequestTestHTTP
,
3483 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders
) {
3484 ASSERT_TRUE(test_server_
.Start());
3487 BlockingNetworkDelegate
network_delegate(
3488 BlockingNetworkDelegate::SYNCHRONOUS
);
3490 TestURLRequestContext
context(true);
3491 context
.set_network_delegate(&network_delegate
);
3494 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
3497 GURL
url(test_server_
.GetURL("auth-basic"));
3498 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3502 HttpRequestHeaders headers
;
3503 EXPECT_TRUE(r
->GetFullRequestHeaders(&headers
));
3504 EXPECT_FALSE(headers
.HasHeader("Authorization"));
3507 base::RunLoop().Run();
3509 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3510 EXPECT_EQ(0, r
->status().error());
3511 EXPECT_EQ(200, r
->GetResponseCode());
3512 EXPECT_TRUE(d
.auth_required_called());
3513 EXPECT_EQ(1, network_delegate
.created_requests());
3514 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3516 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3519 // Tests that the network delegate can synchronously complete OnAuthRequired
3520 // by setting credentials.
3521 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredSyncSetAuth
) {
3522 ASSERT_TRUE(test_server_
.Start());
3525 BlockingNetworkDelegate
network_delegate(
3526 BlockingNetworkDelegate::SYNCHRONOUS
);
3527 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3528 network_delegate
.set_auth_retval(
3529 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
);
3531 network_delegate
.set_auth_credentials(AuthCredentials(kUser
, kSecret
));
3533 TestURLRequestContext
context(true);
3534 context
.set_network_delegate(&network_delegate
);
3538 GURL
url(test_server_
.GetURL("auth-basic"));
3539 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3541 base::RunLoop().Run();
3543 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3544 EXPECT_EQ(0, r
->status().error());
3545 EXPECT_EQ(200, r
->GetResponseCode());
3546 EXPECT_FALSE(d
.auth_required_called());
3547 EXPECT_EQ(1, network_delegate
.created_requests());
3548 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3550 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3553 // Same as above, but also tests that GetFullRequestHeaders returns the proper
3554 // headers (for the first or second request) when called at the proper times.
3555 TEST_F(URLRequestTestHTTP
,
3556 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders
) {
3557 ASSERT_TRUE(test_server_
.Start());
3560 BlockingNetworkDelegate
network_delegate(
3561 BlockingNetworkDelegate::SYNCHRONOUS
);
3562 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3563 network_delegate
.set_auth_retval(
3564 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
);
3566 network_delegate
.set_auth_credentials(AuthCredentials(kUser
, kSecret
));
3568 TestURLRequestContext
context(true);
3569 context
.set_network_delegate(&network_delegate
);
3573 GURL
url(test_server_
.GetURL("auth-basic"));
3574 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3576 base::RunLoop().Run();
3578 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3579 EXPECT_EQ(0, r
->status().error());
3580 EXPECT_EQ(200, r
->GetResponseCode());
3581 EXPECT_FALSE(d
.auth_required_called());
3582 EXPECT_EQ(1, network_delegate
.created_requests());
3583 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3586 HttpRequestHeaders headers
;
3587 EXPECT_TRUE(r
->GetFullRequestHeaders(&headers
));
3588 EXPECT_TRUE(headers
.HasHeader("Authorization"));
3591 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3594 // Tests that the network delegate can synchronously complete OnAuthRequired
3595 // by cancelling authentication.
3596 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredSyncCancel
) {
3597 ASSERT_TRUE(test_server_
.Start());
3600 BlockingNetworkDelegate
network_delegate(
3601 BlockingNetworkDelegate::SYNCHRONOUS
);
3602 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3603 network_delegate
.set_auth_retval(
3604 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
);
3606 TestURLRequestContext
context(true);
3607 context
.set_network_delegate(&network_delegate
);
3611 GURL
url(test_server_
.GetURL("auth-basic"));
3612 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3614 base::RunLoop().Run();
3616 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3617 EXPECT_EQ(OK
, r
->status().error());
3618 EXPECT_EQ(401, r
->GetResponseCode());
3619 EXPECT_FALSE(d
.auth_required_called());
3620 EXPECT_EQ(1, network_delegate
.created_requests());
3621 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3623 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3626 // Tests that the network delegate can asynchronously complete OnAuthRequired
3627 // by taking no action. This indicates that the NetworkDelegate does not want
3628 // to handle the challenge, and is passing the buck along to the
3629 // URLRequest::Delegate.
3630 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredAsyncNoAction
) {
3631 ASSERT_TRUE(test_server_
.Start());
3634 BlockingNetworkDelegate
network_delegate(
3635 BlockingNetworkDelegate::AUTO_CALLBACK
);
3636 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3638 TestURLRequestContext
context(true);
3639 context
.set_network_delegate(&network_delegate
);
3642 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
3645 GURL
url(test_server_
.GetURL("auth-basic"));
3646 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3648 base::RunLoop().Run();
3650 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3651 EXPECT_EQ(0, r
->status().error());
3652 EXPECT_EQ(200, r
->GetResponseCode());
3653 EXPECT_TRUE(d
.auth_required_called());
3654 EXPECT_EQ(1, network_delegate
.created_requests());
3655 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3657 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3660 // Tests that the network delegate can asynchronously complete OnAuthRequired
3661 // by setting credentials.
3662 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredAsyncSetAuth
) {
3663 ASSERT_TRUE(test_server_
.Start());
3666 BlockingNetworkDelegate
network_delegate(
3667 BlockingNetworkDelegate::AUTO_CALLBACK
);
3668 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3669 network_delegate
.set_auth_retval(
3670 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
);
3672 AuthCredentials
auth_credentials(kUser
, kSecret
);
3673 network_delegate
.set_auth_credentials(auth_credentials
);
3675 TestURLRequestContext
context(true);
3676 context
.set_network_delegate(&network_delegate
);
3680 GURL
url(test_server_
.GetURL("auth-basic"));
3681 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3683 base::RunLoop().Run();
3685 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3686 EXPECT_EQ(0, r
->status().error());
3688 EXPECT_EQ(200, r
->GetResponseCode());
3689 EXPECT_FALSE(d
.auth_required_called());
3690 EXPECT_EQ(1, network_delegate
.created_requests());
3691 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3693 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3696 // Tests that the network delegate can asynchronously complete OnAuthRequired
3697 // by cancelling authentication.
3698 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredAsyncCancel
) {
3699 ASSERT_TRUE(test_server_
.Start());
3702 BlockingNetworkDelegate
network_delegate(
3703 BlockingNetworkDelegate::AUTO_CALLBACK
);
3704 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3705 network_delegate
.set_auth_retval(
3706 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
);
3708 TestURLRequestContext
context(true);
3709 context
.set_network_delegate(&network_delegate
);
3713 GURL
url(test_server_
.GetURL("auth-basic"));
3714 scoped_ptr
<URLRequest
> r(context
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
3716 base::RunLoop().Run();
3718 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
3719 EXPECT_EQ(OK
, r
->status().error());
3720 EXPECT_EQ(401, r
->GetResponseCode());
3721 EXPECT_FALSE(d
.auth_required_called());
3722 EXPECT_EQ(1, network_delegate
.created_requests());
3723 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3725 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3728 // Tests that we can handle when a network request was canceled while we were
3729 // waiting for the network delegate.
3730 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3731 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelWhileWaiting1
) {
3732 ASSERT_TRUE(test_server_
.Start());
3735 BlockingNetworkDelegate
network_delegate(
3736 BlockingNetworkDelegate::USER_CALLBACK
);
3737 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
);
3739 TestURLRequestContext
context(true);
3740 context
.set_network_delegate(&network_delegate
);
3744 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3745 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
3748 base::RunLoop().Run();
3749 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
,
3750 network_delegate
.stage_blocked_for_callback());
3751 EXPECT_EQ(0, network_delegate
.completed_requests());
3752 // Cancel before callback.
3754 // Ensure that network delegate is notified.
3755 EXPECT_EQ(1, network_delegate
.completed_requests());
3756 EXPECT_EQ(URLRequestStatus::CANCELED
, r
->status().status());
3757 EXPECT_EQ(ERR_ABORTED
, r
->status().error());
3758 EXPECT_EQ(1, network_delegate
.created_requests());
3759 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3761 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3764 // Tests that we can handle when a network request was canceled while we were
3765 // waiting for the network delegate.
3766 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3767 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelWhileWaiting2
) {
3768 ASSERT_TRUE(test_server_
.Start());
3771 BlockingNetworkDelegate
network_delegate(
3772 BlockingNetworkDelegate::USER_CALLBACK
);
3773 network_delegate
.set_block_on(
3774 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
);
3776 TestURLRequestContext
context(true);
3777 context
.set_network_delegate(&network_delegate
);
3781 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3782 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
3785 base::RunLoop().Run();
3786 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
,
3787 network_delegate
.stage_blocked_for_callback());
3788 EXPECT_EQ(0, network_delegate
.completed_requests());
3789 // Cancel before callback.
3791 // Ensure that network delegate is notified.
3792 EXPECT_EQ(1, network_delegate
.completed_requests());
3793 EXPECT_EQ(URLRequestStatus::CANCELED
, r
->status().status());
3794 EXPECT_EQ(ERR_ABORTED
, r
->status().error());
3795 EXPECT_EQ(1, network_delegate
.created_requests());
3796 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3798 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3801 // Tests that we can handle when a network request was canceled while we were
3802 // waiting for the network delegate.
3803 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3804 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelWhileWaiting3
) {
3805 ASSERT_TRUE(test_server_
.Start());
3808 BlockingNetworkDelegate
network_delegate(
3809 BlockingNetworkDelegate::USER_CALLBACK
);
3810 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED
);
3812 TestURLRequestContext
context(true);
3813 context
.set_network_delegate(&network_delegate
);
3817 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3818 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
3821 base::RunLoop().Run();
3822 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED
,
3823 network_delegate
.stage_blocked_for_callback());
3824 EXPECT_EQ(0, network_delegate
.completed_requests());
3825 // Cancel before callback.
3827 // Ensure that network delegate is notified.
3828 EXPECT_EQ(1, network_delegate
.completed_requests());
3829 EXPECT_EQ(URLRequestStatus::CANCELED
, r
->status().status());
3830 EXPECT_EQ(ERR_ABORTED
, r
->status().error());
3831 EXPECT_EQ(1, network_delegate
.created_requests());
3832 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3834 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3837 // Tests that we can handle when a network request was canceled while we were
3838 // waiting for the network delegate.
3839 // Part 4: Request is cancelled while waiting for OnAuthRequired callback.
3840 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelWhileWaiting4
) {
3841 ASSERT_TRUE(test_server_
.Start());
3844 BlockingNetworkDelegate
network_delegate(
3845 BlockingNetworkDelegate::USER_CALLBACK
);
3846 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3848 TestURLRequestContext
context(true);
3849 context
.set_network_delegate(&network_delegate
);
3853 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3854 test_server_
.GetURL("auth-basic"), DEFAULT_PRIORITY
, &d
));
3857 base::RunLoop().Run();
3858 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED
,
3859 network_delegate
.stage_blocked_for_callback());
3860 EXPECT_EQ(0, network_delegate
.completed_requests());
3861 // Cancel before callback.
3863 // Ensure that network delegate is notified.
3864 EXPECT_EQ(1, network_delegate
.completed_requests());
3865 EXPECT_EQ(URLRequestStatus::CANCELED
, r
->status().status());
3866 EXPECT_EQ(ERR_ABORTED
, r
->status().error());
3867 EXPECT_EQ(1, network_delegate
.created_requests());
3868 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3870 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3873 // In this unit test, we're using the HTTPTestServer as a proxy server and
3874 // issuing a CONNECT request with the magic host name "www.server-auth.com".
3875 // The HTTPTestServer will return a 401 response, which we should balk at.
3876 TEST_F(URLRequestTestHTTP
, UnexpectedServerAuthTest
) {
3877 ASSERT_TRUE(test_server_
.Start());
3879 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
3880 TestURLRequestContextWithProxy
context(
3881 test_server_
.host_port_pair().ToString(), &network_delegate
);
3885 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
3886 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY
, &d
));
3889 EXPECT_TRUE(r
->is_pending());
3891 base::RunLoop().Run();
3893 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
3894 // The proxy server is not set before failure.
3895 EXPECT_TRUE(r
->proxy_server().IsEmpty());
3896 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED
, r
->status().error());
3900 TEST_F(URLRequestTestHTTP
, GetTest_NoCache
) {
3901 ASSERT_TRUE(test_server_
.Start());
3905 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
3906 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
3909 EXPECT_TRUE(r
->is_pending());
3911 base::RunLoop().Run();
3913 EXPECT_EQ(1, d
.response_started_count());
3914 EXPECT_FALSE(d
.received_data_before_response());
3915 EXPECT_NE(0, d
.bytes_received());
3916 EXPECT_EQ(test_server_
.host_port_pair().host(),
3917 r
->GetSocketAddress().host());
3918 EXPECT_EQ(test_server_
.host_port_pair().port(),
3919 r
->GetSocketAddress().port());
3921 // TODO(eroman): Add back the NetLog tests...
3925 // This test has the server send a large number of cookies to the client.
3926 // To ensure that no number of cookies causes a crash, a galloping binary
3927 // search is used to estimate that maximum number of cookies that are accepted
3928 // by the browser. Beyond the maximum number, the request will fail with
3929 // ERR_RESPONSE_HEADERS_TOO_BIG.
3931 // http://crbug.com/177916
3932 #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3934 #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3935 #endif // defined(OS_WIN)
3936 TEST_F(URLRequestTestHTTP
, MAYBE_GetTest_ManyCookies
) {
3937 ASSERT_TRUE(test_server_
.Start());
3939 int lower_bound
= 0;
3940 int upper_bound
= 1;
3942 // Double the number of cookies until the response header limits are
3944 while (DoManyCookiesRequest(upper_bound
)) {
3945 lower_bound
= upper_bound
;
3947 ASSERT_LT(upper_bound
, 1000000);
3950 int tolerance
= static_cast<int>(upper_bound
* 0.005);
3954 // Perform a binary search to find the highest possible number of cookies,
3955 // within the desired tolerance.
3956 while (upper_bound
- lower_bound
>= tolerance
) {
3957 int num_cookies
= (lower_bound
+ upper_bound
) / 2;
3959 if (DoManyCookiesRequest(num_cookies
))
3960 lower_bound
= num_cookies
;
3962 upper_bound
= num_cookies
;
3964 // Success: the test did not crash.
3967 TEST_F(URLRequestTestHTTP
, GetTest
) {
3968 ASSERT_TRUE(test_server_
.Start());
3972 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
3973 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
3976 EXPECT_TRUE(r
->is_pending());
3978 base::RunLoop().Run();
3980 EXPECT_EQ(1, d
.response_started_count());
3981 EXPECT_FALSE(d
.received_data_before_response());
3982 EXPECT_NE(0, d
.bytes_received());
3983 EXPECT_EQ(test_server_
.host_port_pair().host(),
3984 r
->GetSocketAddress().host());
3985 EXPECT_EQ(test_server_
.host_port_pair().port(),
3986 r
->GetSocketAddress().port());
3990 TEST_F(URLRequestTestHTTP
, GetTest_GetFullRequestHeaders
) {
3991 ASSERT_TRUE(test_server_
.Start());
3995 GURL
test_url(test_server_
.GetURL(std::string()));
3996 scoped_ptr
<URLRequest
> r(
3997 default_context_
.CreateRequest(test_url
, DEFAULT_PRIORITY
, &d
));
3999 HttpRequestHeaders headers
;
4000 EXPECT_FALSE(r
->GetFullRequestHeaders(&headers
));
4003 EXPECT_TRUE(r
->is_pending());
4005 base::RunLoop().Run();
4007 EXPECT_EQ(1, d
.response_started_count());
4008 EXPECT_FALSE(d
.received_data_before_response());
4009 EXPECT_NE(0, d
.bytes_received());
4010 EXPECT_EQ(test_server_
.host_port_pair().host(),
4011 r
->GetSocketAddress().host());
4012 EXPECT_EQ(test_server_
.host_port_pair().port(),
4013 r
->GetSocketAddress().port());
4015 EXPECT_TRUE(d
.have_full_request_headers());
4016 CheckFullRequestHeaders(d
.full_request_headers(), test_url
);
4020 TEST_F(URLRequestTestHTTP
, GetTestLoadTiming
) {
4021 ASSERT_TRUE(test_server_
.Start());
4025 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
4026 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
4029 EXPECT_TRUE(r
->is_pending());
4031 base::RunLoop().Run();
4033 LoadTimingInfo load_timing_info
;
4034 r
->GetLoadTimingInfo(&load_timing_info
);
4035 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
4037 EXPECT_EQ(1, d
.response_started_count());
4038 EXPECT_FALSE(d
.received_data_before_response());
4039 EXPECT_NE(0, d
.bytes_received());
4040 EXPECT_EQ(test_server_
.host_port_pair().host(),
4041 r
->GetSocketAddress().host());
4042 EXPECT_EQ(test_server_
.host_port_pair().port(),
4043 r
->GetSocketAddress().port());
4047 TEST_F(URLRequestTestHTTP
, GetZippedTest
) {
4048 ASSERT_TRUE(test_server_
.Start());
4050 // Parameter that specifies the Content-Length field in the response:
4051 // C - Compressed length.
4052 // U - Uncompressed length.
4053 // L - Large length (larger than both C & U).
4054 // M - Medium length (between C & U).
4055 // S - Small length (smaller than both C & U).
4056 const char test_parameters
[] = "CULMS";
4057 const int num_tests
= arraysize(test_parameters
)- 1; // Skip NULL.
4058 // C & U should be OK.
4059 // L & M are larger than the data sent, and show an error.
4060 // S has too little data, but we seem to accept it.
4061 const bool test_expect_success
[num_tests
] =
4062 { true, true, false, false, true };
4064 for (int i
= 0; i
< num_tests
; i
++) {
4067 std::string test_file
=
4068 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
4069 test_parameters
[i
]);
4071 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
4072 TestURLRequestContext
context(true);
4073 context
.set_network_delegate(&network_delegate
);
4076 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
4077 test_server_
.GetURL(test_file
), DEFAULT_PRIORITY
, &d
));
4079 EXPECT_TRUE(r
->is_pending());
4081 base::RunLoop().Run();
4083 EXPECT_EQ(1, d
.response_started_count());
4084 EXPECT_FALSE(d
.received_data_before_response());
4085 VLOG(1) << " Received " << d
.bytes_received() << " bytes"
4086 << " status = " << r
->status().status()
4087 << " error = " << r
->status().error();
4088 if (test_expect_success
[i
]) {
4089 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status())
4090 << " Parameter = \"" << test_file
<< "\"";
4092 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
4093 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH
, r
->status().error())
4094 << " Parameter = \"" << test_file
<< "\"";
4100 TEST_F(URLRequestTestHTTP
, NetworkQualityEstimator
) {
4101 ASSERT_TRUE(test_server_
.Start());
4102 // Enable requests to local host to be used for network quality estimation.
4103 std::map
<std::string
, std::string
> variation_params
;
4104 NetworkQualityEstimator
estimator(scoped_ptr
<net::ExternalEstimateProvider
>(),
4105 variation_params
, true, true);
4108 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
4109 TestURLRequestContext
context(true);
4110 context
.set_network_quality_estimator(&estimator
);
4111 context
.set_network_delegate(&network_delegate
);
4114 std::string url
= "echo.html";
4116 scoped_ptr
<URLRequest
> r(
4117 context
.CreateRequest(test_server_
.GetURL(url
), DEFAULT_PRIORITY
, &d
));
4120 base::RunLoop().Run();
4122 base::TimeDelta rtt
;
4124 EXPECT_TRUE(estimator
.GetRTTEstimate(&rtt
));
4125 EXPECT_TRUE(estimator
.GetDownlinkThroughputKbpsEstimate(&kbps
));
4126 EXPECT_GE(rtt
, base::TimeDelta());
4127 EXPECT_LT(rtt
, base::TimeDelta::Max());
4130 // Verify that histograms are not populated. They should populate only when
4131 // there is a change in ConnectionType.
4132 base::HistogramTester histogram_tester
;
4133 histogram_tester
.ExpectTotalCount("NQE.PeakKbps.Unknown", 0);
4134 histogram_tester
.ExpectTotalCount("NQE.FastestRTT.Unknown", 0);
4136 NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests(
4137 NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI
);
4138 base::MessageLoop::current()->RunUntilIdle();
4139 histogram_tester
.ExpectTotalCount("NQE.PeakKbps.Unknown", 1);
4140 histogram_tester
.ExpectTotalCount("NQE.FastestRTT.Unknown", 1);
4143 TEST_F(URLRequestTestHTTP
, RedirectLoadTiming
) {
4144 ASSERT_TRUE(test_server_
.Start());
4146 GURL destination_url
= test_server_
.GetURL(std::string());
4148 test_server_
.GetURL("server-redirect?" + destination_url
.spec());
4150 scoped_ptr
<URLRequest
> req(
4151 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
4153 base::RunLoop().Run();
4155 EXPECT_EQ(1, d
.response_started_count());
4156 EXPECT_EQ(1, d
.received_redirect_count());
4157 EXPECT_EQ(destination_url
, req
->url());
4158 EXPECT_EQ(original_url
, req
->original_url());
4159 ASSERT_EQ(2U, req
->url_chain().size());
4160 EXPECT_EQ(original_url
, req
->url_chain()[0]);
4161 EXPECT_EQ(destination_url
, req
->url_chain()[1]);
4163 LoadTimingInfo load_timing_info_before_redirect
;
4164 EXPECT_TRUE(default_network_delegate_
.GetLoadTimingInfoBeforeRedirect(
4165 &load_timing_info_before_redirect
));
4166 TestLoadTimingNotReused(load_timing_info_before_redirect
,
4167 CONNECT_TIMING_HAS_DNS_TIMES
);
4169 LoadTimingInfo load_timing_info
;
4170 req
->GetLoadTimingInfo(&load_timing_info
);
4171 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
4173 // Check that a new socket was used on redirect, since the server does not
4174 // supposed keep-alive sockets, and that the times before the redirect are
4175 // before the ones recorded for the second request.
4176 EXPECT_NE(load_timing_info_before_redirect
.socket_log_id
,
4177 load_timing_info
.socket_log_id
);
4178 EXPECT_LE(load_timing_info_before_redirect
.receive_headers_end
,
4179 load_timing_info
.connect_timing
.connect_start
);
4182 TEST_F(URLRequestTestHTTP
, MultipleRedirectTest
) {
4183 ASSERT_TRUE(test_server_
.Start());
4185 GURL destination_url
= test_server_
.GetURL(std::string());
4186 GURL middle_redirect_url
=
4187 test_server_
.GetURL("server-redirect?" + destination_url
.spec());
4188 GURL original_url
= test_server_
.GetURL(
4189 "server-redirect?" + middle_redirect_url
.spec());
4191 scoped_ptr
<URLRequest
> req(
4192 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
4194 base::RunLoop().Run();
4196 EXPECT_EQ(1, d
.response_started_count());
4197 EXPECT_EQ(2, d
.received_redirect_count());
4198 EXPECT_EQ(destination_url
, req
->url());
4199 EXPECT_EQ(original_url
, req
->original_url());
4200 ASSERT_EQ(3U, req
->url_chain().size());
4201 EXPECT_EQ(original_url
, req
->url_chain()[0]);
4202 EXPECT_EQ(middle_redirect_url
, req
->url_chain()[1]);
4203 EXPECT_EQ(destination_url
, req
->url_chain()[2]);
4206 // First and second pieces of information logged by delegates to URLRequests.
4207 const char kFirstDelegateInfo
[] = "Wonderful delegate";
4208 const char kSecondDelegateInfo
[] = "Exciting delegate";
4210 // Logs delegate information to a URLRequest. The first string is logged
4211 // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is
4212 // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then
4213 // another asynchronous call is used to clear the delegate information
4214 // before calling a callback. The object then deletes itself.
4215 class AsyncDelegateLogger
: public base::RefCounted
<AsyncDelegateLogger
> {
4217 typedef base::Callback
<void()> Callback
;
4219 // Each time delegate information is added to the URLRequest, the resulting
4220 // load state is checked. The expected load state after each request is
4221 // passed in as an argument.
4222 static void Run(URLRequest
* url_request
,
4223 LoadState expected_first_load_state
,
4224 LoadState expected_second_load_state
,
4225 LoadState expected_third_load_state
,
4226 const Callback
& callback
) {
4227 AsyncDelegateLogger
* logger
= new AsyncDelegateLogger(
4229 expected_first_load_state
,
4230 expected_second_load_state
,
4231 expected_third_load_state
,
4236 // Checks that the log entries, starting with log_position, contain the
4237 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
4238 // recorded. Returns the index of entry after the expected number of
4239 // events this logged, or entries.size() if there aren't enough entries.
4240 static size_t CheckDelegateInfo(const TestNetLogEntry::List
& entries
,
4241 size_t log_position
) {
4242 // There should be 4 DELEGATE_INFO events: Two begins and two ends.
4243 if (log_position
+ 3 >= entries
.size()) {
4244 ADD_FAILURE() << "Not enough log entries";
4245 return entries
.size();
4247 std::string delegate_info
;
4248 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO
, entries
[log_position
].type
);
4249 EXPECT_EQ(NetLog::PHASE_BEGIN
, entries
[log_position
].phase
);
4250 EXPECT_TRUE(entries
[log_position
].GetStringValue("delegate_info",
4252 EXPECT_EQ(kFirstDelegateInfo
, delegate_info
);
4255 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO
, entries
[log_position
].type
);
4256 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4259 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO
, entries
[log_position
].type
);
4260 EXPECT_EQ(NetLog::PHASE_BEGIN
, entries
[log_position
].phase
);
4261 EXPECT_TRUE(entries
[log_position
].GetStringValue("delegate_info",
4263 EXPECT_EQ(kSecondDelegateInfo
, delegate_info
);
4266 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO
, entries
[log_position
].type
);
4267 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4269 return log_position
+ 1;
4272 // Find delegate request begin and end messages for OnBeforeNetworkStart.
4273 // Returns the position of the end message.
4274 static size_t ExpectBeforeNetworkEvents(const TestNetLogEntry::List
& entries
,
4275 size_t log_position
) {
4277 ExpectLogContainsSomewhereAfter(entries
,
4279 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4280 NetLog::PHASE_BEGIN
);
4281 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
,
4282 entries
[log_position
+ 1].type
);
4283 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
+ 1].phase
);
4284 return log_position
+ 1;
4288 friend class base::RefCounted
<AsyncDelegateLogger
>;
4290 AsyncDelegateLogger(URLRequest
* url_request
,
4291 LoadState expected_first_load_state
,
4292 LoadState expected_second_load_state
,
4293 LoadState expected_third_load_state
,
4294 const Callback
& callback
)
4295 : url_request_(url_request
),
4296 expected_first_load_state_(expected_first_load_state
),
4297 expected_second_load_state_(expected_second_load_state
),
4298 expected_third_load_state_(expected_third_load_state
),
4299 callback_(callback
) {
4302 ~AsyncDelegateLogger() {}
4305 url_request_
->LogBlockedBy(kFirstDelegateInfo
);
4306 LoadStateWithParam load_state
= url_request_
->GetLoadState();
4307 EXPECT_EQ(expected_first_load_state_
, load_state
.state
);
4308 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo
), load_state
.param
);
4309 base::ThreadTaskRunnerHandle::Get()->PostTask(
4310 FROM_HERE
, base::Bind(&AsyncDelegateLogger::LogSecondDelegate
, this));
4313 void LogSecondDelegate() {
4314 url_request_
->LogAndReportBlockedBy(kSecondDelegateInfo
);
4315 LoadStateWithParam load_state
= url_request_
->GetLoadState();
4316 EXPECT_EQ(expected_second_load_state_
, load_state
.state
);
4317 if (expected_second_load_state_
== LOAD_STATE_WAITING_FOR_DELEGATE
) {
4318 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo
), load_state
.param
);
4320 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo
), load_state
.param
);
4322 base::ThreadTaskRunnerHandle::Get()->PostTask(
4323 FROM_HERE
, base::Bind(&AsyncDelegateLogger::LogComplete
, this));
4326 void LogComplete() {
4327 url_request_
->LogUnblocked();
4328 LoadStateWithParam load_state
= url_request_
->GetLoadState();
4329 EXPECT_EQ(expected_third_load_state_
, load_state
.state
);
4330 if (expected_second_load_state_
== LOAD_STATE_WAITING_FOR_DELEGATE
)
4331 EXPECT_EQ(base::string16(), load_state
.param
);
4335 URLRequest
* url_request_
;
4336 const int expected_first_load_state_
;
4337 const int expected_second_load_state_
;
4338 const int expected_third_load_state_
;
4339 const Callback callback_
;
4341 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger
);
4344 // NetworkDelegate that logs delegate information before a request is started,
4345 // before headers are sent, when headers are read, and when auth information
4346 // is requested. Uses AsyncDelegateLogger.
4347 class AsyncLoggingNetworkDelegate
: public TestNetworkDelegate
{
4349 AsyncLoggingNetworkDelegate() {}
4350 ~AsyncLoggingNetworkDelegate() override
{}
4352 // NetworkDelegate implementation.
4353 int OnBeforeURLRequest(URLRequest
* request
,
4354 const CompletionCallback
& callback
,
4355 GURL
* new_url
) override
{
4356 TestNetworkDelegate::OnBeforeURLRequest(request
, callback
, new_url
);
4357 return RunCallbackAsynchronously(request
, callback
);
4360 int OnBeforeSendHeaders(URLRequest
* request
,
4361 const CompletionCallback
& callback
,
4362 HttpRequestHeaders
* headers
) override
{
4363 TestNetworkDelegate::OnBeforeSendHeaders(request
, callback
, headers
);
4364 return RunCallbackAsynchronously(request
, callback
);
4367 int OnHeadersReceived(
4368 URLRequest
* request
,
4369 const CompletionCallback
& callback
,
4370 const HttpResponseHeaders
* original_response_headers
,
4371 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
,
4372 GURL
* allowed_unsafe_redirect_url
) override
{
4373 TestNetworkDelegate::OnHeadersReceived(request
,
4375 original_response_headers
,
4376 override_response_headers
,
4377 allowed_unsafe_redirect_url
);
4378 return RunCallbackAsynchronously(request
, callback
);
4381 NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
4382 URLRequest
* request
,
4383 const AuthChallengeInfo
& auth_info
,
4384 const AuthCallback
& callback
,
4385 AuthCredentials
* credentials
) override
{
4386 AsyncDelegateLogger::Run(
4388 LOAD_STATE_WAITING_FOR_DELEGATE
,
4389 LOAD_STATE_WAITING_FOR_DELEGATE
,
4390 LOAD_STATE_WAITING_FOR_DELEGATE
,
4391 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume
,
4392 callback
, credentials
));
4393 return AUTH_REQUIRED_RESPONSE_IO_PENDING
;
4397 static int RunCallbackAsynchronously(
4398 URLRequest
* request
,
4399 const CompletionCallback
& callback
) {
4400 AsyncDelegateLogger::Run(
4402 LOAD_STATE_WAITING_FOR_DELEGATE
,
4403 LOAD_STATE_WAITING_FOR_DELEGATE
,
4404 LOAD_STATE_WAITING_FOR_DELEGATE
,
4405 base::Bind(callback
, OK
));
4406 return ERR_IO_PENDING
;
4409 static void SetAuthAndResume(const AuthCallback
& callback
,
4410 AuthCredentials
* credentials
) {
4411 *credentials
= AuthCredentials(kUser
, kSecret
);
4412 callback
.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
);
4415 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate
);
4418 // URLRequest::Delegate that logs delegate information when the headers
4419 // are received, when each read completes, and during redirects. Uses
4420 // AsyncDelegateLogger. Can optionally cancel a request in any phase.
4422 // Inherits from TestDelegate to reuse the TestDelegate code to handle
4423 // advancing to the next step in most cases, as well as cancellation.
4424 class AsyncLoggingUrlRequestDelegate
: public TestDelegate
{
4428 CANCEL_ON_RECEIVED_REDIRECT
,
4429 CANCEL_ON_RESPONSE_STARTED
,
4430 CANCEL_ON_READ_COMPLETED
4433 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage
)
4434 : cancel_stage_(cancel_stage
) {
4435 if (cancel_stage
== CANCEL_ON_RECEIVED_REDIRECT
)
4436 set_cancel_in_received_redirect(true);
4437 else if (cancel_stage
== CANCEL_ON_RESPONSE_STARTED
)
4438 set_cancel_in_response_started(true);
4439 else if (cancel_stage
== CANCEL_ON_READ_COMPLETED
)
4440 set_cancel_in_received_data(true);
4442 ~AsyncLoggingUrlRequestDelegate() override
{}
4444 // URLRequest::Delegate implementation:
4445 void OnReceivedRedirect(URLRequest
* request
,
4446 const RedirectInfo
& redirect_info
,
4447 bool* defer_redirect
) override
{
4448 *defer_redirect
= true;
4449 AsyncDelegateLogger::Run(
4451 LOAD_STATE_WAITING_FOR_DELEGATE
,
4452 LOAD_STATE_WAITING_FOR_DELEGATE
,
4453 LOAD_STATE_WAITING_FOR_DELEGATE
,
4455 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete
,
4456 base::Unretained(this), request
, redirect_info
));
4459 void OnResponseStarted(URLRequest
* request
) override
{
4460 AsyncDelegateLogger::Run(
4462 LOAD_STATE_WAITING_FOR_DELEGATE
,
4463 LOAD_STATE_WAITING_FOR_DELEGATE
,
4464 LOAD_STATE_WAITING_FOR_DELEGATE
,
4466 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete
,
4467 base::Unretained(this), request
));
4470 void OnReadCompleted(URLRequest
* request
, int bytes_read
) override
{
4471 AsyncDelegateLogger::Run(
4477 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete
,
4478 base::Unretained(this), request
, bytes_read
));
4482 void OnReceivedRedirectLoggingComplete(URLRequest
* request
,
4483 const RedirectInfo
& redirect_info
) {
4484 bool defer_redirect
= false;
4485 TestDelegate::OnReceivedRedirect(request
, redirect_info
, &defer_redirect
);
4486 // FollowDeferredRedirect should not be called after cancellation.
4487 if (cancel_stage_
== CANCEL_ON_RECEIVED_REDIRECT
)
4489 if (!defer_redirect
)
4490 request
->FollowDeferredRedirect();
4493 void OnResponseStartedLoggingComplete(URLRequest
* request
) {
4494 // The parent class continues the request.
4495 TestDelegate::OnResponseStarted(request
);
4498 void AfterReadCompletedLoggingComplete(URLRequest
* request
, int bytes_read
) {
4499 // The parent class continues the request.
4500 TestDelegate::OnReadCompleted(request
, bytes_read
);
4503 const CancelStage cancel_stage_
;
4505 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate
);
4508 // Tests handling of delegate info before a request starts.
4509 TEST_F(URLRequestTestHTTP
, DelegateInfoBeforeStart
) {
4510 ASSERT_TRUE(test_server_
.Start());
4512 TestDelegate request_delegate
;
4513 TestURLRequestContext
context(true);
4514 context
.set_network_delegate(NULL
);
4515 context
.set_net_log(&net_log_
);
4519 scoped_ptr
<URLRequest
> r(
4520 context
.CreateRequest(test_server_
.GetURL("empty.html"),
4521 DEFAULT_PRIORITY
, &request_delegate
));
4522 LoadStateWithParam load_state
= r
->GetLoadState();
4523 EXPECT_EQ(LOAD_STATE_IDLE
, load_state
.state
);
4524 EXPECT_EQ(base::string16(), load_state
.param
);
4526 AsyncDelegateLogger::Run(
4528 LOAD_STATE_WAITING_FOR_DELEGATE
,
4529 LOAD_STATE_WAITING_FOR_DELEGATE
,
4531 base::Bind(&URLRequest::Start
, base::Unretained(r
.get())));
4533 base::RunLoop().Run();
4535 EXPECT_EQ(200, r
->GetResponseCode());
4536 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
4539 TestNetLogEntry::List entries
;
4540 net_log_
.GetEntries(&entries
);
4541 size_t log_position
= ExpectLogContainsSomewhereAfter(
4544 NetLog::TYPE_DELEGATE_INFO
,
4545 NetLog::PHASE_BEGIN
);
4547 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
, log_position
);
4549 // Nothing else should add any delegate info to the request.
4550 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4551 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4554 // Tests handling of delegate info from a network delegate.
4555 TEST_F(URLRequestTestHTTP
, NetworkDelegateInfo
) {
4556 ASSERT_TRUE(test_server_
.Start());
4558 TestDelegate request_delegate
;
4559 AsyncLoggingNetworkDelegate network_delegate
;
4560 TestURLRequestContext
context(true);
4561 context
.set_network_delegate(&network_delegate
);
4562 context
.set_net_log(&net_log_
);
4566 scoped_ptr
<URLRequest
> r(
4567 context
.CreateRequest(test_server_
.GetURL("simple.html"),
4568 DEFAULT_PRIORITY
, &request_delegate
));
4569 LoadStateWithParam load_state
= r
->GetLoadState();
4570 EXPECT_EQ(LOAD_STATE_IDLE
, load_state
.state
);
4571 EXPECT_EQ(base::string16(), load_state
.param
);
4574 base::RunLoop().Run();
4576 EXPECT_EQ(200, r
->GetResponseCode());
4577 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
4578 EXPECT_EQ(1, network_delegate
.created_requests());
4579 EXPECT_EQ(0, network_delegate
.destroyed_requests());
4581 EXPECT_EQ(1, network_delegate
.destroyed_requests());
4583 size_t log_position
= 0;
4584 TestNetLogEntry::List entries
;
4585 net_log_
.GetEntries(&entries
);
4586 for (size_t i
= 0; i
< 3; ++i
) {
4587 log_position
= ExpectLogContainsSomewhereAfter(
4590 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4591 NetLog::PHASE_BEGIN
);
4593 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4596 ASSERT_LT(log_position
, entries
.size());
4597 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4598 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4601 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4602 entries
, log_position
+ 1);
4606 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4607 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4610 // Tests handling of delegate info from a network delegate in the case of an
4612 TEST_F(URLRequestTestHTTP
, NetworkDelegateInfoRedirect
) {
4613 ASSERT_TRUE(test_server_
.Start());
4615 TestDelegate request_delegate
;
4616 AsyncLoggingNetworkDelegate network_delegate
;
4617 TestURLRequestContext
context(true);
4618 context
.set_network_delegate(&network_delegate
);
4619 context
.set_net_log(&net_log_
);
4623 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
4624 test_server_
.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY
,
4625 &request_delegate
));
4626 LoadStateWithParam load_state
= r
->GetLoadState();
4627 EXPECT_EQ(LOAD_STATE_IDLE
, load_state
.state
);
4628 EXPECT_EQ(base::string16(), load_state
.param
);
4631 base::RunLoop().Run();
4633 EXPECT_EQ(200, r
->GetResponseCode());
4634 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
4635 EXPECT_EQ(2, network_delegate
.created_requests());
4636 EXPECT_EQ(0, network_delegate
.destroyed_requests());
4638 EXPECT_EQ(1, network_delegate
.destroyed_requests());
4640 size_t log_position
= 0;
4641 TestNetLogEntry::List entries
;
4642 net_log_
.GetEntries(&entries
);
4643 // The NetworkDelegate logged information in OnBeforeURLRequest,
4644 // OnBeforeSendHeaders, and OnHeadersReceived.
4645 for (size_t i
= 0; i
< 3; ++i
) {
4646 log_position
= ExpectLogContainsSomewhereAfter(
4649 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4650 NetLog::PHASE_BEGIN
);
4652 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4655 ASSERT_LT(log_position
, entries
.size());
4656 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4657 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4660 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4661 entries
, log_position
+ 1);
4665 // The URLRequest::Delegate then gets informed about the redirect.
4666 log_position
= ExpectLogContainsSomewhereAfter(
4669 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4670 NetLog::PHASE_BEGIN
);
4672 // The NetworkDelegate logged information in the same three events as before.
4673 for (size_t i
= 0; i
< 3; ++i
) {
4674 log_position
= ExpectLogContainsSomewhereAfter(
4677 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4678 NetLog::PHASE_BEGIN
);
4680 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4683 ASSERT_LT(log_position
, entries
.size());
4684 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4685 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4688 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4689 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4692 // Tests handling of delegate info from a network delegate in the case of HTTP
4694 TEST_F(URLRequestTestHTTP
, NetworkDelegateInfoAuth
) {
4695 ASSERT_TRUE(test_server_
.Start());
4697 TestDelegate request_delegate
;
4698 AsyncLoggingNetworkDelegate network_delegate
;
4699 TestURLRequestContext
context(true);
4700 context
.set_network_delegate(&network_delegate
);
4701 context
.set_net_log(&net_log_
);
4705 scoped_ptr
<URLRequest
> r(
4706 context
.CreateRequest(test_server_
.GetURL("auth-basic"),
4707 DEFAULT_PRIORITY
, &request_delegate
));
4708 LoadStateWithParam load_state
= r
->GetLoadState();
4709 EXPECT_EQ(LOAD_STATE_IDLE
, load_state
.state
);
4710 EXPECT_EQ(base::string16(), load_state
.param
);
4713 base::RunLoop().Run();
4715 EXPECT_EQ(200, r
->GetResponseCode());
4716 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
4717 EXPECT_EQ(1, network_delegate
.created_requests());
4718 EXPECT_EQ(0, network_delegate
.destroyed_requests());
4720 EXPECT_EQ(1, network_delegate
.destroyed_requests());
4722 size_t log_position
= 0;
4723 TestNetLogEntry::List entries
;
4724 net_log_
.GetEntries(&entries
);
4725 // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4726 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4727 // OnBeforeURLRequest and OnBeforeSendHeaders.
4728 for (size_t i
= 0; i
< 6; ++i
) {
4729 log_position
= ExpectLogContainsSomewhereAfter(
4732 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4733 NetLog::PHASE_BEGIN
);
4735 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4738 ASSERT_LT(log_position
, entries
.size());
4739 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4740 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4743 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4744 entries
, log_position
+ 1);
4748 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4749 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4752 // Tests handling of delegate info from a URLRequest::Delegate.
4753 TEST_F(URLRequestTestHTTP
, URLRequestDelegateInfo
) {
4754 ASSERT_TRUE(test_server_
.Start());
4756 AsyncLoggingUrlRequestDelegate
request_delegate(
4757 AsyncLoggingUrlRequestDelegate::NO_CANCEL
);
4758 TestURLRequestContext
context(true);
4759 context
.set_network_delegate(NULL
);
4760 context
.set_net_log(&net_log_
);
4764 // A chunked response with delays between chunks is used to make sure that
4765 // attempts by the URLRequest delegate to log information while reading the
4766 // body are ignored. Since they are ignored, this test is robust against
4767 // the possibility of multiple reads being combined in the unlikely event
4769 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
4770 test_server_
.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY
,
4771 &request_delegate
));
4772 LoadStateWithParam load_state
= r
->GetLoadState();
4774 base::RunLoop().Run();
4776 EXPECT_EQ(200, r
->GetResponseCode());
4777 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
4780 TestNetLogEntry::List entries
;
4781 net_log_
.GetEntries(&entries
);
4783 size_t log_position
= 0;
4785 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4786 entries
, log_position
);
4788 // The delegate info should only have been logged on header complete. Other
4789 // times it should silently be ignored.
4791 ExpectLogContainsSomewhereAfter(entries
,
4793 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4794 NetLog::PHASE_BEGIN
);
4796 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4799 ASSERT_LT(log_position
, entries
.size());
4800 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4801 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4803 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4804 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4805 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4806 entries
, log_position
+ 1, NetLog::TYPE_URL_REQUEST_DELEGATE
));
4809 // Tests handling of delegate info from a URLRequest::Delegate in the case of
4810 // an HTTP redirect.
4811 TEST_F(URLRequestTestHTTP
, URLRequestDelegateInfoOnRedirect
) {
4812 ASSERT_TRUE(test_server_
.Start());
4814 AsyncLoggingUrlRequestDelegate
request_delegate(
4815 AsyncLoggingUrlRequestDelegate::NO_CANCEL
);
4816 TestURLRequestContext
context(true);
4817 context
.set_network_delegate(NULL
);
4818 context
.set_net_log(&net_log_
);
4822 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
4823 test_server_
.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY
,
4824 &request_delegate
));
4825 LoadStateWithParam load_state
= r
->GetLoadState();
4827 base::RunLoop().Run();
4829 EXPECT_EQ(200, r
->GetResponseCode());
4830 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
4833 TestNetLogEntry::List entries
;
4834 net_log_
.GetEntries(&entries
);
4836 // Delegate info should only have been logged in OnReceivedRedirect and
4837 // OnResponseStarted.
4838 size_t log_position
= 0;
4839 for (int i
= 0; i
< 2; ++i
) {
4841 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4842 entries
, log_position
) + 1;
4845 log_position
= ExpectLogContainsSomewhereAfter(
4848 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4849 NetLog::PHASE_BEGIN
);
4851 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4854 ASSERT_LT(log_position
, entries
.size());
4855 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4856 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4859 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4860 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4861 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4862 entries
, log_position
+ 1, NetLog::TYPE_URL_REQUEST_DELEGATE
));
4865 // Tests handling of delegate info from a URLRequest::Delegate in the case of
4866 // an HTTP redirect, with cancellation at various points.
4867 TEST_F(URLRequestTestHTTP
, URLRequestDelegateOnRedirectCancelled
) {
4868 ASSERT_TRUE(test_server_
.Start());
4870 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages
[] = {
4871 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT
,
4872 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED
,
4873 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED
,
4876 for (size_t test_case
= 0; test_case
< arraysize(kCancelStages
);
4878 AsyncLoggingUrlRequestDelegate
request_delegate(kCancelStages
[test_case
]);
4879 TestURLRequestContext
context(true);
4881 context
.set_network_delegate(NULL
);
4882 context
.set_net_log(&net_log
);
4886 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
4887 test_server_
.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY
,
4888 &request_delegate
));
4889 LoadStateWithParam load_state
= r
->GetLoadState();
4891 base::RunLoop().Run();
4892 EXPECT_EQ(URLRequestStatus::CANCELED
, r
->status().status());
4895 TestNetLogEntry::List entries
;
4896 net_log
.GetEntries(&entries
);
4898 // Delegate info is always logged in both OnReceivedRedirect and
4899 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4900 // OnResponseStarted delegate call is after cancellation, but logging is
4901 // still currently supported in that call.
4902 size_t log_position
= 0;
4903 for (int i
= 0; i
< 2; ++i
) {
4905 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4906 entries
, log_position
) + 1;
4909 log_position
= ExpectLogContainsSomewhereAfter(
4912 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4913 NetLog::PHASE_BEGIN
);
4915 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4918 ASSERT_LT(log_position
, entries
.size());
4919 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4920 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4923 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4924 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4925 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4926 entries
, log_position
+ 1, NetLog::TYPE_URL_REQUEST_DELEGATE
));
4932 const char kExtraHeader
[] = "Allow-Snafu";
4933 const char kExtraValue
[] = "fubar";
4935 class RedirectWithAdditionalHeadersDelegate
: public TestDelegate
{
4936 void OnReceivedRedirect(URLRequest
* request
,
4937 const RedirectInfo
& redirect_info
,
4938 bool* defer_redirect
) override
{
4939 TestDelegate::OnReceivedRedirect(request
, redirect_info
, defer_redirect
);
4940 request
->SetExtraRequestHeaderByName(kExtraHeader
, kExtraValue
, false);
4946 TEST_F(URLRequestTestHTTP
, RedirectWithAdditionalHeadersTest
) {
4947 ASSERT_TRUE(test_server_
.Start());
4949 GURL destination_url
= test_server_
.GetURL(
4950 "echoheader?" + std::string(kExtraHeader
));
4951 GURL original_url
= test_server_
.GetURL(
4952 "server-redirect?" + destination_url
.spec());
4953 RedirectWithAdditionalHeadersDelegate d
;
4954 scoped_ptr
<URLRequest
> req(
4955 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
4957 base::RunLoop().Run();
4960 const HttpRequestHeaders
& headers
= req
->extra_request_headers();
4961 EXPECT_TRUE(headers
.GetHeader(kExtraHeader
, &value
));
4962 EXPECT_EQ(kExtraValue
, value
);
4963 EXPECT_FALSE(req
->is_pending());
4964 EXPECT_FALSE(req
->is_redirecting());
4965 EXPECT_EQ(kExtraValue
, d
.data_received());
4970 const char kExtraHeaderToRemove
[] = "To-Be-Removed";
4972 class RedirectWithHeaderRemovalDelegate
: public TestDelegate
{
4973 void OnReceivedRedirect(URLRequest
* request
,
4974 const RedirectInfo
& redirect_info
,
4975 bool* defer_redirect
) override
{
4976 TestDelegate::OnReceivedRedirect(request
, redirect_info
, defer_redirect
);
4977 request
->RemoveRequestHeaderByName(kExtraHeaderToRemove
);
4983 TEST_F(URLRequestTestHTTP
, RedirectWithHeaderRemovalTest
) {
4984 ASSERT_TRUE(test_server_
.Start());
4986 GURL destination_url
= test_server_
.GetURL(
4987 "echoheader?" + std::string(kExtraHeaderToRemove
));
4988 GURL original_url
= test_server_
.GetURL(
4989 "server-redirect?" + destination_url
.spec());
4990 RedirectWithHeaderRemovalDelegate d
;
4991 scoped_ptr
<URLRequest
> req(
4992 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
4993 req
->SetExtraRequestHeaderByName(kExtraHeaderToRemove
, "dummy", false);
4995 base::RunLoop().Run();
4998 const HttpRequestHeaders
& headers
= req
->extra_request_headers();
4999 EXPECT_FALSE(headers
.GetHeader(kExtraHeaderToRemove
, &value
));
5000 EXPECT_FALSE(req
->is_pending());
5001 EXPECT_FALSE(req
->is_redirecting());
5002 EXPECT_EQ("None", d
.data_received());
5005 TEST_F(URLRequestTestHTTP
, CancelTest
) {
5008 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5009 GURL("http://www.google.com/"), DEFAULT_PRIORITY
, &d
));
5012 EXPECT_TRUE(r
->is_pending());
5016 base::RunLoop().Run();
5018 // We expect to receive OnResponseStarted even though the request has been
5020 EXPECT_EQ(1, d
.response_started_count());
5021 EXPECT_EQ(0, d
.bytes_received());
5022 EXPECT_FALSE(d
.received_data_before_response());
5026 TEST_F(URLRequestTestHTTP
, CancelTest2
) {
5027 ASSERT_TRUE(test_server_
.Start());
5031 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5032 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
5034 d
.set_cancel_in_response_started(true);
5037 EXPECT_TRUE(r
->is_pending());
5039 base::RunLoop().Run();
5041 EXPECT_EQ(1, d
.response_started_count());
5042 EXPECT_EQ(0, d
.bytes_received());
5043 EXPECT_FALSE(d
.received_data_before_response());
5044 EXPECT_EQ(URLRequestStatus::CANCELED
, r
->status().status());
5048 TEST_F(URLRequestTestHTTP
, CancelTest3
) {
5049 ASSERT_TRUE(test_server_
.Start());
5053 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5054 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
5056 d
.set_cancel_in_received_data(true);
5059 EXPECT_TRUE(r
->is_pending());
5061 base::RunLoop().Run();
5063 EXPECT_EQ(1, d
.response_started_count());
5064 // There is no guarantee about how much data was received
5065 // before the cancel was issued. It could have been 0 bytes,
5066 // or it could have been all the bytes.
5067 // EXPECT_EQ(0, d.bytes_received());
5068 EXPECT_FALSE(d
.received_data_before_response());
5069 EXPECT_EQ(URLRequestStatus::CANCELED
, r
->status().status());
5073 TEST_F(URLRequestTestHTTP
, CancelTest4
) {
5074 ASSERT_TRUE(test_server_
.Start());
5078 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5079 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
5082 EXPECT_TRUE(r
->is_pending());
5084 // The request will be implicitly canceled when it is destroyed. The
5085 // test delegate must not post a quit message when this happens because
5086 // this test doesn't actually have a message loop. The quit message would
5087 // get put on this thread's message queue and the next test would exit
5088 // early, causing problems.
5089 d
.set_quit_on_complete(false);
5091 // expect things to just cleanup properly.
5093 // we won't actually get a received reponse here because we've never run the
5095 EXPECT_FALSE(d
.received_data_before_response());
5096 EXPECT_EQ(0, d
.bytes_received());
5099 TEST_F(URLRequestTestHTTP
, CancelTest5
) {
5100 ASSERT_TRUE(test_server_
.Start());
5105 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5106 test_server_
.GetURL("cachetime"), DEFAULT_PRIORITY
, &d
));
5108 base::RunLoop().Run();
5109 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
5112 // cancel read from cache (see bug 990242)
5115 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5116 test_server_
.GetURL("cachetime"), DEFAULT_PRIORITY
, &d
));
5119 base::RunLoop().Run();
5121 EXPECT_EQ(URLRequestStatus::CANCELED
, r
->status().status());
5122 EXPECT_EQ(1, d
.response_started_count());
5123 EXPECT_EQ(0, d
.bytes_received());
5124 EXPECT_FALSE(d
.received_data_before_response());
5128 TEST_F(URLRequestTestHTTP
, PostTest
) {
5129 ASSERT_TRUE(test_server_
.Start());
5130 HTTPUploadDataOperationTest("POST");
5133 TEST_F(URLRequestTestHTTP
, PutTest
) {
5134 ASSERT_TRUE(test_server_
.Start());
5135 HTTPUploadDataOperationTest("PUT");
5138 TEST_F(URLRequestTestHTTP
, PostEmptyTest
) {
5139 ASSERT_TRUE(test_server_
.Start());
5143 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5144 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
));
5145 r
->set_method("POST");
5148 EXPECT_TRUE(r
->is_pending());
5150 base::RunLoop().Run();
5152 ASSERT_EQ(1, d
.response_started_count())
5153 << "request failed: " << r
->status().status()
5154 << ", error: " << r
->status().error();
5156 EXPECT_FALSE(d
.received_data_before_response());
5157 EXPECT_TRUE(d
.data_received().empty());
5161 TEST_F(URLRequestTestHTTP
, PostFileTest
) {
5162 ASSERT_TRUE(test_server_
.Start());
5166 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5167 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
));
5168 r
->set_method("POST");
5171 PathService::Get(base::DIR_EXE
, &dir
);
5172 base::SetCurrentDirectory(dir
);
5174 ScopedVector
<UploadElementReader
> element_readers
;
5176 base::FilePath path
;
5177 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
5178 path
= path
.Append(kTestFilePath
);
5179 path
= path
.Append(FILE_PATH_LITERAL("with-headers.html"));
5180 element_readers
.push_back(
5181 new UploadFileElementReader(base::ThreadTaskRunnerHandle::Get().get(),
5182 path
, 0, kuint64max
, base::Time()));
5183 r
->set_upload(make_scoped_ptr
<UploadDataStream
>(
5184 new ElementsUploadDataStream(element_readers
.Pass(), 0)));
5187 EXPECT_TRUE(r
->is_pending());
5189 base::RunLoop().Run();
5192 ASSERT_EQ(true, base::GetFileSize(path
, &size64
));
5193 ASSERT_LE(size64
, std::numeric_limits
<int>::max());
5194 int size
= static_cast<int>(size64
);
5195 scoped_ptr
<char[]> buf(new char[size
]);
5197 ASSERT_EQ(size
, base::ReadFile(path
, buf
.get(), size
));
5199 ASSERT_EQ(1, d
.response_started_count())
5200 << "request failed: " << r
->status().status()
5201 << ", error: " << r
->status().error();
5203 EXPECT_FALSE(d
.received_data_before_response());
5205 EXPECT_EQ(size
, d
.bytes_received());
5206 EXPECT_EQ(std::string(&buf
[0], size
), d
.data_received());
5210 TEST_F(URLRequestTestHTTP
, PostUnreadableFileTest
) {
5211 ASSERT_TRUE(test_server_
.Start());
5215 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5216 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
));
5217 r
->set_method("POST");
5219 ScopedVector
<UploadElementReader
> element_readers
;
5221 element_readers
.push_back(new UploadFileElementReader(
5222 base::ThreadTaskRunnerHandle::Get().get(),
5223 base::FilePath(FILE_PATH_LITERAL(
5224 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
5225 0, kuint64max
, base::Time()));
5226 r
->set_upload(make_scoped_ptr
<UploadDataStream
>(
5227 new ElementsUploadDataStream(element_readers
.Pass(), 0)));
5230 EXPECT_TRUE(r
->is_pending());
5232 base::RunLoop().Run();
5234 EXPECT_TRUE(d
.request_failed());
5235 EXPECT_FALSE(d
.received_data_before_response());
5236 EXPECT_EQ(0, d
.bytes_received());
5237 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
5238 EXPECT_EQ(ERR_FILE_NOT_FOUND
, r
->status().error());
5242 TEST_F(URLRequestTestHTTP
, TestPostChunkedDataBeforeStart
) {
5243 ASSERT_TRUE(test_server_
.Start());
5247 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5248 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
));
5249 r
->EnableChunkedUpload();
5250 r
->set_method("POST");
5251 AddChunksToUpload(r
.get());
5253 EXPECT_TRUE(r
->is_pending());
5255 base::RunLoop().Run();
5257 VerifyReceivedDataMatchesChunks(r
.get(), &d
);
5261 TEST_F(URLRequestTestHTTP
, TestPostChunkedDataJustAfterStart
) {
5262 ASSERT_TRUE(test_server_
.Start());
5266 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5267 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
));
5268 r
->EnableChunkedUpload();
5269 r
->set_method("POST");
5271 EXPECT_TRUE(r
->is_pending());
5272 AddChunksToUpload(r
.get());
5273 base::RunLoop().Run();
5275 VerifyReceivedDataMatchesChunks(r
.get(), &d
);
5279 TEST_F(URLRequestTestHTTP
, TestPostChunkedDataAfterStart
) {
5280 ASSERT_TRUE(test_server_
.Start());
5284 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5285 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
));
5286 r
->EnableChunkedUpload();
5287 r
->set_method("POST");
5289 EXPECT_TRUE(r
->is_pending());
5291 base::RunLoop().RunUntilIdle();
5292 AddChunksToUpload(r
.get());
5293 base::RunLoop().Run();
5295 VerifyReceivedDataMatchesChunks(r
.get(), &d
);
5299 TEST_F(URLRequestTestHTTP
, ResponseHeadersTest
) {
5300 ASSERT_TRUE(test_server_
.Start());
5303 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
5304 test_server_
.GetURL("files/with-headers.html"), DEFAULT_PRIORITY
, &d
));
5306 base::RunLoop().Run();
5308 const HttpResponseHeaders
* headers
= req
->response_headers();
5310 // Simple sanity check that response_info() accesses the same data.
5311 EXPECT_EQ(headers
, req
->response_info().headers
.get());
5314 EXPECT_TRUE(headers
->GetNormalizedHeader("cache-control", &header
));
5315 EXPECT_EQ("private", header
);
5318 EXPECT_TRUE(headers
->GetNormalizedHeader("content-type", &header
));
5319 EXPECT_EQ("text/html; charset=ISO-8859-1", header
);
5321 // The response has two "X-Multiple-Entries" headers.
5322 // This verfies our output has them concatenated together.
5324 EXPECT_TRUE(headers
->GetNormalizedHeader("x-multiple-entries", &header
));
5325 EXPECT_EQ("a, b", header
);
5328 TEST_F(URLRequestTestHTTP
, ProcessSTS
) {
5329 SpawnedTestServer::SSLOptions
ssl_options(
5330 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN
);
5331 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5333 base::FilePath(kTestFilePath
));
5334 ASSERT_TRUE(https_test_server
.Start());
5336 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5339 scoped_ptr
<URLRequest
> request(default_context_
.CreateRequest(
5340 https_test_server
.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY
,
5343 base::RunLoop().Run();
5345 TransportSecurityState
* security_state
=
5346 default_context_
.transport_security_state();
5347 TransportSecurityState::STSState sts_state
;
5348 TransportSecurityState::PKPState pkp_state
;
5350 security_state
->GetDynamicSTSState(test_server_hostname
, &sts_state
));
5352 security_state
->GetDynamicPKPState(test_server_hostname
, &pkp_state
));
5353 EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS
,
5354 sts_state
.upgrade_mode
);
5355 EXPECT_TRUE(sts_state
.include_subdomains
);
5356 EXPECT_FALSE(pkp_state
.include_subdomains
);
5357 #if defined(OS_ANDROID)
5358 // Android's CertVerifyProc does not (yet) handle pins.
5360 EXPECT_FALSE(pkp_state
.HasPublicKeyPins());
5364 TEST_F(URLRequestTestHTTP
, STSNotProcessedOnIP
) {
5365 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5366 SpawnedTestServer::SSLOptions(),
5367 base::FilePath(kTestFilePath
));
5368 ASSERT_TRUE(https_test_server
.Start());
5369 // Make sure this test fails if the test server is changed to not
5370 // listen on an IP by default.
5371 ASSERT_TRUE(https_test_server
.GetURL("").HostIsIPAddress());
5372 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5375 scoped_ptr
<URLRequest
> request(default_context_
.CreateRequest(
5376 https_test_server
.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY
,
5379 base::RunLoop().Run();
5381 TransportSecurityState
* security_state
=
5382 default_context_
.transport_security_state();
5383 TransportSecurityState::STSState sts_state
;
5385 security_state
->GetDynamicSTSState(test_server_hostname
, &sts_state
));
5388 // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
5389 // reject HPKP headers, and a test setting only HPKP headers will fail (no
5390 // PKPState present because header rejected).
5391 #if defined(OS_ANDROID)
5392 #define MAYBE_ProcessPKP DISABLED_ProcessPKP
5393 #define MAYBE_ProcessPKPAndSendReport DISABLED_ProcessPKPAndSendReport
5394 #define MAYBE_ProcessPKPReportOnly DISABLED_ProcessPKPReportOnly
5395 #define MAYBE_ProcessPKPReportOnlyWithNoViolation \
5396 DISABLED_ProcessPKPReportOnlyWithNoViolation
5398 #define MAYBE_ProcessPKP ProcessPKP
5399 #define MAYBE_ProcessPKPAndSendReport ProcessPKPAndSendReport
5400 #define MAYBE_ProcessPKPReportOnly ProcessPKPReportOnly
5401 #define MAYBE_ProcessPKPReportOnlyWithNoViolation \
5402 ProcessPKPReportOnlyWithNoViolation
5406 const char kHPKPReportUri
[] = "https://hpkp-report.test";
5409 // Tests that enabling HPKP on a domain does not affect the HSTS
5410 // validity/expiration.
5411 TEST_F(URLRequestTestHTTP
, MAYBE_ProcessPKP
) {
5412 GURL
report_uri(kHPKPReportUri
);
5413 SpawnedTestServer::SSLOptions
ssl_options(
5414 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN
);
5415 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5417 base::FilePath(kTestFilePath
));
5418 ASSERT_TRUE(https_test_server
.Start());
5420 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5423 scoped_ptr
<URLRequest
> request(default_context_
.CreateRequest(
5424 https_test_server
.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY
,
5427 base::RunLoop().Run();
5429 TransportSecurityState
* security_state
=
5430 default_context_
.transport_security_state();
5431 TransportSecurityState::STSState sts_state
;
5432 TransportSecurityState::PKPState pkp_state
;
5434 security_state
->GetDynamicSTSState(test_server_hostname
, &sts_state
));
5436 security_state
->GetDynamicPKPState(test_server_hostname
, &pkp_state
));
5437 EXPECT_EQ(TransportSecurityState::STSState::MODE_DEFAULT
,
5438 sts_state
.upgrade_mode
);
5439 EXPECT_FALSE(sts_state
.include_subdomains
);
5440 EXPECT_FALSE(pkp_state
.include_subdomains
);
5441 EXPECT_TRUE(pkp_state
.HasPublicKeyPins());
5442 EXPECT_EQ(report_uri
, pkp_state
.report_uri
);
5443 EXPECT_NE(sts_state
.expiry
, pkp_state
.expiry
);
5446 // Tests that reports get sent on HPKP violations when a report-uri is set.
5447 TEST_F(URLRequestTestHTTP
, MAYBE_ProcessPKPAndSendReport
) {
5448 GURL
report_uri(kHPKPReportUri
);
5449 SpawnedTestServer::SSLOptions
ssl_options(
5450 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN
);
5451 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5453 base::FilePath(kTestFilePath
));
5455 ASSERT_TRUE(https_test_server
.Start());
5457 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5459 // Set up a pin for |test_server_hostname|.
5460 TransportSecurityState security_state
;
5461 const base::Time
current_time(base::Time::Now());
5462 const base::Time expiry
= current_time
+ base::TimeDelta::FromSeconds(1000);
5463 HashValueVector hashes
;
5466 // The values here don't matter, as long as they are different from
5467 // the mocked CertVerifyResult below.
5469 hash1
.FromString("sha256/1111111111111111111111111111111111111111111="));
5471 hash2
.FromString("sha256/2222222222222222222222222222222222222222222="));
5472 hashes
.push_back(hash1
);
5473 hashes
.push_back(hash2
);
5474 security_state
.AddHPKP(test_server_hostname
, expiry
,
5475 false, /* include subdomains */
5476 hashes
, report_uri
);
5478 MockCertificateReportSender mock_report_sender
;
5479 security_state
.SetReportSender(&mock_report_sender
);
5481 // Set up a MockCertVerifier to trigger a violation of the previously
5483 scoped_refptr
<X509Certificate
> cert
= https_test_server
.GetCertificate();
5486 MockCertVerifier cert_verifier
;
5487 CertVerifyResult verify_result
;
5488 verify_result
.verified_cert
= cert
;
5489 verify_result
.is_issued_by_known_root
= true;
5492 hash3
.FromString("sha256/3333333333333333333333333333333333333333333="));
5493 verify_result
.public_key_hashes
.push_back(hash3
);
5494 cert_verifier
.AddResultForCert(cert
.get(), verify_result
, OK
);
5496 TestNetworkDelegate network_delegate
;
5497 TestURLRequestContext
context(true);
5498 context
.set_transport_security_state(&security_state
);
5499 context
.set_network_delegate(&network_delegate
);
5500 context
.set_cert_verifier(&cert_verifier
);
5503 // Now send a request to trigger the violation.
5505 scoped_ptr
<URLRequest
> violating_request(context
.CreateRequest(
5506 https_test_server
.GetURL("files/simple.html"), DEFAULT_PRIORITY
, &d
));
5507 violating_request
->Start();
5508 base::RunLoop().Run();
5510 // Check that a report was sent.
5511 EXPECT_EQ(report_uri
, mock_report_sender
.latest_report_uri());
5512 ASSERT_FALSE(mock_report_sender
.latest_report().empty());
5513 scoped_ptr
<base::Value
> value(
5514 base::JSONReader::Read(mock_report_sender
.latest_report()));
5516 ASSERT_TRUE(value
->IsType(base::Value::TYPE_DICTIONARY
));
5517 base::DictionaryValue
* report_dict
;
5518 ASSERT_TRUE(value
->GetAsDictionary(&report_dict
));
5519 std::string report_hostname
;
5520 EXPECT_TRUE(report_dict
->GetString("hostname", &report_hostname
));
5521 EXPECT_EQ(test_server_hostname
, report_hostname
);
5524 // Tests that reports get sent on requests with
5525 // Public-Key-Pins-Report-Only headers.
5526 TEST_F(URLRequestTestHTTP
, MAYBE_ProcessPKPReportOnly
) {
5527 GURL
report_uri(kHPKPReportUri
);
5528 SpawnedTestServer::SSLOptions
ssl_options(
5529 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN
);
5530 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5532 base::FilePath(kTestFilePath
));
5534 ASSERT_TRUE(https_test_server
.Start());
5536 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5538 TransportSecurityState security_state
;
5539 MockCertificateReportSender mock_report_sender
;
5540 security_state
.SetReportSender(&mock_report_sender
);
5542 // Set up a MockCertVerifier to violate the pin in the Report-Only
5544 scoped_refptr
<X509Certificate
> cert
= https_test_server
.GetCertificate();
5547 MockCertVerifier cert_verifier
;
5548 CertVerifyResult verify_result
;
5549 verify_result
.verified_cert
= cert
;
5550 verify_result
.is_issued_by_known_root
= true;
5552 // This value doesn't matter, as long as it is different from the pins
5553 // for the request to hpkp-headers-report-only.html.
5555 hash
.FromString("sha256/1111111111111111111111111111111111111111111="));
5556 verify_result
.public_key_hashes
.push_back(hash
);
5557 cert_verifier
.AddResultForCert(cert
.get(), verify_result
, OK
);
5559 TestNetworkDelegate network_delegate
;
5560 TestURLRequestContext
context(true);
5561 context
.set_transport_security_state(&security_state
);
5562 context
.set_network_delegate(&network_delegate
);
5563 context
.set_cert_verifier(&cert_verifier
);
5566 // Now send a request to trigger the violation.
5568 scoped_ptr
<URLRequest
> violating_request(context
.CreateRequest(
5569 https_test_server
.GetURL("files/hpkp-headers-report-only.html"),
5570 DEFAULT_PRIORITY
, &d
));
5571 violating_request
->Start();
5572 base::RunLoop().Run();
5574 // Check that a report was sent.
5575 EXPECT_EQ(report_uri
, mock_report_sender
.latest_report_uri());
5576 ASSERT_FALSE(mock_report_sender
.latest_report().empty());
5577 scoped_ptr
<base::Value
> value(
5578 base::JSONReader::Read(mock_report_sender
.latest_report()));
5580 ASSERT_TRUE(value
->IsType(base::Value::TYPE_DICTIONARY
));
5581 base::DictionaryValue
* report_dict
;
5582 ASSERT_TRUE(value
->GetAsDictionary(&report_dict
));
5583 std::string report_hostname
;
5584 EXPECT_TRUE(report_dict
->GetString("hostname", &report_hostname
));
5585 EXPECT_EQ(test_server_hostname
, report_hostname
);
5588 // Tests that reports do not get sent on requests with
5589 // Public-Key-Pins-Report-Only headers that don't have pin violations.
5590 TEST_F(URLRequestTestHTTP
, MAYBE_ProcessPKPReportOnlyWithNoViolation
) {
5591 GURL
report_uri(kHPKPReportUri
);
5592 SpawnedTestServer::SSLOptions
ssl_options(
5593 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN
);
5594 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5596 base::FilePath(kTestFilePath
));
5598 ASSERT_TRUE(https_test_server
.Start());
5600 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5602 TransportSecurityState security_state
;
5603 MockCertificateReportSender mock_report_sender
;
5604 security_state
.SetReportSender(&mock_report_sender
);
5606 TestNetworkDelegate network_delegate
;
5607 MockCertVerifier mock_cert_verifier
;
5608 TestURLRequestContext
context(true);
5609 context
.set_transport_security_state(&security_state
);
5610 context
.set_network_delegate(&network_delegate
);
5611 context
.set_cert_verifier(&mock_cert_verifier
);
5612 mock_cert_verifier
.set_default_result(OK
);
5615 // Now send a request that does not trigger the violation.
5617 scoped_ptr
<URLRequest
> request(context
.CreateRequest(
5618 https_test_server
.GetURL("files/hpkp-headers-report-only.html"),
5619 DEFAULT_PRIORITY
, &d
));
5621 base::RunLoop().Run();
5623 // Check that a report was not sent.
5624 EXPECT_EQ(GURL(), mock_report_sender
.latest_report_uri());
5625 EXPECT_EQ(std::string(), mock_report_sender
.latest_report());
5628 TEST_F(URLRequestTestHTTP
, PKPNotProcessedOnIP
) {
5629 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5630 SpawnedTestServer::SSLOptions(),
5631 base::FilePath(kTestFilePath
));
5632 ASSERT_TRUE(https_test_server
.Start());
5633 // Make sure this test fails if the test server is changed to not
5634 // listen on an IP by default.
5635 ASSERT_TRUE(https_test_server
.GetURL("").HostIsIPAddress());
5636 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5639 scoped_ptr
<URLRequest
> request(default_context_
.CreateRequest(
5640 https_test_server
.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY
,
5643 base::RunLoop().Run();
5645 TransportSecurityState
* security_state
=
5646 default_context_
.transport_security_state();
5647 TransportSecurityState::PKPState pkp_state
;
5649 security_state
->GetDynamicPKPState(test_server_hostname
, &pkp_state
));
5652 TEST_F(URLRequestTestHTTP
, ProcessSTSOnce
) {
5653 SpawnedTestServer::SSLOptions
ssl_options(
5654 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN
);
5655 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5657 base::FilePath(kTestFilePath
));
5658 ASSERT_TRUE(https_test_server
.Start());
5660 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5663 scoped_ptr
<URLRequest
> request(default_context_
.CreateRequest(
5664 https_test_server
.GetURL("files/hsts-multiple-headers.html"),
5665 DEFAULT_PRIORITY
, &d
));
5667 base::RunLoop().Run();
5669 // We should have set parameters from the first header, not the second.
5670 TransportSecurityState
* security_state
=
5671 default_context_
.transport_security_state();
5672 TransportSecurityState::STSState sts_state
;
5674 security_state
->GetDynamicSTSState(test_server_hostname
, &sts_state
));
5675 EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS
,
5676 sts_state
.upgrade_mode
);
5677 EXPECT_FALSE(sts_state
.include_subdomains
);
5678 EXPECT_FALSE(sts_state
.include_subdomains
);
5681 TEST_F(URLRequestTestHTTP
, ProcessSTSAndPKP
) {
5682 SpawnedTestServer::SSLOptions
ssl_options(
5683 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN
);
5684 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5686 base::FilePath(kTestFilePath
));
5687 ASSERT_TRUE(https_test_server
.Start());
5689 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5692 scoped_ptr
<URLRequest
> request(default_context_
.CreateRequest(
5693 https_test_server
.GetURL("files/hsts-and-hpkp-headers.html"),
5694 DEFAULT_PRIORITY
, &d
));
5696 base::RunLoop().Run();
5698 // We should have set parameters from the first header, not the second.
5699 TransportSecurityState
* security_state
=
5700 default_context_
.transport_security_state();
5701 TransportSecurityState::STSState sts_state
;
5702 TransportSecurityState::PKPState pkp_state
;
5704 security_state
->GetDynamicSTSState(test_server_hostname
, &sts_state
));
5706 security_state
->GetDynamicPKPState(test_server_hostname
, &pkp_state
));
5707 EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS
,
5708 sts_state
.upgrade_mode
);
5709 #if defined(OS_ANDROID)
5710 // Android's CertVerifyProc does not (yet) handle pins.
5712 EXPECT_TRUE(pkp_state
.HasPublicKeyPins());
5714 EXPECT_NE(sts_state
.expiry
, pkp_state
.expiry
);
5716 // Even though there is an HSTS header asserting includeSubdomains, it is
5717 // the *second* such header, and we MUST process only the first.
5718 EXPECT_FALSE(sts_state
.include_subdomains
);
5719 // includeSubdomains does not occur in the test HPKP header.
5720 EXPECT_FALSE(pkp_state
.include_subdomains
);
5723 // Tests that when multiple HPKP headers are present, asserting different
5724 // policies, that only the first such policy is processed.
5725 TEST_F(URLRequestTestHTTP
, ProcessSTSAndPKP2
) {
5726 SpawnedTestServer::SSLOptions
ssl_options(
5727 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN
);
5728 SpawnedTestServer
https_test_server(SpawnedTestServer::TYPE_HTTPS
,
5730 base::FilePath(kTestFilePath
));
5731 ASSERT_TRUE(https_test_server
.Start());
5733 std::string test_server_hostname
= https_test_server
.GetURL("").host();
5736 scoped_ptr
<URLRequest
> request(default_context_
.CreateRequest(
5737 https_test_server
.GetURL("files/hsts-and-hpkp-headers2.html"),
5738 DEFAULT_PRIORITY
, &d
));
5740 base::RunLoop().Run();
5742 TransportSecurityState
* security_state
=
5743 default_context_
.transport_security_state();
5744 TransportSecurityState::STSState sts_state
;
5745 TransportSecurityState::PKPState pkp_state
;
5747 security_state
->GetDynamicSTSState(test_server_hostname
, &sts_state
));
5749 security_state
->GetDynamicPKPState(test_server_hostname
, &pkp_state
));
5750 EXPECT_EQ(TransportSecurityState::STSState::MODE_FORCE_HTTPS
,
5751 sts_state
.upgrade_mode
);
5752 #if defined(OS_ANDROID)
5753 // Android's CertVerifyProc does not (yet) handle pins.
5755 EXPECT_TRUE(pkp_state
.HasPublicKeyPins());
5757 EXPECT_NE(sts_state
.expiry
, pkp_state
.expiry
);
5759 EXPECT_TRUE(sts_state
.include_subdomains
);
5760 EXPECT_FALSE(pkp_state
.include_subdomains
);
5763 TEST_F(URLRequestTestHTTP
, ContentTypeNormalizationTest
) {
5764 ASSERT_TRUE(test_server_
.Start());
5767 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
5768 test_server_
.GetURL("files/content-type-normalization.html"),
5769 DEFAULT_PRIORITY
, &d
));
5771 base::RunLoop().Run();
5773 std::string mime_type
;
5774 req
->GetMimeType(&mime_type
);
5775 EXPECT_EQ("text/html", mime_type
);
5777 std::string charset
;
5778 req
->GetCharset(&charset
);
5779 EXPECT_EQ("utf-8", charset
);
5783 TEST_F(URLRequestTestHTTP
, ProtocolHandlerAndFactoryRestrictDataRedirects
) {
5784 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5785 GURL
data_url("data:,foo");
5786 DataProtocolHandler data_protocol_handler
;
5787 EXPECT_FALSE(data_protocol_handler
.IsSafeRedirectTarget(data_url
));
5789 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5790 EXPECT_FALSE(job_factory_
->IsSafeRedirectTarget(data_url
));
5793 #if !defined(DISABLE_FILE_SUPPORT)
5794 TEST_F(URLRequestTestHTTP
, ProtocolHandlerAndFactoryRestrictFileRedirects
) {
5795 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5796 GURL
file_url("file:///foo.txt");
5797 FileProtocolHandler
file_protocol_handler(
5798 base::ThreadTaskRunnerHandle::Get());
5799 EXPECT_FALSE(file_protocol_handler
.IsSafeRedirectTarget(file_url
));
5801 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5802 EXPECT_FALSE(job_factory_
->IsSafeRedirectTarget(file_url
));
5805 TEST_F(URLRequestTestHTTP
, RestrictFileRedirects
) {
5806 ASSERT_TRUE(test_server_
.Start());
5809 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
5810 test_server_
.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY
,
5813 base::RunLoop().Run();
5815 EXPECT_EQ(URLRequestStatus::FAILED
, req
->status().status());
5816 EXPECT_EQ(ERR_UNSAFE_REDIRECT
, req
->status().error());
5818 #endif // !defined(DISABLE_FILE_SUPPORT)
5820 TEST_F(URLRequestTestHTTP
, RestrictDataRedirects
) {
5821 ASSERT_TRUE(test_server_
.Start());
5824 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
5825 test_server_
.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY
,
5828 base::MessageLoop::current()->Run();
5830 EXPECT_EQ(URLRequestStatus::FAILED
, req
->status().status());
5831 EXPECT_EQ(ERR_UNSAFE_REDIRECT
, req
->status().error());
5834 TEST_F(URLRequestTestHTTP
, RedirectToInvalidURL
) {
5835 ASSERT_TRUE(test_server_
.Start());
5838 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
5839 test_server_
.GetURL("files/redirect-to-invalid-url.html"),
5840 DEFAULT_PRIORITY
, &d
));
5842 base::RunLoop().Run();
5844 EXPECT_EQ(URLRequestStatus::FAILED
, req
->status().status());
5845 EXPECT_EQ(ERR_INVALID_URL
, req
->status().error());
5848 // Make sure redirects are cached, despite not reading their bodies.
5849 TEST_F(URLRequestTestHTTP
, CacheRedirect
) {
5850 ASSERT_TRUE(test_server_
.Start());
5852 test_server_
.GetURL("files/redirect302-to-echo-cacheable");
5856 scoped_ptr
<URLRequest
> req(
5857 default_context_
.CreateRequest(redirect_url
, DEFAULT_PRIORITY
, &d
));
5859 base::RunLoop().Run();
5860 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
5861 EXPECT_EQ(1, d
.received_redirect_count());
5862 EXPECT_EQ(test_server_
.GetURL("echo"), req
->url());
5867 d
.set_quit_on_redirect(true);
5868 scoped_ptr
<URLRequest
> req(
5869 default_context_
.CreateRequest(redirect_url
, DEFAULT_PRIORITY
, &d
));
5871 base::RunLoop().Run();
5873 EXPECT_EQ(1, d
.received_redirect_count());
5874 EXPECT_EQ(0, d
.response_started_count());
5875 EXPECT_TRUE(req
->was_cached());
5877 req
->FollowDeferredRedirect();
5878 base::RunLoop().Run();
5879 EXPECT_EQ(1, d
.received_redirect_count());
5880 EXPECT_EQ(1, d
.response_started_count());
5881 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
5882 EXPECT_EQ(test_server_
.GetURL("echo"), req
->url());
5886 // Make sure a request isn't cached when a NetworkDelegate forces a redirect
5887 // when the headers are read, since the body won't have been read.
5888 TEST_F(URLRequestTestHTTP
, NoCacheOnNetworkDelegateRedirect
) {
5889 ASSERT_TRUE(test_server_
.Start());
5890 // URL that is normally cached.
5891 GURL initial_url
= test_server_
.GetURL("cachetime");
5894 // Set up the TestNetworkDelegate tp force a redirect.
5895 GURL redirect_to_url
= test_server_
.GetURL("echo");
5896 default_network_delegate_
.set_redirect_on_headers_received_url(
5900 scoped_ptr
<URLRequest
> req(
5901 default_context_
.CreateRequest(initial_url
, DEFAULT_PRIORITY
, &d
));
5903 base::RunLoop().Run();
5904 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
5905 EXPECT_EQ(1, d
.received_redirect_count());
5906 EXPECT_EQ(redirect_to_url
, req
->url());
5911 scoped_ptr
<URLRequest
> req(
5912 default_context_
.CreateRequest(initial_url
, DEFAULT_PRIORITY
, &d
));
5914 base::RunLoop().Run();
5916 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
5917 EXPECT_FALSE(req
->was_cached());
5918 EXPECT_EQ(0, d
.received_redirect_count());
5919 EXPECT_EQ(initial_url
, req
->url());
5923 // Tests that redirection to an unsafe URL is allowed when it has been marked as
5925 TEST_F(URLRequestTestHTTP
, UnsafeRedirectToWhitelistedUnsafeURL
) {
5926 ASSERT_TRUE(test_server_
.Start());
5928 GURL
unsafe_url("data:text/html,this-is-considered-an-unsafe-url");
5929 default_network_delegate_
.set_redirect_on_headers_received_url(unsafe_url
);
5930 default_network_delegate_
.set_allowed_unsafe_redirect_url(unsafe_url
);
5934 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5935 test_server_
.GetURL("whatever"), DEFAULT_PRIORITY
, &d
));
5938 base::RunLoop().Run();
5940 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
5942 EXPECT_EQ(2U, r
->url_chain().size());
5943 EXPECT_EQ(OK
, r
->status().error());
5944 EXPECT_EQ(unsafe_url
, r
->url());
5945 EXPECT_EQ("this-is-considered-an-unsafe-url", d
.data_received());
5949 // Tests that a redirect to a different unsafe URL is blocked, even after adding
5950 // some other URL to the whitelist.
5951 TEST_F(URLRequestTestHTTP
, UnsafeRedirectToDifferentUnsafeURL
) {
5952 ASSERT_TRUE(test_server_
.Start());
5954 GURL
unsafe_url("data:text/html,something");
5955 GURL
different_unsafe_url("data:text/html,something-else");
5956 default_network_delegate_
.set_redirect_on_headers_received_url(unsafe_url
);
5957 default_network_delegate_
.set_allowed_unsafe_redirect_url(
5958 different_unsafe_url
);
5962 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
5963 test_server_
.GetURL("whatever"), DEFAULT_PRIORITY
, &d
));
5966 base::RunLoop().Run();
5968 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
5969 EXPECT_EQ(ERR_UNSAFE_REDIRECT
, r
->status().error());
5973 // Redirects from an URL with fragment to an unsafe URL with fragment should
5974 // be allowed, and the reference fragment of the target URL should be preserved.
5975 TEST_F(URLRequestTestHTTP
, UnsafeRedirectWithDifferentReferenceFragment
) {
5976 ASSERT_TRUE(test_server_
.Start());
5978 GURL
original_url(test_server_
.GetURL("original#fragment1"));
5979 GURL
unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5980 GURL
expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5982 default_network_delegate_
.set_redirect_on_headers_received_url(unsafe_url
);
5983 default_network_delegate_
.set_allowed_unsafe_redirect_url(unsafe_url
);
5987 scoped_ptr
<URLRequest
> r(
5988 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
5991 base::RunLoop().Run();
5993 EXPECT_EQ(2U, r
->url_chain().size());
5994 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
5995 EXPECT_EQ(OK
, r
->status().error());
5996 EXPECT_EQ(original_url
, r
->original_url());
5997 EXPECT_EQ(expected_url
, r
->url());
6001 // When a delegate has specified a safe redirect URL, but it does not match the
6002 // redirect target, then do not prevent the reference fragment from being added.
6003 TEST_F(URLRequestTestHTTP
, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl
) {
6004 ASSERT_TRUE(test_server_
.Start());
6006 GURL
original_url(test_server_
.GetURL("original#expected-fragment"));
6007 GURL
unsafe_url("data:text/html,this-url-does-not-match-redirect-url");
6008 GURL
redirect_url(test_server_
.GetURL("target"));
6009 GURL
expected_redirect_url(test_server_
.GetURL("target#expected-fragment"));
6011 default_network_delegate_
.set_redirect_on_headers_received_url(redirect_url
);
6012 default_network_delegate_
.set_allowed_unsafe_redirect_url(unsafe_url
);
6016 scoped_ptr
<URLRequest
> r(
6017 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
6020 base::RunLoop().Run();
6022 EXPECT_EQ(2U, r
->url_chain().size());
6023 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
6024 EXPECT_EQ(OK
, r
->status().error());
6025 EXPECT_EQ(original_url
, r
->original_url());
6026 EXPECT_EQ(expected_redirect_url
, r
->url());
6030 // When a delegate has specified a safe redirect URL, assume that the redirect
6031 // URL should not be changed. In particular, the reference fragment should not
6033 TEST_F(URLRequestTestHTTP
, RedirectWithReferenceFragment
) {
6034 ASSERT_TRUE(test_server_
.Start());
6036 GURL
original_url(test_server_
.GetURL("original#should-not-be-appended"));
6037 GURL
redirect_url("data:text/html,expect-no-reference-fragment");
6039 default_network_delegate_
.set_redirect_on_headers_received_url(redirect_url
);
6040 default_network_delegate_
.set_allowed_unsafe_redirect_url(redirect_url
);
6044 scoped_ptr
<URLRequest
> r(
6045 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
6048 base::RunLoop().Run();
6050 EXPECT_EQ(2U, r
->url_chain().size());
6051 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
6052 EXPECT_EQ(OK
, r
->status().error());
6053 EXPECT_EQ(original_url
, r
->original_url());
6054 EXPECT_EQ(redirect_url
, r
->url());
6058 // When a URLRequestRedirectJob is created, the redirection must be followed and
6059 // the reference fragment of the target URL must not be modified.
6060 TEST_F(URLRequestTestHTTP
, RedirectJobWithReferenceFragment
) {
6061 ASSERT_TRUE(test_server_
.Start());
6063 GURL
original_url(test_server_
.GetURL("original#should-not-be-appended"));
6064 GURL
redirect_url(test_server_
.GetURL("echo"));
6067 scoped_ptr
<URLRequest
> r(
6068 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
6070 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
6071 r
.get(), &default_network_delegate_
, redirect_url
,
6072 URLRequestRedirectJob::REDIRECT_302_FOUND
, "Very Good Reason");
6073 AddTestInterceptor()->set_main_intercept_job(job
);
6076 base::RunLoop().Run();
6078 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
6079 EXPECT_EQ(OK
, r
->status().error());
6080 EXPECT_EQ(original_url
, r
->original_url());
6081 EXPECT_EQ(redirect_url
, r
->url());
6084 TEST_F(URLRequestTestHTTP
, NoUserPassInReferrer
) {
6085 ASSERT_TRUE(test_server_
.Start());
6088 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6089 test_server_
.GetURL("echoheader?Referer"), DEFAULT_PRIORITY
, &d
));
6090 req
->SetReferrer("http://user:pass@foo.com/");
6092 base::RunLoop().Run();
6094 EXPECT_EQ(std::string("http://foo.com/"), d
.data_received());
6097 TEST_F(URLRequestTestHTTP
, NoFragmentInReferrer
) {
6098 ASSERT_TRUE(test_server_
.Start());
6101 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6102 test_server_
.GetURL("echoheader?Referer"), DEFAULT_PRIORITY
, &d
));
6103 req
->SetReferrer("http://foo.com/test#fragment");
6105 base::RunLoop().Run();
6107 EXPECT_EQ(std::string("http://foo.com/test"), d
.data_received());
6110 TEST_F(URLRequestTestHTTP
, EmptyReferrerAfterValidReferrer
) {
6111 ASSERT_TRUE(test_server_
.Start());
6114 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6115 test_server_
.GetURL("echoheader?Referer"), DEFAULT_PRIORITY
, &d
));
6116 req
->SetReferrer("http://foo.com/test#fragment");
6117 req
->SetReferrer("");
6119 base::RunLoop().Run();
6121 EXPECT_EQ(std::string("None"), d
.data_received());
6124 // Defer network start and then resume, checking that the request was a success
6125 // and bytes were received.
6126 TEST_F(URLRequestTestHTTP
, DeferredBeforeNetworkStart
) {
6127 ASSERT_TRUE(test_server_
.Start());
6131 d
.set_quit_on_network_start(true);
6132 GURL
test_url(test_server_
.GetURL("echo"));
6133 scoped_ptr
<URLRequest
> req(
6134 default_context_
.CreateRequest(test_url
, DEFAULT_PRIORITY
, &d
));
6137 base::RunLoop().Run();
6139 EXPECT_EQ(1, d
.received_before_network_start_count());
6140 EXPECT_EQ(0, d
.response_started_count());
6142 req
->ResumeNetworkStart();
6143 base::RunLoop().Run();
6145 EXPECT_EQ(1, d
.response_started_count());
6146 EXPECT_NE(0, d
.bytes_received());
6147 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
6151 // Check that OnBeforeNetworkStart is only called once even if there is a
6153 TEST_F(URLRequestTestHTTP
, BeforeNetworkStartCalledOnce
) {
6154 ASSERT_TRUE(test_server_
.Start());
6158 d
.set_quit_on_redirect(true);
6159 d
.set_quit_on_network_start(true);
6160 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6161 test_server_
.GetURL("server-redirect?echo"), DEFAULT_PRIORITY
, &d
));
6164 base::RunLoop().Run();
6166 EXPECT_EQ(1, d
.received_before_network_start_count());
6167 EXPECT_EQ(0, d
.response_started_count());
6168 EXPECT_EQ(0, d
.received_redirect_count());
6170 req
->ResumeNetworkStart();
6171 base::RunLoop().Run();
6173 EXPECT_EQ(1, d
.received_redirect_count());
6174 req
->FollowDeferredRedirect();
6175 base::RunLoop().Run();
6177 // Check that the redirect's new network transaction does not get propagated
6178 // to a second OnBeforeNetworkStart() notification.
6179 EXPECT_EQ(1, d
.received_before_network_start_count());
6181 EXPECT_EQ(1, d
.response_started_count());
6182 EXPECT_NE(0, d
.bytes_received());
6183 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
6187 // Cancel the request after learning that the request would use the network.
6188 TEST_F(URLRequestTestHTTP
, CancelOnBeforeNetworkStart
) {
6189 ASSERT_TRUE(test_server_
.Start());
6193 d
.set_quit_on_network_start(true);
6194 GURL
test_url(test_server_
.GetURL("echo"));
6195 scoped_ptr
<URLRequest
> req(
6196 default_context_
.CreateRequest(test_url
, DEFAULT_PRIORITY
, &d
));
6199 base::RunLoop().Run();
6201 EXPECT_EQ(1, d
.received_before_network_start_count());
6202 EXPECT_EQ(0, d
.response_started_count());
6205 base::RunLoop().Run();
6207 EXPECT_EQ(1, d
.response_started_count());
6208 EXPECT_EQ(0, d
.bytes_received());
6209 EXPECT_EQ(URLRequestStatus::CANCELED
, req
->status().status());
6213 TEST_F(URLRequestTestHTTP
, CancelRedirect
) {
6214 ASSERT_TRUE(test_server_
.Start());
6218 d
.set_cancel_in_received_redirect(true);
6219 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6220 test_server_
.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY
, &d
));
6222 base::RunLoop().Run();
6224 EXPECT_EQ(1, d
.response_started_count());
6225 EXPECT_EQ(0, d
.bytes_received());
6226 EXPECT_FALSE(d
.received_data_before_response());
6227 EXPECT_EQ(URLRequestStatus::CANCELED
, req
->status().status());
6231 TEST_F(URLRequestTestHTTP
, DeferredRedirect
) {
6232 ASSERT_TRUE(test_server_
.Start());
6236 d
.set_quit_on_redirect(true);
6237 GURL
test_url(test_server_
.GetURL("files/redirect-test.html"));
6238 scoped_ptr
<URLRequest
> req(
6239 default_context_
.CreateRequest(test_url
, DEFAULT_PRIORITY
, &d
));
6242 base::RunLoop().Run();
6244 EXPECT_EQ(1, d
.received_redirect_count());
6246 req
->FollowDeferredRedirect();
6247 base::RunLoop().Run();
6249 EXPECT_EQ(1, d
.response_started_count());
6250 EXPECT_FALSE(d
.received_data_before_response());
6251 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
6253 base::FilePath path
;
6254 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
6255 path
= path
.Append(kTestFilePath
);
6256 path
= path
.Append(FILE_PATH_LITERAL("with-headers.html"));
6258 std::string contents
;
6259 EXPECT_TRUE(base::ReadFileToString(path
, &contents
));
6260 EXPECT_EQ(contents
, d
.data_received());
6264 TEST_F(URLRequestTestHTTP
, DeferredRedirect_GetFullRequestHeaders
) {
6265 ASSERT_TRUE(test_server_
.Start());
6269 d
.set_quit_on_redirect(true);
6270 GURL
test_url(test_server_
.GetURL("files/redirect-test.html"));
6271 scoped_ptr
<URLRequest
> req(
6272 default_context_
.CreateRequest(test_url
, DEFAULT_PRIORITY
, &d
));
6274 EXPECT_FALSE(d
.have_full_request_headers());
6277 base::RunLoop().Run();
6279 EXPECT_EQ(1, d
.received_redirect_count());
6280 EXPECT_TRUE(d
.have_full_request_headers());
6281 CheckFullRequestHeaders(d
.full_request_headers(), test_url
);
6282 d
.ClearFullRequestHeaders();
6284 req
->FollowDeferredRedirect();
6285 base::RunLoop().Run();
6287 GURL
target_url(test_server_
.GetURL("files/with-headers.html"));
6288 EXPECT_EQ(1, d
.response_started_count());
6289 EXPECT_TRUE(d
.have_full_request_headers());
6290 CheckFullRequestHeaders(d
.full_request_headers(), target_url
);
6291 EXPECT_FALSE(d
.received_data_before_response());
6292 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
6294 base::FilePath path
;
6295 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
6296 path
= path
.Append(kTestFilePath
);
6297 path
= path
.Append(FILE_PATH_LITERAL("with-headers.html"));
6299 std::string contents
;
6300 EXPECT_TRUE(base::ReadFileToString(path
, &contents
));
6301 EXPECT_EQ(contents
, d
.data_received());
6305 TEST_F(URLRequestTestHTTP
, CancelDeferredRedirect
) {
6306 ASSERT_TRUE(test_server_
.Start());
6310 d
.set_quit_on_redirect(true);
6311 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6312 test_server_
.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY
, &d
));
6314 base::RunLoop().Run();
6316 EXPECT_EQ(1, d
.received_redirect_count());
6319 base::RunLoop().Run();
6321 EXPECT_EQ(1, d
.response_started_count());
6322 EXPECT_EQ(0, d
.bytes_received());
6323 EXPECT_FALSE(d
.received_data_before_response());
6324 EXPECT_EQ(URLRequestStatus::CANCELED
, req
->status().status());
6328 TEST_F(URLRequestTestHTTP
, VaryHeader
) {
6329 ASSERT_TRUE(test_server_
.Start());
6331 // Populate the cache.
6334 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6335 test_server_
.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY
, &d
));
6336 HttpRequestHeaders headers
;
6337 headers
.SetHeader("foo", "1");
6338 req
->SetExtraRequestHeaders(headers
);
6340 base::RunLoop().Run();
6342 LoadTimingInfo load_timing_info
;
6343 req
->GetLoadTimingInfo(&load_timing_info
);
6344 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
6347 // Expect a cache hit.
6350 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6351 test_server_
.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY
, &d
));
6352 HttpRequestHeaders headers
;
6353 headers
.SetHeader("foo", "1");
6354 req
->SetExtraRequestHeaders(headers
);
6356 base::RunLoop().Run();
6358 EXPECT_TRUE(req
->was_cached());
6360 LoadTimingInfo load_timing_info
;
6361 req
->GetLoadTimingInfo(&load_timing_info
);
6362 TestLoadTimingCacheHitNoNetwork(load_timing_info
);
6365 // Expect a cache miss.
6368 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6369 test_server_
.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY
, &d
));
6370 HttpRequestHeaders headers
;
6371 headers
.SetHeader("foo", "2");
6372 req
->SetExtraRequestHeaders(headers
);
6374 base::RunLoop().Run();
6376 EXPECT_FALSE(req
->was_cached());
6378 LoadTimingInfo load_timing_info
;
6379 req
->GetLoadTimingInfo(&load_timing_info
);
6380 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
6384 TEST_F(URLRequestTestHTTP
, BasicAuth
) {
6385 ASSERT_TRUE(test_server_
.Start());
6387 // populate the cache
6390 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
6392 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
6393 test_server_
.GetURL("auth-basic"), DEFAULT_PRIORITY
, &d
));
6396 base::RunLoop().Run();
6398 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
6401 // repeat request with end-to-end validation. since auth-basic results in a
6402 // cachable page, we expect this test to result in a 304. in which case, the
6403 // response should be fetched from the cache.
6406 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
6408 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
6409 test_server_
.GetURL("auth-basic"), DEFAULT_PRIORITY
, &d
));
6410 r
->SetLoadFlags(LOAD_VALIDATE_CACHE
);
6413 base::RunLoop().Run();
6415 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
6417 // Should be the same cached document.
6418 EXPECT_TRUE(r
->was_cached());
6422 // Check that Set-Cookie headers in 401 responses are respected.
6423 // http://crbug.com/6450
6424 TEST_F(URLRequestTestHTTP
, BasicAuthWithCookies
) {
6425 ASSERT_TRUE(test_server_
.Start());
6427 GURL url_requiring_auth
=
6428 test_server_
.GetURL("auth-basic?set-cookie-if-challenged");
6430 // Request a page that will give a 401 containing a Set-Cookie header.
6431 // Verify that when the transaction is restarted, it includes the new cookie.
6433 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
6434 TestURLRequestContext
context(true);
6435 context
.set_network_delegate(&network_delegate
);
6439 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
6441 scoped_ptr
<URLRequest
> r(
6442 context
.CreateRequest(url_requiring_auth
, DEFAULT_PRIORITY
, &d
));
6445 base::RunLoop().Run();
6447 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
6449 // Make sure we sent the cookie in the restarted transaction.
6450 EXPECT_TRUE(d
.data_received().find("Cookie: got_challenged=true")
6451 != std::string::npos
);
6454 // Same test as above, except this time the restart is initiated earlier
6455 // (without user intervention since identity is embedded in the URL).
6457 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
6458 TestURLRequestContext
context(true);
6459 context
.set_network_delegate(&network_delegate
);
6464 GURL::Replacements replacements
;
6465 replacements
.SetUsernameStr("user2");
6466 replacements
.SetPasswordStr("secret");
6467 GURL url_with_identity
= url_requiring_auth
.ReplaceComponents(replacements
);
6469 scoped_ptr
<URLRequest
> r(
6470 context
.CreateRequest(url_with_identity
, DEFAULT_PRIORITY
, &d
));
6473 base::RunLoop().Run();
6475 EXPECT_TRUE(d
.data_received().find("user2/secret") != std::string::npos
);
6477 // Make sure we sent the cookie in the restarted transaction.
6478 EXPECT_TRUE(d
.data_received().find("Cookie: got_challenged=true")
6479 != std::string::npos
);
6483 // Tests that load timing works as expected with auth and the cache.
6484 TEST_F(URLRequestTestHTTP
, BasicAuthLoadTiming
) {
6485 ASSERT_TRUE(test_server_
.Start());
6487 // populate the cache
6490 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
6492 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
6493 test_server_
.GetURL("auth-basic"), DEFAULT_PRIORITY
, &d
));
6496 base::RunLoop().Run();
6498 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
6500 LoadTimingInfo load_timing_info_before_auth
;
6501 EXPECT_TRUE(default_network_delegate_
.GetLoadTimingInfoBeforeAuth(
6502 &load_timing_info_before_auth
));
6503 TestLoadTimingNotReused(load_timing_info_before_auth
,
6504 CONNECT_TIMING_HAS_DNS_TIMES
);
6506 LoadTimingInfo load_timing_info
;
6507 r
->GetLoadTimingInfo(&load_timing_info
);
6508 // The test server does not support keep alive sockets, so the second
6509 // request with auth should use a new socket.
6510 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
6511 EXPECT_NE(load_timing_info_before_auth
.socket_log_id
,
6512 load_timing_info
.socket_log_id
);
6513 EXPECT_LE(load_timing_info_before_auth
.receive_headers_end
,
6514 load_timing_info
.connect_timing
.connect_start
);
6517 // Repeat request with end-to-end validation. Since auth-basic results in a
6518 // cachable page, we expect this test to result in a 304. In which case, the
6519 // response should be fetched from the cache.
6522 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
6524 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
6525 test_server_
.GetURL("auth-basic"), DEFAULT_PRIORITY
, &d
));
6526 r
->SetLoadFlags(LOAD_VALIDATE_CACHE
);
6529 base::RunLoop().Run();
6531 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
6533 // Should be the same cached document.
6534 EXPECT_TRUE(r
->was_cached());
6536 // Since there was a request that went over the wire, the load timing
6537 // information should include connection times.
6538 LoadTimingInfo load_timing_info
;
6539 r
->GetLoadTimingInfo(&load_timing_info
);
6540 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
6544 // In this test, we do a POST which the server will 302 redirect.
6545 // The subsequent transaction should use GET, and should not send the
6546 // Content-Type header.
6547 // http://code.google.com/p/chromium/issues/detail?id=843
6548 TEST_F(URLRequestTestHTTP
, Post302RedirectGet
) {
6549 ASSERT_TRUE(test_server_
.Start());
6551 const char kData
[] = "hello world";
6554 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6555 test_server_
.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY
, &d
));
6556 req
->set_method("POST");
6557 req
->set_upload(CreateSimpleUploadData(kData
));
6559 // Set headers (some of which are specific to the POST).
6560 HttpRequestHeaders headers
;
6561 headers
.AddHeadersFromString(
6562 "Content-Type: multipart/form-data; "
6563 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
6564 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
6565 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
6566 "Accept-Language: en-US,en\r\n"
6567 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
6568 "Content-Length: 11\r\n"
6569 "Origin: http://localhost:1337/");
6570 req
->SetExtraRequestHeaders(headers
);
6572 base::RunLoop().Run();
6574 std::string mime_type
;
6575 req
->GetMimeType(&mime_type
);
6576 EXPECT_EQ("text/html", mime_type
);
6578 const std::string
& data
= d
.data_received();
6580 // Check that the post-specific headers were stripped:
6581 EXPECT_FALSE(ContainsString(data
, "Content-Length:"));
6582 EXPECT_FALSE(ContainsString(data
, "Content-Type:"));
6583 EXPECT_FALSE(ContainsString(data
, "Origin:"));
6585 // These extra request headers should not have been stripped.
6586 EXPECT_TRUE(ContainsString(data
, "Accept:"));
6587 EXPECT_TRUE(ContainsString(data
, "Accept-Language:"));
6588 EXPECT_TRUE(ContainsString(data
, "Accept-Charset:"));
6591 // The following tests check that we handle mutating the request for HTTP
6592 // redirects as expected.
6593 // See https://crbug.com/56373, https://crbug.com/102130, and
6594 // https://crbug.com/465517.
6596 TEST_F(URLRequestTestHTTP
, Redirect301Tests
) {
6597 ASSERT_TRUE(test_server_
.Start());
6599 const GURL url
= test_server_
.GetURL("files/redirect301-to-echo");
6600 const GURL https_redirect_url
=
6601 test_server_
.GetURL("files/redirect301-to-https");
6603 HTTPRedirectMethodTest(url
, "POST", "GET", true);
6604 HTTPRedirectMethodTest(url
, "PUT", "PUT", true);
6605 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
6607 HTTPRedirectOriginHeaderTest(url
, "GET", "GET", url
.GetOrigin().spec());
6608 HTTPRedirectOriginHeaderTest(https_redirect_url
, "GET", "GET", "null");
6609 HTTPRedirectOriginHeaderTest(url
, "POST", "GET", std::string());
6610 HTTPRedirectOriginHeaderTest(https_redirect_url
, "POST", "GET",
6614 TEST_F(URLRequestTestHTTP
, Redirect302Tests
) {
6615 ASSERT_TRUE(test_server_
.Start());
6617 const GURL url
= test_server_
.GetURL("files/redirect302-to-echo");
6618 const GURL https_redirect_url
=
6619 test_server_
.GetURL("files/redirect302-to-https");
6621 HTTPRedirectMethodTest(url
, "POST", "GET", true);
6622 HTTPRedirectMethodTest(url
, "PUT", "PUT", true);
6623 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
6625 HTTPRedirectOriginHeaderTest(url
, "GET", "GET", url
.GetOrigin().spec());
6626 HTTPRedirectOriginHeaderTest(https_redirect_url
, "GET", "GET", "null");
6627 HTTPRedirectOriginHeaderTest(url
, "POST", "GET", std::string());
6628 HTTPRedirectOriginHeaderTest(https_redirect_url
, "POST", "GET",
6632 TEST_F(URLRequestTestHTTP
, Redirect303Tests
) {
6633 ASSERT_TRUE(test_server_
.Start());
6635 const GURL url
= test_server_
.GetURL("files/redirect303-to-echo");
6636 const GURL https_redirect_url
=
6637 test_server_
.GetURL("files/redirect303-to-https");
6639 HTTPRedirectMethodTest(url
, "POST", "GET", true);
6640 HTTPRedirectMethodTest(url
, "PUT", "GET", true);
6641 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
6643 HTTPRedirectOriginHeaderTest(url
, "GET", "GET", url
.GetOrigin().spec());
6644 HTTPRedirectOriginHeaderTest(https_redirect_url
, "GET", "GET", "null");
6645 HTTPRedirectOriginHeaderTest(url
, "POST", "GET", std::string());
6646 HTTPRedirectOriginHeaderTest(https_redirect_url
, "POST", "GET",
6650 TEST_F(URLRequestTestHTTP
, Redirect307Tests
) {
6651 ASSERT_TRUE(test_server_
.Start());
6653 const GURL url
= test_server_
.GetURL("files/redirect307-to-echo");
6654 const GURL https_redirect_url
=
6655 test_server_
.GetURL("files/redirect307-to-https");
6657 HTTPRedirectMethodTest(url
, "POST", "POST", true);
6658 HTTPRedirectMethodTest(url
, "PUT", "PUT", true);
6659 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
6661 HTTPRedirectOriginHeaderTest(url
, "GET", "GET", url
.GetOrigin().spec());
6662 HTTPRedirectOriginHeaderTest(https_redirect_url
, "GET", "GET", "null");
6663 HTTPRedirectOriginHeaderTest(url
, "POST", "POST", url
.GetOrigin().spec());
6664 HTTPRedirectOriginHeaderTest(https_redirect_url
, "POST", "POST", "null");
6667 TEST_F(URLRequestTestHTTP
, Redirect308Tests
) {
6668 ASSERT_TRUE(test_server_
.Start());
6670 const GURL url
= test_server_
.GetURL("files/redirect308-to-echo");
6671 const GURL https_redirect_url
=
6672 test_server_
.GetURL("files/redirect308-to-https");
6674 HTTPRedirectMethodTest(url
, "POST", "POST", true);
6675 HTTPRedirectMethodTest(url
, "PUT", "PUT", true);
6676 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
6678 HTTPRedirectOriginHeaderTest(url
, "GET", "GET", url
.GetOrigin().spec());
6679 HTTPRedirectOriginHeaderTest(https_redirect_url
, "GET", "GET", "null");
6680 HTTPRedirectOriginHeaderTest(url
, "POST", "POST", url
.GetOrigin().spec());
6681 HTTPRedirectOriginHeaderTest(https_redirect_url
, "POST", "POST", "null");
6684 // Make sure that 308 responses without bodies are not treated as redirects.
6685 // Certain legacy apis that pre-date the response code expect this behavior
6686 // (Like Google Drive).
6687 TEST_F(URLRequestTestHTTP
, NoRedirectOn308WithoutLocationHeader
) {
6688 ASSERT_TRUE(test_server_
.Start());
6691 const GURL url
= test_server_
.GetURL("files/308-without-location-header");
6693 scoped_ptr
<URLRequest
> request(
6694 default_context_
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
6697 base::RunLoop().Run();
6698 EXPECT_EQ(URLRequestStatus::SUCCESS
, request
->status().status());
6699 EXPECT_EQ(OK
, request
->status().error());
6700 EXPECT_EQ(0, d
.received_redirect_count());
6701 EXPECT_EQ(308, request
->response_headers()->response_code());
6702 EXPECT_EQ("This is not a redirect.", d
.data_received());
6705 TEST_F(URLRequestTestHTTP
, Redirect302PreserveReferenceFragment
) {
6706 ASSERT_TRUE(test_server_
.Start());
6708 GURL
original_url(test_server_
.GetURL("files/redirect302-to-echo#fragment"));
6709 GURL
expected_url(test_server_
.GetURL("echo#fragment"));
6713 scoped_ptr
<URLRequest
> r(
6714 default_context_
.CreateRequest(original_url
, DEFAULT_PRIORITY
, &d
));
6717 base::RunLoop().Run();
6719 EXPECT_EQ(2U, r
->url_chain().size());
6720 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
6721 EXPECT_EQ(OK
, r
->status().error());
6722 EXPECT_EQ(original_url
, r
->original_url());
6723 EXPECT_EQ(expected_url
, r
->url());
6727 TEST_F(URLRequestTestHTTP
, RedirectPreserveFirstPartyURL
) {
6728 ASSERT_TRUE(test_server_
.Start());
6730 GURL
url(test_server_
.GetURL("files/redirect302-to-echo"));
6731 GURL
first_party_url("http://example.com");
6735 scoped_ptr
<URLRequest
> r(
6736 default_context_
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
6737 r
->set_first_party_for_cookies(first_party_url
);
6740 base::RunLoop().Run();
6742 EXPECT_EQ(2U, r
->url_chain().size());
6743 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
6744 EXPECT_EQ(OK
, r
->status().error());
6745 EXPECT_EQ(first_party_url
, r
->first_party_for_cookies());
6749 TEST_F(URLRequestTestHTTP
, RedirectUpdateFirstPartyURL
) {
6750 ASSERT_TRUE(test_server_
.Start());
6752 GURL
url(test_server_
.GetURL("files/redirect302-to-echo"));
6753 GURL
original_first_party_url("http://example.com");
6754 GURL
expected_first_party_url(test_server_
.GetURL("echo"));
6758 scoped_ptr
<URLRequest
> r(
6759 default_context_
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
6760 r
->set_first_party_for_cookies(original_first_party_url
);
6761 r
->set_first_party_url_policy(
6762 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT
);
6765 base::RunLoop().Run();
6767 EXPECT_EQ(2U, r
->url_chain().size());
6768 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
->status().status());
6769 EXPECT_EQ(OK
, r
->status().error());
6770 EXPECT_EQ(expected_first_party_url
, r
->first_party_for_cookies());
6774 TEST_F(URLRequestTestHTTP
, InterceptPost302RedirectGet
) {
6775 ASSERT_TRUE(test_server_
.Start());
6777 const char kData
[] = "hello world";
6780 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6781 test_server_
.GetURL("empty.html"), DEFAULT_PRIORITY
, &d
));
6782 req
->set_method("POST");
6783 req
->set_upload(CreateSimpleUploadData(kData
));
6784 HttpRequestHeaders headers
;
6785 headers
.SetHeader(HttpRequestHeaders::kContentLength
,
6786 base::SizeTToString(arraysize(kData
) - 1));
6787 req
->SetExtraRequestHeaders(headers
);
6789 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
6790 req
.get(), &default_network_delegate_
, test_server_
.GetURL("echo"),
6791 URLRequestRedirectJob::REDIRECT_302_FOUND
, "Very Good Reason");
6792 AddTestInterceptor()->set_main_intercept_job(job
);
6795 base::RunLoop().Run();
6796 EXPECT_EQ("GET", req
->method());
6799 TEST_F(URLRequestTestHTTP
, InterceptPost307RedirectPost
) {
6800 ASSERT_TRUE(test_server_
.Start());
6802 const char kData
[] = "hello world";
6805 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6806 test_server_
.GetURL("empty.html"), DEFAULT_PRIORITY
, &d
));
6807 req
->set_method("POST");
6808 req
->set_upload(CreateSimpleUploadData(kData
));
6809 HttpRequestHeaders headers
;
6810 headers
.SetHeader(HttpRequestHeaders::kContentLength
,
6811 base::SizeTToString(arraysize(kData
) - 1));
6812 req
->SetExtraRequestHeaders(headers
);
6814 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
6815 req
.get(), &default_network_delegate_
, test_server_
.GetURL("echo"),
6816 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT
,
6817 "Very Good Reason");
6818 AddTestInterceptor()->set_main_intercept_job(job
);
6821 base::RunLoop().Run();
6822 EXPECT_EQ("POST", req
->method());
6823 EXPECT_EQ(kData
, d
.data_received());
6826 // Check that default A-L header is sent.
6827 TEST_F(URLRequestTestHTTP
, DefaultAcceptLanguage
) {
6828 ASSERT_TRUE(test_server_
.Start());
6830 StaticHttpUserAgentSettings
settings("en", std::string());
6831 TestNetworkDelegate network_delegate
; // Must outlive URLRequests.
6832 TestURLRequestContext
context(true);
6833 context
.set_network_delegate(&network_delegate
);
6834 context
.set_http_user_agent_settings(&settings
);
6838 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
6839 test_server_
.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY
, &d
));
6841 base::RunLoop().Run();
6842 EXPECT_EQ("en", d
.data_received());
6845 // Check that an empty A-L header is not sent. http://crbug.com/77365.
6846 TEST_F(URLRequestTestHTTP
, EmptyAcceptLanguage
) {
6847 ASSERT_TRUE(test_server_
.Start());
6849 std::string empty_string
; // Avoid most vexing parse on line below.
6850 StaticHttpUserAgentSettings
settings(empty_string
, empty_string
);
6851 TestNetworkDelegate network_delegate
; // Must outlive URLRequests.
6852 TestURLRequestContext
context(true);
6853 context
.set_network_delegate(&network_delegate
);
6855 // We override the language after initialization because empty entries
6856 // get overridden by Init().
6857 context
.set_http_user_agent_settings(&settings
);
6860 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
6861 test_server_
.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY
, &d
));
6863 base::RunLoop().Run();
6864 EXPECT_EQ("None", d
.data_received());
6867 // Check that if request overrides the A-L header, the default is not appended.
6868 // See http://crbug.com/20894
6869 TEST_F(URLRequestTestHTTP
, OverrideAcceptLanguage
) {
6870 ASSERT_TRUE(test_server_
.Start());
6873 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6874 test_server_
.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY
, &d
));
6875 HttpRequestHeaders headers
;
6876 headers
.SetHeader(HttpRequestHeaders::kAcceptLanguage
, "ru");
6877 req
->SetExtraRequestHeaders(headers
);
6879 base::RunLoop().Run();
6880 EXPECT_EQ(std::string("ru"), d
.data_received());
6883 // Check that default A-E header is sent.
6884 TEST_F(URLRequestTestHTTP
, DefaultAcceptEncoding
) {
6885 ASSERT_TRUE(test_server_
.Start());
6888 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6889 test_server_
.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY
, &d
));
6890 HttpRequestHeaders headers
;
6891 req
->SetExtraRequestHeaders(headers
);
6893 base::RunLoop().Run();
6894 EXPECT_TRUE(ContainsString(d
.data_received(), "gzip"));
6897 // Check that if request overrides the A-E header, the default is not appended.
6898 // See http://crbug.com/47381
6899 TEST_F(URLRequestTestHTTP
, OverrideAcceptEncoding
) {
6900 ASSERT_TRUE(test_server_
.Start());
6903 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6904 test_server_
.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY
, &d
));
6905 HttpRequestHeaders headers
;
6906 headers
.SetHeader(HttpRequestHeaders::kAcceptEncoding
, "identity");
6907 req
->SetExtraRequestHeaders(headers
);
6909 base::RunLoop().Run();
6910 EXPECT_FALSE(ContainsString(d
.data_received(), "gzip"));
6911 EXPECT_TRUE(ContainsString(d
.data_received(), "identity"));
6914 // Check that setting the A-C header sends the proper header.
6915 TEST_F(URLRequestTestHTTP
, SetAcceptCharset
) {
6916 ASSERT_TRUE(test_server_
.Start());
6919 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6920 test_server_
.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY
, &d
));
6921 HttpRequestHeaders headers
;
6922 headers
.SetHeader(HttpRequestHeaders::kAcceptCharset
, "koi-8r");
6923 req
->SetExtraRequestHeaders(headers
);
6925 base::RunLoop().Run();
6926 EXPECT_EQ(std::string("koi-8r"), d
.data_received());
6929 // Check that default User-Agent header is sent.
6930 TEST_F(URLRequestTestHTTP
, DefaultUserAgent
) {
6931 ASSERT_TRUE(test_server_
.Start());
6934 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6935 test_server_
.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY
, &d
));
6937 base::RunLoop().Run();
6938 EXPECT_EQ(default_context_
.http_user_agent_settings()->GetUserAgent(),
6942 // Check that if request overrides the User-Agent header,
6943 // the default is not appended.
6944 TEST_F(URLRequestTestHTTP
, OverrideUserAgent
) {
6945 ASSERT_TRUE(test_server_
.Start());
6948 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6949 test_server_
.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY
, &d
));
6950 HttpRequestHeaders headers
;
6951 headers
.SetHeader(HttpRequestHeaders::kUserAgent
, "Lynx (textmode)");
6952 req
->SetExtraRequestHeaders(headers
);
6954 base::RunLoop().Run();
6955 EXPECT_EQ(std::string("Lynx (textmode)"), d
.data_received());
6958 // Check that a NULL HttpUserAgentSettings causes the corresponding empty
6959 // User-Agent header to be sent but does not send the Accept-Language and
6960 // Accept-Charset headers.
6961 TEST_F(URLRequestTestHTTP
, EmptyHttpUserAgentSettings
) {
6962 ASSERT_TRUE(test_server_
.Start());
6964 TestNetworkDelegate network_delegate
; // Must outlive URLRequests.
6965 TestURLRequestContext
context(true);
6966 context
.set_network_delegate(&network_delegate
);
6968 // We override the HttpUserAgentSettings after initialization because empty
6969 // entries get overridden by Init().
6970 context
.set_http_user_agent_settings(NULL
);
6973 const char* request
;
6974 const char* expected_response
;
6975 } tests
[] = { { "echoheader?Accept-Language", "None" },
6976 { "echoheader?Accept-Charset", "None" },
6977 { "echoheader?User-Agent", "" } };
6979 for (size_t i
= 0; i
< arraysize(tests
); i
++) {
6981 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
6982 test_server_
.GetURL(tests
[i
].request
), DEFAULT_PRIORITY
, &d
));
6984 base::RunLoop().Run();
6985 EXPECT_EQ(tests
[i
].expected_response
, d
.data_received())
6986 << " Request = \"" << tests
[i
].request
<< "\"";
6990 // Make sure that URLRequest passes on its priority updates to
6991 // newly-created jobs after the first one.
6992 TEST_F(URLRequestTestHTTP
, SetSubsequentJobPriority
) {
6993 ASSERT_TRUE(test_server_
.Start());
6996 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
6997 test_server_
.GetURL("empty.html"), DEFAULT_PRIORITY
, &d
));
6998 EXPECT_EQ(DEFAULT_PRIORITY
, req
->priority());
7000 scoped_refptr
<URLRequestRedirectJob
> redirect_job
=
7001 new URLRequestRedirectJob(
7002 req
.get(), &default_network_delegate_
, test_server_
.GetURL("echo"),
7003 URLRequestRedirectJob::REDIRECT_302_FOUND
, "Very Good Reason");
7004 AddTestInterceptor()->set_main_intercept_job(redirect_job
.get());
7006 req
->SetPriority(LOW
);
7008 EXPECT_TRUE(req
->is_pending());
7010 scoped_refptr
<URLRequestTestJob
> job
=
7011 new URLRequestTestJob(req
.get(), &default_network_delegate_
);
7012 AddTestInterceptor()->set_main_intercept_job(job
.get());
7014 // Should trigger |job| to be started.
7015 base::RunLoop().Run();
7016 EXPECT_EQ(LOW
, job
->priority());
7019 // Check that creating a network request while entering/exiting suspend mode
7020 // fails as it should. This is the only case where an HttpTransactionFactory
7021 // does not return an HttpTransaction.
7022 TEST_F(URLRequestTestHTTP
, NetworkSuspendTest
) {
7023 // Create a new HttpNetworkLayer that thinks it's suspended.
7024 HttpNetworkSession::Params params
;
7025 params
.host_resolver
= default_context_
.host_resolver();
7026 params
.cert_verifier
= default_context_
.cert_verifier();
7027 params
.transport_security_state
= default_context_
.transport_security_state();
7028 params
.proxy_service
= default_context_
.proxy_service();
7029 params
.ssl_config_service
= default_context_
.ssl_config_service();
7030 params
.http_auth_handler_factory
=
7031 default_context_
.http_auth_handler_factory();
7032 params
.network_delegate
= &default_network_delegate_
;
7033 params
.http_server_properties
= default_context_
.http_server_properties();
7034 scoped_ptr
<HttpNetworkLayer
> network_layer(
7035 new HttpNetworkLayer(new HttpNetworkSession(params
)));
7036 network_layer
->OnSuspend();
7038 HttpCache
http_cache(network_layer
.release(), default_context_
.net_log(),
7039 HttpCache::DefaultBackend::InMemory(0));
7041 TestURLRequestContext
context(true);
7042 context
.set_http_transaction_factory(&http_cache
);
7046 scoped_ptr
<URLRequest
> req(
7047 context
.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY
, &d
));
7049 base::RunLoop().Run();
7051 EXPECT_TRUE(d
.request_failed());
7052 EXPECT_EQ(URLRequestStatus::FAILED
, req
->status().status());
7053 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED
, req
->status().error());
7056 // Check that creating a network request while entering/exiting suspend mode
7057 // fails as it should in the case there is no cache. This is the only case
7058 // where an HttpTransactionFactory does not return an HttpTransaction.
7059 TEST_F(URLRequestTestHTTP
, NetworkSuspendTestNoCache
) {
7060 // Create a new HttpNetworkLayer that thinks it's suspended.
7061 HttpNetworkSession::Params params
;
7062 params
.host_resolver
= default_context_
.host_resolver();
7063 params
.cert_verifier
= default_context_
.cert_verifier();
7064 params
.transport_security_state
= default_context_
.transport_security_state();
7065 params
.proxy_service
= default_context_
.proxy_service();
7066 params
.ssl_config_service
= default_context_
.ssl_config_service();
7067 params
.http_auth_handler_factory
=
7068 default_context_
.http_auth_handler_factory();
7069 params
.network_delegate
= &default_network_delegate_
;
7070 params
.http_server_properties
= default_context_
.http_server_properties();
7071 HttpNetworkLayer
network_layer(new HttpNetworkSession(params
));
7072 network_layer
.OnSuspend();
7074 TestURLRequestContext
context(true);
7075 context
.set_http_transaction_factory(&network_layer
);
7079 scoped_ptr
<URLRequest
> req(
7080 context
.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY
, &d
));
7082 base::RunLoop().Run();
7084 EXPECT_TRUE(d
.request_failed());
7085 EXPECT_EQ(URLRequestStatus::FAILED
, req
->status().status());
7086 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED
, req
->status().error());
7089 TEST_F(URLRequestTestHTTP
, NetworkAccessedSetOnNetworkRequest
) {
7090 ASSERT_TRUE(test_server_
.Start());
7093 GURL
test_url(test_server_
.GetURL(std::string()));
7094 scoped_ptr
<URLRequest
> req(
7095 default_context_
.CreateRequest(test_url
, DEFAULT_PRIORITY
, &d
));
7098 base::RunLoop().Run();
7100 EXPECT_TRUE(req
->response_info().network_accessed
);
7103 TEST_F(URLRequestTestHTTP
, NetworkAccessedClearOnCachedResponse
) {
7104 ASSERT_TRUE(test_server_
.Start());
7106 // Populate the cache.
7108 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
7109 test_server_
.GetURL("cachetime"), DEFAULT_PRIORITY
, &d
));
7111 base::RunLoop().Run();
7113 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
7114 EXPECT_TRUE(req
->response_info().network_accessed
);
7115 EXPECT_FALSE(req
->response_info().was_cached
);
7117 req
= default_context_
.CreateRequest(test_server_
.GetURL("cachetime"),
7118 DEFAULT_PRIORITY
, &d
);
7120 base::RunLoop().Run();
7122 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
->status().status());
7123 EXPECT_FALSE(req
->response_info().network_accessed
);
7124 EXPECT_TRUE(req
->response_info().was_cached
);
7127 TEST_F(URLRequestTestHTTP
, NetworkAccessedClearOnLoadOnlyFromCache
) {
7128 ASSERT_TRUE(test_server_
.Start());
7131 GURL
test_url(test_server_
.GetURL(std::string()));
7132 scoped_ptr
<URLRequest
> req(
7133 default_context_
.CreateRequest(test_url
, DEFAULT_PRIORITY
, &d
));
7134 req
->SetLoadFlags(LOAD_ONLY_FROM_CACHE
);
7137 base::RunLoop().Run();
7139 EXPECT_FALSE(req
->response_info().network_accessed
);
7142 class URLRequestInterceptorTestHTTP
: public URLRequestTestHTTP
{
7144 // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture,
7145 // ideally remove the dependency on URLRequestTestJob, and maybe move these
7146 // tests into the factory tests.
7147 URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL
) {
7150 void SetUpFactory() override
{
7151 interceptor_
= new MockURLRequestInterceptor();
7152 job_factory_
.reset(new URLRequestInterceptingJobFactory(
7153 job_factory_
.Pass(), make_scoped_ptr(interceptor_
)));
7156 MockURLRequestInterceptor
* interceptor() const {
7157 return interceptor_
;
7161 MockURLRequestInterceptor
* interceptor_
;
7164 TEST_F(URLRequestInterceptorTestHTTP
,
7165 NetworkDelegateNotificationOnRedirectIntercept
) {
7166 interceptor()->set_intercept_redirect(true);
7167 interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers());
7168 interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data());
7170 ASSERT_TRUE(test_server()->Start());
7173 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
7174 test_server()->GetURL("files/redirect-test.html"), DEFAULT_PRIORITY
, &d
));
7176 base::RunLoop().Run();
7178 EXPECT_TRUE(interceptor()->did_intercept_redirect());
7179 // Check we got one good response
7180 EXPECT_TRUE(req
->status().is_success());
7181 if (req
->status().is_success())
7182 EXPECT_EQ(200, req
->response_headers()->response_code());
7184 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d
.data_received());
7185 EXPECT_EQ(1, d
.response_started_count());
7186 EXPECT_EQ(0, d
.received_redirect_count());
7188 EXPECT_EQ(1, default_network_delegate()->created_requests());
7189 EXPECT_EQ(1, default_network_delegate()->before_send_headers_count());
7190 EXPECT_EQ(1, default_network_delegate()->headers_received_count());
7193 TEST_F(URLRequestInterceptorTestHTTP
,
7194 NetworkDelegateNotificationOnErrorIntercept
) {
7195 // Intercept that error and respond with an OK response.
7196 interceptor()->set_intercept_final_response(true);
7197 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
7198 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
7199 default_network_delegate()->set_can_be_intercepted_on_error(true);
7201 ASSERT_TRUE(test_server()->Start());
7204 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
7205 test_server()->GetURL("files/two-content-lengths.html"), DEFAULT_PRIORITY
,
7207 req
->set_method("GET");
7209 base::RunLoop().Run();
7211 EXPECT_TRUE(interceptor()->did_intercept_final());
7213 // Check we received one good response.
7214 EXPECT_TRUE(req
->status().is_success());
7215 if (req
->status().is_success())
7216 EXPECT_EQ(200, req
->response_headers()->response_code());
7217 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d
.data_received());
7218 EXPECT_EQ(1, d
.response_started_count());
7219 EXPECT_EQ(0, d
.received_redirect_count());
7221 EXPECT_EQ(1, default_network_delegate()->created_requests());
7222 EXPECT_EQ(1, default_network_delegate()->before_send_headers_count());
7223 EXPECT_EQ(0, default_network_delegate()->headers_received_count());
7226 TEST_F(URLRequestInterceptorTestHTTP
,
7227 NetworkDelegateNotificationOnResponseIntercept
) {
7228 // Intercept that error and respond with an OK response.
7229 interceptor()->set_intercept_final_response(true);
7231 // Intercept with a real URLRequestHttpJob.
7232 interceptor()->set_use_url_request_http_job(true);
7234 ASSERT_TRUE(test_server()->Start());
7237 scoped_ptr
<URLRequest
> req(default_context().CreateRequest(
7238 test_server()->GetURL("files/simple.html"), DEFAULT_PRIORITY
, &d
));
7239 req
->set_method("GET");
7241 base::RunLoop().Run();
7243 EXPECT_TRUE(interceptor()->did_intercept_final());
7245 // Check we received one good response.
7246 EXPECT_TRUE(req
->status().is_success());
7247 if (req
->status().is_success())
7248 EXPECT_EQ(200, req
->response_headers()->response_code());
7249 EXPECT_EQ("hello", d
.data_received());
7250 EXPECT_EQ(1, d
.response_started_count());
7251 EXPECT_EQ(0, d
.received_redirect_count());
7253 EXPECT_EQ(1, default_network_delegate()->created_requests());
7254 EXPECT_EQ(2, default_network_delegate()->before_send_headers_count());
7255 EXPECT_EQ(2, default_network_delegate()->headers_received_count());
7258 class URLRequestTestReferrerPolicy
: public URLRequestTest
{
7260 URLRequestTestReferrerPolicy() {}
7262 void InstantiateSameOriginServers(SpawnedTestServer::Type origin_type
) {
7263 origin_server_
.reset(new SpawnedTestServer(
7264 origin_type
, SpawnedTestServer::kLocalhost
,
7265 origin_type
== SpawnedTestServer::TYPE_HTTPS
7266 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
7267 : base::FilePath(kTestFilePath
)));
7268 ASSERT_TRUE(origin_server_
->Start());
7271 void InstantiateCrossOriginServers(SpawnedTestServer::Type origin_type
,
7272 SpawnedTestServer::Type destination_type
) {
7273 origin_server_
.reset(new SpawnedTestServer(
7274 origin_type
, SpawnedTestServer::kLocalhost
,
7275 origin_type
== SpawnedTestServer::TYPE_HTTPS
7276 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
7277 : base::FilePath(kTestFilePath
)));
7278 ASSERT_TRUE(origin_server_
->Start());
7280 destination_server_
.reset(new SpawnedTestServer(
7281 destination_type
, SpawnedTestServer::kLocalhost
,
7282 destination_type
== SpawnedTestServer::TYPE_HTTPS
7283 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
7284 : base::FilePath(kTestFilePath
)));
7285 ASSERT_TRUE(destination_server_
->Start());
7288 void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy
,
7289 const GURL
& referrer
,
7290 const GURL
& expected
) {
7291 // Create and execute the request: we'll only have a |destination_server_|
7292 // if the origins are meant to be distinct. Otherwise, we'll use the
7293 // |origin_server_| for both endpoints.
7294 GURL destination_url
=
7295 destination_server_
? destination_server_
->GetURL("echoheader?Referer")
7296 : origin_server_
->GetURL("echoheader?Referer");
7298 origin_server_
->GetURL("server-redirect?" + destination_url
.spec());
7301 scoped_ptr
<URLRequest
> req(
7302 default_context_
.CreateRequest(origin_url
, DEFAULT_PRIORITY
, &d
));
7303 req
->set_referrer_policy(policy
);
7304 req
->SetReferrer(referrer
.spec());
7306 base::RunLoop().Run();
7308 EXPECT_EQ(1, d
.response_started_count());
7309 EXPECT_EQ(1, d
.received_redirect_count());
7310 EXPECT_EQ(destination_url
, req
->url());
7311 EXPECT_TRUE(req
->status().is_success());
7312 EXPECT_EQ(200, req
->response_headers()->response_code());
7314 EXPECT_EQ(expected
.spec(), req
->referrer());
7315 if (expected
.is_empty())
7316 EXPECT_EQ("None", d
.data_received());
7318 EXPECT_EQ(expected
.spec(), d
.data_received());
7321 SpawnedTestServer
* origin_server() const { return origin_server_
.get(); }
7324 scoped_ptr
<SpawnedTestServer
> origin_server_
;
7325 scoped_ptr
<SpawnedTestServer
> destination_server_
;
7328 TEST_F(URLRequestTestReferrerPolicy
, HTTPToSameOriginHTTP
) {
7329 InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTP
);
7331 VerifyReferrerAfterRedirect(
7332 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
7333 origin_server()->GetURL("path/to/file.html"),
7334 origin_server()->GetURL("path/to/file.html"));
7336 VerifyReferrerAfterRedirect(
7337 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN
,
7338 origin_server()->GetURL("path/to/file.html"),
7339 origin_server()->GetURL("path/to/file.html"));
7341 VerifyReferrerAfterRedirect(
7342 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN
,
7343 origin_server()->GetURL("path/to/file.html"),
7344 origin_server()->GetURL("path/to/file.html"));
7346 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER
,
7347 origin_server()->GetURL("path/to/file.html"),
7348 origin_server()->GetURL("path/to/file.html"));
7351 // Can't spin up more than one SpawnedTestServer on Android.
7352 #if defined(OS_ANDROID)
7353 #define MAYBE_HTTPToCrossOriginHTTP DISABLED_HTTPToCrosOriginHTTP
7355 #define MAYBE_HTTPToCrossOriginHTTP HTTPToCrossOriginHTTP
7357 TEST_F(URLRequestTestReferrerPolicy
, MAYBE_HTTPToCrossOriginHTTP
) {
7358 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP
,
7359 SpawnedTestServer::TYPE_HTTP
);
7361 VerifyReferrerAfterRedirect(
7362 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
7363 origin_server()->GetURL("path/to/file.html"),
7364 origin_server()->GetURL("path/to/file.html"));
7366 VerifyReferrerAfterRedirect(
7367 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN
,
7368 origin_server()->GetURL("path/to/file.html"),
7369 origin_server()->GetURL(std::string()));
7371 VerifyReferrerAfterRedirect(
7372 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN
,
7373 origin_server()->GetURL("path/to/file.html"),
7374 origin_server()->GetURL(std::string()));
7376 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER
,
7377 origin_server()->GetURL("path/to/file.html"),
7378 origin_server()->GetURL("path/to/file.html"));
7381 TEST_F(URLRequestTestReferrerPolicy
, HTTPSToSameOriginHTTPS
) {
7382 InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTPS
);
7384 VerifyReferrerAfterRedirect(
7385 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
7386 origin_server()->GetURL("path/to/file.html"),
7387 origin_server()->GetURL("path/to/file.html"));
7389 VerifyReferrerAfterRedirect(
7390 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN
,
7391 origin_server()->GetURL("path/to/file.html"),
7392 origin_server()->GetURL("path/to/file.html"));
7394 VerifyReferrerAfterRedirect(
7395 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN
,
7396 origin_server()->GetURL("path/to/file.html"),
7397 origin_server()->GetURL("path/to/file.html"));
7399 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER
,
7400 origin_server()->GetURL("path/to/file.html"),
7401 origin_server()->GetURL("path/to/file.html"));
7404 // Can't spin up more than one SpawnedTestServer on Android.
7405 #if defined(OS_ANDROID)
7406 #define MAYBE_HTTPSToCrossOriginHTTPS DISABLED_HTTPSToCrosOriginHTTPS
7408 #define MAYBE_HTTPSToCrossOriginHTTPS HTTPSToCrossOriginHTTPS
7410 TEST_F(URLRequestTestReferrerPolicy
, MAYBE_HTTPSToCrossOriginHTTPS
) {
7411 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS
,
7412 SpawnedTestServer::TYPE_HTTPS
);
7414 VerifyReferrerAfterRedirect(
7415 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
7416 origin_server()->GetURL("path/to/file.html"),
7417 origin_server()->GetURL("path/to/file.html"));
7419 VerifyReferrerAfterRedirect(
7420 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN
,
7421 origin_server()->GetURL("path/to/file.html"),
7422 origin_server()->GetURL(std::string()));
7424 VerifyReferrerAfterRedirect(
7425 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN
,
7426 origin_server()->GetURL("path/to/file.html"),
7427 origin_server()->GetURL(std::string()));
7429 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER
,
7430 origin_server()->GetURL("path/to/file.html"),
7431 origin_server()->GetURL("path/to/file.html"));
7434 // Can't spin up more than one SpawnedTestServer on Android.
7435 #if defined(OS_ANDROID)
7436 #define MAYBE_HTTPToHTTPS DISABLED_HTTPToHTTPS
7438 #define MAYBE_HTTPToHTTPS HTTPToHTTPS
7440 TEST_F(URLRequestTestReferrerPolicy
, MAYBE_HTTPToHTTPS
) {
7441 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP
,
7442 SpawnedTestServer::TYPE_HTTPS
);
7444 VerifyReferrerAfterRedirect(
7445 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
7446 origin_server()->GetURL("path/to/file.html"),
7447 origin_server()->GetURL("path/to/file.html"));
7449 VerifyReferrerAfterRedirect(
7450 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN
,
7451 origin_server()->GetURL("path/to/file.html"),
7452 origin_server()->GetURL(std::string()));
7454 VerifyReferrerAfterRedirect(
7455 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN
,
7456 origin_server()->GetURL("path/to/file.html"),
7457 origin_server()->GetURL(std::string()));
7459 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER
,
7460 origin_server()->GetURL("path/to/file.html"),
7461 origin_server()->GetURL("path/to/file.html"));
7464 // Can't spin up more than one SpawnedTestServer on Android.
7465 #if defined(OS_ANDROID)
7466 #define MAYBE_HTTPSToHTTP DISABLED_HTTPSToHTTP
7468 #define MAYBE_HTTPSToHTTP HTTPSToHTTP
7470 TEST_F(URLRequestTestReferrerPolicy
, MAYBE_HTTPSToHTTP
) {
7471 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS
,
7472 SpawnedTestServer::TYPE_HTTP
);
7474 VerifyReferrerAfterRedirect(
7475 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE
,
7476 origin_server()->GetURL("path/to/file.html"), GURL());
7478 VerifyReferrerAfterRedirect(
7479 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN
,
7480 origin_server()->GetURL("path/to/file.html"), GURL());
7482 VerifyReferrerAfterRedirect(
7483 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN
,
7484 origin_server()->GetURL("path/to/file.html"),
7485 origin_server()->GetURL(std::string()));
7487 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER
,
7488 origin_server()->GetURL("path/to/file.html"),
7489 origin_server()->GetURL("path/to/file.html"));
7492 class HTTPSRequestTest
: public testing::Test
{
7494 HTTPSRequestTest() : default_context_(true) {
7495 default_context_
.set_network_delegate(&default_network_delegate_
);
7496 default_context_
.Init();
7498 ~HTTPSRequestTest() override
{}
7501 TestNetworkDelegate default_network_delegate_
; // Must outlive URLRequest.
7502 TestURLRequestContext default_context_
;
7505 TEST_F(HTTPSRequestTest
, HTTPSGetTest
) {
7506 SpawnedTestServer
test_server(
7507 SpawnedTestServer::TYPE_HTTPS
,
7508 SpawnedTestServer::kLocalhost
,
7509 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7510 ASSERT_TRUE(test_server
.Start());
7514 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
7515 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
7517 EXPECT_TRUE(r
->is_pending());
7519 base::RunLoop().Run();
7521 EXPECT_EQ(1, d
.response_started_count());
7522 EXPECT_FALSE(d
.received_data_before_response());
7523 EXPECT_NE(0, d
.bytes_received());
7524 CheckSSLInfo(r
->ssl_info());
7525 EXPECT_EQ(test_server
.host_port_pair().host(),
7526 r
->GetSocketAddress().host());
7527 EXPECT_EQ(test_server
.host_port_pair().port(),
7528 r
->GetSocketAddress().port());
7532 TEST_F(HTTPSRequestTest
, HTTPSMismatchedTest
) {
7533 SpawnedTestServer::SSLOptions
ssl_options(
7534 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
);
7535 SpawnedTestServer
test_server(
7536 SpawnedTestServer::TYPE_HTTPS
,
7538 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7539 ASSERT_TRUE(test_server
.Start());
7541 bool err_allowed
= true;
7542 for (int i
= 0; i
< 2 ; i
++, err_allowed
= !err_allowed
) {
7545 d
.set_allow_certificate_errors(err_allowed
);
7546 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
7547 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
7550 EXPECT_TRUE(r
->is_pending());
7552 base::RunLoop().Run();
7554 EXPECT_EQ(1, d
.response_started_count());
7555 EXPECT_FALSE(d
.received_data_before_response());
7556 EXPECT_TRUE(d
.have_certificate_errors());
7558 EXPECT_NE(0, d
.bytes_received());
7559 CheckSSLInfo(r
->ssl_info());
7561 EXPECT_EQ(0, d
.bytes_received());
7567 TEST_F(HTTPSRequestTest
, HTTPSExpiredTest
) {
7568 SpawnedTestServer::SSLOptions
ssl_options(
7569 SpawnedTestServer::SSLOptions::CERT_EXPIRED
);
7570 SpawnedTestServer
test_server(
7571 SpawnedTestServer::TYPE_HTTPS
,
7573 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7574 ASSERT_TRUE(test_server
.Start());
7576 // Iterate from false to true, just so that we do the opposite of the
7577 // previous test in order to increase test coverage.
7578 bool err_allowed
= false;
7579 for (int i
= 0; i
< 2 ; i
++, err_allowed
= !err_allowed
) {
7582 d
.set_allow_certificate_errors(err_allowed
);
7583 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
7584 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
7587 EXPECT_TRUE(r
->is_pending());
7589 base::RunLoop().Run();
7591 EXPECT_EQ(1, d
.response_started_count());
7592 EXPECT_FALSE(d
.received_data_before_response());
7593 EXPECT_TRUE(d
.have_certificate_errors());
7595 EXPECT_NE(0, d
.bytes_received());
7596 CheckSSLInfo(r
->ssl_info());
7598 EXPECT_EQ(0, d
.bytes_received());
7604 // Tests that servers which require a deprecated cipher suite still work.
7605 TEST_F(HTTPSRequestTest
, CipherFallbackTest
) {
7607 default_context_
.set_net_log(&net_log
);
7609 SpawnedTestServer::SSLOptions ssl_options
;
7610 ssl_options
.bulk_ciphers
= SpawnedTestServer::SSLOptions::BULK_CIPHER_RC4
;
7611 SpawnedTestServer
test_server(
7612 SpawnedTestServer::TYPE_HTTPS
, ssl_options
,
7613 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7614 ASSERT_TRUE(test_server
.Start());
7617 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
7618 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
7620 EXPECT_TRUE(r
->is_pending());
7622 base::RunLoop().Run();
7624 EXPECT_EQ(1, d
.response_started_count());
7625 EXPECT_FALSE(d
.received_data_before_response());
7626 EXPECT_NE(0, d
.bytes_received());
7627 CheckSSLInfo(r
->ssl_info());
7628 EXPECT_EQ(test_server
.host_port_pair().host(), r
->GetSocketAddress().host());
7629 EXPECT_EQ(test_server
.host_port_pair().port(), r
->GetSocketAddress().port());
7631 // No version downgrade should have been necessary.
7632 EXPECT_FALSE(r
->ssl_info().connection_status
&
7633 SSL_CONNECTION_VERSION_FALLBACK
);
7634 int expected_version
= SSL_CONNECTION_VERSION_TLS1_2
;
7635 if (SSLClientSocket::GetMaxSupportedSSLVersion() <
7636 SSL_PROTOCOL_VERSION_TLS1_2
) {
7637 expected_version
= SSL_CONNECTION_VERSION_TLS1_1
;
7639 EXPECT_EQ(expected_version
,
7640 SSLConnectionStatusToVersion(r
->ssl_info().connection_status
));
7642 TestNetLogEntry::List entries
;
7643 net_log
.GetEntries(&entries
);
7644 ExpectLogContainsSomewhere(entries
, 0, NetLog::TYPE_SSL_CIPHER_FALLBACK
,
7645 NetLog::PHASE_NONE
);
7648 // This tests that a load of www.google.com with a certificate error sets
7649 // the |certificate_errors_are_fatal| flag correctly. This flag will cause
7650 // the interstitial to be fatal.
7651 TEST_F(HTTPSRequestTest
, HTTPSPreloadedHSTSTest
) {
7652 SpawnedTestServer::SSLOptions
ssl_options(
7653 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
);
7654 SpawnedTestServer
test_server(
7655 SpawnedTestServer::TYPE_HTTPS
,
7657 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7658 ASSERT_TRUE(test_server
.Start());
7660 // We require that the URL be www.google.com in order to pick up the
7661 // preloaded HSTS entries in the TransportSecurityState. This means that we
7662 // have to use a MockHostResolver in order to direct www.google.com to the
7663 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
7665 MockHostResolver host_resolver
;
7666 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
7667 TestURLRequestContext
context(true);
7668 context
.set_network_delegate(&network_delegate
);
7669 context
.set_host_resolver(&host_resolver
);
7670 TransportSecurityState transport_security_state
;
7671 context
.set_transport_security_state(&transport_security_state
);
7675 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
7676 GURL(base::StringPrintf("https://www.google.com:%d",
7677 test_server
.host_port_pair().port())),
7678 DEFAULT_PRIORITY
, &d
));
7681 EXPECT_TRUE(r
->is_pending());
7683 base::RunLoop().Run();
7685 EXPECT_EQ(1, d
.response_started_count());
7686 EXPECT_FALSE(d
.received_data_before_response());
7687 EXPECT_TRUE(d
.have_certificate_errors());
7688 EXPECT_TRUE(d
.certificate_errors_are_fatal());
7691 // This tests that cached HTTPS page loads do not cause any updates to the
7692 // TransportSecurityState.
7693 TEST_F(HTTPSRequestTest
, HTTPSErrorsNoClobberTSSTest
) {
7694 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
7695 // matter. It just has to be any error.
7696 SpawnedTestServer::SSLOptions
ssl_options(
7697 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
);
7698 SpawnedTestServer
test_server(
7699 SpawnedTestServer::TYPE_HTTPS
,
7701 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7702 ASSERT_TRUE(test_server
.Start());
7704 // We require that the URL be www.google.com in order to pick up the static
7705 // and dynamic STS and PKP entries in the TransportSecurityState. This means
7706 // that we have to use a MockHostResolver in order to direct www.google.com to
7707 // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
7709 MockHostResolver host_resolver
;
7710 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
7711 TestURLRequestContext
context(true);
7712 context
.set_network_delegate(&network_delegate
);
7713 context
.set_host_resolver(&host_resolver
);
7714 TransportSecurityState transport_security_state
;
7716 TransportSecurityState::STSState static_sts_state
;
7717 TransportSecurityState::PKPState static_pkp_state
;
7718 EXPECT_TRUE(transport_security_state
.GetStaticDomainState(
7719 "www.google.com", &static_sts_state
, &static_pkp_state
));
7720 context
.set_transport_security_state(&transport_security_state
);
7723 TransportSecurityState::STSState dynamic_sts_state
;
7724 TransportSecurityState::PKPState dynamic_pkp_state
;
7725 EXPECT_FALSE(transport_security_state
.GetDynamicSTSState("www.google.com",
7726 &dynamic_sts_state
));
7727 EXPECT_FALSE(transport_security_state
.GetDynamicPKPState("www.google.com",
7728 &dynamic_pkp_state
));
7731 scoped_ptr
<URLRequest
> r(context
.CreateRequest(
7732 GURL(base::StringPrintf("https://www.google.com:%d",
7733 test_server
.host_port_pair().port())),
7734 DEFAULT_PRIORITY
, &d
));
7737 EXPECT_TRUE(r
->is_pending());
7739 base::RunLoop().Run();
7741 EXPECT_EQ(1, d
.response_started_count());
7742 EXPECT_FALSE(d
.received_data_before_response());
7743 EXPECT_TRUE(d
.have_certificate_errors());
7744 EXPECT_TRUE(d
.certificate_errors_are_fatal());
7746 // Get a fresh copy of the states, and check that they haven't changed.
7747 TransportSecurityState::STSState new_static_sts_state
;
7748 TransportSecurityState::PKPState new_static_pkp_state
;
7749 EXPECT_TRUE(transport_security_state
.GetStaticDomainState(
7750 "www.google.com", &new_static_sts_state
, &new_static_pkp_state
));
7751 TransportSecurityState::STSState new_dynamic_sts_state
;
7752 TransportSecurityState::PKPState new_dynamic_pkp_state
;
7753 EXPECT_FALSE(transport_security_state
.GetDynamicSTSState(
7754 "www.google.com", &new_dynamic_sts_state
));
7755 EXPECT_FALSE(transport_security_state
.GetDynamicPKPState(
7756 "www.google.com", &new_dynamic_pkp_state
));
7758 EXPECT_EQ(new_static_sts_state
.upgrade_mode
, static_sts_state
.upgrade_mode
);
7759 EXPECT_EQ(new_static_sts_state
.include_subdomains
,
7760 static_sts_state
.include_subdomains
);
7761 EXPECT_EQ(new_static_pkp_state
.include_subdomains
,
7762 static_pkp_state
.include_subdomains
);
7763 EXPECT_TRUE(FingerprintsEqual(new_static_pkp_state
.spki_hashes
,
7764 static_pkp_state
.spki_hashes
));
7765 EXPECT_TRUE(FingerprintsEqual(new_static_pkp_state
.bad_spki_hashes
,
7766 static_pkp_state
.bad_spki_hashes
));
7769 // Make sure HSTS preserves a POST request's method and body.
7770 TEST_F(HTTPSRequestTest
, HSTSPreservesPosts
) {
7771 static const char kData
[] = "hello world";
7773 SpawnedTestServer::SSLOptions
ssl_options(
7774 SpawnedTestServer::SSLOptions::CERT_OK
);
7775 SpawnedTestServer
test_server(
7776 SpawnedTestServer::TYPE_HTTPS
,
7778 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7779 ASSERT_TRUE(test_server
.Start());
7782 // Per spec, TransportSecurityState expects a domain name, rather than an IP
7783 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
7784 // the SpawnedTestServer. By default, MockHostResolver maps all hosts
7786 MockHostResolver host_resolver
;
7788 // Force https for www.somewhere.com.
7789 TransportSecurityState transport_security_state
;
7790 base::Time expiry
= base::Time::Now() + base::TimeDelta::FromDays(1000);
7791 bool include_subdomains
= false;
7792 transport_security_state
.AddHSTS("www.somewhere.com", expiry
,
7793 include_subdomains
);
7795 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
7797 TestURLRequestContext
context(true);
7798 context
.set_host_resolver(&host_resolver
);
7799 context
.set_transport_security_state(&transport_security_state
);
7800 context
.set_network_delegate(&network_delegate
);
7804 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
7805 // cause a certificate error. Ignore the error.
7806 d
.set_allow_certificate_errors(true);
7808 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
7809 GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
7810 test_server
.host_port_pair().port())),
7811 DEFAULT_PRIORITY
, &d
));
7812 req
->set_method("POST");
7813 req
->set_upload(CreateSimpleUploadData(kData
));
7816 base::RunLoop().Run();
7818 EXPECT_EQ("https", req
->url().scheme());
7819 EXPECT_EQ("POST", req
->method());
7820 EXPECT_EQ(kData
, d
.data_received());
7822 LoadTimingInfo load_timing_info
;
7823 network_delegate
.GetLoadTimingInfoBeforeRedirect(&load_timing_info
);
7824 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
7825 TestLoadTimingCacheHitNoNetwork(load_timing_info
);
7828 // Make sure that the CORS headers are added to cross-origin HSTS redirects.
7829 TEST_F(HTTPSRequestTest
, HSTSCrossOriginAddHeaders
) {
7830 static const char kOriginHeaderValue
[] = "http://www.example.com";
7832 SpawnedTestServer::SSLOptions
ssl_options(
7833 SpawnedTestServer::SSLOptions::CERT_OK
);
7834 SpawnedTestServer
test_server(
7835 SpawnedTestServer::TYPE_HTTPS
,
7837 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7838 ASSERT_TRUE(test_server
.Start());
7840 // Per spec, TransportSecurityState expects a domain name, rather than an IP
7841 // address, so a MockHostResolver is needed to redirect example.net to the
7842 // SpawnedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by default.
7843 MockHostResolver host_resolver
;
7845 TransportSecurityState transport_security_state
;
7846 base::Time expiry
= base::Time::Now() + base::TimeDelta::FromDays(1);
7847 bool include_subdomains
= false;
7848 transport_security_state
.AddHSTS("example.net", expiry
, include_subdomains
);
7850 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
7852 MockCertVerifier cert_verifier
;
7853 cert_verifier
.set_default_result(OK
);
7855 TestURLRequestContext
context(true);
7856 context
.set_host_resolver(&host_resolver
);
7857 context
.set_transport_security_state(&transport_security_state
);
7858 context
.set_network_delegate(&network_delegate
);
7859 context
.set_cert_verifier(&cert_verifier
);
7862 GURL
hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite",
7863 test_server
.host_port_pair().port()));
7864 url::Replacements
<char> replacements
;
7865 const char kNewScheme
[] = "https";
7866 replacements
.SetScheme(kNewScheme
, url::Component(0, strlen(kNewScheme
)));
7867 GURL hsts_https_url
= hsts_http_url
.ReplaceComponents(replacements
);
7870 // Quit on redirect to allow response header inspection upon redirect.
7871 d
.set_quit_on_redirect(true);
7873 scoped_ptr
<URLRequest
> req(
7874 context
.CreateRequest(hsts_http_url
, DEFAULT_PRIORITY
, &d
));
7875 // Set Origin header to simulate a cross-origin request.
7876 HttpRequestHeaders request_headers
;
7877 request_headers
.SetHeader("Origin", kOriginHeaderValue
);
7878 req
->SetExtraRequestHeaders(request_headers
);
7881 base::RunLoop().Run();
7883 EXPECT_EQ(1, d
.received_redirect_count());
7885 const HttpResponseHeaders
* headers
= req
->response_headers();
7886 std::string redirect_location
;
7887 EXPECT_TRUE(headers
->EnumerateHeader(NULL
, "Location", &redirect_location
));
7888 EXPECT_EQ(hsts_https_url
.spec(), redirect_location
);
7890 std::string received_cors_header
;
7891 EXPECT_TRUE(headers
->EnumerateHeader(NULL
, "Access-Control-Allow-Origin",
7892 &received_cors_header
));
7893 EXPECT_EQ(kOriginHeaderValue
, received_cors_header
);
7896 // This just tests the behaviour of GetHSTSRedirect(). End-to-end tests of HSTS
7897 // are performed in net/websockets/websocket_end_to_end_test.cc.
7898 TEST(WebSocketURLRequestTest
, HSTSApplied
) {
7899 TestNetworkDelegate network_delegate
;
7900 TransportSecurityState transport_security_state
;
7901 base::Time expiry
= base::Time::Now() + base::TimeDelta::FromDays(1);
7902 bool include_subdomains
= false;
7903 transport_security_state
.AddHSTS("example.net", expiry
, include_subdomains
);
7904 TestURLRequestContext
context(true);
7905 context
.set_transport_security_state(&transport_security_state
);
7906 context
.set_network_delegate(&network_delegate
);
7908 GURL
ws_url("ws://example.net/echo");
7909 TestDelegate delegate
;
7910 scoped_ptr
<URLRequest
> request(
7911 context
.CreateRequest(ws_url
, DEFAULT_PRIORITY
, &delegate
));
7912 EXPECT_TRUE(request
->GetHSTSRedirect(&ws_url
));
7913 EXPECT_TRUE(ws_url
.SchemeIs("wss"));
7918 class SSLClientAuthTestDelegate
: public TestDelegate
{
7920 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
7922 void OnCertificateRequested(URLRequest
* request
,
7923 SSLCertRequestInfo
* cert_request_info
) override
{
7924 on_certificate_requested_count_
++;
7925 base::MessageLoop::current()->Quit();
7927 int on_certificate_requested_count() {
7928 return on_certificate_requested_count_
;
7931 int on_certificate_requested_count_
;
7936 // TODO(davidben): Test the rest of the code. Specifically,
7937 // - Filtering which certificates to select.
7938 // - Sending a certificate back.
7939 // - Getting a certificate request in an SSL renegotiation sending the
7941 TEST_F(HTTPSRequestTest
, ClientAuthTest
) {
7942 SpawnedTestServer::SSLOptions ssl_options
;
7943 ssl_options
.request_client_certificate
= true;
7944 SpawnedTestServer
test_server(
7945 SpawnedTestServer::TYPE_HTTPS
,
7947 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7948 ASSERT_TRUE(test_server
.Start());
7950 SSLClientAuthTestDelegate d
;
7952 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
7953 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
7956 EXPECT_TRUE(r
->is_pending());
7958 base::RunLoop().Run();
7960 EXPECT_EQ(1, d
.on_certificate_requested_count());
7961 EXPECT_FALSE(d
.received_data_before_response());
7962 EXPECT_EQ(0, d
.bytes_received());
7964 // Send no certificate.
7965 // TODO(davidben): Get temporary client cert import (with keys) working on
7966 // all platforms so we can test sending a cert as well.
7967 r
->ContinueWithCertificate(NULL
);
7969 base::RunLoop().Run();
7971 EXPECT_EQ(1, d
.response_started_count());
7972 EXPECT_FALSE(d
.received_data_before_response());
7973 EXPECT_NE(0, d
.bytes_received());
7977 TEST_F(HTTPSRequestTest
, ResumeTest
) {
7978 // Test that we attempt a session resume when making two connections to the
7980 SpawnedTestServer::SSLOptions ssl_options
;
7981 ssl_options
.record_resume
= true;
7982 SpawnedTestServer
test_server(
7983 SpawnedTestServer::TYPE_HTTPS
,
7985 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7986 ASSERT_TRUE(test_server
.Start());
7988 SSLClientSocket::ClearSessionCache();
7992 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
7993 test_server
.GetURL("ssl-session-cache"), DEFAULT_PRIORITY
, &d
));
7996 EXPECT_TRUE(r
->is_pending());
7998 base::RunLoop().Run();
8000 EXPECT_EQ(1, d
.response_started_count());
8003 reinterpret_cast<HttpCache
*>(default_context_
.http_transaction_factory())->
8004 CloseAllConnections();
8008 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
8009 test_server
.GetURL("ssl-session-cache"), DEFAULT_PRIORITY
, &d
));
8012 EXPECT_TRUE(r
->is_pending());
8014 base::RunLoop().Run();
8016 // The response will look like;
8021 // With a newline at the end which makes the split think that there are
8024 EXPECT_EQ(1, d
.response_started_count());
8025 std::vector
<std::string
> lines
= base::SplitString(
8026 d
.data_received(), "\n", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
8027 ASSERT_EQ(4u, lines
.size()) << d
.data_received();
8029 std::string session_id
;
8031 for (size_t i
= 0; i
< 2; i
++) {
8032 std::vector
<std::string
> parts
= base::SplitString(
8033 lines
[i
], "\t", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
8034 ASSERT_EQ(2u, parts
.size());
8036 EXPECT_EQ("insert", parts
[0]);
8037 session_id
= parts
[1];
8039 EXPECT_EQ("lookup", parts
[0]);
8040 EXPECT_EQ(session_id
, parts
[1]);
8046 // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be
8047 // the result of fetching "ssl-session-cache" from the test server, indicates
8048 // that exactly two different sessions were inserted, with no lookups etc.
8049 static void AssertTwoDistinctSessionsInserted(const string
& session_info
) {
8050 std::vector
<std::string
> lines
= base::SplitString(
8051 session_info
, "\n", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
8052 ASSERT_EQ(3u, lines
.size()) << session_info
;
8054 std::string session_id
;
8055 for (size_t i
= 0; i
< 2; i
++) {
8056 std::vector
<std::string
> parts
= base::SplitString(
8057 lines
[i
], "\t", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
8058 ASSERT_EQ(2u, parts
.size());
8059 EXPECT_EQ("insert", parts
[0]);
8061 session_id
= parts
[1];
8063 EXPECT_NE(session_id
, parts
[1]);
8068 TEST_F(HTTPSRequestTest
, SSLSessionCacheShardTest
) {
8069 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
8071 SpawnedTestServer::SSLOptions ssl_options
;
8072 ssl_options
.record_resume
= true;
8073 SpawnedTestServer
test_server(
8074 SpawnedTestServer::TYPE_HTTPS
,
8076 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8077 ASSERT_TRUE(test_server
.Start());
8079 SSLClientSocket::ClearSessionCache();
8083 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
8084 test_server
.GetURL("ssl-session-cache"), DEFAULT_PRIORITY
, &d
));
8087 EXPECT_TRUE(r
->is_pending());
8089 base::RunLoop().Run();
8091 EXPECT_EQ(1, d
.response_started_count());
8094 // Now create a new HttpCache with a different ssl_session_cache_shard value.
8095 HttpNetworkSession::Params params
;
8096 params
.host_resolver
= default_context_
.host_resolver();
8097 params
.cert_verifier
= default_context_
.cert_verifier();
8098 params
.transport_security_state
= default_context_
.transport_security_state();
8099 params
.proxy_service
= default_context_
.proxy_service();
8100 params
.ssl_config_service
= default_context_
.ssl_config_service();
8101 params
.http_auth_handler_factory
=
8102 default_context_
.http_auth_handler_factory();
8103 params
.network_delegate
= &default_network_delegate_
;
8104 params
.http_server_properties
= default_context_
.http_server_properties();
8105 params
.ssl_session_cache_shard
= "alternate";
8107 scoped_ptr
<HttpCache
> cache(new HttpCache(
8108 new HttpNetworkSession(params
),
8109 HttpCache::DefaultBackend::InMemory(0)));
8111 default_context_
.set_http_transaction_factory(cache
.get());
8115 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
8116 test_server
.GetURL("ssl-session-cache"), DEFAULT_PRIORITY
, &d
));
8119 EXPECT_TRUE(r
->is_pending());
8121 base::RunLoop().Run();
8123 // The response will look like;
8127 // With a newline at the end which makes the split think that there are
8130 EXPECT_EQ(1, d
.response_started_count());
8131 AssertTwoDistinctSessionsInserted(d
.data_received());
8139 bool IsECDSACipherSuite(uint16_t cipher_suite
) {
8140 const char* key_exchange
;
8144 SSLCipherSuiteToStrings(&key_exchange
, &cipher
, &mac
, &is_aead
, cipher_suite
);
8145 return std::string(key_exchange
).find("ECDSA") != std::string::npos
;
8150 // Test that ECDSA is disabled on Windows XP, where ECDSA certificates cannot be
8152 TEST_F(HTTPSRequestTest
, DisableECDSAOnXP
) {
8153 if (base::win::GetVersion() >= base::win::VERSION_VISTA
) {
8154 LOG(INFO
) << "Skipping test on this version.";
8158 SpawnedTestServer
test_server(
8159 SpawnedTestServer::TYPE_HTTPS
,
8160 SpawnedTestServer::kLocalhost
,
8161 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8162 ASSERT_TRUE(test_server
.Start());
8165 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
8166 test_server
.GetURL("client-cipher-list"), DEFAULT_PRIORITY
, &d
));
8168 EXPECT_TRUE(r
->is_pending());
8170 base::RunLoop().Run();
8172 EXPECT_EQ(1, d
.response_started_count());
8173 std::vector
<std::string
> lines
= base::SplitString(
8174 d
.data_received(), "\n", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
8176 for (size_t i
= 0; i
< lines
.size(); i
++) {
8178 ASSERT_TRUE(base::StringToInt(lines
[i
], &cipher_suite
));
8179 EXPECT_FALSE(IsECDSACipherSuite(cipher_suite
))
8180 << "ClientHello advertised " << cipher_suite
;
8186 class TestSSLConfigService
: public SSLConfigService
{
8188 TestSSLConfigService(bool ev_enabled
,
8189 bool online_rev_checking
,
8190 bool rev_checking_required_local_anchors
)
8191 : ev_enabled_(ev_enabled
),
8192 online_rev_checking_(online_rev_checking
),
8193 rev_checking_required_local_anchors_(
8194 rev_checking_required_local_anchors
),
8195 min_version_(kDefaultSSLVersionMin
),
8196 fallback_min_version_(kDefaultSSLVersionFallbackMin
) {}
8198 void set_min_version(uint16 version
) {
8199 min_version_
= version
;
8202 void set_fallback_min_version(uint16 version
) {
8203 fallback_min_version_
= version
;
8206 // SSLConfigService:
8207 void GetSSLConfig(SSLConfig
* config
) override
{
8208 *config
= SSLConfig();
8209 config
->rev_checking_enabled
= online_rev_checking_
;
8210 config
->verify_ev_cert
= ev_enabled_
;
8211 config
->rev_checking_required_local_anchors
=
8212 rev_checking_required_local_anchors_
;
8213 if (fallback_min_version_
) {
8214 config
->version_fallback_min
= fallback_min_version_
;
8217 config
->version_min
= min_version_
;
8222 ~TestSSLConfigService() override
{}
8225 const bool ev_enabled_
;
8226 const bool online_rev_checking_
;
8227 const bool rev_checking_required_local_anchors_
;
8228 uint16 min_version_
;
8229 uint16 fallback_min_version_
;
8232 class FallbackTestURLRequestContext
: public TestURLRequestContext
{
8234 explicit FallbackTestURLRequestContext(bool delay_initialization
)
8235 : TestURLRequestContext(delay_initialization
) {}
8237 void set_fallback_min_version(uint16 version
) {
8238 TestSSLConfigService
*ssl_config_service
=
8239 new TestSSLConfigService(true /* check for EV */,
8240 false /* online revocation checking */,
8241 false /* require rev. checking for local
8243 ssl_config_service
->set_fallback_min_version(version
);
8244 set_ssl_config_service(ssl_config_service
);
8248 class HTTPSFallbackTest
: public testing::Test
{
8250 HTTPSFallbackTest() : context_(true) {}
8251 ~HTTPSFallbackTest() override
{}
8254 void DoFallbackTest(const SpawnedTestServer::SSLOptions
& ssl_options
) {
8257 delegate_
.set_allow_certificate_errors(true);
8259 SpawnedTestServer
test_server(
8260 SpawnedTestServer::TYPE_HTTPS
,
8262 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8263 ASSERT_TRUE(test_server
.Start());
8265 request_
= context_
.CreateRequest(test_server
.GetURL(std::string()),
8266 DEFAULT_PRIORITY
, &delegate_
);
8269 base::RunLoop().Run();
8272 void set_fallback_min_version(uint16 version
) {
8273 context_
.set_fallback_min_version(version
);
8276 void ExpectConnection(int version
) {
8277 EXPECT_EQ(1, delegate_
.response_started_count());
8278 EXPECT_NE(0, delegate_
.bytes_received());
8279 EXPECT_EQ(version
, SSLConnectionStatusToVersion(
8280 request_
->ssl_info().connection_status
));
8281 EXPECT_TRUE(request_
->ssl_info().connection_status
&
8282 SSL_CONNECTION_VERSION_FALLBACK
);
8285 void ExpectFailure(int error
) {
8286 EXPECT_EQ(1, delegate_
.response_started_count());
8287 EXPECT_FALSE(request_
->status().is_success());
8288 EXPECT_EQ(URLRequestStatus::FAILED
, request_
->status().status());
8289 EXPECT_EQ(error
, request_
->status().error());
8293 TestDelegate delegate_
;
8294 FallbackTestURLRequestContext context_
;
8295 scoped_ptr
<URLRequest
> request_
;
8298 // Tests the TLS 1.0 fallback doesn't happen.
8299 TEST_F(HTTPSFallbackTest
, TLSv1NoFallback
) {
8300 SpawnedTestServer::SSLOptions
ssl_options(
8301 SpawnedTestServer::SSLOptions::CERT_OK
);
8302 ssl_options
.tls_intolerant
=
8303 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1
;
8305 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options
));
8306 ExpectFailure(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION
);
8309 // Tests the TLS 1.1 fallback.
8310 TEST_F(HTTPSFallbackTest
, TLSv1_1Fallback
) {
8311 if (SSLClientSocket::GetMaxSupportedSSLVersion() <
8312 SSL_PROTOCOL_VERSION_TLS1_2
) {
8316 SpawnedTestServer::SSLOptions
ssl_options(
8317 SpawnedTestServer::SSLOptions::CERT_OK
);
8318 ssl_options
.tls_intolerant
=
8319 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2
;
8321 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options
));
8322 ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1
);
8325 // Tests that the TLS 1.1 fallback triggers on closed connections.
8326 TEST_F(HTTPSFallbackTest
, TLSv1_1FallbackClosed
) {
8327 if (SSLClientSocket::GetMaxSupportedSSLVersion() <
8328 SSL_PROTOCOL_VERSION_TLS1_2
) {
8332 SpawnedTestServer::SSLOptions
ssl_options(
8333 SpawnedTestServer::SSLOptions::CERT_OK
);
8334 ssl_options
.tls_intolerant
=
8335 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2
;
8336 ssl_options
.tls_intolerance_type
=
8337 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE
;
8339 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options
));
8340 ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1
);
8343 // This test is disabled on Android because the remote test server doesn't cause
8345 #if !defined(OS_ANDROID)
8346 // Tests fallback to TLS 1.1 on connection reset.
8347 TEST_F(HTTPSFallbackTest
, TLSv1_1FallbackReset
) {
8348 if (SSLClientSocket::GetMaxSupportedSSLVersion() <
8349 SSL_PROTOCOL_VERSION_TLS1_2
) {
8353 SpawnedTestServer::SSLOptions
ssl_options(
8354 SpawnedTestServer::SSLOptions::CERT_OK
);
8355 ssl_options
.tls_intolerant
=
8356 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_2
;
8357 ssl_options
.tls_intolerance_type
=
8358 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET
;
8360 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options
));
8361 ExpectConnection(SSL_CONNECTION_VERSION_TLS1_1
);
8363 #endif // !OS_ANDROID
8365 // Tests that we don't fallback on handshake failure with servers that implement
8366 // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
8367 TEST_F(HTTPSFallbackTest
, FallbackSCSV
) {
8368 SpawnedTestServer::SSLOptions
ssl_options(
8369 SpawnedTestServer::SSLOptions::CERT_OK
);
8370 // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger
8371 // a version fallback.
8372 ssl_options
.tls_intolerant
=
8373 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1
;
8374 // Have the server process TLS_FALLBACK_SCSV so that version fallback
8375 // connections are rejected.
8376 ssl_options
.fallback_scsv_enabled
= true;
8378 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options
));
8380 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
8381 // intolerance. If the fallback SCSV is processed when the original error
8382 // that caused the fallback should be returned, which should be
8383 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
8384 ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH
);
8387 // Tests that we don't fallback on connection closed with servers that implement
8388 // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
8389 TEST_F(HTTPSFallbackTest
, FallbackSCSVClosed
) {
8390 SpawnedTestServer::SSLOptions
ssl_options(
8391 SpawnedTestServer::SSLOptions::CERT_OK
);
8392 // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger
8393 // a version fallback.
8394 ssl_options
.tls_intolerant
=
8395 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1
;
8396 ssl_options
.tls_intolerance_type
=
8397 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE
;
8398 // Have the server process TLS_FALLBACK_SCSV so that version fallback
8399 // connections are rejected.
8400 ssl_options
.fallback_scsv_enabled
= true;
8402 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options
));
8404 // The original error should be replayed on rejected fallback.
8405 ExpectFailure(ERR_CONNECTION_CLOSED
);
8408 // Test that fallback probe connections don't cause sessions to be cached.
8409 TEST_F(HTTPSRequestTest
, FallbackProbeNoCache
) {
8410 SpawnedTestServer::SSLOptions
ssl_options(
8411 SpawnedTestServer::SSLOptions::CERT_OK
);
8412 ssl_options
.tls_intolerant
=
8413 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1
;
8414 ssl_options
.tls_intolerance_type
=
8415 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE
;
8416 ssl_options
.record_resume
= true;
8418 SpawnedTestServer
test_server(
8419 SpawnedTestServer::TYPE_HTTPS
,
8421 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8422 ASSERT_TRUE(test_server
.Start());
8424 SSLClientSocket::ClearSessionCache();
8426 // Make a connection that does a probe fallback to TLSv1 but fails because
8427 // TLSv1 fallback is disabled. We don't wish a session for this connection to
8428 // be inserted locally.
8430 TestDelegate delegate
;
8431 FallbackTestURLRequestContext
context(true);
8433 context
.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_2
);
8435 scoped_ptr
<URLRequest
> request(context
.CreateRequest(
8436 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &delegate
));
8439 base::RunLoop().Run();
8441 EXPECT_EQ(1, delegate
.response_started_count());
8442 EXPECT_FALSE(request
->status().is_success());
8443 EXPECT_EQ(URLRequestStatus::FAILED
, request
->status().status());
8444 EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION
,
8445 request
->status().error());
8448 // Now allow TLSv1 fallback connections and request the session cache log.
8450 TestDelegate delegate
;
8451 FallbackTestURLRequestContext
context(true);
8452 context
.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1
);
8455 scoped_ptr
<URLRequest
> request(context
.CreateRequest(
8456 test_server
.GetURL("ssl-session-cache"), DEFAULT_PRIORITY
, &delegate
));
8459 base::RunLoop().Run();
8461 EXPECT_EQ(1, delegate
.response_started_count());
8462 EXPECT_NE(0, delegate
.bytes_received());
8464 SSL_CONNECTION_VERSION_TLS1
,
8465 SSLConnectionStatusToVersion(request
->ssl_info().connection_status
));
8466 EXPECT_TRUE(request
->ssl_info().connection_status
&
8467 SSL_CONNECTION_VERSION_FALLBACK
);
8469 std::vector
<std::string
> lines
;
8470 // If no sessions were cached then the server should have seen two sessions
8471 // inserted with no lookups.
8472 AssertTwoDistinctSessionsInserted(delegate
.data_received());
8476 class HTTPSSessionTest
: public testing::Test
{
8478 HTTPSSessionTest() : default_context_(true) {
8479 cert_verifier_
.set_default_result(OK
);
8481 default_context_
.set_network_delegate(&default_network_delegate_
);
8482 default_context_
.set_cert_verifier(&cert_verifier_
);
8483 default_context_
.Init();
8485 ~HTTPSSessionTest() override
{}
8488 MockCertVerifier cert_verifier_
;
8489 TestNetworkDelegate default_network_delegate_
; // Must outlive URLRequest.
8490 TestURLRequestContext default_context_
;
8493 // Tests that session resumption is not attempted if an invalid certificate
8495 TEST_F(HTTPSSessionTest
, DontResumeSessionsForInvalidCertificates
) {
8496 SpawnedTestServer::SSLOptions ssl_options
;
8497 ssl_options
.record_resume
= true;
8498 SpawnedTestServer
test_server(
8499 SpawnedTestServer::TYPE_HTTPS
,
8501 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8502 ASSERT_TRUE(test_server
.Start());
8504 SSLClientSocket::ClearSessionCache();
8506 // Simulate the certificate being expired and attempt a connection.
8507 cert_verifier_
.set_default_result(ERR_CERT_DATE_INVALID
);
8510 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
8511 test_server
.GetURL("ssl-session-cache"), DEFAULT_PRIORITY
, &d
));
8514 EXPECT_TRUE(r
->is_pending());
8516 base::RunLoop().Run();
8518 EXPECT_EQ(1, d
.response_started_count());
8521 reinterpret_cast<HttpCache
*>(default_context_
.http_transaction_factory())->
8522 CloseAllConnections();
8524 // Now change the certificate to be acceptable (so that the response is
8525 // loaded), and ensure that no session id is presented to the peer.
8526 cert_verifier_
.set_default_result(OK
);
8529 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
8530 test_server
.GetURL("ssl-session-cache"), DEFAULT_PRIORITY
, &d
));
8533 EXPECT_TRUE(r
->is_pending());
8535 base::RunLoop().Run();
8537 // The response will look like;
8541 // With a newline at the end which makes the split think that there are
8544 // If a session was presented (eg: a bug), then the response would look
8550 EXPECT_EQ(1, d
.response_started_count());
8551 AssertTwoDistinctSessionsInserted(d
.data_received());
8555 // This the fingerprint of the "Testing CA" certificate used by the testserver.
8556 // See net/data/ssl/certificates/ocsp-test-root.pem.
8557 static const SHA1HashValue kOCSPTestCertFingerprint
=
8558 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
8559 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
8561 // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
8563 static const SHA256HashValue kOCSPTestCertSPKI
= { {
8564 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
8565 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
8566 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
8567 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
8570 // This is the policy OID contained in the certificates that testserver
8572 static const char kOCSPTestCertPolicy
[] = "1.3.6.1.4.1.11129.2.4.1";
8574 class HTTPSOCSPTest
: public HTTPSRequestTest
{
8579 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
8580 kOCSPTestCertFingerprint
,
8581 kOCSPTestCertPolicy
)) {
8584 void SetUp() override
{
8585 SetupContext(&context_
);
8588 scoped_refptr
<X509Certificate
> root_cert
=
8589 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
8590 CHECK_NE(static_cast<X509Certificate
*>(NULL
), root_cert
.get());
8591 test_root_
.reset(new ScopedTestRoot(root_cert
.get()));
8593 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
8594 SetURLRequestContextForNSSHttpIO(&context_
);
8595 EnsureNSSHttpIOInit();
8599 void DoConnection(const SpawnedTestServer::SSLOptions
& ssl_options
,
8600 CertStatus
* out_cert_status
) {
8601 // We always overwrite out_cert_status.
8602 *out_cert_status
= 0;
8603 SpawnedTestServer
test_server(
8604 SpawnedTestServer::TYPE_HTTPS
,
8606 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8607 ASSERT_TRUE(test_server
.Start());
8610 d
.set_allow_certificate_errors(true);
8611 scoped_ptr
<URLRequest
> r(context_
.CreateRequest(
8612 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
));
8615 base::RunLoop().Run();
8617 EXPECT_EQ(1, d
.response_started_count());
8618 *out_cert_status
= r
->ssl_info().cert_status
;
8621 ~HTTPSOCSPTest() override
{
8622 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
8623 ShutdownNSSHttpIO();
8628 // SetupContext configures the URLRequestContext that will be used for making
8629 // connetions to testserver. This can be overridden in test subclasses for
8630 // different behaviour.
8631 virtual void SetupContext(URLRequestContext
* context
) {
8632 context
->set_ssl_config_service(
8633 new TestSSLConfigService(true /* check for EV */,
8634 true /* online revocation checking */,
8635 false /* require rev. checking for local
8639 scoped_ptr
<ScopedTestRoot
> test_root_
;
8640 TestURLRequestContext context_
;
8641 scoped_ptr
<ScopedTestEVPolicy
> ev_test_policy_
;
8644 static CertStatus
ExpectedCertStatusForFailedOnlineRevocationCheck() {
8646 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
8647 // have that ability on other platforms.
8648 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION
;
8654 // SystemSupportsHardFailRevocationChecking returns true iff the current
8655 // operating system supports revocation checking and can distinguish between
8656 // situations where a given certificate lacks any revocation information (eg:
8657 // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
8658 // revocation information cannot be obtained (eg: the CRL was unreachable).
8659 // If it does not, then tests which rely on 'hard fail' behaviour should be
8661 static bool SystemSupportsHardFailRevocationChecking() {
8662 #if defined(OS_WIN) || defined(USE_NSS_CERTS) || defined(OS_IOS)
8669 // SystemUsesChromiumEVMetadata returns true iff the current operating system
8670 // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
8671 // several tests are effected because our testing EV certificate won't be
8672 // recognised as EV.
8673 static bool SystemUsesChromiumEVMetadata() {
8674 #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID)
8675 // http://crbug.com/117478 - OpenSSL does not support EV validation.
8677 #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
8678 // On OS X and Android, we use the system to tell us whether a certificate is
8679 // EV or not and the system won't recognise our testing root.
8686 static bool SystemSupportsOCSP() {
8687 #if defined(USE_OPENSSL_CERTS)
8688 // http://crbug.com/117478 - OpenSSL does not support OCSP.
8690 #elif defined(OS_WIN)
8691 return base::win::GetVersion() >= base::win::VERSION_VISTA
;
8692 #elif defined(OS_ANDROID)
8693 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
8700 static bool SystemSupportsOCSPStapling() {
8701 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
8703 #elif defined(OS_WIN)
8704 return base::win::GetVersion() >= base::win::VERSION_VISTA
;
8710 TEST_F(HTTPSOCSPTest
, Valid
) {
8711 if (!SystemSupportsOCSP()) {
8712 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8716 SpawnedTestServer::SSLOptions
ssl_options(
8717 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8718 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_OK
;
8720 CertStatus cert_status
;
8721 DoConnection(ssl_options
, &cert_status
);
8723 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
8725 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8726 static_cast<bool>(cert_status
& CERT_STATUS_IS_EV
));
8728 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
8731 TEST_F(HTTPSOCSPTest
, Revoked
) {
8732 if (!SystemSupportsOCSP()) {
8733 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8737 SpawnedTestServer::SSLOptions
ssl_options(
8738 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8739 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_REVOKED
;
8741 CertStatus cert_status
;
8742 DoConnection(ssl_options
, &cert_status
);
8744 #if !(defined(OS_MACOSX) && !defined(OS_IOS))
8745 // Doesn't pass on OS X yet for reasons that need to be investigated.
8746 EXPECT_EQ(CERT_STATUS_REVOKED
, cert_status
& CERT_STATUS_ALL_ERRORS
);
8748 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
8749 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
8752 TEST_F(HTTPSOCSPTest
, Invalid
) {
8753 if (!SystemSupportsOCSP()) {
8754 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8758 SpawnedTestServer::SSLOptions
ssl_options(
8759 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8760 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
8762 CertStatus cert_status
;
8763 DoConnection(ssl_options
, &cert_status
);
8765 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8766 cert_status
& CERT_STATUS_ALL_ERRORS
);
8768 // Without a positive OCSP response, we shouldn't show the EV status.
8769 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
8770 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
8773 TEST_F(HTTPSOCSPTest
, ValidStapled
) {
8774 if (!SystemSupportsOCSPStapling()) {
8776 << "Skipping test because system doesn't support OCSP stapling";
8780 SpawnedTestServer::SSLOptions
ssl_options(
8781 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8782 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_OK
;
8783 ssl_options
.staple_ocsp_response
= true;
8784 ssl_options
.ocsp_server_unavailable
= true;
8786 CertStatus cert_status
;
8787 DoConnection(ssl_options
, &cert_status
);
8789 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
8791 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8792 static_cast<bool>(cert_status
& CERT_STATUS_IS_EV
));
8794 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
8797 // Disabled on NSS ports. See https://crbug.com/431716.
8798 #if defined(USE_NSS_CERTS)
8799 #define MAYBE_RevokedStapled DISABLED_RevokedStapled
8801 #define MAYBE_RevokedStapled RevokedStapled
8803 TEST_F(HTTPSOCSPTest
, MAYBE_RevokedStapled
) {
8804 if (!SystemSupportsOCSPStapling()) {
8806 << "Skipping test because system doesn't support OCSP stapling";
8810 SpawnedTestServer::SSLOptions
ssl_options(
8811 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8812 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_REVOKED
;
8813 ssl_options
.staple_ocsp_response
= true;
8814 ssl_options
.ocsp_server_unavailable
= true;
8816 CertStatus cert_status
;
8817 DoConnection(ssl_options
, &cert_status
);
8819 EXPECT_EQ(CERT_STATUS_REVOKED
, cert_status
& CERT_STATUS_ALL_ERRORS
);
8820 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
8821 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
8824 class HTTPSHardFailTest
: public HTTPSOCSPTest
{
8826 void SetupContext(URLRequestContext
* context
) override
{
8827 context
->set_ssl_config_service(
8828 new TestSSLConfigService(false /* check for EV */,
8829 false /* online revocation checking */,
8830 true /* require rev. checking for local
8835 TEST_F(HTTPSHardFailTest
, FailsOnOCSPInvalid
) {
8836 if (!SystemSupportsOCSP()) {
8837 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8841 if (!SystemSupportsHardFailRevocationChecking()) {
8842 LOG(WARNING
) << "Skipping test because system doesn't support hard fail "
8843 << "revocation checking";
8847 SpawnedTestServer::SSLOptions
ssl_options(
8848 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8849 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
8851 CertStatus cert_status
;
8852 DoConnection(ssl_options
, &cert_status
);
8854 EXPECT_EQ(CERT_STATUS_REVOKED
,
8855 cert_status
& CERT_STATUS_REVOKED
);
8857 // Without a positive OCSP response, we shouldn't show the EV status.
8858 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
8861 class HTTPSEVCRLSetTest
: public HTTPSOCSPTest
{
8863 void SetupContext(URLRequestContext
* context
) override
{
8864 context
->set_ssl_config_service(
8865 new TestSSLConfigService(true /* check for EV */,
8866 false /* online revocation checking */,
8867 false /* require rev. checking for local
8872 TEST_F(HTTPSEVCRLSetTest
, MissingCRLSetAndInvalidOCSP
) {
8873 if (!SystemSupportsOCSP()) {
8874 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8878 SpawnedTestServer::SSLOptions
ssl_options(
8879 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8880 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
8881 SSLConfigService::SetCRLSet(scoped_refptr
<CRLSet
>());
8883 CertStatus cert_status
;
8884 DoConnection(ssl_options
, &cert_status
);
8886 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8887 cert_status
& CERT_STATUS_ALL_ERRORS
);
8889 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
8890 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8891 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
8894 TEST_F(HTTPSEVCRLSetTest
, MissingCRLSetAndRevokedOCSP
) {
8895 if (!SystemSupportsOCSP()) {
8896 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8900 SpawnedTestServer::SSLOptions
ssl_options(
8901 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8902 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_REVOKED
;
8903 SSLConfigService::SetCRLSet(scoped_refptr
<CRLSet
>());
8905 CertStatus cert_status
;
8906 DoConnection(ssl_options
, &cert_status
);
8908 // Currently only works for Windows. When using NSS or OS X, it's not
8909 // possible to determine whether the check failed because of actual
8910 // revocation or because there was an OCSP failure.
8912 EXPECT_EQ(CERT_STATUS_REVOKED
, cert_status
& CERT_STATUS_ALL_ERRORS
);
8914 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
8917 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
8918 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8919 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
8922 TEST_F(HTTPSEVCRLSetTest
, MissingCRLSetAndGoodOCSP
) {
8923 if (!SystemSupportsOCSP()) {
8924 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8928 SpawnedTestServer::SSLOptions
ssl_options(
8929 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8930 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_OK
;
8931 SSLConfigService::SetCRLSet(scoped_refptr
<CRLSet
>());
8933 CertStatus cert_status
;
8934 DoConnection(ssl_options
, &cert_status
);
8936 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
8938 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8939 static_cast<bool>(cert_status
& CERT_STATUS_IS_EV
));
8940 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8941 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
8944 TEST_F(HTTPSEVCRLSetTest
, ExpiredCRLSet
) {
8945 if (!SystemSupportsOCSP()) {
8946 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8950 SpawnedTestServer::SSLOptions
ssl_options(
8951 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8952 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
8953 SSLConfigService::SetCRLSet(
8954 scoped_refptr
<CRLSet
>(CRLSet::ExpiredCRLSetForTesting()));
8956 CertStatus cert_status
;
8957 DoConnection(ssl_options
, &cert_status
);
8959 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8960 cert_status
& CERT_STATUS_ALL_ERRORS
);
8962 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
8963 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8964 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
8967 TEST_F(HTTPSEVCRLSetTest
, FreshCRLSetCovered
) {
8968 if (!SystemSupportsOCSP()) {
8969 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8973 SpawnedTestServer::SSLOptions
ssl_options(
8974 SpawnedTestServer::SSLOptions::CERT_AUTO
);
8975 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
8976 SSLConfigService::SetCRLSet(
8977 scoped_refptr
<CRLSet
>(CRLSet::ForTesting(
8978 false, &kOCSPTestCertSPKI
, "")));
8980 CertStatus cert_status
;
8981 DoConnection(ssl_options
, &cert_status
);
8983 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
8984 // revocation check for EV.
8985 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
8986 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8987 static_cast<bool>(cert_status
& CERT_STATUS_IS_EV
));
8989 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
8992 TEST_F(HTTPSEVCRLSetTest
, FreshCRLSetNotCovered
) {
8993 if (!SystemSupportsOCSP()) {
8994 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
8998 SpawnedTestServer::SSLOptions
ssl_options(
8999 SpawnedTestServer::SSLOptions::CERT_AUTO
);
9000 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
9001 SSLConfigService::SetCRLSet(
9002 scoped_refptr
<CRLSet
>(CRLSet::EmptyCRLSetForTesting()));
9004 CertStatus cert_status
= 0;
9005 DoConnection(ssl_options
, &cert_status
);
9007 // Even with a fresh CRLSet, we should still do online revocation checks when
9008 // the certificate chain isn't covered by the CRLSet, which it isn't in this
9010 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
9011 cert_status
& CERT_STATUS_ALL_ERRORS
);
9013 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
9014 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
9015 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
9018 TEST_F(HTTPSEVCRLSetTest
, ExpiredCRLSetAndRevokedNonEVCert
) {
9019 // Test that when EV verification is requested, but online revocation
9020 // checking is disabled, and the leaf certificate is not in fact EV, that
9021 // no revocation checking actually happens.
9022 if (!SystemSupportsOCSP()) {
9023 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
9027 // Unmark the certificate's OID as EV, which should disable revocation
9028 // checking (as per the user preference)
9029 ev_test_policy_
.reset();
9031 SpawnedTestServer::SSLOptions
ssl_options(
9032 SpawnedTestServer::SSLOptions::CERT_AUTO
);
9033 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_REVOKED
;
9034 SSLConfigService::SetCRLSet(
9035 scoped_refptr
<CRLSet
>(CRLSet::ExpiredCRLSetForTesting()));
9037 CertStatus cert_status
;
9038 DoConnection(ssl_options
, &cert_status
);
9040 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
9042 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
9043 EXPECT_FALSE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
9046 class HTTPSCRLSetTest
: public HTTPSOCSPTest
{
9048 void SetupContext(URLRequestContext
* context
) override
{
9049 context
->set_ssl_config_service(
9050 new TestSSLConfigService(false /* check for EV */,
9051 false /* online revocation checking */,
9052 false /* require rev. checking for local
9057 TEST_F(HTTPSCRLSetTest
, ExpiredCRLSet
) {
9058 SpawnedTestServer::SSLOptions
ssl_options(
9059 SpawnedTestServer::SSLOptions::CERT_AUTO
);
9060 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
9061 SSLConfigService::SetCRLSet(
9062 scoped_refptr
<CRLSet
>(CRLSet::ExpiredCRLSetForTesting()));
9064 CertStatus cert_status
;
9065 DoConnection(ssl_options
, &cert_status
);
9067 // If we're not trying EV verification then, even if the CRLSet has expired,
9068 // we don't fall back to online revocation checks.
9069 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
9070 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
9071 EXPECT_FALSE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
9074 TEST_F(HTTPSCRLSetTest
, CRLSetRevoked
) {
9075 #if defined(OS_ANDROID)
9076 LOG(WARNING
) << "Skipping test because system doesn't support CRLSets";
9080 SpawnedTestServer::SSLOptions
ssl_options(
9081 SpawnedTestServer::SSLOptions::CERT_AUTO
);
9082 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_OK
;
9083 ssl_options
.cert_serial
= 10;
9084 SSLConfigService::SetCRLSet(
9085 scoped_refptr
<CRLSet
>(CRLSet::ForTesting(
9086 false, &kOCSPTestCertSPKI
, "\x0a")));
9088 CertStatus cert_status
= 0;
9089 DoConnection(ssl_options
, &cert_status
);
9091 // If the certificate is recorded as revoked in the CRLSet, that should be
9092 // reflected without online revocation checking.
9093 EXPECT_EQ(CERT_STATUS_REVOKED
, cert_status
& CERT_STATUS_ALL_ERRORS
);
9094 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
9096 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
9098 #endif // !defined(OS_IOS)
9100 #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID)
9101 // These tests aren't passing on Android. Either the RemoteTestServer isn't
9102 // starting up successfully, or it can't access the test files.
9103 // TODO(mmenke): Fix this. See http://crbug.com/495220
9104 class URLRequestTestFTP
: public URLRequestTest
{
9107 : ftp_transaction_factory_(&host_resolver_
),
9108 test_server_(SpawnedTestServer::TYPE_FTP
,
9109 SpawnedTestServer::kLocalhost
,
9110 base::FilePath(kTestFilePath
)) {
9111 // Can't use |default_context_|'s HostResolver to set up the
9112 // FTPTransactionFactory because it hasn't been created yet.
9113 default_context_
.set_host_resolver(&host_resolver_
);
9116 // URLRequestTest interface:
9117 void SetUpFactory() override
{
9118 // Add FTP support to the default URLRequestContext.
9119 job_factory_impl_
->SetProtocolHandler(
9121 make_scoped_ptr(new FtpProtocolHandler(&ftp_transaction_factory_
)));
9124 std::string
GetTestFileContents() {
9125 base::FilePath path
;
9126 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT
, &path
));
9127 path
= path
.Append(kTestFilePath
);
9128 path
= path
.AppendASCII(kFtpTestFile
);
9129 std::string contents
;
9130 EXPECT_TRUE(base::ReadFileToString(path
, &contents
));
9135 MockHostResolver host_resolver_
;
9136 FtpNetworkLayer ftp_transaction_factory_
;
9138 SpawnedTestServer test_server_
;
9141 // Make sure an FTP request using an unsafe ports fails.
9142 TEST_F(URLRequestTestFTP
, UnsafePort
) {
9143 GURL
url("ftp://127.0.0.1:7");
9147 scoped_ptr
<URLRequest
> r(
9148 default_context_
.CreateRequest(url
, DEFAULT_PRIORITY
, &d
));
9150 EXPECT_TRUE(r
->is_pending());
9152 base::RunLoop().Run();
9154 EXPECT_FALSE(r
->is_pending());
9155 EXPECT_EQ(URLRequestStatus::FAILED
, r
->status().status());
9156 EXPECT_EQ(ERR_UNSAFE_PORT
, r
->status().error());
9160 TEST_F(URLRequestTestFTP
, FTPDirectoryListing
) {
9161 ASSERT_TRUE(test_server_
.Start());
9165 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9166 test_server_
.GetURL("/"), DEFAULT_PRIORITY
, &d
));
9168 EXPECT_TRUE(r
->is_pending());
9170 base::RunLoop().Run();
9172 EXPECT_FALSE(r
->is_pending());
9173 EXPECT_EQ(1, d
.response_started_count());
9174 EXPECT_FALSE(d
.received_data_before_response());
9175 EXPECT_LT(0, d
.bytes_received());
9176 EXPECT_EQ(test_server_
.host_port_pair().host(),
9177 r
->GetSocketAddress().host());
9178 EXPECT_EQ(test_server_
.host_port_pair().port(),
9179 r
->GetSocketAddress().port());
9183 TEST_F(URLRequestTestFTP
, FTPGetTestAnonymous
) {
9184 ASSERT_TRUE(test_server_
.Start());
9188 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9189 test_server_
.GetURL(kFtpTestFile
), DEFAULT_PRIORITY
, &d
));
9191 EXPECT_TRUE(r
->is_pending());
9193 base::RunLoop().Run();
9195 EXPECT_FALSE(r
->is_pending());
9196 EXPECT_EQ(1, d
.response_started_count());
9197 EXPECT_FALSE(d
.received_data_before_response());
9198 EXPECT_EQ(GetTestFileContents(), d
.data_received());
9199 EXPECT_EQ(test_server_
.host_port_pair().host(),
9200 r
->GetSocketAddress().host());
9201 EXPECT_EQ(test_server_
.host_port_pair().port(),
9202 r
->GetSocketAddress().port());
9206 TEST_F(URLRequestTestFTP
, FTPGetTest
) {
9207 ASSERT_TRUE(test_server_
.Start());
9211 scoped_ptr
<URLRequest
> r(
9212 default_context_
.CreateRequest(test_server_
.GetURLWithUserAndPassword(
9213 kFtpTestFile
, "chrome", "chrome"),
9214 DEFAULT_PRIORITY
, &d
));
9216 EXPECT_TRUE(r
->is_pending());
9218 base::RunLoop().Run();
9220 EXPECT_FALSE(r
->is_pending());
9221 EXPECT_EQ(1, d
.response_started_count());
9222 EXPECT_FALSE(d
.received_data_before_response());
9223 EXPECT_EQ(GetTestFileContents(), d
.data_received());
9224 EXPECT_EQ(test_server_
.host_port_pair().host(),
9225 r
->GetSocketAddress().host());
9226 EXPECT_EQ(test_server_
.host_port_pair().port(),
9227 r
->GetSocketAddress().port());
9229 LoadTimingInfo load_timing_info
;
9230 r
->GetLoadTimingInfo(&load_timing_info
);
9231 TestLoadTimingNoHttpResponse(load_timing_info
);
9235 TEST_F(URLRequestTestFTP
, FTPCheckWrongPassword
) {
9236 ASSERT_TRUE(test_server_
.Start());
9240 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9241 test_server_
.GetURLWithUserAndPassword(kFtpTestFile
, "chrome",
9243 DEFAULT_PRIORITY
, &d
));
9245 EXPECT_TRUE(r
->is_pending());
9247 base::RunLoop().Run();
9249 EXPECT_FALSE(r
->is_pending());
9250 EXPECT_EQ(1, d
.response_started_count());
9251 EXPECT_FALSE(d
.received_data_before_response());
9252 EXPECT_EQ(d
.bytes_received(), 0);
9256 TEST_F(URLRequestTestFTP
, FTPCheckWrongPasswordRestart
) {
9257 ASSERT_TRUE(test_server_
.Start());
9260 // Set correct login credentials. The delegate will be asked for them when
9261 // the initial login with wrong credentials will fail.
9262 d
.set_credentials(AuthCredentials(kChrome
, kChrome
));
9264 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9265 test_server_
.GetURLWithUserAndPassword(kFtpTestFile
, "chrome",
9267 DEFAULT_PRIORITY
, &d
));
9269 EXPECT_TRUE(r
->is_pending());
9271 base::RunLoop().Run();
9273 EXPECT_FALSE(r
->is_pending());
9274 EXPECT_EQ(1, d
.response_started_count());
9275 EXPECT_FALSE(d
.received_data_before_response());
9276 EXPECT_EQ(GetTestFileContents(), d
.data_received());
9280 TEST_F(URLRequestTestFTP
, FTPCheckWrongUser
) {
9281 ASSERT_TRUE(test_server_
.Start());
9285 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9286 test_server_
.GetURLWithUserAndPassword(kFtpTestFile
, "wrong_user",
9288 DEFAULT_PRIORITY
, &d
));
9290 EXPECT_TRUE(r
->is_pending());
9292 base::RunLoop().Run();
9294 EXPECT_FALSE(r
->is_pending());
9295 EXPECT_EQ(1, d
.response_started_count());
9296 EXPECT_FALSE(d
.received_data_before_response());
9297 EXPECT_EQ(0, d
.bytes_received());
9301 TEST_F(URLRequestTestFTP
, FTPCheckWrongUserRestart
) {
9302 ASSERT_TRUE(test_server_
.Start());
9305 // Set correct login credentials. The delegate will be asked for them when
9306 // the initial login with wrong credentials will fail.
9307 d
.set_credentials(AuthCredentials(kChrome
, kChrome
));
9309 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9310 test_server_
.GetURLWithUserAndPassword(kFtpTestFile
, "wrong_user",
9312 DEFAULT_PRIORITY
, &d
));
9314 EXPECT_TRUE(r
->is_pending());
9316 base::RunLoop().Run();
9318 EXPECT_FALSE(r
->is_pending());
9319 EXPECT_EQ(1, d
.response_started_count());
9320 EXPECT_FALSE(d
.received_data_before_response());
9321 EXPECT_EQ(GetTestFileContents(), d
.data_received());
9325 TEST_F(URLRequestTestFTP
, FTPCacheURLCredentials
) {
9326 ASSERT_TRUE(test_server_
.Start());
9328 scoped_ptr
<TestDelegate
> d(new TestDelegate
);
9330 // Pass correct login identity in the URL.
9331 scoped_ptr
<URLRequest
> r(
9332 default_context_
.CreateRequest(test_server_
.GetURLWithUserAndPassword(
9333 kFtpTestFile
, "chrome", "chrome"),
9334 DEFAULT_PRIORITY
, d
.get()));
9336 EXPECT_TRUE(r
->is_pending());
9338 base::RunLoop().Run();
9340 EXPECT_FALSE(r
->is_pending());
9341 EXPECT_EQ(1, d
->response_started_count());
9342 EXPECT_FALSE(d
->received_data_before_response());
9343 EXPECT_EQ(GetTestFileContents(), d
->data_received());
9346 d
.reset(new TestDelegate
);
9348 // This request should use cached identity from previous request.
9349 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9350 test_server_
.GetURL(kFtpTestFile
), DEFAULT_PRIORITY
, d
.get()));
9352 EXPECT_TRUE(r
->is_pending());
9354 base::RunLoop().Run();
9356 EXPECT_FALSE(r
->is_pending());
9357 EXPECT_EQ(1, d
->response_started_count());
9358 EXPECT_FALSE(d
->received_data_before_response());
9359 EXPECT_EQ(GetTestFileContents(), d
->data_received());
9363 TEST_F(URLRequestTestFTP
, FTPCacheLoginBoxCredentials
) {
9364 ASSERT_TRUE(test_server_
.Start());
9366 scoped_ptr
<TestDelegate
> d(new TestDelegate
);
9367 // Set correct login credentials. The delegate will be asked for them when
9368 // the initial login with wrong credentials will fail.
9369 d
->set_credentials(AuthCredentials(kChrome
, kChrome
));
9371 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9372 test_server_
.GetURLWithUserAndPassword(kFtpTestFile
, "chrome",
9374 DEFAULT_PRIORITY
, d
.get()));
9376 EXPECT_TRUE(r
->is_pending());
9378 base::RunLoop().Run();
9380 EXPECT_FALSE(r
->is_pending());
9381 EXPECT_EQ(1, d
->response_started_count());
9382 EXPECT_FALSE(d
->received_data_before_response());
9383 EXPECT_EQ(GetTestFileContents(), d
->data_received());
9386 // Use a new delegate without explicit credentials. The cached ones should be
9388 d
.reset(new TestDelegate
);
9390 // Don't pass wrong credentials in the URL, they would override valid cached
9392 scoped_ptr
<URLRequest
> r(default_context_
.CreateRequest(
9393 test_server_
.GetURL(kFtpTestFile
), DEFAULT_PRIORITY
, d
.get()));
9395 EXPECT_TRUE(r
->is_pending());
9397 base::RunLoop().Run();
9399 EXPECT_FALSE(r
->is_pending());
9400 EXPECT_EQ(1, d
->response_started_count());
9401 EXPECT_FALSE(d
->received_data_before_response());
9402 EXPECT_EQ(GetTestFileContents(), d
->data_received());
9405 #endif // !defined(DISABLE_FTP_SUPPORT)
9407 TEST_F(URLRequestTest
, NetworkAccessedClearBeforeNetworkStart
) {
9409 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
9410 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
9411 d
.set_quit_on_network_start(true);
9413 EXPECT_FALSE(req
->response_info().network_accessed
);
9416 base::RunLoop().Run();
9418 EXPECT_EQ(1, d
.received_before_network_start_count());
9419 EXPECT_EQ(0, d
.response_started_count());
9420 EXPECT_FALSE(req
->response_info().network_accessed
);
9422 req
->ResumeNetworkStart();
9423 base::RunLoop().Run();
9426 TEST_F(URLRequestTest
, NetworkAccessedClearOnDataRequest
) {
9428 scoped_ptr
<URLRequest
> req(
9429 default_context_
.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY
, &d
));
9431 EXPECT_FALSE(req
->response_info().network_accessed
);
9434 base::RunLoop().Run();
9436 EXPECT_EQ(1, default_network_delegate_
.completed_requests());
9437 EXPECT_FALSE(req
->response_info().network_accessed
);
9440 TEST_F(URLRequestTest
, NetworkAccessedSetOnHostResolutionFailure
) {
9441 MockHostResolver host_resolver
;
9442 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
9443 TestURLRequestContext
context(true);
9444 context
.set_network_delegate(&network_delegate
);
9445 context
.set_host_resolver(&host_resolver
);
9446 host_resolver
.rules()->AddSimulatedFailure("*");
9450 scoped_ptr
<URLRequest
> req(context
.CreateRequest(
9451 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
));
9453 EXPECT_FALSE(req
->response_info().network_accessed
);
9456 base::RunLoop().Run();
9457 EXPECT_TRUE(req
->response_info().network_accessed
);
9460 // Test that URLRequest is canceled correctly and with detached request
9461 // URLRequestRedirectJob does not crash in StartAsync.
9462 // See http://crbug.com/508900
9463 TEST_F(URLRequestTest
, URLRequestRedirectJobDetachRequestNoCrash
) {
9465 scoped_ptr
<URLRequest
> req(default_context_
.CreateRequest(
9466 GURL("http://not-a-real-domain/"), DEFAULT_PRIORITY
, &d
));
9468 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
9469 req
.get(), &default_network_delegate_
,
9470 GURL("http://this-should-never-be-navigated-to/"),
9471 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT
, "Jumbo shrimp");
9472 AddTestInterceptor()->set_main_intercept_job(job
);
9476 job
->DetachRequest();
9477 base::RunLoop().RunUntilIdle();
9478 EXPECT_EQ(URLRequestStatus::CANCELED
, req
->status().status());
9479 EXPECT_EQ(0, d
.received_redirect_count());