1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "build/build_config.h"
15 #include "base/basictypes.h"
16 #include "base/bind.h"
17 #include "base/compiler_specific.h"
18 #include "base/file_util.h"
19 #include "base/files/scoped_temp_dir.h"
20 #include "base/format_macros.h"
21 #include "base/memory/weak_ptr.h"
22 #include "base/message_loop/message_loop.h"
23 #include "base/message_loop/message_loop_proxy.h"
24 #include "base/path_service.h"
25 #include "base/run_loop.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_piece.h"
28 #include "base/strings/string_split.h"
29 #include "base/strings/string_util.h"
30 #include "base/strings/stringprintf.h"
31 #include "base/strings/utf_string_conversions.h"
32 #include "net/base/capturing_net_log.h"
33 #include "net/base/load_flags.h"
34 #include "net/base/load_timing_info.h"
35 #include "net/base/load_timing_info_test_util.h"
36 #include "net/base/net_errors.h"
37 #include "net/base/net_log.h"
38 #include "net/base/net_log_unittest.h"
39 #include "net/base/net_module.h"
40 #include "net/base/net_util.h"
41 #include "net/base/request_priority.h"
42 #include "net/base/test_data_directory.h"
43 #include "net/base/upload_bytes_element_reader.h"
44 #include "net/base/upload_data_stream.h"
45 #include "net/base/upload_file_element_reader.h"
46 #include "net/cert/ev_root_ca_metadata.h"
47 #include "net/cert/mock_cert_verifier.h"
48 #include "net/cert/test_root_certs.h"
49 #include "net/cookies/cookie_monster.h"
50 #include "net/cookies/cookie_store_test_helpers.h"
51 #include "net/disk_cache/disk_cache.h"
52 #include "net/dns/mock_host_resolver.h"
53 #include "net/ftp/ftp_network_layer.h"
54 #include "net/http/http_byte_range.h"
55 #include "net/http/http_cache.h"
56 #include "net/http/http_network_layer.h"
57 #include "net/http/http_network_session.h"
58 #include "net/http/http_request_headers.h"
59 #include "net/http/http_response_headers.h"
60 #include "net/http/http_util.h"
61 #include "net/ocsp/nss_ocsp.h"
62 #include "net/proxy/proxy_service.h"
63 #include "net/socket/ssl_client_socket.h"
64 #include "net/ssl/ssl_connection_status_flags.h"
65 #include "net/test/cert_test_util.h"
66 #include "net/test/spawned_test_server/spawned_test_server.h"
67 #include "net/url_request/data_protocol_handler.h"
68 #include "net/url_request/file_protocol_handler.h"
69 #include "net/url_request/ftp_protocol_handler.h"
70 #include "net/url_request/static_http_user_agent_settings.h"
71 #include "net/url_request/url_request.h"
72 #include "net/url_request/url_request_file_dir_job.h"
73 #include "net/url_request/url_request_http_job.h"
74 #include "net/url_request/url_request_job_factory_impl.h"
75 #include "net/url_request/url_request_redirect_job.h"
76 #include "net/url_request/url_request_test_job.h"
77 #include "net/url_request/url_request_test_util.h"
78 #include "testing/gtest/include/gtest/gtest.h"
79 #include "testing/platform_test.h"
82 #include "base/win/scoped_com_initializer.h"
83 #include "base/win/scoped_comptr.h"
84 #include "base/win/windows_version.h"
87 using base::ASCIIToUTF16
;
94 const base::string16
kChrome(ASCIIToUTF16("chrome"));
95 const base::string16
kSecret(ASCIIToUTF16("secret"));
96 const base::string16
kUser(ASCIIToUTF16("user"));
98 // Tests load timing information in the case a fresh connection was used, with
100 void TestLoadTimingNotReused(const net::LoadTimingInfo
& load_timing_info
,
101 int connect_timing_flags
) {
102 EXPECT_FALSE(load_timing_info
.socket_reused
);
103 EXPECT_NE(net::NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
105 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
106 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
108 EXPECT_LE(load_timing_info
.request_start
,
109 load_timing_info
.connect_timing
.connect_start
);
110 ExpectConnectTimingHasTimes(load_timing_info
.connect_timing
,
111 connect_timing_flags
);
112 EXPECT_LE(load_timing_info
.connect_timing
.connect_end
,
113 load_timing_info
.send_start
);
114 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
115 EXPECT_LE(load_timing_info
.send_end
, load_timing_info
.receive_headers_end
);
117 EXPECT_TRUE(load_timing_info
.proxy_resolve_start
.is_null());
118 EXPECT_TRUE(load_timing_info
.proxy_resolve_end
.is_null());
121 // Same as above, but with proxy times.
122 void TestLoadTimingNotReusedWithProxy(
123 const net::LoadTimingInfo
& load_timing_info
,
124 int connect_timing_flags
) {
125 EXPECT_FALSE(load_timing_info
.socket_reused
);
126 EXPECT_NE(net::NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
128 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
129 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
131 EXPECT_LE(load_timing_info
.request_start
,
132 load_timing_info
.proxy_resolve_start
);
133 EXPECT_LE(load_timing_info
.proxy_resolve_start
,
134 load_timing_info
.proxy_resolve_end
);
135 EXPECT_LE(load_timing_info
.proxy_resolve_end
,
136 load_timing_info
.connect_timing
.connect_start
);
137 ExpectConnectTimingHasTimes(load_timing_info
.connect_timing
,
138 connect_timing_flags
);
139 EXPECT_LE(load_timing_info
.connect_timing
.connect_end
,
140 load_timing_info
.send_start
);
141 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
142 EXPECT_LE(load_timing_info
.send_end
, load_timing_info
.receive_headers_end
);
145 // Same as above, but with a reused socket and proxy times.
146 void TestLoadTimingReusedWithProxy(
147 const net::LoadTimingInfo
& load_timing_info
) {
148 EXPECT_TRUE(load_timing_info
.socket_reused
);
149 EXPECT_NE(net::NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
151 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
152 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
154 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
156 EXPECT_LE(load_timing_info
.request_start
,
157 load_timing_info
.proxy_resolve_start
);
158 EXPECT_LE(load_timing_info
.proxy_resolve_start
,
159 load_timing_info
.proxy_resolve_end
);
160 EXPECT_LE(load_timing_info
.proxy_resolve_end
,
161 load_timing_info
.send_start
);
162 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
163 EXPECT_LE(load_timing_info
.send_end
, load_timing_info
.receive_headers_end
);
166 // Tests load timing information in the case of a cache hit, when no cache
167 // validation request was sent over the wire.
168 base::StringPiece
TestNetResourceProvider(int key
) {
172 void FillBuffer(char* buffer
, size_t len
) {
173 static bool called
= false;
176 int seed
= static_cast<int>(Time::Now().ToInternalValue());
180 for (size_t i
= 0; i
< len
; i
++) {
181 buffer
[i
] = static_cast<char>(rand());
188 void TestLoadTimingCacheHitNoNetwork(
189 const net::LoadTimingInfo
& load_timing_info
) {
190 EXPECT_FALSE(load_timing_info
.socket_reused
);
191 EXPECT_EQ(net::NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
193 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
194 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
196 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
197 EXPECT_LE(load_timing_info
.request_start
, load_timing_info
.send_start
);
198 EXPECT_LE(load_timing_info
.send_start
, load_timing_info
.send_end
);
199 EXPECT_LE(load_timing_info
.send_end
, load_timing_info
.receive_headers_end
);
201 EXPECT_TRUE(load_timing_info
.proxy_resolve_start
.is_null());
202 EXPECT_TRUE(load_timing_info
.proxy_resolve_end
.is_null());
205 // Tests load timing in the case that there is no HTTP response. This can be
206 // used to test in the case of errors or non-HTTP requests.
207 void TestLoadTimingNoHttpResponse(
208 const net::LoadTimingInfo
& load_timing_info
) {
209 EXPECT_FALSE(load_timing_info
.socket_reused
);
210 EXPECT_EQ(net::NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
212 // Only the request times should be non-null.
213 EXPECT_FALSE(load_timing_info
.request_start_time
.is_null());
214 EXPECT_FALSE(load_timing_info
.request_start
.is_null());
216 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
218 EXPECT_TRUE(load_timing_info
.proxy_resolve_start
.is_null());
219 EXPECT_TRUE(load_timing_info
.proxy_resolve_end
.is_null());
220 EXPECT_TRUE(load_timing_info
.send_start
.is_null());
221 EXPECT_TRUE(load_timing_info
.send_end
.is_null());
222 EXPECT_TRUE(load_timing_info
.receive_headers_end
.is_null());
225 // Do a case-insensitive search through |haystack| for |needle|.
226 bool ContainsString(const std::string
& haystack
, const char* needle
) {
227 std::string::const_iterator it
=
228 std::search(haystack
.begin(),
231 needle
+ strlen(needle
),
232 base::CaseInsensitiveCompare
<char>());
233 return it
!= haystack
.end();
236 UploadDataStream
* CreateSimpleUploadData(const char* data
) {
237 scoped_ptr
<UploadElementReader
> reader(
238 new UploadBytesElementReader(data
, strlen(data
)));
239 return UploadDataStream::CreateWithReader(reader
.Pass(), 0);
242 // Verify that the SSLInfo of a successful SSL connection has valid values.
243 void CheckSSLInfo(const SSLInfo
& ssl_info
) {
244 // -1 means unknown. 0 means no encryption.
245 EXPECT_GT(ssl_info
.security_bits
, 0);
247 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
248 int cipher_suite
= SSLConnectionStatusToCipherSuite(
249 ssl_info
.connection_status
);
250 EXPECT_NE(0, cipher_suite
);
253 void CheckFullRequestHeaders(const HttpRequestHeaders
& headers
,
254 const GURL
& host_url
) {
255 std::string sent_value
;
257 EXPECT_TRUE(headers
.GetHeader("Host", &sent_value
));
258 EXPECT_EQ(GetHostAndOptionalPort(host_url
), sent_value
);
260 EXPECT_TRUE(headers
.GetHeader("Connection", &sent_value
));
261 EXPECT_EQ("keep-alive", sent_value
);
264 bool FingerprintsEqual(const HashValueVector
& a
, const HashValueVector
& b
) {
265 size_t size
= a
.size();
267 if (size
!= b
.size())
270 for (size_t i
= 0; i
< size
; ++i
) {
271 if (!a
[i
].Equals(b
[i
]))
277 #endif // !defined(OS_IOS)
279 // A network delegate that allows the user to choose a subset of request stages
280 // to block in. When blocking, the delegate can do one of the following:
281 // * synchronously return a pre-specified error code, or
282 // * asynchronously return that value via an automatically called callback,
284 // * block and wait for the user to do a callback.
285 // Additionally, the user may also specify a redirect URL -- then each request
286 // with the current URL different from the redirect target will be redirected
287 // to that target, in the on-before-URL-request stage, independent of whether
288 // the delegate blocks in ON_BEFORE_URL_REQUEST or not.
289 class BlockingNetworkDelegate
: public TestNetworkDelegate
{
291 // Stages in which the delegate can block.
294 ON_BEFORE_URL_REQUEST
= 1 << 0,
295 ON_BEFORE_SEND_HEADERS
= 1 << 1,
296 ON_HEADERS_RECEIVED
= 1 << 2,
297 ON_AUTH_REQUIRED
= 1 << 3
300 // Behavior during blocked stages. During other stages, just
301 // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
303 SYNCHRONOUS
, // No callback, returns specified return values.
304 AUTO_CALLBACK
, // |this| posts a task to run the callback using the
305 // specified return codes.
306 USER_CALLBACK
, // User takes care of doing a callback. |retval_| and
307 // |auth_retval_| are ignored. In every blocking stage the
308 // message loop is quit.
311 // Creates a delegate which does not block at all.
312 explicit BlockingNetworkDelegate(BlockMode block_mode
);
314 // For users to trigger a callback returning |response|.
315 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
316 // Only call if |block_mode_| == USER_CALLBACK.
317 void DoCallback(int response
);
318 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response
);
321 void set_retval(int retval
) {
322 ASSERT_NE(USER_CALLBACK
, block_mode_
);
323 ASSERT_NE(ERR_IO_PENDING
, retval
);
324 ASSERT_NE(OK
, retval
);
328 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
329 // |auth_credentials_| will be passed with the response.
330 void set_auth_retval(AuthRequiredResponse auth_retval
) {
331 ASSERT_NE(USER_CALLBACK
, block_mode_
);
332 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING
, auth_retval
);
333 auth_retval_
= auth_retval
;
335 void set_auth_credentials(const AuthCredentials
& auth_credentials
) {
336 auth_credentials_
= auth_credentials
;
339 void set_redirect_url(const GURL
& url
) {
343 void set_block_on(int block_on
) {
344 block_on_
= block_on
;
347 // Allows the user to check in which state did we block.
348 Stage
stage_blocked_for_callback() const {
349 EXPECT_EQ(USER_CALLBACK
, block_mode_
);
350 return stage_blocked_for_callback_
;
354 void RunCallback(int response
, const CompletionCallback
& callback
);
355 void RunAuthCallback(AuthRequiredResponse response
,
356 const AuthCallback
& callback
);
358 // TestNetworkDelegate implementation.
359 virtual int OnBeforeURLRequest(URLRequest
* request
,
360 const CompletionCallback
& callback
,
361 GURL
* new_url
) OVERRIDE
;
363 virtual int OnBeforeSendHeaders(URLRequest
* request
,
364 const CompletionCallback
& callback
,
365 HttpRequestHeaders
* headers
) OVERRIDE
;
367 virtual int OnHeadersReceived(
369 const CompletionCallback
& callback
,
370 const HttpResponseHeaders
* original_response_headers
,
371 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
) OVERRIDE
;
373 virtual NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
375 const AuthChallengeInfo
& auth_info
,
376 const AuthCallback
& callback
,
377 AuthCredentials
* credentials
) OVERRIDE
;
379 // Resets the callbacks and |stage_blocked_for_callback_|.
382 // Checks whether we should block in |stage|. If yes, returns an error code
383 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
384 int MaybeBlockStage(Stage stage
, const CompletionCallback
& callback
);
386 // Configuration parameters, can be adjusted by public methods:
387 const BlockMode block_mode_
;
389 // Values returned on blocking stages when mode is SYNCHRONOUS or
390 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
391 int retval_
; // To be returned in non-auth stages.
392 AuthRequiredResponse auth_retval_
;
394 GURL redirect_url_
; // Used if non-empty.
395 int block_on_
; // Bit mask: in which stages to block.
397 // |auth_credentials_| will be copied to |*target_auth_credential_| on
399 AuthCredentials auth_credentials_
;
400 AuthCredentials
* target_auth_credentials_
;
402 // Internal variables, not set by not the user:
403 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
405 Stage stage_blocked_for_callback_
;
407 // Callback objects stored during blocking stages.
408 CompletionCallback callback_
;
409 AuthCallback auth_callback_
;
411 base::WeakPtrFactory
<BlockingNetworkDelegate
> weak_factory_
;
413 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate
);
416 BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode
)
417 : block_mode_(block_mode
),
419 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION
),
421 target_auth_credentials_(NULL
),
422 stage_blocked_for_callback_(NOT_BLOCKED
),
423 weak_factory_(this) {
426 void BlockingNetworkDelegate::DoCallback(int response
) {
427 ASSERT_EQ(USER_CALLBACK
, block_mode_
);
428 ASSERT_NE(NOT_BLOCKED
, stage_blocked_for_callback_
);
429 ASSERT_NE(ON_AUTH_REQUIRED
, stage_blocked_for_callback_
);
430 CompletionCallback callback
= callback_
;
432 RunCallback(response
, callback
);
435 void BlockingNetworkDelegate::DoAuthCallback(
436 NetworkDelegate::AuthRequiredResponse response
) {
437 ASSERT_EQ(USER_CALLBACK
, block_mode_
);
438 ASSERT_EQ(ON_AUTH_REQUIRED
, stage_blocked_for_callback_
);
439 AuthCallback auth_callback
= auth_callback_
;
441 RunAuthCallback(response
, auth_callback
);
444 void BlockingNetworkDelegate::RunCallback(int response
,
445 const CompletionCallback
& callback
) {
446 callback
.Run(response
);
449 void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response
,
450 const AuthCallback
& callback
) {
451 if (auth_retval_
== AUTH_REQUIRED_RESPONSE_SET_AUTH
) {
452 ASSERT_TRUE(target_auth_credentials_
!= NULL
);
453 *target_auth_credentials_
= auth_credentials_
;
455 callback
.Run(response
);
458 int BlockingNetworkDelegate::OnBeforeURLRequest(
460 const CompletionCallback
& callback
,
462 if (redirect_url_
== request
->url())
463 return OK
; // We've already seen this request and redirected elsewhere.
465 TestNetworkDelegate::OnBeforeURLRequest(request
, callback
, new_url
);
467 if (!redirect_url_
.is_empty())
468 *new_url
= redirect_url_
;
470 return MaybeBlockStage(ON_BEFORE_URL_REQUEST
, callback
);
473 int BlockingNetworkDelegate::OnBeforeSendHeaders(
475 const CompletionCallback
& callback
,
476 HttpRequestHeaders
* headers
) {
477 TestNetworkDelegate::OnBeforeSendHeaders(request
, callback
, headers
);
479 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS
, callback
);
482 int BlockingNetworkDelegate::OnHeadersReceived(
484 const CompletionCallback
& callback
,
485 const HttpResponseHeaders
* original_response_headers
,
486 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
) {
487 TestNetworkDelegate::OnHeadersReceived(
488 request
, callback
, original_response_headers
,
489 override_response_headers
);
491 return MaybeBlockStage(ON_HEADERS_RECEIVED
, callback
);
494 NetworkDelegate::AuthRequiredResponse
BlockingNetworkDelegate::OnAuthRequired(
496 const AuthChallengeInfo
& auth_info
,
497 const AuthCallback
& callback
,
498 AuthCredentials
* credentials
) {
499 TestNetworkDelegate::OnAuthRequired(request
, auth_info
, callback
,
501 // Check that the user has provided callback for the previous blocked stage.
502 EXPECT_EQ(NOT_BLOCKED
, stage_blocked_for_callback_
);
504 if ((block_on_
& ON_AUTH_REQUIRED
) == 0) {
505 return AUTH_REQUIRED_RESPONSE_NO_ACTION
;
508 target_auth_credentials_
= credentials
;
510 switch (block_mode_
) {
512 if (auth_retval_
== AUTH_REQUIRED_RESPONSE_SET_AUTH
)
513 *target_auth_credentials_
= auth_credentials_
;
517 base::MessageLoop::current()->PostTask(
519 base::Bind(&BlockingNetworkDelegate::RunAuthCallback
,
520 weak_factory_
.GetWeakPtr(), auth_retval_
, callback
));
521 return AUTH_REQUIRED_RESPONSE_IO_PENDING
;
524 auth_callback_
= callback
;
525 stage_blocked_for_callback_
= ON_AUTH_REQUIRED
;
526 base::MessageLoop::current()->PostTask(FROM_HERE
,
527 base::MessageLoop::QuitClosure());
528 return AUTH_REQUIRED_RESPONSE_IO_PENDING
;
531 return AUTH_REQUIRED_RESPONSE_NO_ACTION
; // Dummy value.
534 void BlockingNetworkDelegate::Reset() {
535 EXPECT_NE(NOT_BLOCKED
, stage_blocked_for_callback_
);
536 stage_blocked_for_callback_
= NOT_BLOCKED
;
538 auth_callback_
.Reset();
541 int BlockingNetworkDelegate::MaybeBlockStage(
542 BlockingNetworkDelegate::Stage stage
,
543 const CompletionCallback
& callback
) {
544 // Check that the user has provided callback for the previous blocked stage.
545 EXPECT_EQ(NOT_BLOCKED
, stage_blocked_for_callback_
);
547 if ((block_on_
& stage
) == 0) {
551 switch (block_mode_
) {
553 EXPECT_NE(OK
, retval_
);
557 base::MessageLoop::current()->PostTask(
559 base::Bind(&BlockingNetworkDelegate::RunCallback
,
560 weak_factory_
.GetWeakPtr(), retval_
, callback
));
561 return ERR_IO_PENDING
;
564 callback_
= callback
;
565 stage_blocked_for_callback_
= stage
;
566 base::MessageLoop::current()->PostTask(FROM_HERE
,
567 base::MessageLoop::QuitClosure());
568 return ERR_IO_PENDING
;
574 class TestURLRequestContextWithProxy
: public TestURLRequestContext
{
576 // Does not own |delegate|.
577 TestURLRequestContextWithProxy(const std::string
& proxy
,
578 NetworkDelegate
* delegate
)
579 : TestURLRequestContext(true) {
580 context_storage_
.set_proxy_service(ProxyService::CreateFixed(proxy
));
581 set_network_delegate(delegate
);
584 virtual ~TestURLRequestContextWithProxy() {}
589 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.
590 class URLRequestTest
: public PlatformTest
{
592 URLRequestTest() : default_context_(true) {
593 default_context_
.set_network_delegate(&default_network_delegate_
);
594 default_context_
.set_net_log(&net_log_
);
595 job_factory_
.SetProtocolHandler("data", new DataProtocolHandler
);
596 job_factory_
.SetProtocolHandler(
597 "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
598 default_context_
.set_job_factory(&job_factory_
);
599 default_context_
.Init();
601 virtual ~URLRequestTest() {
602 // URLRequestJobs may post clean-up tasks on destruction.
603 base::RunLoop().RunUntilIdle();
606 // Adds the TestJobInterceptor to the default context.
607 TestJobInterceptor
* AddTestInterceptor() {
608 TestJobInterceptor
* protocol_handler_
= new TestJobInterceptor();
609 job_factory_
.SetProtocolHandler("http", NULL
);
610 job_factory_
.SetProtocolHandler("http", protocol_handler_
);
611 return protocol_handler_
;
615 CapturingNetLog net_log_
;
616 TestNetworkDelegate default_network_delegate_
; // Must outlive URLRequest.
617 URLRequestJobFactoryImpl job_factory_
;
618 TestURLRequestContext default_context_
;
621 TEST_F(URLRequestTest
, AboutBlankTest
) {
624 URLRequest
r(GURL("about:blank"), DEFAULT_PRIORITY
, &d
, &default_context_
);
627 EXPECT_TRUE(r
.is_pending());
629 base::RunLoop().Run();
631 EXPECT_TRUE(!r
.is_pending());
632 EXPECT_FALSE(d
.received_data_before_response());
633 EXPECT_EQ(d
.bytes_received(), 0);
634 EXPECT_EQ("", r
.GetSocketAddress().host());
635 EXPECT_EQ(0, r
.GetSocketAddress().port());
637 HttpRequestHeaders headers
;
638 EXPECT_FALSE(r
.GetFullRequestHeaders(&headers
));
642 TEST_F(URLRequestTest
, DataURLImageTest
) {
645 // Use our nice little Chrome logo.
648 "data:image/png;base64,"
649 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3"
650 "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD"
651 "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t"
652 "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9"
653 "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1"
654 "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z"
655 "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW"
656 "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW"
657 "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb"
658 "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5"
659 "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV"
660 "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq"
661 "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F"
662 "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB"
663 "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM"
664 "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm"
665 "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En"
666 "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
672 EXPECT_TRUE(r
.is_pending());
674 base::RunLoop().Run();
676 EXPECT_TRUE(!r
.is_pending());
677 EXPECT_FALSE(d
.received_data_before_response());
678 EXPECT_EQ(d
.bytes_received(), 911);
679 EXPECT_EQ("", r
.GetSocketAddress().host());
680 EXPECT_EQ(0, r
.GetSocketAddress().port());
682 HttpRequestHeaders headers
;
683 EXPECT_FALSE(r
.GetFullRequestHeaders(&headers
));
687 TEST_F(URLRequestTest
, FileTest
) {
688 base::FilePath app_path
;
689 PathService::Get(base::FILE_EXE
, &app_path
);
690 GURL app_url
= FilePathToFileURL(app_path
);
694 URLRequest
r(app_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
697 EXPECT_TRUE(r
.is_pending());
699 base::RunLoop().Run();
701 int64 file_size
= -1;
702 EXPECT_TRUE(base::GetFileSize(app_path
, &file_size
));
704 EXPECT_TRUE(!r
.is_pending());
705 EXPECT_EQ(1, d
.response_started_count());
706 EXPECT_FALSE(d
.received_data_before_response());
707 EXPECT_EQ(d
.bytes_received(), static_cast<int>(file_size
));
708 EXPECT_EQ("", r
.GetSocketAddress().host());
709 EXPECT_EQ(0, r
.GetSocketAddress().port());
711 HttpRequestHeaders headers
;
712 EXPECT_FALSE(r
.GetFullRequestHeaders(&headers
));
716 TEST_F(URLRequestTest
, FileTestCancel
) {
717 base::FilePath app_path
;
718 PathService::Get(base::FILE_EXE
, &app_path
);
719 GURL app_url
= FilePathToFileURL(app_path
);
723 URLRequest
r(app_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
726 EXPECT_TRUE(r
.is_pending());
729 // Async cancellation should be safe even when URLRequest has been already
731 base::RunLoop().RunUntilIdle();
734 TEST_F(URLRequestTest
, FileTestFullSpecifiedRange
) {
735 const size_t buffer_size
= 4000;
736 scoped_ptr
<char[]> buffer(new char[buffer_size
]);
737 FillBuffer(buffer
.get(), buffer_size
);
739 base::FilePath temp_path
;
740 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path
));
741 GURL temp_url
= FilePathToFileURL(temp_path
);
742 EXPECT_TRUE(base::WriteFile(temp_path
, buffer
.get(), buffer_size
));
745 EXPECT_TRUE(base::GetFileSize(temp_path
, &file_size
));
747 const size_t first_byte_position
= 500;
748 const size_t last_byte_position
= buffer_size
- first_byte_position
;
749 const size_t content_length
= last_byte_position
- first_byte_position
+ 1;
750 std::string
partial_buffer_string(buffer
.get() + first_byte_position
,
751 buffer
.get() + last_byte_position
+ 1);
755 URLRequest
r(temp_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
757 HttpRequestHeaders headers
;
759 HttpRequestHeaders::kRange
,
760 net::HttpByteRange::Bounded(
761 first_byte_position
, last_byte_position
).GetHeaderValue());
762 r
.SetExtraRequestHeaders(headers
);
764 EXPECT_TRUE(r
.is_pending());
766 base::RunLoop().Run();
767 EXPECT_TRUE(!r
.is_pending());
768 EXPECT_EQ(1, d
.response_started_count());
769 EXPECT_FALSE(d
.received_data_before_response());
770 EXPECT_EQ(static_cast<int>(content_length
), d
.bytes_received());
771 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
772 EXPECT_TRUE(partial_buffer_string
== d
.data_received());
775 EXPECT_TRUE(base::DeleteFile(temp_path
, false));
778 TEST_F(URLRequestTest
, FileTestHalfSpecifiedRange
) {
779 const size_t buffer_size
= 4000;
780 scoped_ptr
<char[]> buffer(new char[buffer_size
]);
781 FillBuffer(buffer
.get(), buffer_size
);
783 base::FilePath temp_path
;
784 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path
));
785 GURL temp_url
= FilePathToFileURL(temp_path
);
786 EXPECT_TRUE(base::WriteFile(temp_path
, buffer
.get(), buffer_size
));
789 EXPECT_TRUE(base::GetFileSize(temp_path
, &file_size
));
791 const size_t first_byte_position
= 500;
792 const size_t last_byte_position
= buffer_size
- 1;
793 const size_t content_length
= last_byte_position
- first_byte_position
+ 1;
794 std::string
partial_buffer_string(buffer
.get() + first_byte_position
,
795 buffer
.get() + last_byte_position
+ 1);
799 URLRequest
r(temp_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
801 HttpRequestHeaders headers
;
802 headers
.SetHeader(HttpRequestHeaders::kRange
,
803 net::HttpByteRange::RightUnbounded(
804 first_byte_position
).GetHeaderValue());
805 r
.SetExtraRequestHeaders(headers
);
807 EXPECT_TRUE(r
.is_pending());
809 base::RunLoop().Run();
810 EXPECT_TRUE(!r
.is_pending());
811 EXPECT_EQ(1, d
.response_started_count());
812 EXPECT_FALSE(d
.received_data_before_response());
813 EXPECT_EQ(static_cast<int>(content_length
), d
.bytes_received());
814 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
815 EXPECT_TRUE(partial_buffer_string
== d
.data_received());
818 EXPECT_TRUE(base::DeleteFile(temp_path
, false));
821 TEST_F(URLRequestTest
, FileTestMultipleRanges
) {
822 const size_t buffer_size
= 400000;
823 scoped_ptr
<char[]> buffer(new char[buffer_size
]);
824 FillBuffer(buffer
.get(), buffer_size
);
826 base::FilePath temp_path
;
827 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path
));
828 GURL temp_url
= FilePathToFileURL(temp_path
);
829 EXPECT_TRUE(base::WriteFile(temp_path
, buffer
.get(), buffer_size
));
832 EXPECT_TRUE(base::GetFileSize(temp_path
, &file_size
));
836 URLRequest
r(temp_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
838 HttpRequestHeaders headers
;
839 headers
.SetHeader(HttpRequestHeaders::kRange
, "bytes=0-0,10-200,200-300");
840 r
.SetExtraRequestHeaders(headers
);
842 EXPECT_TRUE(r
.is_pending());
844 base::RunLoop().Run();
845 EXPECT_TRUE(d
.request_failed());
848 EXPECT_TRUE(base::DeleteFile(temp_path
, false));
851 TEST_F(URLRequestTest
, AllowFileURLs
) {
852 base::ScopedTempDir temp_dir
;
853 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
854 base::FilePath test_file
;
855 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir
.path(), &test_file
));
856 std::string
test_data("monkey");
857 base::WriteFile(test_file
, test_data
.data(), test_data
.size());
858 GURL test_file_url
= net::FilePathToFileURL(test_file
);
862 TestNetworkDelegate network_delegate
;
863 network_delegate
.set_can_access_files(true);
864 default_context_
.set_network_delegate(&network_delegate
);
865 URLRequest
r(test_file_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
867 base::RunLoop().Run();
868 EXPECT_FALSE(d
.request_failed());
869 EXPECT_EQ(test_data
, d
.data_received());
874 TestNetworkDelegate network_delegate
;
875 network_delegate
.set_can_access_files(false);
876 default_context_
.set_network_delegate(&network_delegate
);
877 URLRequest
r(test_file_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
879 base::RunLoop().Run();
880 EXPECT_TRUE(d
.request_failed());
881 EXPECT_EQ("", d
.data_received());
885 TEST_F(URLRequestTest
, InvalidUrlTest
) {
888 URLRequest
r(GURL("invalid url"), DEFAULT_PRIORITY
, &d
, &default_context_
);
891 EXPECT_TRUE(r
.is_pending());
893 base::RunLoop().Run();
894 EXPECT_TRUE(d
.request_failed());
899 TEST_F(URLRequestTest
, ResolveShortcutTest
) {
900 base::FilePath app_path
;
901 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
902 app_path
= app_path
.AppendASCII("net");
903 app_path
= app_path
.AppendASCII("data");
904 app_path
= app_path
.AppendASCII("url_request_unittest");
905 app_path
= app_path
.AppendASCII("with-headers.html");
907 std::wstring lnk_path
= app_path
.value() + L
".lnk";
909 base::win::ScopedCOMInitializer com_initializer
;
911 // Temporarily create a shortcut for test
913 base::win::ScopedComPtr
<IShellLink
> shell
;
914 ASSERT_TRUE(SUCCEEDED(shell
.CreateInstance(CLSID_ShellLink
, NULL
,
915 CLSCTX_INPROC_SERVER
)));
916 base::win::ScopedComPtr
<IPersistFile
> persist
;
917 ASSERT_TRUE(SUCCEEDED(shell
.QueryInterface(persist
.Receive())));
918 EXPECT_TRUE(SUCCEEDED(shell
->SetPath(app_path
.value().c_str())));
919 EXPECT_TRUE(SUCCEEDED(shell
->SetDescription(L
"ResolveShortcutTest")));
920 EXPECT_TRUE(SUCCEEDED(persist
->Save(lnk_path
.c_str(), TRUE
)));
925 URLRequest
r(FilePathToFileURL(base::FilePath(lnk_path
)),
931 EXPECT_TRUE(r
.is_pending());
933 base::RunLoop().Run();
935 WIN32_FILE_ATTRIBUTE_DATA data
;
936 GetFileAttributesEx(app_path
.value().c_str(),
937 GetFileExInfoStandard
, &data
);
938 HANDLE file
= CreateFile(app_path
.value().c_str(), GENERIC_READ
,
939 FILE_SHARE_READ
, NULL
, OPEN_EXISTING
,
940 FILE_ATTRIBUTE_NORMAL
, NULL
);
941 EXPECT_NE(INVALID_HANDLE_VALUE
, file
);
942 scoped_ptr
<char[]> buffer(new char[data
.nFileSizeLow
]);
945 result
= ReadFile(file
, buffer
.get(), data
.nFileSizeLow
,
947 std::string
content(buffer
.get(), read_size
);
950 EXPECT_TRUE(!r
.is_pending());
951 EXPECT_EQ(1, d
.received_redirect_count());
952 EXPECT_EQ(content
, d
.data_received());
955 // Clean the shortcut
956 DeleteFile(lnk_path
.c_str());
958 #endif // defined(OS_WIN)
960 TEST_F(URLRequestTest
, FileDirCancelTest
) {
961 // Put in mock resource provider.
962 NetModule::SetResourceProvider(TestNetResourceProvider
);
966 base::FilePath file_path
;
967 PathService::Get(base::DIR_SOURCE_ROOT
, &file_path
);
968 file_path
= file_path
.Append(FILE_PATH_LITERAL("net"));
969 file_path
= file_path
.Append(FILE_PATH_LITERAL("data"));
972 FilePathToFileURL(file_path
), DEFAULT_PRIORITY
, &d
, &default_context_
);
974 EXPECT_TRUE(req
.is_pending());
976 d
.set_cancel_in_received_data_pending(true);
978 base::RunLoop().Run();
981 // Take out mock resource provider.
982 NetModule::SetResourceProvider(NULL
);
985 TEST_F(URLRequestTest
, FileDirOutputSanity
) {
986 // Verify the general sanity of the the output of the file:
987 // directory lister by checking for the output of a known existing
989 const char sentinel_name
[] = "filedir-sentinel";
992 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
993 path
= path
.Append(FILE_PATH_LITERAL("net"));
994 path
= path
.Append(FILE_PATH_LITERAL("data"));
995 path
= path
.Append(FILE_PATH_LITERAL("url_request_unittest"));
999 FilePathToFileURL(path
), DEFAULT_PRIORITY
, &d
, &default_context_
);
1001 base::RunLoop().Run();
1003 // Generate entry for the sentinel file.
1004 base::FilePath sentinel_path
= path
.AppendASCII(sentinel_name
);
1005 base::File::Info info
;
1006 EXPECT_TRUE(base::GetFileInfo(sentinel_path
, &info
));
1007 EXPECT_GT(info
.size
, 0);
1008 std::string sentinel_output
= GetDirectoryListingEntry(
1009 base::string16(sentinel_name
, sentinel_name
+ strlen(sentinel_name
)),
1010 std::string(sentinel_name
),
1013 info
.last_modified
);
1015 ASSERT_LT(0, d
.bytes_received());
1016 ASSERT_FALSE(d
.request_failed());
1017 ASSERT_TRUE(req
.status().is_success());
1018 // Check for the entry generated for the "sentinel" file.
1019 const std::string
& data
= d
.data_received();
1020 ASSERT_NE(data
.find(sentinel_output
), std::string::npos
);
1023 TEST_F(URLRequestTest
, FileDirRedirectNoCrash
) {
1024 // There is an implicit redirect when loading a file path that matches a
1025 // directory and does not end with a slash. Ensure that following such
1026 // redirects does not crash. See http://crbug.com/18686.
1028 base::FilePath path
;
1029 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
1030 path
= path
.Append(FILE_PATH_LITERAL("net"));
1031 path
= path
.Append(FILE_PATH_LITERAL("data"));
1032 path
= path
.Append(FILE_PATH_LITERAL("url_request_unittest"));
1036 FilePathToFileURL(path
), DEFAULT_PRIORITY
, &d
, &default_context_
);
1038 base::RunLoop().Run();
1040 ASSERT_EQ(1, d
.received_redirect_count());
1041 ASSERT_LT(0, d
.bytes_received());
1042 ASSERT_FALSE(d
.request_failed());
1043 ASSERT_TRUE(req
.status().is_success());
1047 // Don't accept the url "file:///" on windows. See http://crbug.com/1474.
1048 TEST_F(URLRequestTest
, FileDirRedirectSingleSlash
) {
1050 URLRequest
req(GURL("file:///"), DEFAULT_PRIORITY
, &d
, &default_context_
);
1052 base::RunLoop().Run();
1054 ASSERT_EQ(1, d
.received_redirect_count());
1055 ASSERT_FALSE(req
.status().is_success());
1059 // Custom URLRequestJobs for use with interceptor tests
1060 class RestartTestJob
: public URLRequestTestJob
{
1062 RestartTestJob(URLRequest
* request
, NetworkDelegate
* network_delegate
)
1063 : URLRequestTestJob(request
, network_delegate
, true) {}
1065 virtual void StartAsync() OVERRIDE
{
1066 this->NotifyRestartRequired();
1069 virtual ~RestartTestJob() {}
1072 class CancelTestJob
: public URLRequestTestJob
{
1074 explicit CancelTestJob(URLRequest
* request
, NetworkDelegate
* network_delegate
)
1075 : URLRequestTestJob(request
, network_delegate
, true) {}
1077 virtual void StartAsync() OVERRIDE
{
1081 virtual ~CancelTestJob() {}
1084 class CancelThenRestartTestJob
: public URLRequestTestJob
{
1086 explicit CancelThenRestartTestJob(URLRequest
* request
,
1087 NetworkDelegate
* network_delegate
)
1088 : URLRequestTestJob(request
, network_delegate
, true) {
1091 virtual void StartAsync() OVERRIDE
{
1093 this->NotifyRestartRequired();
1096 virtual ~CancelThenRestartTestJob() {}
1099 // An Interceptor for use with interceptor tests
1100 class TestInterceptor
: URLRequest::Interceptor
{
1103 : intercept_main_request_(false), restart_main_request_(false),
1104 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1105 simulate_main_network_error_(false),
1106 intercept_redirect_(false), cancel_redirect_request_(false),
1107 intercept_final_response_(false), cancel_final_request_(false),
1108 did_intercept_main_(false), did_restart_main_(false),
1109 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1110 did_simulate_error_main_(false),
1111 did_intercept_redirect_(false), did_cancel_redirect_(false),
1112 did_intercept_final_(false), did_cancel_final_(false) {
1113 URLRequest::Deprecated::RegisterRequestInterceptor(this);
1116 virtual ~TestInterceptor() {
1117 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
1120 virtual URLRequestJob
* MaybeIntercept(
1121 URLRequest
* request
,
1122 NetworkDelegate
* network_delegate
) OVERRIDE
{
1123 if (restart_main_request_
) {
1124 restart_main_request_
= false;
1125 did_restart_main_
= true;
1126 return new RestartTestJob(request
, network_delegate
);
1128 if (cancel_main_request_
) {
1129 cancel_main_request_
= false;
1130 did_cancel_main_
= true;
1131 return new CancelTestJob(request
, network_delegate
);
1133 if (cancel_then_restart_main_request_
) {
1134 cancel_then_restart_main_request_
= false;
1135 did_cancel_then_restart_main_
= true;
1136 return new CancelThenRestartTestJob(request
, network_delegate
);
1138 if (simulate_main_network_error_
) {
1139 simulate_main_network_error_
= false;
1140 did_simulate_error_main_
= true;
1141 // will error since the requeted url is not one of its canned urls
1142 return new URLRequestTestJob(request
, network_delegate
, true);
1144 if (!intercept_main_request_
)
1146 intercept_main_request_
= false;
1147 did_intercept_main_
= true;
1148 URLRequestTestJob
* job
= new URLRequestTestJob(request
,
1153 job
->set_load_timing_info(main_request_load_timing_info_
);
1157 virtual URLRequestJob
* MaybeInterceptRedirect(
1158 URLRequest
* request
,
1159 NetworkDelegate
* network_delegate
,
1160 const GURL
& location
) OVERRIDE
{
1161 if (cancel_redirect_request_
) {
1162 cancel_redirect_request_
= false;
1163 did_cancel_redirect_
= true;
1164 return new CancelTestJob(request
, network_delegate
);
1166 if (!intercept_redirect_
)
1168 intercept_redirect_
= false;
1169 did_intercept_redirect_
= true;
1170 return new URLRequestTestJob(request
,
1177 virtual URLRequestJob
* MaybeInterceptResponse(
1178 URLRequest
* request
, NetworkDelegate
* network_delegate
) OVERRIDE
{
1179 if (cancel_final_request_
) {
1180 cancel_final_request_
= false;
1181 did_cancel_final_
= true;
1182 return new CancelTestJob(request
, network_delegate
);
1184 if (!intercept_final_response_
)
1186 intercept_final_response_
= false;
1187 did_intercept_final_
= true;
1188 return new URLRequestTestJob(request
,
1195 // Whether to intercept the main request, and if so the response to return and
1196 // the LoadTimingInfo to use.
1197 bool intercept_main_request_
;
1198 std::string main_headers_
;
1199 std::string main_data_
;
1200 LoadTimingInfo main_request_load_timing_info_
;
1202 // Other actions we take at MaybeIntercept time
1203 bool restart_main_request_
;
1204 bool cancel_main_request_
;
1205 bool cancel_then_restart_main_request_
;
1206 bool simulate_main_network_error_
;
1208 // Whether to intercept redirects, and if so the response to return.
1209 bool intercept_redirect_
;
1210 std::string redirect_headers_
;
1211 std::string redirect_data_
;
1213 // Other actions we can take at MaybeInterceptRedirect time
1214 bool cancel_redirect_request_
;
1216 // Whether to intercept final response, and if so the response to return.
1217 bool intercept_final_response_
;
1218 std::string final_headers_
;
1219 std::string final_data_
;
1221 // Other actions we can take at MaybeInterceptResponse time
1222 bool cancel_final_request_
;
1224 // If we did something or not
1225 bool did_intercept_main_
;
1226 bool did_restart_main_
;
1227 bool did_cancel_main_
;
1228 bool did_cancel_then_restart_main_
;
1229 bool did_simulate_error_main_
;
1230 bool did_intercept_redirect_
;
1231 bool did_cancel_redirect_
;
1232 bool did_intercept_final_
;
1233 bool did_cancel_final_
;
1235 // Static getters for canned response header and data strings
1237 static std::string
ok_data() {
1238 return URLRequestTestJob::test_data_1();
1241 static std::string
ok_headers() {
1242 return URLRequestTestJob::test_headers();
1245 static std::string
redirect_data() {
1246 return std::string();
1249 static std::string
redirect_headers() {
1250 return URLRequestTestJob::test_redirect_headers();
1253 static std::string
error_data() {
1254 return std::string("ohhh nooooo mr. bill!");
1257 static std::string
error_headers() {
1258 return URLRequestTestJob::test_error_headers();
1262 TEST_F(URLRequestTest
, Intercept
) {
1263 TestInterceptor interceptor
;
1265 // intercept the main request and respond with a simple response
1266 interceptor
.intercept_main_request_
= true;
1267 interceptor
.main_headers_
= TestInterceptor::ok_headers();
1268 interceptor
.main_data_
= TestInterceptor::ok_data();
1271 URLRequest
req(GURL("http://test_intercept/foo"),
1275 base::SupportsUserData::Data
* user_data0
= new base::SupportsUserData::Data();
1276 base::SupportsUserData::Data
* user_data1
= new base::SupportsUserData::Data();
1277 base::SupportsUserData::Data
* user_data2
= new base::SupportsUserData::Data();
1278 req
.SetUserData(NULL
, user_data0
);
1279 req
.SetUserData(&user_data1
, user_data1
);
1280 req
.SetUserData(&user_data2
, user_data2
);
1281 req
.set_method("GET");
1283 base::RunLoop().Run();
1285 // Make sure we can retrieve our specific user data
1286 EXPECT_EQ(user_data0
, req
.GetUserData(NULL
));
1287 EXPECT_EQ(user_data1
, req
.GetUserData(&user_data1
));
1288 EXPECT_EQ(user_data2
, req
.GetUserData(&user_data2
));
1290 // Check the interceptor got called as expected
1291 EXPECT_TRUE(interceptor
.did_intercept_main_
);
1293 // Check we got one good response
1294 EXPECT_TRUE(req
.status().is_success());
1295 EXPECT_EQ(200, req
.response_headers()->response_code());
1296 EXPECT_EQ(TestInterceptor::ok_data(), d
.data_received());
1297 EXPECT_EQ(1, d
.response_started_count());
1298 EXPECT_EQ(0, d
.received_redirect_count());
1301 TEST_F(URLRequestTest
, InterceptRedirect
) {
1302 TestInterceptor interceptor
;
1304 // intercept the main request and respond with a redirect
1305 interceptor
.intercept_main_request_
= true;
1306 interceptor
.main_headers_
= TestInterceptor::redirect_headers();
1307 interceptor
.main_data_
= TestInterceptor::redirect_data();
1309 // intercept that redirect and respond a final OK response
1310 interceptor
.intercept_redirect_
= true;
1311 interceptor
.redirect_headers_
= TestInterceptor::ok_headers();
1312 interceptor
.redirect_data_
= TestInterceptor::ok_data();
1315 URLRequest
req(GURL("http://test_intercept/foo"),
1319 req
.set_method("GET");
1321 base::RunLoop().Run();
1323 // Check the interceptor got called as expected
1324 EXPECT_TRUE(interceptor
.did_intercept_main_
);
1325 EXPECT_TRUE(interceptor
.did_intercept_redirect_
);
1327 // Check we got one good response
1328 EXPECT_TRUE(req
.status().is_success());
1329 if (req
.status().is_success()) {
1330 EXPECT_EQ(200, req
.response_headers()->response_code());
1332 EXPECT_EQ(TestInterceptor::ok_data(), d
.data_received());
1333 EXPECT_EQ(1, d
.response_started_count());
1334 EXPECT_EQ(0, d
.received_redirect_count());
1337 TEST_F(URLRequestTest
, InterceptServerError
) {
1338 TestInterceptor interceptor
;
1340 // intercept the main request to generate a server error response
1341 interceptor
.intercept_main_request_
= true;
1342 interceptor
.main_headers_
= TestInterceptor::error_headers();
1343 interceptor
.main_data_
= TestInterceptor::error_data();
1345 // intercept that error and respond with an OK response
1346 interceptor
.intercept_final_response_
= true;
1347 interceptor
.final_headers_
= TestInterceptor::ok_headers();
1348 interceptor
.final_data_
= TestInterceptor::ok_data();
1351 URLRequest
req(GURL("http://test_intercept/foo"),
1355 req
.set_method("GET");
1357 base::RunLoop().Run();
1359 // Check the interceptor got called as expected
1360 EXPECT_TRUE(interceptor
.did_intercept_main_
);
1361 EXPECT_TRUE(interceptor
.did_intercept_final_
);
1363 // Check we got one good response
1364 EXPECT_TRUE(req
.status().is_success());
1365 EXPECT_EQ(200, req
.response_headers()->response_code());
1366 EXPECT_EQ(TestInterceptor::ok_data(), d
.data_received());
1367 EXPECT_EQ(1, d
.response_started_count());
1368 EXPECT_EQ(0, d
.received_redirect_count());
1371 TEST_F(URLRequestTest
, InterceptNetworkError
) {
1372 TestInterceptor interceptor
;
1374 // intercept the main request to simulate a network error
1375 interceptor
.simulate_main_network_error_
= true;
1377 // intercept that error and respond with an OK response
1378 interceptor
.intercept_final_response_
= true;
1379 interceptor
.final_headers_
= TestInterceptor::ok_headers();
1380 interceptor
.final_data_
= TestInterceptor::ok_data();
1383 URLRequest
req(GURL("http://test_intercept/foo"),
1387 req
.set_method("GET");
1389 base::RunLoop().Run();
1391 // Check the interceptor got called as expected
1392 EXPECT_TRUE(interceptor
.did_simulate_error_main_
);
1393 EXPECT_TRUE(interceptor
.did_intercept_final_
);
1395 // Check we received one good response
1396 EXPECT_TRUE(req
.status().is_success());
1397 EXPECT_EQ(200, req
.response_headers()->response_code());
1398 EXPECT_EQ(TestInterceptor::ok_data(), d
.data_received());
1399 EXPECT_EQ(1, d
.response_started_count());
1400 EXPECT_EQ(0, d
.received_redirect_count());
1403 TEST_F(URLRequestTest
, InterceptRestartRequired
) {
1404 TestInterceptor interceptor
;
1406 // restart the main request
1407 interceptor
.restart_main_request_
= true;
1409 // then intercept the new main request and respond with an OK response
1410 interceptor
.intercept_main_request_
= true;
1411 interceptor
.main_headers_
= TestInterceptor::ok_headers();
1412 interceptor
.main_data_
= TestInterceptor::ok_data();
1415 URLRequest
req(GURL("http://test_intercept/foo"),
1419 req
.set_method("GET");
1421 base::RunLoop().Run();
1423 // Check the interceptor got called as expected
1424 EXPECT_TRUE(interceptor
.did_restart_main_
);
1425 EXPECT_TRUE(interceptor
.did_intercept_main_
);
1427 // Check we received one good response
1428 EXPECT_TRUE(req
.status().is_success());
1429 if (req
.status().is_success()) {
1430 EXPECT_EQ(200, req
.response_headers()->response_code());
1432 EXPECT_EQ(TestInterceptor::ok_data(), d
.data_received());
1433 EXPECT_EQ(1, d
.response_started_count());
1434 EXPECT_EQ(0, d
.received_redirect_count());
1437 TEST_F(URLRequestTest
, InterceptRespectsCancelMain
) {
1438 TestInterceptor interceptor
;
1440 // intercept the main request and cancel from within the restarted job
1441 interceptor
.cancel_main_request_
= true;
1443 // setup to intercept final response and override it with an OK response
1444 interceptor
.intercept_final_response_
= true;
1445 interceptor
.final_headers_
= TestInterceptor::ok_headers();
1446 interceptor
.final_data_
= TestInterceptor::ok_data();
1449 URLRequest
req(GURL("http://test_intercept/foo"),
1453 req
.set_method("GET");
1455 base::RunLoop().Run();
1457 // Check the interceptor got called as expected
1458 EXPECT_TRUE(interceptor
.did_cancel_main_
);
1459 EXPECT_FALSE(interceptor
.did_intercept_final_
);
1461 // Check we see a canceled request
1462 EXPECT_FALSE(req
.status().is_success());
1463 EXPECT_EQ(URLRequestStatus::CANCELED
, req
.status().status());
1466 TEST_F(URLRequestTest
, InterceptRespectsCancelRedirect
) {
1467 TestInterceptor interceptor
;
1469 // intercept the main request and respond with a redirect
1470 interceptor
.intercept_main_request_
= true;
1471 interceptor
.main_headers_
= TestInterceptor::redirect_headers();
1472 interceptor
.main_data_
= TestInterceptor::redirect_data();
1474 // intercept the redirect and cancel from within that job
1475 interceptor
.cancel_redirect_request_
= true;
1477 // setup to intercept final response and override it with an OK response
1478 interceptor
.intercept_final_response_
= true;
1479 interceptor
.final_headers_
= TestInterceptor::ok_headers();
1480 interceptor
.final_data_
= TestInterceptor::ok_data();
1483 URLRequest
req(GURL("http://test_intercept/foo"),
1487 req
.set_method("GET");
1489 base::RunLoop().Run();
1491 // Check the interceptor got called as expected
1492 EXPECT_TRUE(interceptor
.did_intercept_main_
);
1493 EXPECT_TRUE(interceptor
.did_cancel_redirect_
);
1494 EXPECT_FALSE(interceptor
.did_intercept_final_
);
1496 // Check we see a canceled request
1497 EXPECT_FALSE(req
.status().is_success());
1498 EXPECT_EQ(URLRequestStatus::CANCELED
, req
.status().status());
1501 TEST_F(URLRequestTest
, InterceptRespectsCancelFinal
) {
1502 TestInterceptor interceptor
;
1504 // intercept the main request to simulate a network error
1505 interceptor
.simulate_main_network_error_
= true;
1507 // setup to intercept final response and cancel from within that job
1508 interceptor
.cancel_final_request_
= true;
1511 URLRequest
req(GURL("http://test_intercept/foo"),
1515 req
.set_method("GET");
1517 base::RunLoop().Run();
1519 // Check the interceptor got called as expected
1520 EXPECT_TRUE(interceptor
.did_simulate_error_main_
);
1521 EXPECT_TRUE(interceptor
.did_cancel_final_
);
1523 // Check we see a canceled request
1524 EXPECT_FALSE(req
.status().is_success());
1525 EXPECT_EQ(URLRequestStatus::CANCELED
, req
.status().status());
1528 TEST_F(URLRequestTest
, InterceptRespectsCancelInRestart
) {
1529 TestInterceptor interceptor
;
1531 // intercept the main request and cancel then restart from within that job
1532 interceptor
.cancel_then_restart_main_request_
= true;
1534 // setup to intercept final response and override it with an OK response
1535 interceptor
.intercept_final_response_
= true;
1536 interceptor
.final_headers_
= TestInterceptor::ok_headers();
1537 interceptor
.final_data_
= TestInterceptor::ok_data();
1540 URLRequest
req(GURL("http://test_intercept/foo"),
1544 req
.set_method("GET");
1546 base::RunLoop().Run();
1548 // Check the interceptor got called as expected
1549 EXPECT_TRUE(interceptor
.did_cancel_then_restart_main_
);
1550 EXPECT_FALSE(interceptor
.did_intercept_final_
);
1552 // Check we see a canceled request
1553 EXPECT_FALSE(req
.status().is_success());
1554 EXPECT_EQ(URLRequestStatus::CANCELED
, req
.status().status());
1557 LoadTimingInfo
RunLoadTimingTest(const LoadTimingInfo
& job_load_timing
,
1558 URLRequestContext
* context
) {
1559 TestInterceptor interceptor
;
1560 interceptor
.intercept_main_request_
= true;
1561 interceptor
.main_request_load_timing_info_
= job_load_timing
;
1564 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY
, &d
, context
);
1566 base::RunLoop().Run();
1568 LoadTimingInfo resulting_load_timing
;
1569 req
.GetLoadTimingInfo(&resulting_load_timing
);
1571 // None of these should be modified by the URLRequest.
1572 EXPECT_EQ(job_load_timing
.socket_reused
, resulting_load_timing
.socket_reused
);
1573 EXPECT_EQ(job_load_timing
.socket_log_id
, resulting_load_timing
.socket_log_id
);
1574 EXPECT_EQ(job_load_timing
.send_start
, resulting_load_timing
.send_start
);
1575 EXPECT_EQ(job_load_timing
.send_end
, resulting_load_timing
.send_end
);
1576 EXPECT_EQ(job_load_timing
.receive_headers_end
,
1577 resulting_load_timing
.receive_headers_end
);
1579 return resulting_load_timing
;
1582 // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1583 // reused. |connect_time_flags| is used to indicate if there should be dns
1584 // or SSL times, and |used_proxy| is used for proxy times.
1585 LoadTimingInfo
NormalLoadTimingInfo(base::TimeTicks now
,
1586 int connect_time_flags
,
1588 LoadTimingInfo load_timing
;
1589 load_timing
.socket_log_id
= 1;
1592 load_timing
.proxy_resolve_start
= now
+ base::TimeDelta::FromDays(1);
1593 load_timing
.proxy_resolve_end
= now
+ base::TimeDelta::FromDays(2);
1596 LoadTimingInfo::ConnectTiming
& connect_timing
= load_timing
.connect_timing
;
1597 if (connect_time_flags
& CONNECT_TIMING_HAS_DNS_TIMES
) {
1598 connect_timing
.dns_start
= now
+ base::TimeDelta::FromDays(3);
1599 connect_timing
.dns_end
= now
+ base::TimeDelta::FromDays(4);
1601 connect_timing
.connect_start
= now
+ base::TimeDelta::FromDays(5);
1602 if (connect_time_flags
& CONNECT_TIMING_HAS_SSL_TIMES
) {
1603 connect_timing
.ssl_start
= now
+ base::TimeDelta::FromDays(6);
1604 connect_timing
.ssl_end
= now
+ base::TimeDelta::FromDays(7);
1606 connect_timing
.connect_end
= now
+ base::TimeDelta::FromDays(8);
1608 load_timing
.send_start
= now
+ base::TimeDelta::FromDays(9);
1609 load_timing
.send_end
= now
+ base::TimeDelta::FromDays(10);
1610 load_timing
.receive_headers_end
= now
+ base::TimeDelta::FromDays(11);
1614 // Same as above, but in the case of a reused socket.
1615 LoadTimingInfo
NormalLoadTimingInfoReused(base::TimeTicks now
,
1617 LoadTimingInfo load_timing
;
1618 load_timing
.socket_log_id
= 1;
1619 load_timing
.socket_reused
= true;
1622 load_timing
.proxy_resolve_start
= now
+ base::TimeDelta::FromDays(1);
1623 load_timing
.proxy_resolve_end
= now
+ base::TimeDelta::FromDays(2);
1626 load_timing
.send_start
= now
+ base::TimeDelta::FromDays(9);
1627 load_timing
.send_end
= now
+ base::TimeDelta::FromDays(10);
1628 load_timing
.receive_headers_end
= now
+ base::TimeDelta::FromDays(11);
1632 // Basic test that the intercept + load timing tests work.
1633 TEST_F(URLRequestTest
, InterceptLoadTiming
) {
1634 base::TimeTicks now
= base::TimeTicks::Now();
1635 LoadTimingInfo job_load_timing
=
1636 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_DNS_TIMES
, false);
1638 LoadTimingInfo load_timing_result
=
1639 RunLoadTimingTest(job_load_timing
, &default_context_
);
1641 // Nothing should have been changed by the URLRequest.
1642 EXPECT_EQ(job_load_timing
.proxy_resolve_start
,
1643 load_timing_result
.proxy_resolve_start
);
1644 EXPECT_EQ(job_load_timing
.proxy_resolve_end
,
1645 load_timing_result
.proxy_resolve_end
);
1646 EXPECT_EQ(job_load_timing
.connect_timing
.dns_start
,
1647 load_timing_result
.connect_timing
.dns_start
);
1648 EXPECT_EQ(job_load_timing
.connect_timing
.dns_end
,
1649 load_timing_result
.connect_timing
.dns_end
);
1650 EXPECT_EQ(job_load_timing
.connect_timing
.connect_start
,
1651 load_timing_result
.connect_timing
.connect_start
);
1652 EXPECT_EQ(job_load_timing
.connect_timing
.connect_end
,
1653 load_timing_result
.connect_timing
.connect_end
);
1654 EXPECT_EQ(job_load_timing
.connect_timing
.ssl_start
,
1655 load_timing_result
.connect_timing
.ssl_start
);
1656 EXPECT_EQ(job_load_timing
.connect_timing
.ssl_end
,
1657 load_timing_result
.connect_timing
.ssl_end
);
1659 // Redundant sanity check.
1660 TestLoadTimingNotReused(load_timing_result
, CONNECT_TIMING_HAS_DNS_TIMES
);
1663 // Another basic test, with proxy and SSL times, but no DNS times.
1664 TEST_F(URLRequestTest
, InterceptLoadTimingProxy
) {
1665 base::TimeTicks now
= base::TimeTicks::Now();
1666 LoadTimingInfo job_load_timing
=
1667 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_SSL_TIMES
, true);
1669 LoadTimingInfo load_timing_result
=
1670 RunLoadTimingTest(job_load_timing
, &default_context_
);
1672 // Nothing should have been changed by the URLRequest.
1673 EXPECT_EQ(job_load_timing
.proxy_resolve_start
,
1674 load_timing_result
.proxy_resolve_start
);
1675 EXPECT_EQ(job_load_timing
.proxy_resolve_end
,
1676 load_timing_result
.proxy_resolve_end
);
1677 EXPECT_EQ(job_load_timing
.connect_timing
.dns_start
,
1678 load_timing_result
.connect_timing
.dns_start
);
1679 EXPECT_EQ(job_load_timing
.connect_timing
.dns_end
,
1680 load_timing_result
.connect_timing
.dns_end
);
1681 EXPECT_EQ(job_load_timing
.connect_timing
.connect_start
,
1682 load_timing_result
.connect_timing
.connect_start
);
1683 EXPECT_EQ(job_load_timing
.connect_timing
.connect_end
,
1684 load_timing_result
.connect_timing
.connect_end
);
1685 EXPECT_EQ(job_load_timing
.connect_timing
.ssl_start
,
1686 load_timing_result
.connect_timing
.ssl_start
);
1687 EXPECT_EQ(job_load_timing
.connect_timing
.ssl_end
,
1688 load_timing_result
.connect_timing
.ssl_end
);
1690 // Redundant sanity check.
1691 TestLoadTimingNotReusedWithProxy(load_timing_result
,
1692 CONNECT_TIMING_HAS_SSL_TIMES
);
1695 // Make sure that URLRequest correctly adjusts proxy times when they're before
1696 // |request_start|, due to already having a connected socket. This happens in
1697 // the case of reusing a SPDY session or HTTP pipeline. The connected socket is
1698 // not considered reused in this test (May be a preconnect).
1700 // To mix things up from the test above, assumes DNS times but no SSL times.
1701 TEST_F(URLRequestTest
, InterceptLoadTimingEarlyProxyResolution
) {
1702 base::TimeTicks now
= base::TimeTicks::Now();
1703 LoadTimingInfo job_load_timing
=
1704 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_DNS_TIMES
, true);
1705 job_load_timing
.proxy_resolve_start
= now
- base::TimeDelta::FromDays(6);
1706 job_load_timing
.proxy_resolve_end
= now
- base::TimeDelta::FromDays(5);
1707 job_load_timing
.connect_timing
.dns_start
= now
- base::TimeDelta::FromDays(4);
1708 job_load_timing
.connect_timing
.dns_end
= now
- base::TimeDelta::FromDays(3);
1709 job_load_timing
.connect_timing
.connect_start
=
1710 now
- base::TimeDelta::FromDays(2);
1711 job_load_timing
.connect_timing
.connect_end
=
1712 now
- base::TimeDelta::FromDays(1);
1714 LoadTimingInfo load_timing_result
=
1715 RunLoadTimingTest(job_load_timing
, &default_context_
);
1717 // Proxy times, connect times, and DNS times should all be replaced with
1719 EXPECT_EQ(load_timing_result
.request_start
,
1720 load_timing_result
.proxy_resolve_start
);
1721 EXPECT_EQ(load_timing_result
.request_start
,
1722 load_timing_result
.proxy_resolve_end
);
1723 EXPECT_EQ(load_timing_result
.request_start
,
1724 load_timing_result
.connect_timing
.dns_start
);
1725 EXPECT_EQ(load_timing_result
.request_start
,
1726 load_timing_result
.connect_timing
.dns_end
);
1727 EXPECT_EQ(load_timing_result
.request_start
,
1728 load_timing_result
.connect_timing
.connect_start
);
1729 EXPECT_EQ(load_timing_result
.request_start
,
1730 load_timing_result
.connect_timing
.connect_end
);
1732 // Other times should have been left null.
1733 TestLoadTimingNotReusedWithProxy(load_timing_result
,
1734 CONNECT_TIMING_HAS_DNS_TIMES
);
1737 // Same as above, but in the reused case.
1738 TEST_F(URLRequestTest
, InterceptLoadTimingEarlyProxyResolutionReused
) {
1739 base::TimeTicks now
= base::TimeTicks::Now();
1740 LoadTimingInfo job_load_timing
= NormalLoadTimingInfoReused(now
, true);
1741 job_load_timing
.proxy_resolve_start
= now
- base::TimeDelta::FromDays(4);
1742 job_load_timing
.proxy_resolve_end
= now
- base::TimeDelta::FromDays(3);
1744 LoadTimingInfo load_timing_result
=
1745 RunLoadTimingTest(job_load_timing
, &default_context_
);
1747 // Proxy times and connect times should all be replaced with request_start.
1748 EXPECT_EQ(load_timing_result
.request_start
,
1749 load_timing_result
.proxy_resolve_start
);
1750 EXPECT_EQ(load_timing_result
.request_start
,
1751 load_timing_result
.proxy_resolve_end
);
1753 // Other times should have been left null.
1754 TestLoadTimingReusedWithProxy(load_timing_result
);
1757 // Make sure that URLRequest correctly adjusts connect times when they're before
1758 // |request_start|, due to reusing a connected socket. The connected socket is
1759 // not considered reused in this test (May be a preconnect).
1761 // To mix things up, the request has SSL times, but no DNS times.
1762 TEST_F(URLRequestTest
, InterceptLoadTimingEarlyConnect
) {
1763 base::TimeTicks now
= base::TimeTicks::Now();
1764 LoadTimingInfo job_load_timing
=
1765 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_SSL_TIMES
, false);
1766 job_load_timing
.connect_timing
.connect_start
=
1767 now
- base::TimeDelta::FromDays(1);
1768 job_load_timing
.connect_timing
.ssl_start
= now
- base::TimeDelta::FromDays(2);
1769 job_load_timing
.connect_timing
.ssl_end
= now
- base::TimeDelta::FromDays(3);
1770 job_load_timing
.connect_timing
.connect_end
=
1771 now
- base::TimeDelta::FromDays(4);
1773 LoadTimingInfo load_timing_result
=
1774 RunLoadTimingTest(job_load_timing
, &default_context_
);
1776 // Connect times, and SSL times should be replaced with request_start.
1777 EXPECT_EQ(load_timing_result
.request_start
,
1778 load_timing_result
.connect_timing
.connect_start
);
1779 EXPECT_EQ(load_timing_result
.request_start
,
1780 load_timing_result
.connect_timing
.ssl_start
);
1781 EXPECT_EQ(load_timing_result
.request_start
,
1782 load_timing_result
.connect_timing
.ssl_end
);
1783 EXPECT_EQ(load_timing_result
.request_start
,
1784 load_timing_result
.connect_timing
.connect_end
);
1786 // Other times should have been left null.
1787 TestLoadTimingNotReused(load_timing_result
, CONNECT_TIMING_HAS_SSL_TIMES
);
1790 // Make sure that URLRequest correctly adjusts connect times when they're before
1791 // |request_start|, due to reusing a connected socket in the case that there
1792 // are also proxy times. The connected socket is not considered reused in this
1793 // test (May be a preconnect).
1795 // In this test, there are no SSL or DNS times.
1796 TEST_F(URLRequestTest
, InterceptLoadTimingEarlyConnectWithProxy
) {
1797 base::TimeTicks now
= base::TimeTicks::Now();
1798 LoadTimingInfo job_load_timing
=
1799 NormalLoadTimingInfo(now
, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY
, true);
1800 job_load_timing
.connect_timing
.connect_start
=
1801 now
- base::TimeDelta::FromDays(1);
1802 job_load_timing
.connect_timing
.connect_end
=
1803 now
- base::TimeDelta::FromDays(2);
1805 LoadTimingInfo load_timing_result
=
1806 RunLoadTimingTest(job_load_timing
, &default_context_
);
1808 // Connect times should be replaced with proxy_resolve_end.
1809 EXPECT_EQ(load_timing_result
.proxy_resolve_end
,
1810 load_timing_result
.connect_timing
.connect_start
);
1811 EXPECT_EQ(load_timing_result
.proxy_resolve_end
,
1812 load_timing_result
.connect_timing
.connect_end
);
1814 // Other times should have been left null.
1815 TestLoadTimingNotReusedWithProxy(load_timing_result
,
1816 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY
);
1819 // Check that two different URL requests have different identifiers.
1820 TEST_F(URLRequestTest
, Identifiers
) {
1822 TestURLRequestContext context
;
1824 GURL("http://example.com"), DEFAULT_PRIORITY
, &d
, &context
);
1825 TestURLRequest
other_req(
1826 GURL("http://example.com"), DEFAULT_PRIORITY
, &d
, &context
);
1828 ASSERT_NE(req
.identifier(), other_req
.identifier());
1831 // Check that a failure to connect to the proxy is reported to the network
1833 TEST_F(URLRequestTest
, NetworkDelegateProxyError
) {
1834 MockHostResolver host_resolver
;
1835 host_resolver
.rules()->AddSimulatedFailure("*");
1837 TestNetworkDelegate network_delegate
; // Must outlive URLRequests.
1838 TestURLRequestContextWithProxy
context("myproxy:70", &network_delegate
);
1841 URLRequest
req(GURL("http://example.com"), DEFAULT_PRIORITY
, &d
, &context
);
1842 req
.set_method("GET");
1845 base::RunLoop().Run();
1847 // Check we see a failed request.
1848 EXPECT_FALSE(req
.status().is_success());
1849 EXPECT_EQ(URLRequestStatus::FAILED
, req
.status().status());
1850 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED
, req
.status().error());
1852 EXPECT_EQ(1, network_delegate
.error_count());
1853 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED
, network_delegate
.last_error());
1854 EXPECT_EQ(1, network_delegate
.completed_requests());
1857 // Make sure that net::NetworkDelegate::NotifyCompleted is called if
1858 // content is empty.
1859 TEST_F(URLRequestTest
, RequestCompletionForEmptyResponse
) {
1861 URLRequest
req(GURL("data:,"), DEFAULT_PRIORITY
, &d
, &default_context_
);
1863 base::RunLoop().Run();
1864 EXPECT_EQ("", d
.data_received());
1865 EXPECT_EQ(1, default_network_delegate_
.completed_requests());
1868 // Make sure that SetPriority actually sets the URLRequest's priority
1869 // correctly, both before and after start.
1870 TEST_F(URLRequestTest
, SetPriorityBasic
) {
1872 URLRequest
req(GURL("http://test_intercept/foo"),
1876 EXPECT_EQ(DEFAULT_PRIORITY
, req
.priority());
1878 req
.SetPriority(LOW
);
1879 EXPECT_EQ(LOW
, req
.priority());
1882 EXPECT_EQ(LOW
, req
.priority());
1884 req
.SetPriority(MEDIUM
);
1885 EXPECT_EQ(MEDIUM
, req
.priority());
1888 // Make sure that URLRequest calls SetPriority on a job before calling
1890 TEST_F(URLRequestTest
, SetJobPriorityBeforeJobStart
) {
1892 URLRequest
req(GURL("http://test_intercept/foo"),
1896 EXPECT_EQ(DEFAULT_PRIORITY
, req
.priority());
1898 scoped_refptr
<URLRequestTestJob
> job
=
1899 new URLRequestTestJob(&req
, &default_network_delegate_
);
1900 AddTestInterceptor()->set_main_intercept_job(job
.get());
1901 EXPECT_EQ(DEFAULT_PRIORITY
, job
->priority());
1903 req
.SetPriority(LOW
);
1906 EXPECT_EQ(LOW
, job
->priority());
1909 // Make sure that URLRequest passes on its priority updates to its
1911 TEST_F(URLRequestTest
, SetJobPriority
) {
1913 URLRequest
req(GURL("http://test_intercept/foo"),
1918 scoped_refptr
<URLRequestTestJob
> job
=
1919 new URLRequestTestJob(&req
, &default_network_delegate_
);
1920 AddTestInterceptor()->set_main_intercept_job(job
.get());
1922 req
.SetPriority(LOW
);
1924 EXPECT_EQ(LOW
, job
->priority());
1926 req
.SetPriority(MEDIUM
);
1927 EXPECT_EQ(MEDIUM
, req
.priority());
1928 EXPECT_EQ(MEDIUM
, job
->priority());
1931 // Setting the IGNORE_LIMITS load flag should be okay if the priority
1932 // is MAXIMUM_PRIORITY.
1933 TEST_F(URLRequestTest
, PriorityIgnoreLimits
) {
1935 URLRequest
req(GURL("http://test_intercept/foo"),
1939 EXPECT_EQ(MAXIMUM_PRIORITY
, req
.priority());
1941 scoped_refptr
<URLRequestTestJob
> job
=
1942 new URLRequestTestJob(&req
, &default_network_delegate_
);
1943 AddTestInterceptor()->set_main_intercept_job(job
.get());
1945 req
.SetLoadFlags(LOAD_IGNORE_LIMITS
);
1946 EXPECT_EQ(MAXIMUM_PRIORITY
, req
.priority());
1948 req
.SetPriority(MAXIMUM_PRIORITY
);
1949 EXPECT_EQ(MAXIMUM_PRIORITY
, req
.priority());
1952 EXPECT_EQ(MAXIMUM_PRIORITY
, req
.priority());
1953 EXPECT_EQ(MAXIMUM_PRIORITY
, job
->priority());
1956 // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
1957 #if !defined(OS_IOS)
1958 // A subclass of SpawnedTestServer that uses a statically-configured hostname.
1959 // This is to work around mysterious failures in chrome_frame_net_tests. See:
1960 // http://crbug.com/114369
1961 // TODO(erikwright): remove or update as needed; see http://crbug.com/334634.
1962 class LocalHttpTestServer
: public SpawnedTestServer
{
1964 explicit LocalHttpTestServer(const base::FilePath
& document_root
)
1965 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
1966 ScopedCustomUrlRequestTestHttpHost::value(),
1968 LocalHttpTestServer()
1969 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP
,
1970 ScopedCustomUrlRequestTestHttpHost::value(),
1971 base::FilePath()) {}
1974 TEST_F(URLRequestTest
, DelayedCookieCallback
) {
1975 LocalHttpTestServer test_server
;
1976 ASSERT_TRUE(test_server
.Start());
1978 TestURLRequestContext context
;
1979 scoped_refptr
<DelayedCookieMonster
> delayed_cm
=
1980 new DelayedCookieMonster();
1981 scoped_refptr
<CookieStore
> cookie_store
= delayed_cm
;
1982 context
.set_cookie_store(delayed_cm
.get());
1986 TestNetworkDelegate network_delegate
;
1987 context
.set_network_delegate(&network_delegate
);
1989 URLRequest
req(test_server
.GetURL("set-cookie?CookieToNotSend=1"),
1994 base::RunLoop().Run();
1995 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
1996 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
1997 EXPECT_EQ(1, network_delegate
.set_cookie_count());
2000 // Verify that the cookie is set.
2002 TestNetworkDelegate network_delegate
;
2003 context
.set_network_delegate(&network_delegate
);
2005 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2010 base::RunLoop().Run();
2012 EXPECT_TRUE(d
.data_received().find("CookieToNotSend=1")
2013 != std::string::npos
);
2014 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2015 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2019 TEST_F(URLRequestTest
, DoNotSendCookies
) {
2020 LocalHttpTestServer test_server
;
2021 ASSERT_TRUE(test_server
.Start());
2025 TestNetworkDelegate network_delegate
;
2026 default_context_
.set_network_delegate(&network_delegate
);
2028 URLRequest
req(test_server
.GetURL("set-cookie?CookieToNotSend=1"),
2033 base::RunLoop().Run();
2034 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2035 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2038 // Verify that the cookie is set.
2040 TestNetworkDelegate network_delegate
;
2041 default_context_
.set_network_delegate(&network_delegate
);
2043 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2048 base::RunLoop().Run();
2050 EXPECT_TRUE(d
.data_received().find("CookieToNotSend=1")
2051 != std::string::npos
);
2052 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2053 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2056 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2058 TestNetworkDelegate network_delegate
;
2059 default_context_
.set_network_delegate(&network_delegate
);
2061 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2065 req
.SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES
);
2067 base::RunLoop().Run();
2069 EXPECT_TRUE(d
.data_received().find("Cookie: CookieToNotSend=1")
2070 == std::string::npos
);
2072 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2073 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2074 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2078 TEST_F(URLRequestTest
, DoNotSaveCookies
) {
2079 LocalHttpTestServer test_server
;
2080 ASSERT_TRUE(test_server
.Start());
2084 TestNetworkDelegate network_delegate
;
2085 default_context_
.set_network_delegate(&network_delegate
);
2087 URLRequest
req(test_server
.GetURL("set-cookie?CookieToNotUpdate=2"),
2092 base::RunLoop().Run();
2094 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2095 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2096 EXPECT_EQ(1, network_delegate
.set_cookie_count());
2099 // Try to set-up another cookie and update the previous cookie.
2101 TestNetworkDelegate network_delegate
;
2102 default_context_
.set_network_delegate(&network_delegate
);
2105 test_server
.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2109 req
.SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES
);
2112 base::RunLoop().Run();
2114 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2115 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2116 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2117 EXPECT_EQ(0, network_delegate
.set_cookie_count());
2120 // Verify the cookies weren't saved or updated.
2122 TestNetworkDelegate network_delegate
;
2123 default_context_
.set_network_delegate(&network_delegate
);
2125 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2130 base::RunLoop().Run();
2132 EXPECT_TRUE(d
.data_received().find("CookieToNotSave=1")
2133 == std::string::npos
);
2134 EXPECT_TRUE(d
.data_received().find("CookieToNotUpdate=2")
2135 != std::string::npos
);
2137 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2138 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2139 EXPECT_EQ(0, network_delegate
.set_cookie_count());
2143 TEST_F(URLRequestTest
, DoNotSendCookies_ViaPolicy
) {
2144 LocalHttpTestServer test_server
;
2145 ASSERT_TRUE(test_server
.Start());
2149 TestNetworkDelegate network_delegate
;
2150 default_context_
.set_network_delegate(&network_delegate
);
2152 URLRequest
req(test_server
.GetURL("set-cookie?CookieToNotSend=1"),
2157 base::RunLoop().Run();
2159 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2160 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2163 // Verify that the cookie is set.
2165 TestNetworkDelegate network_delegate
;
2166 default_context_
.set_network_delegate(&network_delegate
);
2168 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2173 base::RunLoop().Run();
2175 EXPECT_TRUE(d
.data_received().find("CookieToNotSend=1")
2176 != std::string::npos
);
2178 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2179 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2182 // Verify that the cookie isn't sent.
2184 TestNetworkDelegate network_delegate
;
2185 default_context_
.set_network_delegate(&network_delegate
);
2187 network_delegate
.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES
);
2188 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2193 base::RunLoop().Run();
2195 EXPECT_TRUE(d
.data_received().find("Cookie: CookieToNotSend=1")
2196 == std::string::npos
);
2198 EXPECT_EQ(1, network_delegate
.blocked_get_cookies_count());
2199 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2203 TEST_F(URLRequestTest
, DoNotSaveCookies_ViaPolicy
) {
2204 LocalHttpTestServer test_server
;
2205 ASSERT_TRUE(test_server
.Start());
2209 TestNetworkDelegate network_delegate
;
2210 default_context_
.set_network_delegate(&network_delegate
);
2212 URLRequest
req(test_server
.GetURL("set-cookie?CookieToNotUpdate=2"),
2217 base::RunLoop().Run();
2219 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2220 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2223 // Try to set-up another cookie and update the previous cookie.
2225 TestNetworkDelegate network_delegate
;
2226 default_context_
.set_network_delegate(&network_delegate
);
2228 network_delegate
.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE
);
2230 test_server
.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2236 base::RunLoop().Run();
2238 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2239 EXPECT_EQ(2, network_delegate
.blocked_set_cookie_count());
2242 // Verify the cookies weren't saved or updated.
2244 TestNetworkDelegate network_delegate
;
2245 default_context_
.set_network_delegate(&network_delegate
);
2247 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2252 base::RunLoop().Run();
2254 EXPECT_TRUE(d
.data_received().find("CookieToNotSave=1")
2255 == std::string::npos
);
2256 EXPECT_TRUE(d
.data_received().find("CookieToNotUpdate=2")
2257 != std::string::npos
);
2259 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2260 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2264 TEST_F(URLRequestTest
, DoNotSaveEmptyCookies
) {
2265 LocalHttpTestServer test_server
;
2266 ASSERT_TRUE(test_server
.Start());
2268 // Set up an empty cookie.
2270 TestNetworkDelegate network_delegate
;
2271 default_context_
.set_network_delegate(&network_delegate
);
2273 URLRequest
req(test_server
.GetURL("set-cookie"),
2278 base::RunLoop().Run();
2280 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2281 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2282 EXPECT_EQ(0, network_delegate
.set_cookie_count());
2286 TEST_F(URLRequestTest
, DoNotSendCookies_ViaPolicy_Async
) {
2287 LocalHttpTestServer test_server
;
2288 ASSERT_TRUE(test_server
.Start());
2292 TestNetworkDelegate network_delegate
;
2293 default_context_
.set_network_delegate(&network_delegate
);
2295 URLRequest
req(test_server
.GetURL("set-cookie?CookieToNotSend=1"),
2300 base::RunLoop().Run();
2302 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2303 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2306 // Verify that the cookie is set.
2308 TestNetworkDelegate network_delegate
;
2309 default_context_
.set_network_delegate(&network_delegate
);
2311 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2316 base::RunLoop().Run();
2318 EXPECT_TRUE(d
.data_received().find("CookieToNotSend=1")
2319 != std::string::npos
);
2321 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2322 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2325 // Verify that the cookie isn't sent.
2327 TestNetworkDelegate network_delegate
;
2328 default_context_
.set_network_delegate(&network_delegate
);
2330 network_delegate
.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES
);
2331 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2336 base::RunLoop().Run();
2338 EXPECT_TRUE(d
.data_received().find("Cookie: CookieToNotSend=1")
2339 == std::string::npos
);
2341 EXPECT_EQ(1, network_delegate
.blocked_get_cookies_count());
2342 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2346 TEST_F(URLRequestTest
, DoNotSaveCookies_ViaPolicy_Async
) {
2347 LocalHttpTestServer test_server
;
2348 ASSERT_TRUE(test_server
.Start());
2352 TestNetworkDelegate network_delegate
;
2353 default_context_
.set_network_delegate(&network_delegate
);
2355 URLRequest
req(test_server
.GetURL("set-cookie?CookieToNotUpdate=2"),
2360 base::RunLoop().Run();
2362 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2363 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2366 // Try to set-up another cookie and update the previous cookie.
2368 TestNetworkDelegate network_delegate
;
2369 default_context_
.set_network_delegate(&network_delegate
);
2371 network_delegate
.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE
);
2373 test_server
.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2379 base::RunLoop().Run();
2381 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2382 EXPECT_EQ(2, network_delegate
.blocked_set_cookie_count());
2385 // Verify the cookies weren't saved or updated.
2387 TestNetworkDelegate network_delegate
;
2388 default_context_
.set_network_delegate(&network_delegate
);
2390 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2395 base::RunLoop().Run();
2397 EXPECT_TRUE(d
.data_received().find("CookieToNotSave=1")
2398 == std::string::npos
);
2399 EXPECT_TRUE(d
.data_received().find("CookieToNotUpdate=2")
2400 != std::string::npos
);
2402 EXPECT_EQ(0, network_delegate
.blocked_get_cookies_count());
2403 EXPECT_EQ(0, network_delegate
.blocked_set_cookie_count());
2407 // FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2408 // value for the |fixed_date| argument given to the constructor.
2409 class FixedDateNetworkDelegate
: public TestNetworkDelegate
{
2411 explicit FixedDateNetworkDelegate(const std::string
& fixed_date
)
2412 : fixed_date_(fixed_date
) {}
2413 virtual ~FixedDateNetworkDelegate() {}
2415 // net::NetworkDelegate implementation
2416 virtual int OnHeadersReceived(
2417 net::URLRequest
* request
,
2418 const net::CompletionCallback
& callback
,
2419 const net::HttpResponseHeaders
* original_response_headers
,
2420 scoped_refptr
<net::HttpResponseHeaders
>* override_response_headers
)
2424 std::string fixed_date_
;
2426 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate
);
2429 int FixedDateNetworkDelegate::OnHeadersReceived(
2430 net::URLRequest
* request
,
2431 const net::CompletionCallback
& callback
,
2432 const net::HttpResponseHeaders
* original_response_headers
,
2433 scoped_refptr
<net::HttpResponseHeaders
>* override_response_headers
) {
2434 net::HttpResponseHeaders
* new_response_headers
=
2435 new net::HttpResponseHeaders(original_response_headers
->raw_headers());
2437 new_response_headers
->RemoveHeader("Date");
2438 new_response_headers
->AddHeader("Date: " + fixed_date_
);
2440 *override_response_headers
= new_response_headers
;
2441 return TestNetworkDelegate::OnHeadersReceived(request
,
2443 original_response_headers
,
2444 override_response_headers
);
2447 // Test that cookie expiration times are adjusted for server/client clock
2448 // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2449 // headers by defaulting to GMT. (crbug.com/135131)
2450 TEST_F(URLRequestTest
, AcceptClockSkewCookieWithWrongDateTimezone
) {
2451 LocalHttpTestServer test_server
;
2452 ASSERT_TRUE(test_server
.Start());
2454 // Set up an expired cookie.
2456 TestNetworkDelegate network_delegate
;
2457 default_context_
.set_network_delegate(&network_delegate
);
2461 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2466 base::RunLoop().Run();
2468 // Verify that the cookie is not set.
2470 TestNetworkDelegate network_delegate
;
2471 default_context_
.set_network_delegate(&network_delegate
);
2473 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2478 base::RunLoop().Run();
2480 EXPECT_TRUE(d
.data_received().find("StillGood=1") == std::string::npos
);
2482 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2484 FixedDateNetworkDelegate
network_delegate("18-Apr-1977 22:49:13 UTC");
2485 default_context_
.set_network_delegate(&network_delegate
);
2489 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2494 base::RunLoop().Run();
2496 // Verify that the cookie is set.
2498 TestNetworkDelegate network_delegate
;
2499 default_context_
.set_network_delegate(&network_delegate
);
2501 URLRequest
req(test_server
.GetURL("echoheader?Cookie"),
2506 base::RunLoop().Run();
2508 EXPECT_TRUE(d
.data_received().find("StillGood=1") != std::string::npos
);
2513 // Check that it is impossible to change the referrer in the extra headers of
2515 TEST_F(URLRequestTest
, DoNotOverrideReferrer
) {
2516 LocalHttpTestServer test_server
;
2517 ASSERT_TRUE(test_server
.Start());
2519 // If extra headers contain referer and the request contains a referer,
2520 // only the latter shall be respected.
2523 URLRequest
req(test_server
.GetURL("echoheader?Referer"),
2527 req
.SetReferrer("http://foo.com/");
2529 HttpRequestHeaders headers
;
2530 headers
.SetHeader(HttpRequestHeaders::kReferer
, "http://bar.com/");
2531 req
.SetExtraRequestHeaders(headers
);
2534 base::RunLoop().Run();
2536 EXPECT_EQ("http://foo.com/", d
.data_received());
2539 // If extra headers contain a referer but the request does not, no referer
2540 // shall be sent in the header.
2543 URLRequest
req(test_server
.GetURL("echoheader?Referer"),
2548 HttpRequestHeaders headers
;
2549 headers
.SetHeader(HttpRequestHeaders::kReferer
, "http://bar.com/");
2550 req
.SetExtraRequestHeaders(headers
);
2551 req
.SetLoadFlags(LOAD_VALIDATE_CACHE
);
2554 base::RunLoop().Run();
2556 EXPECT_EQ("None", d
.data_received());
2560 class URLRequestTestHTTP
: public URLRequestTest
{
2562 URLRequestTestHTTP()
2563 : test_server_(base::FilePath(FILE_PATH_LITERAL(
2564 "net/data/url_request_unittest"))) {
2568 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2569 // |request_method| is the method to use for the initial request.
2570 // |redirect_method| is the method that is expected to be used for the second
2571 // request, after redirection.
2572 // If |include_data| is true, data is uploaded with the request. The
2573 // response body is expected to match it exactly, if and only if
2574 // |request_method| == |redirect_method|.
2575 void HTTPRedirectMethodTest(const GURL
& redirect_url
,
2576 const std::string
& request_method
,
2577 const std::string
& redirect_method
,
2578 bool include_data
) {
2579 static const char kData
[] = "hello world";
2581 URLRequest
req(redirect_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
2582 req
.set_method(request_method
);
2584 req
.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData
)));
2585 HttpRequestHeaders headers
;
2586 headers
.SetHeader(HttpRequestHeaders::kContentLength
,
2587 base::UintToString(arraysize(kData
) - 1));
2588 req
.SetExtraRequestHeaders(headers
);
2591 base::RunLoop().Run();
2592 EXPECT_EQ(redirect_method
, req
.method());
2593 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
2594 EXPECT_EQ(OK
, req
.status().error());
2596 if (request_method
== redirect_method
) {
2597 EXPECT_EQ(kData
, d
.data_received());
2599 EXPECT_NE(kData
, d
.data_received());
2603 LOG(WARNING
) << "Request method was: " << request_method
;
2606 void HTTPUploadDataOperationTest(const std::string
& method
) {
2607 const int kMsgSize
= 20000; // multiple of 10
2608 const int kIterations
= 50;
2609 char* uploadBytes
= new char[kMsgSize
+1];
2610 char* ptr
= uploadBytes
;
2612 for (int idx
= 0; idx
< kMsgSize
/10; idx
++) {
2613 memcpy(ptr
, "----------", 10);
2615 if (idx
% 100 == 0) {
2622 uploadBytes
[kMsgSize
] = '\0';
2624 for (int i
= 0; i
< kIterations
; ++i
) {
2627 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
, &default_context_
);
2628 r
.set_method(method
.c_str());
2630 r
.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes
)));
2633 EXPECT_TRUE(r
.is_pending());
2635 base::RunLoop().Run();
2637 ASSERT_EQ(1, d
.response_started_count())
2638 << "request failed: " << r
.status().status()
2639 << ", os error: " << r
.status().error();
2641 EXPECT_FALSE(d
.received_data_before_response());
2642 EXPECT_EQ(uploadBytes
, d
.data_received());
2644 delete[] uploadBytes
;
2647 void AddChunksToUpload(URLRequest
* r
) {
2648 r
->AppendChunkToUpload("a", 1, false);
2649 r
->AppendChunkToUpload("bcd", 3, false);
2650 r
->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2651 r
->AppendChunkToUpload("\r\n\r\n", 4, false);
2652 r
->AppendChunkToUpload("0", 1, false);
2653 r
->AppendChunkToUpload("2323", 4, true);
2656 void VerifyReceivedDataMatchesChunks(URLRequest
* r
, TestDelegate
* d
) {
2657 // This should match the chunks sent by AddChunksToUpload().
2658 const std::string expected_data
=
2659 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2661 ASSERT_EQ(1, d
->response_started_count())
2662 << "request failed: " << r
->status().status()
2663 << ", os error: " << r
->status().error();
2665 EXPECT_FALSE(d
->received_data_before_response());
2667 EXPECT_EQ(expected_data
.size(), static_cast<size_t>(d
->bytes_received()));
2668 EXPECT_EQ(expected_data
, d
->data_received());
2671 bool DoManyCookiesRequest(int num_cookies
) {
2673 URLRequest
r(test_server_
.GetURL("set-many-cookies?" +
2674 base::IntToString(num_cookies
)),
2680 EXPECT_TRUE(r
.is_pending());
2682 base::RunLoop().Run();
2684 bool is_success
= r
.status().is_success();
2687 EXPECT_TRUE(r
.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG
);
2688 // The test server appears to be unable to handle subsequent requests
2689 // after this error is triggered. Force it to restart.
2690 EXPECT_TRUE(test_server_
.Stop());
2691 EXPECT_TRUE(test_server_
.Start());
2697 LocalHttpTestServer test_server_
;
2700 // In this unit test, we're using the HTTPTestServer as a proxy server and
2701 // issuing a CONNECT request with the magic host name "www.redirect.com".
2702 // The HTTPTestServer will return a 302 response, which we should not
2704 TEST_F(URLRequestTestHTTP
, ProxyTunnelRedirectTest
) {
2705 ASSERT_TRUE(test_server_
.Start());
2707 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
2708 TestURLRequestContextWithProxy
context(
2709 test_server_
.host_port_pair().ToString(), &network_delegate
);
2714 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY
, &d
, &context
);
2716 EXPECT_TRUE(r
.is_pending());
2718 base::RunLoop().Run();
2720 EXPECT_EQ(URLRequestStatus::FAILED
, r
.status().status());
2721 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED
, r
.status().error());
2722 EXPECT_EQ(1, d
.response_started_count());
2723 // We should not have followed the redirect.
2724 EXPECT_EQ(0, d
.received_redirect_count());
2728 // This is the same as the previous test, but checks that the network delegate
2729 // registers the error.
2730 TEST_F(URLRequestTestHTTP
, NetworkDelegateTunnelConnectionFailed
) {
2731 ASSERT_TRUE(test_server_
.Start());
2733 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
2734 TestURLRequestContextWithProxy
context(
2735 test_server_
.host_port_pair().ToString(), &network_delegate
);
2740 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY
, &d
, &context
);
2742 EXPECT_TRUE(r
.is_pending());
2744 base::RunLoop().Run();
2746 EXPECT_EQ(URLRequestStatus::FAILED
, r
.status().status());
2747 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED
, r
.status().error());
2748 EXPECT_EQ(1, d
.response_started_count());
2749 // We should not have followed the redirect.
2750 EXPECT_EQ(0, d
.received_redirect_count());
2752 EXPECT_EQ(1, network_delegate
.error_count());
2753 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED
, network_delegate
.last_error());
2757 // Tests that we can block and asynchronously return OK in various stages.
2758 TEST_F(URLRequestTestHTTP
, NetworkDelegateBlockAsynchronously
) {
2759 static const BlockingNetworkDelegate::Stage blocking_stages
[] = {
2760 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
,
2761 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
,
2762 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
2764 static const size_t blocking_stages_length
= arraysize(blocking_stages
);
2766 ASSERT_TRUE(test_server_
.Start());
2769 BlockingNetworkDelegate
network_delegate(
2770 BlockingNetworkDelegate::USER_CALLBACK
);
2771 network_delegate
.set_block_on(
2772 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
|
2773 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
|
2774 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
);
2776 TestURLRequestContext
context(true);
2777 context
.set_network_delegate(&network_delegate
);
2782 test_server_
.GetURL("empty.html"), DEFAULT_PRIORITY
, &d
, &context
);
2785 for (size_t i
= 0; i
< blocking_stages_length
; ++i
) {
2786 base::RunLoop().Run();
2787 EXPECT_EQ(blocking_stages
[i
],
2788 network_delegate
.stage_blocked_for_callback());
2789 network_delegate
.DoCallback(OK
);
2791 base::RunLoop().Run();
2792 EXPECT_EQ(200, r
.GetResponseCode());
2793 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
2794 EXPECT_EQ(1, network_delegate
.created_requests());
2795 EXPECT_EQ(0, network_delegate
.destroyed_requests());
2797 EXPECT_EQ(1, network_delegate
.destroyed_requests());
2800 // Tests that the network delegate can block and cancel a request.
2801 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequest
) {
2802 ASSERT_TRUE(test_server_
.Start());
2805 BlockingNetworkDelegate
network_delegate(
2806 BlockingNetworkDelegate::AUTO_CALLBACK
);
2807 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
);
2808 network_delegate
.set_retval(ERR_EMPTY_RESPONSE
);
2810 TestURLRequestContextWithProxy
context(
2811 test_server_
.host_port_pair().ToString(), &network_delegate
);
2815 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
, &context
);
2818 base::RunLoop().Run();
2820 EXPECT_EQ(URLRequestStatus::FAILED
, r
.status().status());
2821 EXPECT_EQ(ERR_EMPTY_RESPONSE
, r
.status().error());
2822 EXPECT_EQ(1, network_delegate
.created_requests());
2823 EXPECT_EQ(0, network_delegate
.destroyed_requests());
2825 EXPECT_EQ(1, network_delegate
.destroyed_requests());
2828 // Helper function for NetworkDelegateCancelRequestAsynchronously and
2829 // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
2830 // delegate operating in |block_mode| and a request for |url|. It blocks the
2831 // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
2832 void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode
,
2833 BlockingNetworkDelegate::Stage stage
,
2836 BlockingNetworkDelegate
network_delegate(block_mode
);
2837 network_delegate
.set_retval(ERR_BLOCKED_BY_CLIENT
);
2838 network_delegate
.set_block_on(stage
);
2840 TestURLRequestContext
context(true);
2841 context
.set_network_delegate(&network_delegate
);
2845 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
2848 base::RunLoop().Run();
2850 EXPECT_EQ(URLRequestStatus::FAILED
, r
.status().status());
2851 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT
, r
.status().error());
2852 EXPECT_EQ(1, network_delegate
.created_requests());
2853 EXPECT_EQ(0, network_delegate
.destroyed_requests());
2855 EXPECT_EQ(1, network_delegate
.destroyed_requests());
2858 // The following 3 tests check that the network delegate can cancel a request
2859 // synchronously in various stages of the request.
2860 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestSynchronously1
) {
2861 ASSERT_TRUE(test_server_
.Start());
2862 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS
,
2863 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
,
2864 test_server_
.GetURL(std::string()));
2867 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestSynchronously2
) {
2868 ASSERT_TRUE(test_server_
.Start());
2869 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS
,
2870 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
,
2871 test_server_
.GetURL(std::string()));
2874 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestSynchronously3
) {
2875 ASSERT_TRUE(test_server_
.Start());
2876 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS
,
2877 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
,
2878 test_server_
.GetURL(std::string()));
2881 // The following 3 tests check that the network delegate can cancel a request
2882 // asynchronously in various stages of the request.
2883 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestAsynchronously1
) {
2884 ASSERT_TRUE(test_server_
.Start());
2885 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK
,
2886 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
,
2887 test_server_
.GetURL(std::string()));
2890 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestAsynchronously2
) {
2891 ASSERT_TRUE(test_server_
.Start());
2892 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK
,
2893 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
,
2894 test_server_
.GetURL(std::string()));
2897 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelRequestAsynchronously3
) {
2898 ASSERT_TRUE(test_server_
.Start());
2899 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK
,
2900 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
,
2901 test_server_
.GetURL(std::string()));
2904 // Tests that the network delegate can block and redirect a request to a new
2906 TEST_F(URLRequestTestHTTP
, NetworkDelegateRedirectRequest
) {
2907 ASSERT_TRUE(test_server_
.Start());
2910 BlockingNetworkDelegate
network_delegate(
2911 BlockingNetworkDelegate::AUTO_CALLBACK
);
2912 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
);
2913 GURL
redirect_url(test_server_
.GetURL("simple.html"));
2914 network_delegate
.set_redirect_url(redirect_url
);
2916 TestURLRequestContextWithProxy
context(
2917 test_server_
.host_port_pair().ToString(), &network_delegate
);
2920 GURL
original_url(test_server_
.GetURL("empty.html"));
2921 URLRequest
r(original_url
, DEFAULT_PRIORITY
, &d
, &context
);
2924 base::RunLoop().Run();
2926 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
2927 EXPECT_EQ(0, r
.status().error());
2928 EXPECT_EQ(redirect_url
, r
.url());
2929 EXPECT_EQ(original_url
, r
.original_url());
2930 EXPECT_EQ(2U, r
.url_chain().size());
2931 EXPECT_EQ(1, network_delegate
.created_requests());
2932 EXPECT_EQ(0, network_delegate
.destroyed_requests());
2934 EXPECT_EQ(1, network_delegate
.destroyed_requests());
2937 // Tests that the network delegate can block and redirect a request to a new
2938 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
2939 TEST_F(URLRequestTestHTTP
, NetworkDelegateRedirectRequestSynchronously
) {
2940 ASSERT_TRUE(test_server_
.Start());
2943 BlockingNetworkDelegate
network_delegate(
2944 BlockingNetworkDelegate::SYNCHRONOUS
);
2945 GURL
redirect_url(test_server_
.GetURL("simple.html"));
2946 network_delegate
.set_redirect_url(redirect_url
);
2948 TestURLRequestContextWithProxy
context(
2949 test_server_
.host_port_pair().ToString(), &network_delegate
);
2952 GURL
original_url(test_server_
.GetURL("empty.html"));
2953 URLRequest
r(original_url
, DEFAULT_PRIORITY
, &d
, &context
);
2956 base::RunLoop().Run();
2958 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
2959 EXPECT_EQ(0, r
.status().error());
2960 EXPECT_EQ(redirect_url
, r
.url());
2961 EXPECT_EQ(original_url
, r
.original_url());
2962 EXPECT_EQ(2U, r
.url_chain().size());
2963 EXPECT_EQ(1, network_delegate
.created_requests());
2964 EXPECT_EQ(0, network_delegate
.destroyed_requests());
2966 EXPECT_EQ(1, network_delegate
.destroyed_requests());
2969 // Tests that redirects caused by the network delegate preserve POST data.
2970 TEST_F(URLRequestTestHTTP
, NetworkDelegateRedirectRequestPost
) {
2971 ASSERT_TRUE(test_server_
.Start());
2973 const char kData
[] = "hello world";
2976 BlockingNetworkDelegate
network_delegate(
2977 BlockingNetworkDelegate::AUTO_CALLBACK
);
2978 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
);
2979 GURL
redirect_url(test_server_
.GetURL("echo"));
2980 network_delegate
.set_redirect_url(redirect_url
);
2982 TestURLRequestContext
context(true);
2983 context
.set_network_delegate(&network_delegate
);
2987 GURL
original_url(test_server_
.GetURL("empty.html"));
2988 URLRequest
r(original_url
, DEFAULT_PRIORITY
, &d
, &context
);
2989 r
.set_method("POST");
2990 r
.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData
)));
2991 HttpRequestHeaders headers
;
2992 headers
.SetHeader(HttpRequestHeaders::kContentLength
,
2993 base::UintToString(arraysize(kData
) - 1));
2994 r
.SetExtraRequestHeaders(headers
);
2996 base::RunLoop().Run();
2998 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
2999 EXPECT_EQ(0, r
.status().error());
3000 EXPECT_EQ(redirect_url
, r
.url());
3001 EXPECT_EQ(original_url
, r
.original_url());
3002 EXPECT_EQ(2U, r
.url_chain().size());
3003 EXPECT_EQ(1, network_delegate
.created_requests());
3004 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3005 EXPECT_EQ("POST", r
.method());
3006 EXPECT_EQ(kData
, d
.data_received());
3008 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3011 // Tests that the network delegate can synchronously complete OnAuthRequired
3012 // by taking no action. This indicates that the NetworkDelegate does not want to
3013 // handle the challenge, and is passing the buck along to the
3014 // URLRequest::Delegate.
3015 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredSyncNoAction
) {
3016 ASSERT_TRUE(test_server_
.Start());
3019 BlockingNetworkDelegate
network_delegate(
3020 BlockingNetworkDelegate::SYNCHRONOUS
);
3022 TestURLRequestContext
context(true);
3023 context
.set_network_delegate(&network_delegate
);
3026 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
3029 GURL
url(test_server_
.GetURL("auth-basic"));
3030 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
3033 base::RunLoop().Run();
3035 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
3036 EXPECT_EQ(0, r
.status().error());
3037 EXPECT_EQ(200, r
.GetResponseCode());
3038 EXPECT_TRUE(d
.auth_required_called());
3039 EXPECT_EQ(1, network_delegate
.created_requests());
3040 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3042 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3045 TEST_F(URLRequestTestHTTP
,
3046 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders
) {
3047 ASSERT_TRUE(test_server_
.Start());
3050 BlockingNetworkDelegate
network_delegate(
3051 BlockingNetworkDelegate::SYNCHRONOUS
);
3053 TestURLRequestContext
context(true);
3054 context
.set_network_delegate(&network_delegate
);
3057 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
3060 GURL
url(test_server_
.GetURL("auth-basic"));
3061 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
3065 HttpRequestHeaders headers
;
3066 EXPECT_TRUE(r
.GetFullRequestHeaders(&headers
));
3067 EXPECT_FALSE(headers
.HasHeader("Authorization"));
3070 base::RunLoop().Run();
3072 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
3073 EXPECT_EQ(0, r
.status().error());
3074 EXPECT_EQ(200, r
.GetResponseCode());
3075 EXPECT_TRUE(d
.auth_required_called());
3076 EXPECT_EQ(1, network_delegate
.created_requests());
3077 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3079 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3082 // Tests that the network delegate can synchronously complete OnAuthRequired
3083 // by setting credentials.
3084 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredSyncSetAuth
) {
3085 ASSERT_TRUE(test_server_
.Start());
3088 BlockingNetworkDelegate
network_delegate(
3089 BlockingNetworkDelegate::SYNCHRONOUS
);
3090 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3091 network_delegate
.set_auth_retval(
3092 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
);
3094 network_delegate
.set_auth_credentials(AuthCredentials(kUser
, kSecret
));
3096 TestURLRequestContext
context(true);
3097 context
.set_network_delegate(&network_delegate
);
3101 GURL
url(test_server_
.GetURL("auth-basic"));
3102 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
3104 base::RunLoop().Run();
3106 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
3107 EXPECT_EQ(0, r
.status().error());
3108 EXPECT_EQ(200, r
.GetResponseCode());
3109 EXPECT_FALSE(d
.auth_required_called());
3110 EXPECT_EQ(1, network_delegate
.created_requests());
3111 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3113 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3116 // Same as above, but also tests that GetFullRequestHeaders returns the proper
3117 // headers (for the first or second request) when called at the proper times.
3118 TEST_F(URLRequestTestHTTP
,
3119 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders
) {
3120 ASSERT_TRUE(test_server_
.Start());
3123 BlockingNetworkDelegate
network_delegate(
3124 BlockingNetworkDelegate::SYNCHRONOUS
);
3125 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3126 network_delegate
.set_auth_retval(
3127 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
);
3129 network_delegate
.set_auth_credentials(AuthCredentials(kUser
, kSecret
));
3131 TestURLRequestContext
context(true);
3132 context
.set_network_delegate(&network_delegate
);
3136 GURL
url(test_server_
.GetURL("auth-basic"));
3137 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
3139 base::RunLoop().Run();
3141 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
3142 EXPECT_EQ(0, r
.status().error());
3143 EXPECT_EQ(200, r
.GetResponseCode());
3144 EXPECT_FALSE(d
.auth_required_called());
3145 EXPECT_EQ(1, network_delegate
.created_requests());
3146 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3149 HttpRequestHeaders headers
;
3150 EXPECT_TRUE(r
.GetFullRequestHeaders(&headers
));
3151 EXPECT_TRUE(headers
.HasHeader("Authorization"));
3154 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3157 // Tests that the network delegate can synchronously complete OnAuthRequired
3158 // by cancelling authentication.
3159 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredSyncCancel
) {
3160 ASSERT_TRUE(test_server_
.Start());
3163 BlockingNetworkDelegate
network_delegate(
3164 BlockingNetworkDelegate::SYNCHRONOUS
);
3165 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3166 network_delegate
.set_auth_retval(
3167 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
);
3169 TestURLRequestContext
context(true);
3170 context
.set_network_delegate(&network_delegate
);
3174 GURL
url(test_server_
.GetURL("auth-basic"));
3175 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
3177 base::RunLoop().Run();
3179 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
3180 EXPECT_EQ(OK
, r
.status().error());
3181 EXPECT_EQ(401, r
.GetResponseCode());
3182 EXPECT_FALSE(d
.auth_required_called());
3183 EXPECT_EQ(1, network_delegate
.created_requests());
3184 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3186 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3189 // Tests that the network delegate can asynchronously complete OnAuthRequired
3190 // by taking no action. This indicates that the NetworkDelegate does not want
3191 // to handle the challenge, and is passing the buck along to the
3192 // URLRequest::Delegate.
3193 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredAsyncNoAction
) {
3194 ASSERT_TRUE(test_server_
.Start());
3197 BlockingNetworkDelegate
network_delegate(
3198 BlockingNetworkDelegate::AUTO_CALLBACK
);
3199 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3201 TestURLRequestContext
context(true);
3202 context
.set_network_delegate(&network_delegate
);
3205 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
3208 GURL
url(test_server_
.GetURL("auth-basic"));
3209 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
3211 base::RunLoop().Run();
3213 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
3214 EXPECT_EQ(0, r
.status().error());
3215 EXPECT_EQ(200, r
.GetResponseCode());
3216 EXPECT_TRUE(d
.auth_required_called());
3217 EXPECT_EQ(1, network_delegate
.created_requests());
3218 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3220 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3223 // Tests that the network delegate can asynchronously complete OnAuthRequired
3224 // by setting credentials.
3225 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredAsyncSetAuth
) {
3226 ASSERT_TRUE(test_server_
.Start());
3229 BlockingNetworkDelegate
network_delegate(
3230 BlockingNetworkDelegate::AUTO_CALLBACK
);
3231 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3232 network_delegate
.set_auth_retval(
3233 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
);
3235 AuthCredentials
auth_credentials(kUser
, kSecret
);
3236 network_delegate
.set_auth_credentials(auth_credentials
);
3238 TestURLRequestContext
context(true);
3239 context
.set_network_delegate(&network_delegate
);
3243 GURL
url(test_server_
.GetURL("auth-basic"));
3244 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
3246 base::RunLoop().Run();
3248 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
3249 EXPECT_EQ(0, r
.status().error());
3251 EXPECT_EQ(200, r
.GetResponseCode());
3252 EXPECT_FALSE(d
.auth_required_called());
3253 EXPECT_EQ(1, network_delegate
.created_requests());
3254 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3256 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3259 // Tests that the network delegate can asynchronously complete OnAuthRequired
3260 // by cancelling authentication.
3261 TEST_F(URLRequestTestHTTP
, NetworkDelegateOnAuthRequiredAsyncCancel
) {
3262 ASSERT_TRUE(test_server_
.Start());
3265 BlockingNetworkDelegate
network_delegate(
3266 BlockingNetworkDelegate::AUTO_CALLBACK
);
3267 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3268 network_delegate
.set_auth_retval(
3269 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH
);
3271 TestURLRequestContext
context(true);
3272 context
.set_network_delegate(&network_delegate
);
3276 GURL
url(test_server_
.GetURL("auth-basic"));
3277 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &context
);
3279 base::RunLoop().Run();
3281 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
3282 EXPECT_EQ(OK
, r
.status().error());
3283 EXPECT_EQ(401, r
.GetResponseCode());
3284 EXPECT_FALSE(d
.auth_required_called());
3285 EXPECT_EQ(1, network_delegate
.created_requests());
3286 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3288 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3291 // Tests that we can handle when a network request was canceled while we were
3292 // waiting for the network delegate.
3293 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3294 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelWhileWaiting1
) {
3295 ASSERT_TRUE(test_server_
.Start());
3298 BlockingNetworkDelegate
network_delegate(
3299 BlockingNetworkDelegate::USER_CALLBACK
);
3300 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
);
3302 TestURLRequestContext
context(true);
3303 context
.set_network_delegate(&network_delegate
);
3308 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
, &context
);
3311 base::RunLoop().Run();
3312 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST
,
3313 network_delegate
.stage_blocked_for_callback());
3314 EXPECT_EQ(0, network_delegate
.completed_requests());
3315 // Cancel before callback.
3317 // Ensure that network delegate is notified.
3318 EXPECT_EQ(1, network_delegate
.completed_requests());
3319 EXPECT_EQ(URLRequestStatus::CANCELED
, r
.status().status());
3320 EXPECT_EQ(ERR_ABORTED
, r
.status().error());
3321 EXPECT_EQ(1, network_delegate
.created_requests());
3322 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3324 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3327 // Tests that we can handle when a network request was canceled while we were
3328 // waiting for the network delegate.
3329 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3330 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelWhileWaiting2
) {
3331 ASSERT_TRUE(test_server_
.Start());
3334 BlockingNetworkDelegate
network_delegate(
3335 BlockingNetworkDelegate::USER_CALLBACK
);
3336 network_delegate
.set_block_on(
3337 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
);
3339 TestURLRequestContext
context(true);
3340 context
.set_network_delegate(&network_delegate
);
3345 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
, &context
);
3348 base::RunLoop().Run();
3349 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS
,
3350 network_delegate
.stage_blocked_for_callback());
3351 EXPECT_EQ(0, network_delegate
.completed_requests());
3352 // Cancel before callback.
3354 // Ensure that network delegate is notified.
3355 EXPECT_EQ(1, network_delegate
.completed_requests());
3356 EXPECT_EQ(URLRequestStatus::CANCELED
, r
.status().status());
3357 EXPECT_EQ(ERR_ABORTED
, r
.status().error());
3358 EXPECT_EQ(1, network_delegate
.created_requests());
3359 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3361 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3364 // Tests that we can handle when a network request was canceled while we were
3365 // waiting for the network delegate.
3366 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3367 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelWhileWaiting3
) {
3368 ASSERT_TRUE(test_server_
.Start());
3371 BlockingNetworkDelegate
network_delegate(
3372 BlockingNetworkDelegate::USER_CALLBACK
);
3373 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED
);
3375 TestURLRequestContext
context(true);
3376 context
.set_network_delegate(&network_delegate
);
3381 test_server_
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
, &context
);
3384 base::RunLoop().Run();
3385 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED
,
3386 network_delegate
.stage_blocked_for_callback());
3387 EXPECT_EQ(0, network_delegate
.completed_requests());
3388 // Cancel before callback.
3390 // Ensure that network delegate is notified.
3391 EXPECT_EQ(1, network_delegate
.completed_requests());
3392 EXPECT_EQ(URLRequestStatus::CANCELED
, r
.status().status());
3393 EXPECT_EQ(ERR_ABORTED
, r
.status().error());
3394 EXPECT_EQ(1, network_delegate
.created_requests());
3395 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3397 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3400 // Tests that we can handle when a network request was canceled while we were
3401 // waiting for the network delegate.
3402 // Part 4: Request is cancelled while waiting for OnAuthRequired callback.
3403 TEST_F(URLRequestTestHTTP
, NetworkDelegateCancelWhileWaiting4
) {
3404 ASSERT_TRUE(test_server_
.Start());
3407 BlockingNetworkDelegate
network_delegate(
3408 BlockingNetworkDelegate::USER_CALLBACK
);
3409 network_delegate
.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED
);
3411 TestURLRequestContext
context(true);
3412 context
.set_network_delegate(&network_delegate
);
3417 test_server_
.GetURL("auth-basic"), DEFAULT_PRIORITY
, &d
, &context
);
3420 base::RunLoop().Run();
3421 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED
,
3422 network_delegate
.stage_blocked_for_callback());
3423 EXPECT_EQ(0, network_delegate
.completed_requests());
3424 // Cancel before callback.
3426 // Ensure that network delegate is notified.
3427 EXPECT_EQ(1, network_delegate
.completed_requests());
3428 EXPECT_EQ(URLRequestStatus::CANCELED
, r
.status().status());
3429 EXPECT_EQ(ERR_ABORTED
, r
.status().error());
3430 EXPECT_EQ(1, network_delegate
.created_requests());
3431 EXPECT_EQ(0, network_delegate
.destroyed_requests());
3433 EXPECT_EQ(1, network_delegate
.destroyed_requests());
3436 // In this unit test, we're using the HTTPTestServer as a proxy server and
3437 // issuing a CONNECT request with the magic host name "www.server-auth.com".
3438 // The HTTPTestServer will return a 401 response, which we should balk at.
3439 TEST_F(URLRequestTestHTTP
, UnexpectedServerAuthTest
) {
3440 ASSERT_TRUE(test_server_
.Start());
3442 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
3443 TestURLRequestContextWithProxy
context(
3444 test_server_
.host_port_pair().ToString(), &network_delegate
);
3449 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY
, &d
, &context
);
3452 EXPECT_TRUE(r
.is_pending());
3454 base::RunLoop().Run();
3456 EXPECT_EQ(URLRequestStatus::FAILED
, r
.status().status());
3457 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED
, r
.status().error());
3461 TEST_F(URLRequestTestHTTP
, GetTest_NoCache
) {
3462 ASSERT_TRUE(test_server_
.Start());
3466 URLRequest
r(test_server_
.GetURL(std::string()),
3472 EXPECT_TRUE(r
.is_pending());
3474 base::RunLoop().Run();
3476 EXPECT_EQ(1, d
.response_started_count());
3477 EXPECT_FALSE(d
.received_data_before_response());
3478 EXPECT_NE(0, d
.bytes_received());
3479 EXPECT_EQ(test_server_
.host_port_pair().host(),
3480 r
.GetSocketAddress().host());
3481 EXPECT_EQ(test_server_
.host_port_pair().port(),
3482 r
.GetSocketAddress().port());
3484 // TODO(eroman): Add back the NetLog tests...
3488 // This test has the server send a large number of cookies to the client.
3489 // To ensure that no number of cookies causes a crash, a galloping binary
3490 // search is used to estimate that maximum number of cookies that are accepted
3491 // by the browser. Beyond the maximum number, the request will fail with
3492 // ERR_RESPONSE_HEADERS_TOO_BIG.
3494 // http://crbug.com/177916
3495 #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3497 #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3498 #endif // defined(OS_WIN)
3499 TEST_F(URLRequestTestHTTP
, MAYBE_GetTest_ManyCookies
) {
3500 ASSERT_TRUE(test_server_
.Start());
3502 int lower_bound
= 0;
3503 int upper_bound
= 1;
3505 // Double the number of cookies until the response header limits are
3507 while (DoManyCookiesRequest(upper_bound
)) {
3508 lower_bound
= upper_bound
;
3510 ASSERT_LT(upper_bound
, 1000000);
3513 int tolerance
= upper_bound
* 0.005;
3517 // Perform a binary search to find the highest possible number of cookies,
3518 // within the desired tolerance.
3519 while (upper_bound
- lower_bound
>= tolerance
) {
3520 int num_cookies
= (lower_bound
+ upper_bound
) / 2;
3522 if (DoManyCookiesRequest(num_cookies
))
3523 lower_bound
= num_cookies
;
3525 upper_bound
= num_cookies
;
3527 // Success: the test did not crash.
3530 TEST_F(URLRequestTestHTTP
, GetTest
) {
3531 ASSERT_TRUE(test_server_
.Start());
3535 URLRequest
r(test_server_
.GetURL(std::string()),
3541 EXPECT_TRUE(r
.is_pending());
3543 base::RunLoop().Run();
3545 EXPECT_EQ(1, d
.response_started_count());
3546 EXPECT_FALSE(d
.received_data_before_response());
3547 EXPECT_NE(0, d
.bytes_received());
3548 EXPECT_EQ(test_server_
.host_port_pair().host(),
3549 r
.GetSocketAddress().host());
3550 EXPECT_EQ(test_server_
.host_port_pair().port(),
3551 r
.GetSocketAddress().port());
3555 TEST_F(URLRequestTestHTTP
, GetTest_GetFullRequestHeaders
) {
3556 ASSERT_TRUE(test_server_
.Start());
3560 GURL
test_url(test_server_
.GetURL(std::string()));
3561 URLRequest
r(test_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
3563 HttpRequestHeaders headers
;
3564 EXPECT_FALSE(r
.GetFullRequestHeaders(&headers
));
3567 EXPECT_TRUE(r
.is_pending());
3569 base::RunLoop().Run();
3571 EXPECT_EQ(1, d
.response_started_count());
3572 EXPECT_FALSE(d
.received_data_before_response());
3573 EXPECT_NE(0, d
.bytes_received());
3574 EXPECT_EQ(test_server_
.host_port_pair().host(),
3575 r
.GetSocketAddress().host());
3576 EXPECT_EQ(test_server_
.host_port_pair().port(),
3577 r
.GetSocketAddress().port());
3579 EXPECT_TRUE(d
.have_full_request_headers());
3580 CheckFullRequestHeaders(d
.full_request_headers(), test_url
);
3584 TEST_F(URLRequestTestHTTP
, GetTestLoadTiming
) {
3585 ASSERT_TRUE(test_server_
.Start());
3589 URLRequest
r(test_server_
.GetURL(std::string()),
3595 EXPECT_TRUE(r
.is_pending());
3597 base::RunLoop().Run();
3599 LoadTimingInfo load_timing_info
;
3600 r
.GetLoadTimingInfo(&load_timing_info
);
3601 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
3603 EXPECT_EQ(1, d
.response_started_count());
3604 EXPECT_FALSE(d
.received_data_before_response());
3605 EXPECT_NE(0, d
.bytes_received());
3606 EXPECT_EQ(test_server_
.host_port_pair().host(),
3607 r
.GetSocketAddress().host());
3608 EXPECT_EQ(test_server_
.host_port_pair().port(),
3609 r
.GetSocketAddress().port());
3613 TEST_F(URLRequestTestHTTP
, GetZippedTest
) {
3614 ASSERT_TRUE(test_server_
.Start());
3616 // Parameter that specifies the Content-Length field in the response:
3617 // C - Compressed length.
3618 // U - Uncompressed length.
3619 // L - Large length (larger than both C & U).
3620 // M - Medium length (between C & U).
3621 // S - Small length (smaller than both C & U).
3622 const char test_parameters
[] = "CULMS";
3623 const int num_tests
= arraysize(test_parameters
)- 1; // Skip NULL.
3624 // C & U should be OK.
3625 // L & M are larger than the data sent, and show an error.
3626 // S has too little data, but we seem to accept it.
3627 const bool test_expect_success
[num_tests
] =
3628 { true, true, false, false, true };
3630 for (int i
= 0; i
< num_tests
; i
++) {
3633 std::string test_file
=
3634 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
3635 test_parameters
[i
]);
3637 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
3638 TestURLRequestContext
context(true);
3639 context
.set_network_delegate(&network_delegate
);
3643 test_server_
.GetURL(test_file
), DEFAULT_PRIORITY
, &d
, &context
);
3645 EXPECT_TRUE(r
.is_pending());
3647 base::RunLoop().Run();
3649 EXPECT_EQ(1, d
.response_started_count());
3650 EXPECT_FALSE(d
.received_data_before_response());
3651 VLOG(1) << " Received " << d
.bytes_received() << " bytes"
3652 << " status = " << r
.status().status()
3653 << " error = " << r
.status().error();
3654 if (test_expect_success
[i
]) {
3655 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status())
3656 << " Parameter = \"" << test_file
<< "\"";
3658 EXPECT_EQ(URLRequestStatus::FAILED
, r
.status().status());
3659 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH
, r
.status().error())
3660 << " Parameter = \"" << test_file
<< "\"";
3666 TEST_F(URLRequestTestHTTP
, HTTPSToHTTPRedirectNoRefererTest
) {
3667 ASSERT_TRUE(test_server_
.Start());
3669 SpawnedTestServer
https_test_server(
3670 SpawnedTestServer::TYPE_HTTPS
, SpawnedTestServer::kLocalhost
,
3671 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
3672 ASSERT_TRUE(https_test_server
.Start());
3674 // An https server is sent a request with an https referer,
3675 // and responds with a redirect to an http url. The http
3676 // server should not be sent the referer.
3677 GURL http_destination
= test_server_
.GetURL(std::string());
3680 https_test_server
.GetURL("server-redirect?" + http_destination
.spec()),
3684 req
.SetReferrer("https://www.referrer.com/");
3686 base::RunLoop().Run();
3688 EXPECT_EQ(1, d
.response_started_count());
3689 EXPECT_EQ(1, d
.received_redirect_count());
3690 EXPECT_EQ(http_destination
, req
.url());
3691 EXPECT_EQ(std::string(), req
.referrer());
3694 TEST_F(URLRequestTestHTTP
, RedirectLoadTiming
) {
3695 ASSERT_TRUE(test_server_
.Start());
3697 GURL destination_url
= test_server_
.GetURL(std::string());
3699 test_server_
.GetURL("server-redirect?" + destination_url
.spec());
3701 URLRequest
req(original_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
3703 base::RunLoop().Run();
3705 EXPECT_EQ(1, d
.response_started_count());
3706 EXPECT_EQ(1, d
.received_redirect_count());
3707 EXPECT_EQ(destination_url
, req
.url());
3708 EXPECT_EQ(original_url
, req
.original_url());
3709 ASSERT_EQ(2U, req
.url_chain().size());
3710 EXPECT_EQ(original_url
, req
.url_chain()[0]);
3711 EXPECT_EQ(destination_url
, req
.url_chain()[1]);
3713 LoadTimingInfo load_timing_info_before_redirect
;
3714 EXPECT_TRUE(default_network_delegate_
.GetLoadTimingInfoBeforeRedirect(
3715 &load_timing_info_before_redirect
));
3716 TestLoadTimingNotReused(load_timing_info_before_redirect
,
3717 CONNECT_TIMING_HAS_DNS_TIMES
);
3719 LoadTimingInfo load_timing_info
;
3720 req
.GetLoadTimingInfo(&load_timing_info
);
3721 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
3723 // Check that a new socket was used on redirect, since the server does not
3724 // supposed keep-alive sockets, and that the times before the redirect are
3725 // before the ones recorded for the second request.
3726 EXPECT_NE(load_timing_info_before_redirect
.socket_log_id
,
3727 load_timing_info
.socket_log_id
);
3728 EXPECT_LE(load_timing_info_before_redirect
.receive_headers_end
,
3729 load_timing_info
.connect_timing
.connect_start
);
3732 TEST_F(URLRequestTestHTTP
, MultipleRedirectTest
) {
3733 ASSERT_TRUE(test_server_
.Start());
3735 GURL destination_url
= test_server_
.GetURL(std::string());
3736 GURL middle_redirect_url
=
3737 test_server_
.GetURL("server-redirect?" + destination_url
.spec());
3738 GURL original_url
= test_server_
.GetURL(
3739 "server-redirect?" + middle_redirect_url
.spec());
3741 URLRequest
req(original_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
3743 base::RunLoop().Run();
3745 EXPECT_EQ(1, d
.response_started_count());
3746 EXPECT_EQ(2, d
.received_redirect_count());
3747 EXPECT_EQ(destination_url
, req
.url());
3748 EXPECT_EQ(original_url
, req
.original_url());
3749 ASSERT_EQ(3U, req
.url_chain().size());
3750 EXPECT_EQ(original_url
, req
.url_chain()[0]);
3751 EXPECT_EQ(middle_redirect_url
, req
.url_chain()[1]);
3752 EXPECT_EQ(destination_url
, req
.url_chain()[2]);
3755 // First and second pieces of information logged by delegates to URLRequests.
3756 const char kFirstDelegateInfo
[] = "Wonderful delegate";
3757 const char kSecondDelegateInfo
[] = "Exciting delegate";
3759 // Logs delegate information to a URLRequest. The first string is logged
3760 // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is
3761 // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then
3762 // another asynchronous call is used to clear the delegate information
3763 // before calling a callback. The object then deletes itself.
3764 class AsyncDelegateLogger
: public base::RefCounted
<AsyncDelegateLogger
> {
3766 typedef base::Callback
<void()> Callback
;
3768 // Each time delegate information is added to the URLRequest, the resulting
3769 // load state is checked. The expected load state after each request is
3770 // passed in as an argument.
3771 static void Run(URLRequest
* url_request
,
3772 LoadState expected_first_load_state
,
3773 LoadState expected_second_load_state
,
3774 LoadState expected_third_load_state
,
3775 const Callback
& callback
) {
3776 AsyncDelegateLogger
* logger
= new AsyncDelegateLogger(
3778 expected_first_load_state
,
3779 expected_second_load_state
,
3780 expected_third_load_state
,
3785 // Checks that the log entries, starting with log_position, contain the
3786 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
3787 // recorded. Returns the index of entry after the expected number of
3788 // events this logged, or entries.size() if there aren't enough entries.
3789 static size_t CheckDelegateInfo(
3790 const CapturingNetLog::CapturedEntryList
& entries
, size_t log_position
) {
3791 // There should be 4 DELEGATE_INFO events: Two begins and two ends.
3792 if (log_position
+ 3 >= entries
.size()) {
3793 ADD_FAILURE() << "Not enough log entries";
3794 return entries
.size();
3796 std::string delegate_info
;
3797 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO
, entries
[log_position
].type
);
3798 EXPECT_EQ(NetLog::PHASE_BEGIN
, entries
[log_position
].phase
);
3799 EXPECT_TRUE(entries
[log_position
].GetStringValue("delegate_info",
3801 EXPECT_EQ(kFirstDelegateInfo
, delegate_info
);
3804 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO
, entries
[log_position
].type
);
3805 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
3808 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO
, entries
[log_position
].type
);
3809 EXPECT_EQ(NetLog::PHASE_BEGIN
, entries
[log_position
].phase
);
3810 EXPECT_TRUE(entries
[log_position
].GetStringValue("delegate_info",
3812 EXPECT_EQ(kSecondDelegateInfo
, delegate_info
);
3815 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO
, entries
[log_position
].type
);
3816 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
3818 return log_position
+ 1;
3821 // Find delegate request begin and end messages for OnBeforeNetworkStart.
3822 // Returns the position of the end message.
3823 static size_t ExpectBeforeNetworkEvents(
3824 const CapturingNetLog::CapturedEntryList
& entries
,
3825 size_t log_position
) {
3827 ExpectLogContainsSomewhereAfter(entries
,
3829 NetLog::TYPE_URL_REQUEST_DELEGATE
,
3830 NetLog::PHASE_BEGIN
);
3831 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
,
3832 entries
[log_position
+ 1].type
);
3833 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
+ 1].phase
);
3834 return log_position
+ 1;
3838 friend class base::RefCounted
<AsyncDelegateLogger
>;
3840 AsyncDelegateLogger(URLRequest
* url_request
,
3841 LoadState expected_first_load_state
,
3842 LoadState expected_second_load_state
,
3843 LoadState expected_third_load_state
,
3844 const Callback
& callback
)
3845 : url_request_(url_request
),
3846 expected_first_load_state_(expected_first_load_state
),
3847 expected_second_load_state_(expected_second_load_state
),
3848 expected_third_load_state_(expected_third_load_state
),
3849 callback_(callback
) {
3852 ~AsyncDelegateLogger() {}
3855 url_request_
->LogBlockedBy(kFirstDelegateInfo
);
3856 LoadStateWithParam load_state
= url_request_
->GetLoadState();
3857 EXPECT_EQ(expected_first_load_state_
, load_state
.state
);
3858 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo
), load_state
.param
);
3859 base::MessageLoop::current()->PostTask(
3861 base::Bind(&AsyncDelegateLogger::LogSecondDelegate
, this));
3864 void LogSecondDelegate() {
3865 url_request_
->LogAndReportBlockedBy(kSecondDelegateInfo
);
3866 LoadStateWithParam load_state
= url_request_
->GetLoadState();
3867 EXPECT_EQ(expected_second_load_state_
, load_state
.state
);
3868 if (expected_second_load_state_
== LOAD_STATE_WAITING_FOR_DELEGATE
) {
3869 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo
), load_state
.param
);
3871 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo
), load_state
.param
);
3873 base::MessageLoop::current()->PostTask(
3875 base::Bind(&AsyncDelegateLogger::LogComplete
, this));
3878 void LogComplete() {
3879 url_request_
->LogUnblocked();
3880 LoadStateWithParam load_state
= url_request_
->GetLoadState();
3881 EXPECT_EQ(expected_third_load_state_
, load_state
.state
);
3882 if (expected_second_load_state_
== LOAD_STATE_WAITING_FOR_DELEGATE
)
3883 EXPECT_EQ(base::string16(), load_state
.param
);
3887 URLRequest
* url_request_
;
3888 const int expected_first_load_state_
;
3889 const int expected_second_load_state_
;
3890 const int expected_third_load_state_
;
3891 const Callback callback_
;
3893 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger
);
3896 // NetworkDelegate that logs delegate information before a request is started,
3897 // before headers are sent, when headers are read, and when auth information
3898 // is requested. Uses AsyncDelegateLogger.
3899 class AsyncLoggingNetworkDelegate
: public TestNetworkDelegate
{
3901 AsyncLoggingNetworkDelegate() {}
3902 virtual ~AsyncLoggingNetworkDelegate() {}
3904 // NetworkDelegate implementation.
3905 virtual int OnBeforeURLRequest(URLRequest
* request
,
3906 const CompletionCallback
& callback
,
3907 GURL
* new_url
) OVERRIDE
{
3908 TestNetworkDelegate::OnBeforeURLRequest(request
, callback
, new_url
);
3909 return RunCallbackAsynchronously(request
, callback
);
3912 virtual int OnBeforeSendHeaders(URLRequest
* request
,
3913 const CompletionCallback
& callback
,
3914 HttpRequestHeaders
* headers
) OVERRIDE
{
3915 TestNetworkDelegate::OnBeforeSendHeaders(request
, callback
, headers
);
3916 return RunCallbackAsynchronously(request
, callback
);
3919 virtual int OnHeadersReceived(
3920 URLRequest
* request
,
3921 const CompletionCallback
& callback
,
3922 const HttpResponseHeaders
* original_response_headers
,
3923 scoped_refptr
<HttpResponseHeaders
>* override_response_headers
) OVERRIDE
{
3924 TestNetworkDelegate::OnHeadersReceived(request
, callback
,
3925 original_response_headers
,
3926 override_response_headers
);
3927 return RunCallbackAsynchronously(request
, callback
);
3930 virtual NetworkDelegate::AuthRequiredResponse
OnAuthRequired(
3931 URLRequest
* request
,
3932 const AuthChallengeInfo
& auth_info
,
3933 const AuthCallback
& callback
,
3934 AuthCredentials
* credentials
) OVERRIDE
{
3935 AsyncDelegateLogger::Run(
3937 LOAD_STATE_WAITING_FOR_DELEGATE
,
3938 LOAD_STATE_WAITING_FOR_DELEGATE
,
3939 LOAD_STATE_WAITING_FOR_DELEGATE
,
3940 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume
,
3941 callback
, credentials
));
3942 return AUTH_REQUIRED_RESPONSE_IO_PENDING
;
3946 static int RunCallbackAsynchronously(
3947 URLRequest
* request
,
3948 const CompletionCallback
& callback
) {
3949 AsyncDelegateLogger::Run(
3951 LOAD_STATE_WAITING_FOR_DELEGATE
,
3952 LOAD_STATE_WAITING_FOR_DELEGATE
,
3953 LOAD_STATE_WAITING_FOR_DELEGATE
,
3954 base::Bind(callback
, OK
));
3955 return ERR_IO_PENDING
;
3958 static void SetAuthAndResume(const AuthCallback
& callback
,
3959 AuthCredentials
* credentials
) {
3960 *credentials
= AuthCredentials(kUser
, kSecret
);
3961 callback
.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH
);
3964 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate
);
3967 // URLRequest::Delegate that logs delegate information when the headers
3968 // are received, when each read completes, and during redirects. Uses
3969 // AsyncDelegateLogger. Can optionally cancel a request in any phase.
3971 // Inherits from TestDelegate to reuse the TestDelegate code to handle
3972 // advancing to the next step in most cases, as well as cancellation.
3973 class AsyncLoggingUrlRequestDelegate
: public TestDelegate
{
3977 CANCEL_ON_RECEIVED_REDIRECT
,
3978 CANCEL_ON_RESPONSE_STARTED
,
3979 CANCEL_ON_READ_COMPLETED
3982 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage
)
3983 : cancel_stage_(cancel_stage
) {
3984 if (cancel_stage
== CANCEL_ON_RECEIVED_REDIRECT
)
3985 set_cancel_in_received_redirect(true);
3986 else if (cancel_stage
== CANCEL_ON_RESPONSE_STARTED
)
3987 set_cancel_in_response_started(true);
3988 else if (cancel_stage
== CANCEL_ON_READ_COMPLETED
)
3989 set_cancel_in_received_data(true);
3991 virtual ~AsyncLoggingUrlRequestDelegate() {}
3993 // URLRequest::Delegate implementation:
3994 void virtual OnReceivedRedirect(URLRequest
* request
,
3995 const GURL
& new_url
,
3996 bool* defer_redirect
) OVERRIDE
{
3997 *defer_redirect
= true;
3998 AsyncDelegateLogger::Run(
4000 LOAD_STATE_WAITING_FOR_DELEGATE
,
4001 LOAD_STATE_WAITING_FOR_DELEGATE
,
4002 LOAD_STATE_WAITING_FOR_DELEGATE
,
4004 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete
,
4005 base::Unretained(this), request
, new_url
));
4008 virtual void OnResponseStarted(URLRequest
* request
) OVERRIDE
{
4009 AsyncDelegateLogger::Run(
4011 LOAD_STATE_WAITING_FOR_DELEGATE
,
4012 LOAD_STATE_WAITING_FOR_DELEGATE
,
4013 LOAD_STATE_WAITING_FOR_DELEGATE
,
4015 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete
,
4016 base::Unretained(this), request
));
4019 virtual void OnReadCompleted(URLRequest
* request
,
4020 int bytes_read
) OVERRIDE
{
4021 AsyncDelegateLogger::Run(
4027 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete
,
4028 base::Unretained(this), request
, bytes_read
));
4032 void OnReceivedRedirectLoggingComplete(URLRequest
* request
,
4033 const GURL
& new_url
) {
4034 bool defer_redirect
= false;
4035 TestDelegate::OnReceivedRedirect(request
, new_url
, &defer_redirect
);
4036 // FollowDeferredRedirect should not be called after cancellation.
4037 if (cancel_stage_
== CANCEL_ON_RECEIVED_REDIRECT
)
4039 if (!defer_redirect
)
4040 request
->FollowDeferredRedirect();
4043 void OnResponseStartedLoggingComplete(URLRequest
* request
) {
4044 // The parent class continues the request.
4045 TestDelegate::OnResponseStarted(request
);
4048 void AfterReadCompletedLoggingComplete(URLRequest
* request
, int bytes_read
) {
4049 // The parent class continues the request.
4050 TestDelegate::OnReadCompleted(request
, bytes_read
);
4053 const CancelStage cancel_stage_
;
4055 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate
);
4058 // Tests handling of delegate info before a request starts.
4059 TEST_F(URLRequestTestHTTP
, DelegateInfoBeforeStart
) {
4060 ASSERT_TRUE(test_server_
.Start());
4062 TestDelegate request_delegate
;
4063 TestURLRequestContext
context(true);
4064 context
.set_network_delegate(NULL
);
4065 context
.set_net_log(&net_log_
);
4069 URLRequest
r(test_server_
.GetURL("empty.html"),
4073 LoadStateWithParam load_state
= r
.GetLoadState();
4074 EXPECT_EQ(LOAD_STATE_IDLE
, load_state
.state
);
4075 EXPECT_EQ(base::string16(), load_state
.param
);
4077 AsyncDelegateLogger::Run(
4079 LOAD_STATE_WAITING_FOR_DELEGATE
,
4080 LOAD_STATE_WAITING_FOR_DELEGATE
,
4082 base::Bind(&URLRequest::Start
, base::Unretained(&r
)));
4084 base::RunLoop().Run();
4086 EXPECT_EQ(200, r
.GetResponseCode());
4087 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
4090 CapturingNetLog::CapturedEntryList entries
;
4091 net_log_
.GetEntries(&entries
);
4092 size_t log_position
= ExpectLogContainsSomewhereAfter(
4095 NetLog::TYPE_DELEGATE_INFO
,
4096 NetLog::PHASE_BEGIN
);
4098 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
, log_position
);
4100 // Nothing else should add any delegate info to the request.
4101 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4102 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4105 // Tests handling of delegate info from a network delegate.
4106 TEST_F(URLRequestTestHTTP
, NetworkDelegateInfo
) {
4107 ASSERT_TRUE(test_server_
.Start());
4109 TestDelegate request_delegate
;
4110 AsyncLoggingNetworkDelegate network_delegate
;
4111 TestURLRequestContext
context(true);
4112 context
.set_network_delegate(&network_delegate
);
4113 context
.set_net_log(&net_log_
);
4117 URLRequest
r(test_server_
.GetURL("simple.html"),
4121 LoadStateWithParam load_state
= r
.GetLoadState();
4122 EXPECT_EQ(LOAD_STATE_IDLE
, load_state
.state
);
4123 EXPECT_EQ(base::string16(), load_state
.param
);
4126 base::RunLoop().Run();
4128 EXPECT_EQ(200, r
.GetResponseCode());
4129 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
4130 EXPECT_EQ(1, network_delegate
.created_requests());
4131 EXPECT_EQ(0, network_delegate
.destroyed_requests());
4133 EXPECT_EQ(1, network_delegate
.destroyed_requests());
4135 size_t log_position
= 0;
4136 CapturingNetLog::CapturedEntryList entries
;
4137 net_log_
.GetEntries(&entries
);
4138 for (size_t i
= 0; i
< 3; ++i
) {
4139 log_position
= ExpectLogContainsSomewhereAfter(
4142 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4143 NetLog::PHASE_BEGIN
);
4145 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4148 ASSERT_LT(log_position
, entries
.size());
4149 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4150 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4153 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4154 entries
, log_position
+ 1);
4158 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4159 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4162 // Tests handling of delegate info from a network delegate in the case of an
4164 TEST_F(URLRequestTestHTTP
, NetworkDelegateInfoRedirect
) {
4165 ASSERT_TRUE(test_server_
.Start());
4167 TestDelegate request_delegate
;
4168 AsyncLoggingNetworkDelegate network_delegate
;
4169 TestURLRequestContext
context(true);
4170 context
.set_network_delegate(&network_delegate
);
4171 context
.set_net_log(&net_log_
);
4175 URLRequest
r(test_server_
.GetURL("server-redirect?simple.html"),
4179 LoadStateWithParam load_state
= r
.GetLoadState();
4180 EXPECT_EQ(LOAD_STATE_IDLE
, load_state
.state
);
4181 EXPECT_EQ(base::string16(), load_state
.param
);
4184 base::RunLoop().Run();
4186 EXPECT_EQ(200, r
.GetResponseCode());
4187 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
4188 EXPECT_EQ(2, network_delegate
.created_requests());
4189 EXPECT_EQ(0, network_delegate
.destroyed_requests());
4191 EXPECT_EQ(1, network_delegate
.destroyed_requests());
4193 size_t log_position
= 0;
4194 CapturingNetLog::CapturedEntryList entries
;
4195 net_log_
.GetEntries(&entries
);
4196 // The NetworkDelegate logged information in OnBeforeURLRequest,
4197 // OnBeforeSendHeaders, and OnHeadersReceived.
4198 for (size_t i
= 0; i
< 3; ++i
) {
4199 log_position
= ExpectLogContainsSomewhereAfter(
4202 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4203 NetLog::PHASE_BEGIN
);
4205 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4208 ASSERT_LT(log_position
, entries
.size());
4209 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4210 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4213 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4214 entries
, log_position
+ 1);
4218 // The URLRequest::Delegate then gets informed about the redirect.
4219 log_position
= ExpectLogContainsSomewhereAfter(
4222 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4223 NetLog::PHASE_BEGIN
);
4225 // The NetworkDelegate logged information in the same three events as before.
4226 for (size_t i
= 0; i
< 3; ++i
) {
4227 log_position
= ExpectLogContainsSomewhereAfter(
4230 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4231 NetLog::PHASE_BEGIN
);
4233 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4236 ASSERT_LT(log_position
, entries
.size());
4237 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4238 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4241 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4242 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4245 // Tests handling of delegate info from a network delegate in the case of HTTP
4247 TEST_F(URLRequestTestHTTP
, NetworkDelegateInfoAuth
) {
4248 ASSERT_TRUE(test_server_
.Start());
4250 TestDelegate request_delegate
;
4251 AsyncLoggingNetworkDelegate network_delegate
;
4252 TestURLRequestContext
context(true);
4253 context
.set_network_delegate(&network_delegate
);
4254 context
.set_net_log(&net_log_
);
4258 URLRequest
r(test_server_
.GetURL("auth-basic"),
4262 LoadStateWithParam load_state
= r
.GetLoadState();
4263 EXPECT_EQ(LOAD_STATE_IDLE
, load_state
.state
);
4264 EXPECT_EQ(base::string16(), load_state
.param
);
4267 base::RunLoop().Run();
4269 EXPECT_EQ(200, r
.GetResponseCode());
4270 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
4271 EXPECT_EQ(1, network_delegate
.created_requests());
4272 EXPECT_EQ(0, network_delegate
.destroyed_requests());
4274 EXPECT_EQ(1, network_delegate
.destroyed_requests());
4276 size_t log_position
= 0;
4277 CapturingNetLog::CapturedEntryList entries
;
4278 net_log_
.GetEntries(&entries
);
4279 // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4280 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4281 // OnBeforeURLRequest and OnBeforeSendHeaders.
4282 for (size_t i
= 0; i
< 6; ++i
) {
4283 log_position
= ExpectLogContainsSomewhereAfter(
4286 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4287 NetLog::PHASE_BEGIN
);
4289 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4292 ASSERT_LT(log_position
, entries
.size());
4293 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4294 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4297 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4298 entries
, log_position
+ 1);
4302 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4303 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4306 // Tests handling of delegate info from a URLRequest::Delegate.
4307 TEST_F(URLRequestTestHTTP
, URLRequestDelegateInfo
) {
4308 ASSERT_TRUE(test_server_
.Start());
4310 AsyncLoggingUrlRequestDelegate
request_delegate(
4311 AsyncLoggingUrlRequestDelegate::NO_CANCEL
);
4312 TestURLRequestContext
context(true);
4313 context
.set_network_delegate(NULL
);
4314 context
.set_net_log(&net_log_
);
4318 // A chunked response with delays between chunks is used to make sure that
4319 // attempts by the URLRequest delegate to log information while reading the
4320 // body are ignored. Since they are ignored, this test is robust against
4321 // the possibility of multiple reads being combined in the unlikely event
4323 URLRequest
r(test_server_
.GetURL("chunked?waitBetweenChunks=20"),
4327 LoadStateWithParam load_state
= r
.GetLoadState();
4329 base::RunLoop().Run();
4331 EXPECT_EQ(200, r
.GetResponseCode());
4332 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
4335 CapturingNetLog::CapturedEntryList entries
;
4336 net_log_
.GetEntries(&entries
);
4338 size_t log_position
= 0;
4340 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4341 entries
, log_position
);
4343 // The delegate info should only have been logged on header complete. Other
4344 // times it should silently be ignored.
4346 ExpectLogContainsSomewhereAfter(entries
,
4348 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4349 NetLog::PHASE_BEGIN
);
4351 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4354 ASSERT_LT(log_position
, entries
.size());
4355 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4356 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4358 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4359 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4360 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4361 entries
, log_position
+ 1, NetLog::TYPE_URL_REQUEST_DELEGATE
));
4364 // Tests handling of delegate info from a URLRequest::Delegate in the case of
4365 // an HTTP redirect.
4366 TEST_F(URLRequestTestHTTP
, URLRequestDelegateInfoOnRedirect
) {
4367 ASSERT_TRUE(test_server_
.Start());
4369 AsyncLoggingUrlRequestDelegate
request_delegate(
4370 AsyncLoggingUrlRequestDelegate::NO_CANCEL
);
4371 TestURLRequestContext
context(true);
4372 context
.set_network_delegate(NULL
);
4373 context
.set_net_log(&net_log_
);
4377 URLRequest
r(test_server_
.GetURL("server-redirect?simple.html"),
4381 LoadStateWithParam load_state
= r
.GetLoadState();
4383 base::RunLoop().Run();
4385 EXPECT_EQ(200, r
.GetResponseCode());
4386 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
4389 CapturingNetLog::CapturedEntryList entries
;
4390 net_log_
.GetEntries(&entries
);
4392 // Delegate info should only have been logged in OnReceivedRedirect and
4393 // OnResponseStarted.
4394 size_t log_position
= 0;
4395 for (int i
= 0; i
< 2; ++i
) {
4397 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4398 entries
, log_position
) + 1;
4401 log_position
= ExpectLogContainsSomewhereAfter(
4404 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4405 NetLog::PHASE_BEGIN
);
4407 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4410 ASSERT_LT(log_position
, entries
.size());
4411 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4412 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4415 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4416 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4417 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4418 entries
, log_position
+ 1, NetLog::TYPE_URL_REQUEST_DELEGATE
));
4421 // Tests handling of delegate info from a URLRequest::Delegate in the case of
4422 // an HTTP redirect, with cancellation at various points.
4423 TEST_F(URLRequestTestHTTP
, URLRequestDelegateOnRedirectCancelled
) {
4424 ASSERT_TRUE(test_server_
.Start());
4426 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages
[] = {
4427 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT
,
4428 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED
,
4429 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED
,
4432 for (size_t test_case
= 0; test_case
< arraysize(kCancelStages
);
4434 AsyncLoggingUrlRequestDelegate
request_delegate(kCancelStages
[test_case
]);
4435 TestURLRequestContext
context(true);
4436 CapturingNetLog net_log
;
4437 context
.set_network_delegate(NULL
);
4438 context
.set_net_log(&net_log
);
4442 URLRequest
r(test_server_
.GetURL("server-redirect?simple.html"),
4446 LoadStateWithParam load_state
= r
.GetLoadState();
4448 base::RunLoop().Run();
4449 EXPECT_EQ(URLRequestStatus::CANCELED
, r
.status().status());
4452 CapturingNetLog::CapturedEntryList entries
;
4453 net_log
.GetEntries(&entries
);
4455 // Delegate info is always logged in both OnReceivedRedirect and
4456 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4457 // OnResponseStarted delegate call is after cancellation, but logging is
4458 // still currently supported in that call.
4459 size_t log_position
= 0;
4460 for (int i
= 0; i
< 2; ++i
) {
4462 log_position
= AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4463 entries
, log_position
) + 1;
4466 log_position
= ExpectLogContainsSomewhereAfter(
4469 NetLog::TYPE_URL_REQUEST_DELEGATE
,
4470 NetLog::PHASE_BEGIN
);
4472 log_position
= AsyncDelegateLogger::CheckDelegateInfo(entries
,
4475 ASSERT_LT(log_position
, entries
.size());
4476 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE
, entries
[log_position
].type
);
4477 EXPECT_EQ(NetLog::PHASE_END
, entries
[log_position
].phase
);
4480 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4481 entries
, log_position
+ 1, NetLog::TYPE_DELEGATE_INFO
));
4482 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4483 entries
, log_position
+ 1, NetLog::TYPE_URL_REQUEST_DELEGATE
));
4489 const char kExtraHeader
[] = "Allow-Snafu";
4490 const char kExtraValue
[] = "fubar";
4492 class RedirectWithAdditionalHeadersDelegate
: public TestDelegate
{
4493 virtual void OnReceivedRedirect(net::URLRequest
* request
,
4494 const GURL
& new_url
,
4495 bool* defer_redirect
) OVERRIDE
{
4496 TestDelegate::OnReceivedRedirect(request
, new_url
, defer_redirect
);
4497 request
->SetExtraRequestHeaderByName(kExtraHeader
, kExtraValue
, false);
4503 TEST_F(URLRequestTestHTTP
, RedirectWithAdditionalHeadersTest
) {
4504 ASSERT_TRUE(test_server_
.Start());
4506 GURL destination_url
= test_server_
.GetURL(
4507 "echoheader?" + std::string(kExtraHeader
));
4508 GURL original_url
= test_server_
.GetURL(
4509 "server-redirect?" + destination_url
.spec());
4510 RedirectWithAdditionalHeadersDelegate d
;
4511 URLRequest
req(original_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
4513 base::RunLoop().Run();
4516 const HttpRequestHeaders
& headers
= req
.extra_request_headers();
4517 EXPECT_TRUE(headers
.GetHeader(kExtraHeader
, &value
));
4518 EXPECT_EQ(kExtraValue
, value
);
4519 EXPECT_FALSE(req
.is_pending());
4520 EXPECT_FALSE(req
.is_redirecting());
4521 EXPECT_EQ(kExtraValue
, d
.data_received());
4526 const char kExtraHeaderToRemove
[] = "To-Be-Removed";
4528 class RedirectWithHeaderRemovalDelegate
: public TestDelegate
{
4529 virtual void OnReceivedRedirect(net::URLRequest
* request
,
4530 const GURL
& new_url
,
4531 bool* defer_redirect
) OVERRIDE
{
4532 TestDelegate::OnReceivedRedirect(request
, new_url
, defer_redirect
);
4533 request
->RemoveRequestHeaderByName(kExtraHeaderToRemove
);
4539 TEST_F(URLRequestTestHTTP
, RedirectWithHeaderRemovalTest
) {
4540 ASSERT_TRUE(test_server_
.Start());
4542 GURL destination_url
= test_server_
.GetURL(
4543 "echoheader?" + std::string(kExtraHeaderToRemove
));
4544 GURL original_url
= test_server_
.GetURL(
4545 "server-redirect?" + destination_url
.spec());
4546 RedirectWithHeaderRemovalDelegate d
;
4547 URLRequest
req(original_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
4548 req
.SetExtraRequestHeaderByName(kExtraHeaderToRemove
, "dummy", false);
4550 base::RunLoop().Run();
4553 const HttpRequestHeaders
& headers
= req
.extra_request_headers();
4554 EXPECT_FALSE(headers
.GetHeader(kExtraHeaderToRemove
, &value
));
4555 EXPECT_FALSE(req
.is_pending());
4556 EXPECT_FALSE(req
.is_redirecting());
4557 EXPECT_EQ("None", d
.data_received());
4560 TEST_F(URLRequestTestHTTP
, CancelTest
) {
4563 URLRequest
r(GURL("http://www.google.com/"),
4569 EXPECT_TRUE(r
.is_pending());
4573 base::RunLoop().Run();
4575 // We expect to receive OnResponseStarted even though the request has been
4577 EXPECT_EQ(1, d
.response_started_count());
4578 EXPECT_EQ(0, d
.bytes_received());
4579 EXPECT_FALSE(d
.received_data_before_response());
4583 TEST_F(URLRequestTestHTTP
, CancelTest2
) {
4584 ASSERT_TRUE(test_server_
.Start());
4588 URLRequest
r(test_server_
.GetURL(std::string()),
4593 d
.set_cancel_in_response_started(true);
4596 EXPECT_TRUE(r
.is_pending());
4598 base::RunLoop().Run();
4600 EXPECT_EQ(1, d
.response_started_count());
4601 EXPECT_EQ(0, d
.bytes_received());
4602 EXPECT_FALSE(d
.received_data_before_response());
4603 EXPECT_EQ(URLRequestStatus::CANCELED
, r
.status().status());
4607 TEST_F(URLRequestTestHTTP
, CancelTest3
) {
4608 ASSERT_TRUE(test_server_
.Start());
4612 URLRequest
r(test_server_
.GetURL(std::string()),
4617 d
.set_cancel_in_received_data(true);
4620 EXPECT_TRUE(r
.is_pending());
4622 base::RunLoop().Run();
4624 EXPECT_EQ(1, d
.response_started_count());
4625 // There is no guarantee about how much data was received
4626 // before the cancel was issued. It could have been 0 bytes,
4627 // or it could have been all the bytes.
4628 // EXPECT_EQ(0, d.bytes_received());
4629 EXPECT_FALSE(d
.received_data_before_response());
4630 EXPECT_EQ(URLRequestStatus::CANCELED
, r
.status().status());
4634 TEST_F(URLRequestTestHTTP
, CancelTest4
) {
4635 ASSERT_TRUE(test_server_
.Start());
4639 URLRequest
r(test_server_
.GetURL(std::string()),
4645 EXPECT_TRUE(r
.is_pending());
4647 // The request will be implicitly canceled when it is destroyed. The
4648 // test delegate must not post a quit message when this happens because
4649 // this test doesn't actually have a message loop. The quit message would
4650 // get put on this thread's message queue and the next test would exit
4651 // early, causing problems.
4652 d
.set_quit_on_complete(false);
4654 // expect things to just cleanup properly.
4656 // we won't actually get a received reponse here because we've never run the
4658 EXPECT_FALSE(d
.received_data_before_response());
4659 EXPECT_EQ(0, d
.bytes_received());
4662 TEST_F(URLRequestTestHTTP
, CancelTest5
) {
4663 ASSERT_TRUE(test_server_
.Start());
4668 URLRequest
r(test_server_
.GetURL("cachetime"),
4673 base::RunLoop().Run();
4674 EXPECT_EQ(URLRequestStatus::SUCCESS
, r
.status().status());
4677 // cancel read from cache (see bug 990242)
4680 URLRequest
r(test_server_
.GetURL("cachetime"),
4686 base::RunLoop().Run();
4688 EXPECT_EQ(URLRequestStatus::CANCELED
, r
.status().status());
4689 EXPECT_EQ(1, d
.response_started_count());
4690 EXPECT_EQ(0, d
.bytes_received());
4691 EXPECT_FALSE(d
.received_data_before_response());
4695 TEST_F(URLRequestTestHTTP
, PostTest
) {
4696 ASSERT_TRUE(test_server_
.Start());
4697 HTTPUploadDataOperationTest("POST");
4700 TEST_F(URLRequestTestHTTP
, PutTest
) {
4701 ASSERT_TRUE(test_server_
.Start());
4702 HTTPUploadDataOperationTest("PUT");
4705 TEST_F(URLRequestTestHTTP
, PostEmptyTest
) {
4706 ASSERT_TRUE(test_server_
.Start());
4711 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
, &default_context_
);
4712 r
.set_method("POST");
4715 EXPECT_TRUE(r
.is_pending());
4717 base::RunLoop().Run();
4719 ASSERT_EQ(1, d
.response_started_count())
4720 << "request failed: " << r
.status().status()
4721 << ", error: " << r
.status().error();
4723 EXPECT_FALSE(d
.received_data_before_response());
4724 EXPECT_TRUE(d
.data_received().empty());
4728 TEST_F(URLRequestTestHTTP
, PostFileTest
) {
4729 ASSERT_TRUE(test_server_
.Start());
4734 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
, &default_context_
);
4735 r
.set_method("POST");
4738 PathService::Get(base::DIR_EXE
, &dir
);
4739 base::SetCurrentDirectory(dir
);
4741 ScopedVector
<UploadElementReader
> element_readers
;
4743 base::FilePath path
;
4744 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
4745 path
= path
.Append(FILE_PATH_LITERAL("net"));
4746 path
= path
.Append(FILE_PATH_LITERAL("data"));
4747 path
= path
.Append(FILE_PATH_LITERAL("url_request_unittest"));
4748 path
= path
.Append(FILE_PATH_LITERAL("with-headers.html"));
4749 element_readers
.push_back(
4750 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
4755 r
.set_upload(make_scoped_ptr(
4756 new UploadDataStream(element_readers
.Pass(), 0)));
4759 EXPECT_TRUE(r
.is_pending());
4761 base::RunLoop().Run();
4764 ASSERT_EQ(true, base::GetFileSize(path
, &size
));
4765 scoped_ptr
<char[]> buf(new char[size
]);
4767 ASSERT_EQ(size
, base::ReadFile(path
, buf
.get(), size
));
4769 ASSERT_EQ(1, d
.response_started_count())
4770 << "request failed: " << r
.status().status()
4771 << ", error: " << r
.status().error();
4773 EXPECT_FALSE(d
.received_data_before_response());
4775 EXPECT_EQ(size
, d
.bytes_received());
4776 EXPECT_EQ(std::string(&buf
[0], size
), d
.data_received());
4780 TEST_F(URLRequestTestHTTP
, PostUnreadableFileTest
) {
4781 ASSERT_TRUE(test_server_
.Start());
4785 URLRequest
r(test_server_
.GetURL("echo"), DEFAULT_PRIORITY
,
4786 &d
, &default_context_
);
4787 r
.set_method("POST");
4789 ScopedVector
<UploadElementReader
> element_readers
;
4791 element_readers
.push_back(new UploadFileElementReader(
4792 base::MessageLoopProxy::current().get(),
4793 base::FilePath(FILE_PATH_LITERAL(
4794 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
4798 r
.set_upload(make_scoped_ptr(
4799 new UploadDataStream(element_readers
.Pass(), 0)));
4802 EXPECT_TRUE(r
.is_pending());
4804 base::RunLoop().Run();
4806 EXPECT_TRUE(d
.request_failed());
4807 EXPECT_FALSE(d
.received_data_before_response());
4808 EXPECT_EQ(0, d
.bytes_received());
4809 EXPECT_EQ(URLRequestStatus::FAILED
, r
.status().status());
4810 EXPECT_EQ(ERR_FILE_NOT_FOUND
, r
.status().error());
4814 TEST_F(URLRequestTestHTTP
, TestPostChunkedDataBeforeStart
) {
4815 ASSERT_TRUE(test_server_
.Start());
4820 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
, &default_context_
);
4821 r
.EnableChunkedUpload();
4822 r
.set_method("POST");
4823 AddChunksToUpload(&r
);
4825 EXPECT_TRUE(r
.is_pending());
4827 base::RunLoop().Run();
4829 VerifyReceivedDataMatchesChunks(&r
, &d
);
4833 TEST_F(URLRequestTestHTTP
, TestPostChunkedDataJustAfterStart
) {
4834 ASSERT_TRUE(test_server_
.Start());
4839 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
, &default_context_
);
4840 r
.EnableChunkedUpload();
4841 r
.set_method("POST");
4843 EXPECT_TRUE(r
.is_pending());
4844 AddChunksToUpload(&r
);
4845 base::RunLoop().Run();
4847 VerifyReceivedDataMatchesChunks(&r
, &d
);
4851 TEST_F(URLRequestTestHTTP
, TestPostChunkedDataAfterStart
) {
4852 ASSERT_TRUE(test_server_
.Start());
4857 test_server_
.GetURL("echo"), DEFAULT_PRIORITY
, &d
, &default_context_
);
4858 r
.EnableChunkedUpload();
4859 r
.set_method("POST");
4861 EXPECT_TRUE(r
.is_pending());
4863 base::RunLoop().RunUntilIdle();
4864 AddChunksToUpload(&r
);
4865 base::RunLoop().Run();
4867 VerifyReceivedDataMatchesChunks(&r
, &d
);
4871 TEST_F(URLRequestTestHTTP
, ResponseHeadersTest
) {
4872 ASSERT_TRUE(test_server_
.Start());
4875 URLRequest
req(test_server_
.GetURL("files/with-headers.html"),
4880 base::RunLoop().Run();
4882 const HttpResponseHeaders
* headers
= req
.response_headers();
4884 // Simple sanity check that response_info() accesses the same data.
4885 EXPECT_EQ(headers
, req
.response_info().headers
.get());
4888 EXPECT_TRUE(headers
->GetNormalizedHeader("cache-control", &header
));
4889 EXPECT_EQ("private", header
);
4892 EXPECT_TRUE(headers
->GetNormalizedHeader("content-type", &header
));
4893 EXPECT_EQ("text/html; charset=ISO-8859-1", header
);
4895 // The response has two "X-Multiple-Entries" headers.
4896 // This verfies our output has them concatenated together.
4898 EXPECT_TRUE(headers
->GetNormalizedHeader("x-multiple-entries", &header
));
4899 EXPECT_EQ("a, b", header
);
4902 TEST_F(URLRequestTestHTTP
, ProcessSTS
) {
4903 SpawnedTestServer::SSLOptions ssl_options
;
4904 SpawnedTestServer
https_test_server(
4905 SpawnedTestServer::TYPE_HTTPS
,
4907 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
4908 ASSERT_TRUE(https_test_server
.Start());
4911 URLRequest
request(https_test_server
.GetURL("files/hsts-headers.html"),
4916 base::RunLoop().Run();
4918 TransportSecurityState
* security_state
=
4919 default_context_
.transport_security_state();
4920 bool sni_available
= true;
4921 TransportSecurityState::DomainState domain_state
;
4922 EXPECT_TRUE(security_state
->GetDomainState(
4923 SpawnedTestServer::kLocalhost
, sni_available
, &domain_state
));
4924 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS
,
4925 domain_state
.upgrade_mode
);
4926 EXPECT_TRUE(domain_state
.sts_include_subdomains
);
4927 EXPECT_FALSE(domain_state
.pkp_include_subdomains
);
4928 #if defined(OS_ANDROID)
4929 // Android's CertVerifyProc does not (yet) handle pins.
4931 EXPECT_FALSE(domain_state
.HasPublicKeyPins());
4935 // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
4936 // reject HPKP headers, and a test setting only HPKP headers will fail (no
4937 // DomainState present because header rejected).
4938 #if defined(OS_ANDROID)
4939 #define MAYBE_ProcessPKP DISABLED_ProcessPKP
4941 #define MAYBE_ProcessPKP ProcessPKP
4944 // Tests that enabling HPKP on a domain does not affect the HSTS
4945 // validity/expiration.
4946 TEST_F(URLRequestTestHTTP
, MAYBE_ProcessPKP
) {
4947 SpawnedTestServer::SSLOptions ssl_options
;
4948 SpawnedTestServer
https_test_server(
4949 SpawnedTestServer::TYPE_HTTPS
,
4951 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
4952 ASSERT_TRUE(https_test_server
.Start());
4955 URLRequest
request(https_test_server
.GetURL("files/hpkp-headers.html"),
4960 base::RunLoop().Run();
4962 TransportSecurityState
* security_state
=
4963 default_context_
.transport_security_state();
4964 bool sni_available
= true;
4965 TransportSecurityState::DomainState domain_state
;
4966 EXPECT_TRUE(security_state
->GetDomainState(
4967 SpawnedTestServer::kLocalhost
, sni_available
, &domain_state
));
4968 EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT
,
4969 domain_state
.upgrade_mode
);
4970 EXPECT_FALSE(domain_state
.sts_include_subdomains
);
4971 EXPECT_FALSE(domain_state
.pkp_include_subdomains
);
4972 EXPECT_TRUE(domain_state
.HasPublicKeyPins());
4973 EXPECT_NE(domain_state
.upgrade_expiry
,
4974 domain_state
.dynamic_spki_hashes_expiry
);
4977 TEST_F(URLRequestTestHTTP
, ProcessSTSOnce
) {
4978 SpawnedTestServer::SSLOptions ssl_options
;
4979 SpawnedTestServer
https_test_server(
4980 SpawnedTestServer::TYPE_HTTPS
,
4982 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
4983 ASSERT_TRUE(https_test_server
.Start());
4987 https_test_server
.GetURL("files/hsts-multiple-headers.html"),
4992 base::RunLoop().Run();
4994 // We should have set parameters from the first header, not the second.
4995 TransportSecurityState
* security_state
=
4996 default_context_
.transport_security_state();
4997 bool sni_available
= true;
4998 TransportSecurityState::DomainState domain_state
;
4999 EXPECT_TRUE(security_state
->GetDomainState(
5000 SpawnedTestServer::kLocalhost
, sni_available
, &domain_state
));
5001 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS
,
5002 domain_state
.upgrade_mode
);
5003 EXPECT_FALSE(domain_state
.sts_include_subdomains
);
5004 EXPECT_FALSE(domain_state
.pkp_include_subdomains
);
5007 TEST_F(URLRequestTestHTTP
, ProcessSTSAndPKP
) {
5008 SpawnedTestServer::SSLOptions ssl_options
;
5009 SpawnedTestServer
https_test_server(
5010 SpawnedTestServer::TYPE_HTTPS
,
5012 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5013 ASSERT_TRUE(https_test_server
.Start());
5017 https_test_server
.GetURL("files/hsts-and-hpkp-headers.html"),
5022 base::RunLoop().Run();
5024 // We should have set parameters from the first header, not the second.
5025 TransportSecurityState
* security_state
=
5026 default_context_
.transport_security_state();
5027 bool sni_available
= true;
5028 TransportSecurityState::DomainState domain_state
;
5029 EXPECT_TRUE(security_state
->GetDomainState(
5030 SpawnedTestServer::kLocalhost
, sni_available
, &domain_state
));
5031 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS
,
5032 domain_state
.upgrade_mode
);
5033 #if defined(OS_ANDROID)
5034 // Android's CertVerifyProc does not (yet) handle pins.
5036 EXPECT_TRUE(domain_state
.HasPublicKeyPins());
5038 EXPECT_NE(domain_state
.upgrade_expiry
,
5039 domain_state
.dynamic_spki_hashes_expiry
);
5041 // Even though there is an HSTS header asserting includeSubdomains, it is
5042 // the *second* such header, and we MUST process only the first.
5043 EXPECT_FALSE(domain_state
.sts_include_subdomains
);
5044 // includeSubdomains does not occur in the test HPKP header.
5045 EXPECT_FALSE(domain_state
.pkp_include_subdomains
);
5048 // Tests that when multiple HPKP headers are present, asserting different
5049 // policies, that only the first such policy is processed.
5050 TEST_F(URLRequestTestHTTP
, ProcessSTSAndPKP2
) {
5051 SpawnedTestServer::SSLOptions ssl_options
;
5052 SpawnedTestServer
https_test_server(
5053 SpawnedTestServer::TYPE_HTTPS
,
5055 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5056 ASSERT_TRUE(https_test_server
.Start());
5060 https_test_server
.GetURL("files/hsts-and-hpkp-headers2.html"),
5065 base::RunLoop().Run();
5067 TransportSecurityState
* security_state
=
5068 default_context_
.transport_security_state();
5069 bool sni_available
= true;
5070 TransportSecurityState::DomainState domain_state
;
5071 EXPECT_TRUE(security_state
->GetDomainState(
5072 SpawnedTestServer::kLocalhost
, sni_available
, &domain_state
));
5073 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS
,
5074 domain_state
.upgrade_mode
);
5075 #if defined(OS_ANDROID)
5076 // Android's CertVerifyProc does not (yet) handle pins.
5078 EXPECT_TRUE(domain_state
.HasPublicKeyPins());
5080 EXPECT_NE(domain_state
.upgrade_expiry
,
5081 domain_state
.dynamic_spki_hashes_expiry
);
5083 EXPECT_TRUE(domain_state
.sts_include_subdomains
);
5084 EXPECT_FALSE(domain_state
.pkp_include_subdomains
);
5087 TEST_F(URLRequestTestHTTP
, ContentTypeNormalizationTest
) {
5088 ASSERT_TRUE(test_server_
.Start());
5091 URLRequest
req(test_server_
.GetURL("files/content-type-normalization.html"),
5096 base::RunLoop().Run();
5098 std::string mime_type
;
5099 req
.GetMimeType(&mime_type
);
5100 EXPECT_EQ("text/html", mime_type
);
5102 std::string charset
;
5103 req
.GetCharset(&charset
);
5104 EXPECT_EQ("utf-8", charset
);
5108 TEST_F(URLRequestTestHTTP
, ProtocolHandlerAndFactoryRestrictRedirects
) {
5109 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5110 GURL
file_url("file:///foo.txt");
5111 GURL
data_url("data:,foo");
5112 FileProtocolHandler
file_protocol_handler(base::MessageLoopProxy::current());
5113 EXPECT_FALSE(file_protocol_handler
.IsSafeRedirectTarget(file_url
));
5114 DataProtocolHandler data_protocol_handler
;
5115 EXPECT_FALSE(data_protocol_handler
.IsSafeRedirectTarget(data_url
));
5117 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5118 EXPECT_FALSE(job_factory_
.IsSafeRedirectTarget(file_url
));
5119 EXPECT_FALSE(job_factory_
.IsSafeRedirectTarget(data_url
));
5122 TEST_F(URLRequestTestHTTP
, RestrictFileRedirects
) {
5123 ASSERT_TRUE(test_server_
.Start());
5126 URLRequest
req(test_server_
.GetURL("files/redirect-to-file.html"),
5131 base::RunLoop().Run();
5133 EXPECT_EQ(URLRequestStatus::FAILED
, req
.status().status());
5134 EXPECT_EQ(ERR_UNSAFE_REDIRECT
, req
.status().error());
5137 TEST_F(URLRequestTestHTTP
, RestrictDataRedirects
) {
5138 ASSERT_TRUE(test_server_
.Start());
5141 URLRequest
req(test_server_
.GetURL("files/redirect-to-data.html"),
5146 base::MessageLoop::current()->Run();
5148 EXPECT_EQ(URLRequestStatus::FAILED
, req
.status().status());
5149 EXPECT_EQ(ERR_UNSAFE_REDIRECT
, req
.status().error());
5152 TEST_F(URLRequestTestHTTP
, RedirectToInvalidURL
) {
5153 ASSERT_TRUE(test_server_
.Start());
5156 URLRequest
req(test_server_
.GetURL("files/redirect-to-invalid-url.html"),
5161 base::RunLoop().Run();
5163 EXPECT_EQ(URLRequestStatus::FAILED
, req
.status().status());
5164 EXPECT_EQ(ERR_INVALID_URL
, req
.status().error());
5167 // Make sure redirects are cached, despite not reading their bodies.
5168 TEST_F(URLRequestTestHTTP
, CacheRedirect
) {
5169 ASSERT_TRUE(test_server_
.Start());
5171 test_server_
.GetURL("files/redirect302-to-echo-cacheable");
5175 URLRequest
req(redirect_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
5177 base::RunLoop().Run();
5178 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
5179 EXPECT_EQ(1, d
.received_redirect_count());
5180 EXPECT_EQ(test_server_
.GetURL("echo"), req
.url());
5185 d
.set_quit_on_redirect(true);
5186 URLRequest
req(redirect_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
5188 base::RunLoop().Run();
5190 EXPECT_EQ(1, d
.received_redirect_count());
5191 EXPECT_EQ(0, d
.response_started_count());
5192 EXPECT_TRUE(req
.was_cached());
5194 req
.FollowDeferredRedirect();
5195 base::RunLoop().Run();
5196 EXPECT_EQ(1, d
.received_redirect_count());
5197 EXPECT_EQ(1, d
.response_started_count());
5198 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
5199 EXPECT_EQ(test_server_
.GetURL("echo"), req
.url());
5203 // Make sure a request isn't cached when a NetworkDelegate forces a redirect
5204 // when the headers are read, since the body won't have been read.
5205 TEST_F(URLRequestTestHTTP
, NoCacheOnNetworkDelegateRedirect
) {
5206 ASSERT_TRUE(test_server_
.Start());
5207 // URL that is normally cached.
5208 GURL initial_url
= test_server_
.GetURL("cachetime");
5211 // Set up the TestNetworkDelegate tp force a redirect.
5212 GURL redirect_to_url
= test_server_
.GetURL("echo");
5213 default_network_delegate_
.set_redirect_on_headers_received_url(
5217 URLRequest
req(initial_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
5219 base::RunLoop().Run();
5220 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
5221 EXPECT_EQ(1, d
.received_redirect_count());
5222 EXPECT_EQ(redirect_to_url
, req
.url());
5227 URLRequest
req(initial_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
5229 base::RunLoop().Run();
5231 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
5232 EXPECT_FALSE(req
.was_cached());
5233 EXPECT_EQ(0, d
.received_redirect_count());
5234 EXPECT_EQ(initial_url
, req
.url());
5238 TEST_F(URLRequestTestHTTP
, NoUserPassInReferrer
) {
5239 ASSERT_TRUE(test_server_
.Start());
5242 URLRequest
req(test_server_
.GetURL("echoheader?Referer"),
5246 req
.SetReferrer("http://user:pass@foo.com/");
5248 base::RunLoop().Run();
5250 EXPECT_EQ(std::string("http://foo.com/"), d
.data_received());
5253 TEST_F(URLRequestTestHTTP
, NoFragmentInReferrer
) {
5254 ASSERT_TRUE(test_server_
.Start());
5257 URLRequest
req(test_server_
.GetURL("echoheader?Referer"),
5261 req
.SetReferrer("http://foo.com/test#fragment");
5263 base::RunLoop().Run();
5265 EXPECT_EQ(std::string("http://foo.com/test"), d
.data_received());
5268 TEST_F(URLRequestTestHTTP
, EmptyReferrerAfterValidReferrer
) {
5269 ASSERT_TRUE(test_server_
.Start());
5272 URLRequest
req(test_server_
.GetURL("echoheader?Referer"),
5276 req
.SetReferrer("http://foo.com/test#fragment");
5277 req
.SetReferrer("");
5279 base::RunLoop().Run();
5281 EXPECT_EQ(std::string("None"), d
.data_received());
5284 // Defer network start and then resume, checking that the request was a success
5285 // and bytes were received.
5286 TEST_F(URLRequestTestHTTP
, DeferredBeforeNetworkStart
) {
5287 ASSERT_TRUE(test_server_
.Start());
5291 d
.set_quit_on_network_start(true);
5292 GURL
test_url(test_server_
.GetURL("echo"));
5293 URLRequest
req(test_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
5296 base::RunLoop().Run();
5298 EXPECT_EQ(1, d
.received_before_network_start_count());
5299 EXPECT_EQ(0, d
.response_started_count());
5301 req
.ResumeNetworkStart();
5302 base::RunLoop().Run();
5304 EXPECT_EQ(1, d
.response_started_count());
5305 EXPECT_NE(0, d
.bytes_received());
5306 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
5310 // Check that OnBeforeNetworkStart is only called once even if there is a
5312 TEST_F(URLRequestTestHTTP
, BeforeNetworkStartCalledOnce
) {
5313 ASSERT_TRUE(test_server_
.Start());
5317 d
.set_quit_on_redirect(true);
5318 d
.set_quit_on_network_start(true);
5319 URLRequest
req(test_server_
.GetURL("server-redirect?echo"),
5325 base::RunLoop().Run();
5327 EXPECT_EQ(1, d
.received_before_network_start_count());
5328 EXPECT_EQ(0, d
.response_started_count());
5329 EXPECT_EQ(0, d
.received_redirect_count());
5331 req
.ResumeNetworkStart();
5332 base::RunLoop().Run();
5334 EXPECT_EQ(1, d
.received_redirect_count());
5335 req
.FollowDeferredRedirect();
5336 base::RunLoop().Run();
5338 // Check that the redirect's new network transaction does not get propagated
5339 // to a second OnBeforeNetworkStart() notification.
5340 EXPECT_EQ(1, d
.received_before_network_start_count());
5342 EXPECT_EQ(1, d
.response_started_count());
5343 EXPECT_NE(0, d
.bytes_received());
5344 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
5348 // Cancel the request after learning that the request would use the network.
5349 TEST_F(URLRequestTestHTTP
, CancelOnBeforeNetworkStart
) {
5350 ASSERT_TRUE(test_server_
.Start());
5354 d
.set_quit_on_network_start(true);
5355 GURL
test_url(test_server_
.GetURL("echo"));
5356 URLRequest
req(test_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
5359 base::RunLoop().Run();
5361 EXPECT_EQ(1, d
.received_before_network_start_count());
5362 EXPECT_EQ(0, d
.response_started_count());
5365 base::RunLoop().Run();
5367 EXPECT_EQ(1, d
.response_started_count());
5368 EXPECT_EQ(0, d
.bytes_received());
5369 EXPECT_EQ(URLRequestStatus::CANCELED
, req
.status().status());
5373 TEST_F(URLRequestTestHTTP
, CancelRedirect
) {
5374 ASSERT_TRUE(test_server_
.Start());
5378 d
.set_cancel_in_received_redirect(true);
5379 URLRequest
req(test_server_
.GetURL("files/redirect-test.html"),
5384 base::RunLoop().Run();
5386 EXPECT_EQ(1, d
.response_started_count());
5387 EXPECT_EQ(0, d
.bytes_received());
5388 EXPECT_FALSE(d
.received_data_before_response());
5389 EXPECT_EQ(URLRequestStatus::CANCELED
, req
.status().status());
5393 TEST_F(URLRequestTestHTTP
, DeferredRedirect
) {
5394 ASSERT_TRUE(test_server_
.Start());
5398 d
.set_quit_on_redirect(true);
5399 GURL
test_url(test_server_
.GetURL("files/redirect-test.html"));
5400 URLRequest
req(test_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
5403 base::RunLoop().Run();
5405 EXPECT_EQ(1, d
.received_redirect_count());
5407 req
.FollowDeferredRedirect();
5408 base::RunLoop().Run();
5410 EXPECT_EQ(1, d
.response_started_count());
5411 EXPECT_FALSE(d
.received_data_before_response());
5412 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
5414 base::FilePath path
;
5415 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
5416 path
= path
.Append(FILE_PATH_LITERAL("net"));
5417 path
= path
.Append(FILE_PATH_LITERAL("data"));
5418 path
= path
.Append(FILE_PATH_LITERAL("url_request_unittest"));
5419 path
= path
.Append(FILE_PATH_LITERAL("with-headers.html"));
5421 std::string contents
;
5422 EXPECT_TRUE(base::ReadFileToString(path
, &contents
));
5423 EXPECT_EQ(contents
, d
.data_received());
5427 TEST_F(URLRequestTestHTTP
, DeferredRedirect_GetFullRequestHeaders
) {
5428 ASSERT_TRUE(test_server_
.Start());
5432 d
.set_quit_on_redirect(true);
5433 GURL
test_url(test_server_
.GetURL("files/redirect-test.html"));
5434 URLRequest
req(test_url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
5436 EXPECT_FALSE(d
.have_full_request_headers());
5439 base::RunLoop().Run();
5441 EXPECT_EQ(1, d
.received_redirect_count());
5442 EXPECT_TRUE(d
.have_full_request_headers());
5443 CheckFullRequestHeaders(d
.full_request_headers(), test_url
);
5444 d
.ClearFullRequestHeaders();
5446 req
.FollowDeferredRedirect();
5447 base::RunLoop().Run();
5449 GURL
target_url(test_server_
.GetURL("files/with-headers.html"));
5450 EXPECT_EQ(1, d
.response_started_count());
5451 EXPECT_TRUE(d
.have_full_request_headers());
5452 CheckFullRequestHeaders(d
.full_request_headers(), target_url
);
5453 EXPECT_FALSE(d
.received_data_before_response());
5454 EXPECT_EQ(URLRequestStatus::SUCCESS
, req
.status().status());
5456 base::FilePath path
;
5457 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
5458 path
= path
.Append(FILE_PATH_LITERAL("net"));
5459 path
= path
.Append(FILE_PATH_LITERAL("data"));
5460 path
= path
.Append(FILE_PATH_LITERAL("url_request_unittest"));
5461 path
= path
.Append(FILE_PATH_LITERAL("with-headers.html"));
5463 std::string contents
;
5464 EXPECT_TRUE(base::ReadFileToString(path
, &contents
));
5465 EXPECT_EQ(contents
, d
.data_received());
5469 TEST_F(URLRequestTestHTTP
, CancelDeferredRedirect
) {
5470 ASSERT_TRUE(test_server_
.Start());
5474 d
.set_quit_on_redirect(true);
5475 URLRequest
req(test_server_
.GetURL("files/redirect-test.html"),
5480 base::RunLoop().Run();
5482 EXPECT_EQ(1, d
.received_redirect_count());
5485 base::RunLoop().Run();
5487 EXPECT_EQ(1, d
.response_started_count());
5488 EXPECT_EQ(0, d
.bytes_received());
5489 EXPECT_FALSE(d
.received_data_before_response());
5490 EXPECT_EQ(URLRequestStatus::CANCELED
, req
.status().status());
5494 TEST_F(URLRequestTestHTTP
, VaryHeader
) {
5495 ASSERT_TRUE(test_server_
.Start());
5497 // Populate the cache.
5500 URLRequest
req(test_server_
.GetURL("echoheadercache?foo"),
5504 HttpRequestHeaders headers
;
5505 headers
.SetHeader("foo", "1");
5506 req
.SetExtraRequestHeaders(headers
);
5508 base::RunLoop().Run();
5510 LoadTimingInfo load_timing_info
;
5511 req
.GetLoadTimingInfo(&load_timing_info
);
5512 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
5515 // Expect a cache hit.
5518 URLRequest
req(test_server_
.GetURL("echoheadercache?foo"),
5522 HttpRequestHeaders headers
;
5523 headers
.SetHeader("foo", "1");
5524 req
.SetExtraRequestHeaders(headers
);
5526 base::RunLoop().Run();
5528 EXPECT_TRUE(req
.was_cached());
5530 LoadTimingInfo load_timing_info
;
5531 req
.GetLoadTimingInfo(&load_timing_info
);
5532 TestLoadTimingCacheHitNoNetwork(load_timing_info
);
5535 // Expect a cache miss.
5538 URLRequest
req(test_server_
.GetURL("echoheadercache?foo"),
5542 HttpRequestHeaders headers
;
5543 headers
.SetHeader("foo", "2");
5544 req
.SetExtraRequestHeaders(headers
);
5546 base::RunLoop().Run();
5548 EXPECT_FALSE(req
.was_cached());
5550 LoadTimingInfo load_timing_info
;
5551 req
.GetLoadTimingInfo(&load_timing_info
);
5552 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
5556 TEST_F(URLRequestTestHTTP
, BasicAuth
) {
5557 ASSERT_TRUE(test_server_
.Start());
5559 // populate the cache
5562 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
5564 URLRequest
r(test_server_
.GetURL("auth-basic"),
5570 base::RunLoop().Run();
5572 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
5575 // repeat request with end-to-end validation. since auth-basic results in a
5576 // cachable page, we expect this test to result in a 304. in which case, the
5577 // response should be fetched from the cache.
5580 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
5582 URLRequest
r(test_server_
.GetURL("auth-basic"),
5586 r
.SetLoadFlags(LOAD_VALIDATE_CACHE
);
5589 base::RunLoop().Run();
5591 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
5593 // Should be the same cached document.
5594 EXPECT_TRUE(r
.was_cached());
5598 // Check that Set-Cookie headers in 401 responses are respected.
5599 // http://crbug.com/6450
5600 TEST_F(URLRequestTestHTTP
, BasicAuthWithCookies
) {
5601 ASSERT_TRUE(test_server_
.Start());
5603 GURL url_requiring_auth
=
5604 test_server_
.GetURL("auth-basic?set-cookie-if-challenged");
5606 // Request a page that will give a 401 containing a Set-Cookie header.
5607 // Verify that when the transaction is restarted, it includes the new cookie.
5609 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
5610 TestURLRequestContext
context(true);
5611 context
.set_network_delegate(&network_delegate
);
5615 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
5617 URLRequest
r(url_requiring_auth
, DEFAULT_PRIORITY
, &d
, &context
);
5620 base::RunLoop().Run();
5622 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
5624 // Make sure we sent the cookie in the restarted transaction.
5625 EXPECT_TRUE(d
.data_received().find("Cookie: got_challenged=true")
5626 != std::string::npos
);
5629 // Same test as above, except this time the restart is initiated earlier
5630 // (without user intervention since identity is embedded in the URL).
5632 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
5633 TestURLRequestContext
context(true);
5634 context
.set_network_delegate(&network_delegate
);
5639 GURL::Replacements replacements
;
5640 std::string
username("user2");
5641 std::string
password("secret");
5642 replacements
.SetUsernameStr(username
);
5643 replacements
.SetPasswordStr(password
);
5644 GURL url_with_identity
= url_requiring_auth
.ReplaceComponents(replacements
);
5646 URLRequest
r(url_with_identity
, DEFAULT_PRIORITY
, &d
, &context
);
5649 base::RunLoop().Run();
5651 EXPECT_TRUE(d
.data_received().find("user2/secret") != std::string::npos
);
5653 // Make sure we sent the cookie in the restarted transaction.
5654 EXPECT_TRUE(d
.data_received().find("Cookie: got_challenged=true")
5655 != std::string::npos
);
5659 // Tests that load timing works as expected with auth and the cache.
5660 TEST_F(URLRequestTestHTTP
, BasicAuthLoadTiming
) {
5661 ASSERT_TRUE(test_server_
.Start());
5663 // populate the cache
5666 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
5668 URLRequest
r(test_server_
.GetURL("auth-basic"),
5674 base::RunLoop().Run();
5676 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
5678 LoadTimingInfo load_timing_info_before_auth
;
5679 EXPECT_TRUE(default_network_delegate_
.GetLoadTimingInfoBeforeAuth(
5680 &load_timing_info_before_auth
));
5681 TestLoadTimingNotReused(load_timing_info_before_auth
,
5682 CONNECT_TIMING_HAS_DNS_TIMES
);
5684 LoadTimingInfo load_timing_info
;
5685 r
.GetLoadTimingInfo(&load_timing_info
);
5686 // The test server does not support keep alive sockets, so the second
5687 // request with auth should use a new socket.
5688 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
5689 EXPECT_NE(load_timing_info_before_auth
.socket_log_id
,
5690 load_timing_info
.socket_log_id
);
5691 EXPECT_LE(load_timing_info_before_auth
.receive_headers_end
,
5692 load_timing_info
.connect_timing
.connect_start
);
5695 // Repeat request with end-to-end validation. Since auth-basic results in a
5696 // cachable page, we expect this test to result in a 304. In which case, the
5697 // response should be fetched from the cache.
5700 d
.set_credentials(AuthCredentials(kUser
, kSecret
));
5702 URLRequest
r(test_server_
.GetURL("auth-basic"),
5706 r
.SetLoadFlags(LOAD_VALIDATE_CACHE
);
5709 base::RunLoop().Run();
5711 EXPECT_TRUE(d
.data_received().find("user/secret") != std::string::npos
);
5713 // Should be the same cached document.
5714 EXPECT_TRUE(r
.was_cached());
5716 // Since there was a request that went over the wire, the load timing
5717 // information should include connection times.
5718 LoadTimingInfo load_timing_info
;
5719 r
.GetLoadTimingInfo(&load_timing_info
);
5720 TestLoadTimingNotReused(load_timing_info
, CONNECT_TIMING_HAS_DNS_TIMES
);
5724 // In this test, we do a POST which the server will 302 redirect.
5725 // The subsequent transaction should use GET, and should not send the
5726 // Content-Type header.
5727 // http://code.google.com/p/chromium/issues/detail?id=843
5728 TEST_F(URLRequestTestHTTP
, Post302RedirectGet
) {
5729 ASSERT_TRUE(test_server_
.Start());
5731 const char kData
[] = "hello world";
5734 URLRequest
req(test_server_
.GetURL("files/redirect-to-echoall"),
5738 req
.set_method("POST");
5739 req
.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData
)));
5741 // Set headers (some of which are specific to the POST).
5742 HttpRequestHeaders headers
;
5743 headers
.AddHeadersFromString(
5744 "Content-Type: multipart/form-data; "
5745 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
5746 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
5747 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
5748 "Accept-Language: en-US,en\r\n"
5749 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
5750 "Content-Length: 11\r\n"
5751 "Origin: http://localhost:1337/");
5752 req
.SetExtraRequestHeaders(headers
);
5754 base::RunLoop().Run();
5756 std::string mime_type
;
5757 req
.GetMimeType(&mime_type
);
5758 EXPECT_EQ("text/html", mime_type
);
5760 const std::string
& data
= d
.data_received();
5762 // Check that the post-specific headers were stripped:
5763 EXPECT_FALSE(ContainsString(data
, "Content-Length:"));
5764 EXPECT_FALSE(ContainsString(data
, "Content-Type:"));
5765 EXPECT_FALSE(ContainsString(data
, "Origin:"));
5767 // These extra request headers should not have been stripped.
5768 EXPECT_TRUE(ContainsString(data
, "Accept:"));
5769 EXPECT_TRUE(ContainsString(data
, "Accept-Language:"));
5770 EXPECT_TRUE(ContainsString(data
, "Accept-Charset:"));
5773 // The following tests check that we handle mutating the request method for
5774 // HTTP redirects as expected.
5775 // See http://crbug.com/56373 and http://crbug.com/102130.
5777 TEST_F(URLRequestTestHTTP
, Redirect301Tests
) {
5778 ASSERT_TRUE(test_server_
.Start());
5780 const GURL url
= test_server_
.GetURL("files/redirect301-to-echo");
5782 HTTPRedirectMethodTest(url
, "POST", "GET", true);
5783 HTTPRedirectMethodTest(url
, "PUT", "PUT", true);
5784 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
5787 TEST_F(URLRequestTestHTTP
, Redirect302Tests
) {
5788 ASSERT_TRUE(test_server_
.Start());
5790 const GURL url
= test_server_
.GetURL("files/redirect302-to-echo");
5792 HTTPRedirectMethodTest(url
, "POST", "GET", true);
5793 HTTPRedirectMethodTest(url
, "PUT", "PUT", true);
5794 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
5797 TEST_F(URLRequestTestHTTP
, Redirect303Tests
) {
5798 ASSERT_TRUE(test_server_
.Start());
5800 const GURL url
= test_server_
.GetURL("files/redirect303-to-echo");
5802 HTTPRedirectMethodTest(url
, "POST", "GET", true);
5803 HTTPRedirectMethodTest(url
, "PUT", "GET", true);
5804 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
5807 TEST_F(URLRequestTestHTTP
, Redirect307Tests
) {
5808 ASSERT_TRUE(test_server_
.Start());
5810 const GURL url
= test_server_
.GetURL("files/redirect307-to-echo");
5812 HTTPRedirectMethodTest(url
, "POST", "POST", true);
5813 HTTPRedirectMethodTest(url
, "PUT", "PUT", true);
5814 HTTPRedirectMethodTest(url
, "HEAD", "HEAD", false);
5817 TEST_F(URLRequestTestHTTP
, InterceptPost302RedirectGet
) {
5818 ASSERT_TRUE(test_server_
.Start());
5820 const char kData
[] = "hello world";
5823 URLRequest
req(test_server_
.GetURL("empty.html"),
5827 req
.set_method("POST");
5828 req
.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData
)));
5829 HttpRequestHeaders headers
;
5830 headers
.SetHeader(HttpRequestHeaders::kContentLength
,
5831 base::UintToString(arraysize(kData
) - 1));
5832 req
.SetExtraRequestHeaders(headers
);
5834 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
5835 &req
, &default_network_delegate_
, test_server_
.GetURL("echo"),
5836 URLRequestRedirectJob::REDIRECT_302_FOUND
, "Very Good Reason");
5837 AddTestInterceptor()->set_main_intercept_job(job
);
5840 base::RunLoop().Run();
5841 EXPECT_EQ("GET", req
.method());
5844 TEST_F(URLRequestTestHTTP
, InterceptPost307RedirectPost
) {
5845 ASSERT_TRUE(test_server_
.Start());
5847 const char kData
[] = "hello world";
5850 URLRequest
req(test_server_
.GetURL("empty.html"),
5854 req
.set_method("POST");
5855 req
.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData
)));
5856 HttpRequestHeaders headers
;
5857 headers
.SetHeader(HttpRequestHeaders::kContentLength
,
5858 base::UintToString(arraysize(kData
) - 1));
5859 req
.SetExtraRequestHeaders(headers
);
5861 URLRequestRedirectJob
* job
= new URLRequestRedirectJob(
5862 &req
, &default_network_delegate_
, test_server_
.GetURL("echo"),
5863 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT
,
5864 "Very Good Reason");
5865 AddTestInterceptor()->set_main_intercept_job(job
);
5868 base::RunLoop().Run();
5869 EXPECT_EQ("POST", req
.method());
5870 EXPECT_EQ(kData
, d
.data_received());
5873 // Check that default A-L header is sent.
5874 TEST_F(URLRequestTestHTTP
, DefaultAcceptLanguage
) {
5875 ASSERT_TRUE(test_server_
.Start());
5877 StaticHttpUserAgentSettings
settings("en", std::string());
5878 TestNetworkDelegate network_delegate
; // Must outlive URLRequests.
5879 TestURLRequestContext
context(true);
5880 context
.set_network_delegate(&network_delegate
);
5881 context
.set_http_user_agent_settings(&settings
);
5885 URLRequest
req(test_server_
.GetURL("echoheader?Accept-Language"),
5890 base::RunLoop().Run();
5891 EXPECT_EQ("en", d
.data_received());
5894 // Check that an empty A-L header is not sent. http://crbug.com/77365.
5895 TEST_F(URLRequestTestHTTP
, EmptyAcceptLanguage
) {
5896 ASSERT_TRUE(test_server_
.Start());
5898 std::string empty_string
; // Avoid most vexing parse on line below.
5899 StaticHttpUserAgentSettings
settings(empty_string
, empty_string
);
5900 TestNetworkDelegate network_delegate
; // Must outlive URLRequests.
5901 TestURLRequestContext
context(true);
5902 context
.set_network_delegate(&network_delegate
);
5904 // We override the language after initialization because empty entries
5905 // get overridden by Init().
5906 context
.set_http_user_agent_settings(&settings
);
5909 URLRequest
req(test_server_
.GetURL("echoheader?Accept-Language"),
5914 base::RunLoop().Run();
5915 EXPECT_EQ("None", d
.data_received());
5918 // Check that if request overrides the A-L header, the default is not appended.
5919 // See http://crbug.com/20894
5920 TEST_F(URLRequestTestHTTP
, OverrideAcceptLanguage
) {
5921 ASSERT_TRUE(test_server_
.Start());
5924 URLRequest
req(test_server_
.GetURL("echoheader?Accept-Language"),
5928 HttpRequestHeaders headers
;
5929 headers
.SetHeader(HttpRequestHeaders::kAcceptLanguage
, "ru");
5930 req
.SetExtraRequestHeaders(headers
);
5932 base::RunLoop().Run();
5933 EXPECT_EQ(std::string("ru"), d
.data_received());
5936 // Check that default A-E header is sent.
5937 TEST_F(URLRequestTestHTTP
, DefaultAcceptEncoding
) {
5938 ASSERT_TRUE(test_server_
.Start());
5941 URLRequest
req(test_server_
.GetURL("echoheader?Accept-Encoding"),
5945 HttpRequestHeaders headers
;
5946 req
.SetExtraRequestHeaders(headers
);
5948 base::RunLoop().Run();
5949 EXPECT_TRUE(ContainsString(d
.data_received(), "gzip"));
5952 // Check that if request overrides the A-E header, the default is not appended.
5953 // See http://crbug.com/47381
5954 TEST_F(URLRequestTestHTTP
, OverrideAcceptEncoding
) {
5955 ASSERT_TRUE(test_server_
.Start());
5958 URLRequest
req(test_server_
.GetURL("echoheader?Accept-Encoding"),
5962 HttpRequestHeaders headers
;
5963 headers
.SetHeader(HttpRequestHeaders::kAcceptEncoding
, "identity");
5964 req
.SetExtraRequestHeaders(headers
);
5966 base::RunLoop().Run();
5967 EXPECT_FALSE(ContainsString(d
.data_received(), "gzip"));
5968 EXPECT_TRUE(ContainsString(d
.data_received(), "identity"));
5971 // Check that setting the A-C header sends the proper header.
5972 TEST_F(URLRequestTestHTTP
, SetAcceptCharset
) {
5973 ASSERT_TRUE(test_server_
.Start());
5976 URLRequest
req(test_server_
.GetURL("echoheader?Accept-Charset"),
5980 HttpRequestHeaders headers
;
5981 headers
.SetHeader(HttpRequestHeaders::kAcceptCharset
, "koi-8r");
5982 req
.SetExtraRequestHeaders(headers
);
5984 base::RunLoop().Run();
5985 EXPECT_EQ(std::string("koi-8r"), d
.data_received());
5988 // Check that default User-Agent header is sent.
5989 TEST_F(URLRequestTestHTTP
, DefaultUserAgent
) {
5990 ASSERT_TRUE(test_server_
.Start());
5993 URLRequest
req(test_server_
.GetURL("echoheader?User-Agent"),
5998 base::RunLoop().Run();
5999 EXPECT_EQ(req
.context()->http_user_agent_settings()->GetUserAgent(),
6003 // Check that if request overrides the User-Agent header,
6004 // the default is not appended.
6005 TEST_F(URLRequestTestHTTP
, OverrideUserAgent
) {
6006 ASSERT_TRUE(test_server_
.Start());
6009 URLRequest
req(test_server_
.GetURL("echoheader?User-Agent"),
6013 HttpRequestHeaders headers
;
6014 headers
.SetHeader(HttpRequestHeaders::kUserAgent
, "Lynx (textmode)");
6015 req
.SetExtraRequestHeaders(headers
);
6017 base::RunLoop().Run();
6018 EXPECT_EQ(std::string("Lynx (textmode)"), d
.data_received());
6021 // Check that a NULL HttpUserAgentSettings causes the corresponding empty
6022 // User-Agent header to be sent but does not send the Accept-Language and
6023 // Accept-Charset headers.
6024 TEST_F(URLRequestTestHTTP
, EmptyHttpUserAgentSettings
) {
6025 ASSERT_TRUE(test_server_
.Start());
6027 TestNetworkDelegate network_delegate
; // Must outlive URLRequests.
6028 TestURLRequestContext
context(true);
6029 context
.set_network_delegate(&network_delegate
);
6031 // We override the HttpUserAgentSettings after initialization because empty
6032 // entries get overridden by Init().
6033 context
.set_http_user_agent_settings(NULL
);
6036 const char* request
;
6037 const char* expected_response
;
6038 } tests
[] = { { "echoheader?Accept-Language", "None" },
6039 { "echoheader?Accept-Charset", "None" },
6040 { "echoheader?User-Agent", "" } };
6042 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(tests
); i
++) {
6045 test_server_
.GetURL(tests
[i
].request
), DEFAULT_PRIORITY
, &d
, &context
);
6047 base::RunLoop().Run();
6048 EXPECT_EQ(tests
[i
].expected_response
, d
.data_received())
6049 << " Request = \"" << tests
[i
].request
<< "\"";
6053 // Make sure that URLRequest passes on its priority updates to
6054 // newly-created jobs after the first one.
6055 TEST_F(URLRequestTestHTTP
, SetSubsequentJobPriority
) {
6056 ASSERT_TRUE(test_server_
.Start());
6059 URLRequest
req(test_server_
.GetURL("empty.html"),
6063 EXPECT_EQ(DEFAULT_PRIORITY
, req
.priority());
6065 scoped_refptr
<URLRequestRedirectJob
> redirect_job
=
6066 new URLRequestRedirectJob(
6067 &req
, &default_network_delegate_
, test_server_
.GetURL("echo"),
6068 URLRequestRedirectJob::REDIRECT_302_FOUND
, "Very Good Reason");
6069 AddTestInterceptor()->set_main_intercept_job(redirect_job
.get());
6071 req
.SetPriority(LOW
);
6073 EXPECT_TRUE(req
.is_pending());
6075 scoped_refptr
<URLRequestTestJob
> job
=
6076 new URLRequestTestJob(&req
, &default_network_delegate_
);
6077 AddTestInterceptor()->set_main_intercept_job(job
.get());
6079 // Should trigger |job| to be started.
6080 base::RunLoop().Run();
6081 EXPECT_EQ(LOW
, job
->priority());
6084 // Check that creating a network request while entering/exiting suspend mode
6085 // fails as it should. This is the only case where an HttpTransactionFactory
6086 // does not return an HttpTransaction.
6087 TEST_F(URLRequestTestHTTP
, NetworkSuspendTest
) {
6088 // Create a new HttpNetworkLayer that thinks it's suspended.
6089 HttpNetworkSession::Params params
;
6090 params
.host_resolver
= default_context_
.host_resolver();
6091 params
.cert_verifier
= default_context_
.cert_verifier();
6092 params
.transport_security_state
= default_context_
.transport_security_state();
6093 params
.proxy_service
= default_context_
.proxy_service();
6094 params
.ssl_config_service
= default_context_
.ssl_config_service();
6095 params
.http_auth_handler_factory
=
6096 default_context_
.http_auth_handler_factory();
6097 params
.network_delegate
= &default_network_delegate_
;
6098 params
.http_server_properties
= default_context_
.http_server_properties();
6099 scoped_ptr
<HttpNetworkLayer
> network_layer(
6100 new HttpNetworkLayer(new HttpNetworkSession(params
)));
6101 network_layer
->OnSuspend();
6103 HttpCache
http_cache(network_layer
.release(), default_context_
.net_log(),
6104 HttpCache::DefaultBackend::InMemory(0));
6106 TestURLRequestContext
context(true);
6107 context
.set_http_transaction_factory(&http_cache
);
6111 URLRequest
req(GURL("http://127.0.0.1/"),
6116 base::RunLoop().Run();
6118 EXPECT_TRUE(d
.request_failed());
6119 EXPECT_EQ(URLRequestStatus::FAILED
, req
.status().status());
6120 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED
, req
.status().error());
6123 // Check that creating a network request while entering/exiting suspend mode
6124 // fails as it should in the case there is no cache. This is the only case
6125 // where an HttpTransactionFactory does not return an HttpTransaction.
6126 TEST_F(URLRequestTestHTTP
, NetworkSuspendTestNoCache
) {
6127 // Create a new HttpNetworkLayer that thinks it's suspended.
6128 HttpNetworkSession::Params params
;
6129 params
.host_resolver
= default_context_
.host_resolver();
6130 params
.cert_verifier
= default_context_
.cert_verifier();
6131 params
.transport_security_state
= default_context_
.transport_security_state();
6132 params
.proxy_service
= default_context_
.proxy_service();
6133 params
.ssl_config_service
= default_context_
.ssl_config_service();
6134 params
.http_auth_handler_factory
=
6135 default_context_
.http_auth_handler_factory();
6136 params
.network_delegate
= &default_network_delegate_
;
6137 params
.http_server_properties
= default_context_
.http_server_properties();
6138 HttpNetworkLayer
network_layer(new HttpNetworkSession(params
));
6139 network_layer
.OnSuspend();
6141 TestURLRequestContext
context(true);
6142 context
.set_http_transaction_factory(&network_layer
);
6146 URLRequest
req(GURL("http://127.0.0.1/"),
6151 base::RunLoop().Run();
6153 EXPECT_TRUE(d
.request_failed());
6154 EXPECT_EQ(URLRequestStatus::FAILED
, req
.status().status());
6155 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED
, req
.status().error());
6158 class HTTPSRequestTest
: public testing::Test
{
6160 HTTPSRequestTest() : default_context_(true) {
6161 default_context_
.set_network_delegate(&default_network_delegate_
);
6162 default_context_
.Init();
6164 virtual ~HTTPSRequestTest() {}
6167 TestNetworkDelegate default_network_delegate_
; // Must outlive URLRequest.
6168 TestURLRequestContext default_context_
;
6171 TEST_F(HTTPSRequestTest
, HTTPSGetTest
) {
6172 SpawnedTestServer
test_server(
6173 SpawnedTestServer::TYPE_HTTPS
,
6174 SpawnedTestServer::kLocalhost
,
6175 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6176 ASSERT_TRUE(test_server
.Start());
6180 URLRequest
r(test_server
.GetURL(std::string()),
6185 EXPECT_TRUE(r
.is_pending());
6187 base::RunLoop().Run();
6189 EXPECT_EQ(1, d
.response_started_count());
6190 EXPECT_FALSE(d
.received_data_before_response());
6191 EXPECT_NE(0, d
.bytes_received());
6192 CheckSSLInfo(r
.ssl_info());
6193 EXPECT_EQ(test_server
.host_port_pair().host(),
6194 r
.GetSocketAddress().host());
6195 EXPECT_EQ(test_server
.host_port_pair().port(),
6196 r
.GetSocketAddress().port());
6200 TEST_F(HTTPSRequestTest
, HTTPSMismatchedTest
) {
6201 SpawnedTestServer::SSLOptions
ssl_options(
6202 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
);
6203 SpawnedTestServer
test_server(
6204 SpawnedTestServer::TYPE_HTTPS
,
6206 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6207 ASSERT_TRUE(test_server
.Start());
6209 bool err_allowed
= true;
6210 for (int i
= 0; i
< 2 ; i
++, err_allowed
= !err_allowed
) {
6213 d
.set_allow_certificate_errors(err_allowed
);
6214 URLRequest
r(test_server
.GetURL(std::string()),
6220 EXPECT_TRUE(r
.is_pending());
6222 base::RunLoop().Run();
6224 EXPECT_EQ(1, d
.response_started_count());
6225 EXPECT_FALSE(d
.received_data_before_response());
6226 EXPECT_TRUE(d
.have_certificate_errors());
6228 EXPECT_NE(0, d
.bytes_received());
6229 CheckSSLInfo(r
.ssl_info());
6231 EXPECT_EQ(0, d
.bytes_received());
6237 TEST_F(HTTPSRequestTest
, HTTPSExpiredTest
) {
6238 SpawnedTestServer::SSLOptions
ssl_options(
6239 SpawnedTestServer::SSLOptions::CERT_EXPIRED
);
6240 SpawnedTestServer
test_server(
6241 SpawnedTestServer::TYPE_HTTPS
,
6243 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6244 ASSERT_TRUE(test_server
.Start());
6246 // Iterate from false to true, just so that we do the opposite of the
6247 // previous test in order to increase test coverage.
6248 bool err_allowed
= false;
6249 for (int i
= 0; i
< 2 ; i
++, err_allowed
= !err_allowed
) {
6252 d
.set_allow_certificate_errors(err_allowed
);
6253 URLRequest
r(test_server
.GetURL(std::string()),
6259 EXPECT_TRUE(r
.is_pending());
6261 base::RunLoop().Run();
6263 EXPECT_EQ(1, d
.response_started_count());
6264 EXPECT_FALSE(d
.received_data_before_response());
6265 EXPECT_TRUE(d
.have_certificate_errors());
6267 EXPECT_NE(0, d
.bytes_received());
6268 CheckSSLInfo(r
.ssl_info());
6270 EXPECT_EQ(0, d
.bytes_received());
6276 // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
6278 TEST_F(HTTPSRequestTest
, TLSv1Fallback
) {
6279 // The OpenSSL library in use may not support TLS 1.1.
6280 #if !defined(USE_OPENSSL)
6281 EXPECT_GT(kDefaultSSLVersionMax
, SSL_PROTOCOL_VERSION_TLS1
);
6283 if (kDefaultSSLVersionMax
<= SSL_PROTOCOL_VERSION_TLS1
)
6286 SpawnedTestServer::SSLOptions
ssl_options(
6287 SpawnedTestServer::SSLOptions::CERT_OK
);
6288 ssl_options
.tls_intolerant
=
6289 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1
;
6290 SpawnedTestServer
test_server(
6291 SpawnedTestServer::TYPE_HTTPS
,
6293 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6294 ASSERT_TRUE(test_server
.Start());
6297 TestURLRequestContext
context(true);
6299 d
.set_allow_certificate_errors(true);
6301 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
, &context
);
6304 base::RunLoop().Run();
6306 EXPECT_EQ(1, d
.response_started_count());
6307 EXPECT_NE(0, d
.bytes_received());
6308 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_TLS1
),
6309 SSLConnectionStatusToVersion(r
.ssl_info().connection_status
));
6310 EXPECT_TRUE(r
.ssl_info().connection_status
& SSL_CONNECTION_VERSION_FALLBACK
);
6313 // Tests that we don't fallback with servers that implement TLS_FALLBACK_SCSV.
6314 #if defined(USE_OPENSSL)
6315 TEST_F(HTTPSRequestTest
, DISABLED_FallbackSCSV
) {
6317 TEST_F(HTTPSRequestTest
, FallbackSCSV
) {
6319 SpawnedTestServer::SSLOptions
ssl_options(
6320 SpawnedTestServer::SSLOptions::CERT_OK
);
6321 // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger
6322 // a version fallback.
6323 ssl_options
.tls_intolerant
=
6324 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL
;
6325 // Have the server process TLS_FALLBACK_SCSV so that version fallback
6326 // connections are rejected.
6327 ssl_options
.fallback_scsv_enabled
= true;
6329 SpawnedTestServer
test_server(
6330 SpawnedTestServer::TYPE_HTTPS
,
6332 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6333 ASSERT_TRUE(test_server
.Start());
6336 TestURLRequestContext
context(true);
6338 d
.set_allow_certificate_errors(true);
6340 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
, &context
);
6343 base::RunLoop().Run();
6345 EXPECT_EQ(1, d
.response_started_count());
6346 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
6347 // intolerance. If the fallback SCSV is processed when the original error
6348 // that caused the fallback should be returned, which should be
6349 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
6350 EXPECT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH
, r
.status().error());
6353 // This tests that a load of www.google.com with a certificate error sets
6354 // the |certificate_errors_are_fatal| flag correctly. This flag will cause
6355 // the interstitial to be fatal.
6356 TEST_F(HTTPSRequestTest
, HTTPSPreloadedHSTSTest
) {
6357 SpawnedTestServer::SSLOptions
ssl_options(
6358 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
);
6359 SpawnedTestServer
test_server(
6360 SpawnedTestServer::TYPE_HTTPS
,
6362 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6363 ASSERT_TRUE(test_server
.Start());
6365 // We require that the URL be www.google.com in order to pick up the
6366 // preloaded HSTS entries in the TransportSecurityState. This means that we
6367 // have to use a MockHostResolver in order to direct www.google.com to the
6368 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
6370 MockHostResolver host_resolver
;
6371 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
6372 TestURLRequestContext
context(true);
6373 context
.set_network_delegate(&network_delegate
);
6374 context
.set_host_resolver(&host_resolver
);
6375 TransportSecurityState transport_security_state
;
6376 context
.set_transport_security_state(&transport_security_state
);
6380 URLRequest
r(GURL(base::StringPrintf("https://www.google.com:%d",
6381 test_server
.host_port_pair().port())),
6387 EXPECT_TRUE(r
.is_pending());
6389 base::RunLoop().Run();
6391 EXPECT_EQ(1, d
.response_started_count());
6392 EXPECT_FALSE(d
.received_data_before_response());
6393 EXPECT_TRUE(d
.have_certificate_errors());
6394 EXPECT_TRUE(d
.certificate_errors_are_fatal());
6397 // This tests that cached HTTPS page loads do not cause any updates to the
6398 // TransportSecurityState.
6399 TEST_F(HTTPSRequestTest
, HTTPSErrorsNoClobberTSSTest
) {
6400 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
6401 // matter. It just has to be any error.
6402 SpawnedTestServer::SSLOptions
ssl_options(
6403 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME
);
6404 SpawnedTestServer
test_server(
6405 SpawnedTestServer::TYPE_HTTPS
,
6407 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6408 ASSERT_TRUE(test_server
.Start());
6410 // We require that the URL be www.google.com in order to pick up the
6411 // preloaded and dynamic HSTS and public key pin entries in the
6412 // TransportSecurityState. This means that we have to use a
6413 // MockHostResolver in order to direct www.google.com to the testserver.
6414 // By default, MockHostResolver maps all hosts to 127.0.0.1.
6416 MockHostResolver host_resolver
;
6417 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
6418 TestURLRequestContext
context(true);
6419 context
.set_network_delegate(&network_delegate
);
6420 context
.set_host_resolver(&host_resolver
);
6421 TransportSecurityState transport_security_state
;
6422 TransportSecurityState::DomainState domain_state
;
6423 EXPECT_TRUE(transport_security_state
.GetDomainState("www.google.com", true,
6425 context
.set_transport_security_state(&transport_security_state
);
6429 URLRequest
r(GURL(base::StringPrintf("https://www.google.com:%d",
6430 test_server
.host_port_pair().port())),
6436 EXPECT_TRUE(r
.is_pending());
6438 base::RunLoop().Run();
6440 EXPECT_EQ(1, d
.response_started_count());
6441 EXPECT_FALSE(d
.received_data_before_response());
6442 EXPECT_TRUE(d
.have_certificate_errors());
6443 EXPECT_TRUE(d
.certificate_errors_are_fatal());
6445 // Get a fresh copy of the state, and check that it hasn't been updated.
6446 TransportSecurityState::DomainState new_domain_state
;
6447 EXPECT_TRUE(transport_security_state
.GetDomainState("www.google.com", true,
6448 &new_domain_state
));
6449 EXPECT_EQ(new_domain_state
.upgrade_mode
, domain_state
.upgrade_mode
);
6450 EXPECT_EQ(new_domain_state
.sts_include_subdomains
,
6451 domain_state
.sts_include_subdomains
);
6452 EXPECT_EQ(new_domain_state
.pkp_include_subdomains
,
6453 domain_state
.pkp_include_subdomains
);
6454 EXPECT_TRUE(FingerprintsEqual(new_domain_state
.static_spki_hashes
,
6455 domain_state
.static_spki_hashes
));
6456 EXPECT_TRUE(FingerprintsEqual(new_domain_state
.dynamic_spki_hashes
,
6457 domain_state
.dynamic_spki_hashes
));
6458 EXPECT_TRUE(FingerprintsEqual(new_domain_state
.bad_static_spki_hashes
,
6459 domain_state
.bad_static_spki_hashes
));
6462 // Make sure HSTS preserves a POST request's method and body.
6463 TEST_F(HTTPSRequestTest
, HSTSPreservesPosts
) {
6464 static const char kData
[] = "hello world";
6466 SpawnedTestServer::SSLOptions
ssl_options(
6467 SpawnedTestServer::SSLOptions::CERT_OK
);
6468 SpawnedTestServer
test_server(
6469 SpawnedTestServer::TYPE_HTTPS
,
6471 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6472 ASSERT_TRUE(test_server
.Start());
6475 // Per spec, TransportSecurityState expects a domain name, rather than an IP
6476 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
6477 // the SpawnedTestServer. By default, MockHostResolver maps all hosts
6479 MockHostResolver host_resolver
;
6481 // Force https for www.somewhere.com.
6482 TransportSecurityState transport_security_state
;
6483 base::Time expiry
= base::Time::Now() + base::TimeDelta::FromDays(1000);
6484 bool include_subdomains
= false;
6485 transport_security_state
.AddHSTS("www.somewhere.com", expiry
,
6486 include_subdomains
);
6488 TestNetworkDelegate network_delegate
; // Must outlive URLRequest.
6490 TestURLRequestContext
context(true);
6491 context
.set_host_resolver(&host_resolver
);
6492 context
.set_transport_security_state(&transport_security_state
);
6493 context
.set_network_delegate(&network_delegate
);
6497 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
6498 // cause a certificate error. Ignore the error.
6499 d
.set_allow_certificate_errors(true);
6501 URLRequest
req(GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
6502 test_server
.host_port_pair().port())),
6506 req
.set_method("POST");
6507 req
.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData
)));
6510 base::RunLoop().Run();
6512 EXPECT_EQ("https", req
.url().scheme());
6513 EXPECT_EQ("POST", req
.method());
6514 EXPECT_EQ(kData
, d
.data_received());
6516 LoadTimingInfo load_timing_info
;
6517 network_delegate
.GetLoadTimingInfoBeforeRedirect(&load_timing_info
);
6518 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
6519 TestLoadTimingCacheHitNoNetwork(load_timing_info
);
6522 TEST_F(HTTPSRequestTest
, SSLv3Fallback
) {
6523 SpawnedTestServer::SSLOptions
ssl_options(
6524 SpawnedTestServer::SSLOptions::CERT_OK
);
6525 ssl_options
.tls_intolerant
=
6526 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL
;
6527 SpawnedTestServer
test_server(
6528 SpawnedTestServer::TYPE_HTTPS
,
6530 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6531 ASSERT_TRUE(test_server
.Start());
6534 TestURLRequestContext
context(true);
6536 d
.set_allow_certificate_errors(true);
6538 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
, &context
);
6541 base::RunLoop().Run();
6543 EXPECT_EQ(1, d
.response_started_count());
6544 EXPECT_NE(0, d
.bytes_received());
6545 EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_SSL3
),
6546 SSLConnectionStatusToVersion(r
.ssl_info().connection_status
));
6547 EXPECT_TRUE(r
.ssl_info().connection_status
& SSL_CONNECTION_VERSION_FALLBACK
);
6552 class SSLClientAuthTestDelegate
: public TestDelegate
{
6554 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
6556 virtual void OnCertificateRequested(
6557 URLRequest
* request
,
6558 SSLCertRequestInfo
* cert_request_info
) OVERRIDE
{
6559 on_certificate_requested_count_
++;
6560 base::MessageLoop::current()->Quit();
6562 int on_certificate_requested_count() {
6563 return on_certificate_requested_count_
;
6566 int on_certificate_requested_count_
;
6571 // TODO(davidben): Test the rest of the code. Specifically,
6572 // - Filtering which certificates to select.
6573 // - Sending a certificate back.
6574 // - Getting a certificate request in an SSL renegotiation sending the
6576 TEST_F(HTTPSRequestTest
, ClientAuthTest
) {
6577 SpawnedTestServer::SSLOptions ssl_options
;
6578 ssl_options
.request_client_certificate
= true;
6579 SpawnedTestServer
test_server(
6580 SpawnedTestServer::TYPE_HTTPS
,
6582 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6583 ASSERT_TRUE(test_server
.Start());
6585 SSLClientAuthTestDelegate d
;
6587 URLRequest
r(test_server
.GetURL(std::string()),
6593 EXPECT_TRUE(r
.is_pending());
6595 base::RunLoop().Run();
6597 EXPECT_EQ(1, d
.on_certificate_requested_count());
6598 EXPECT_FALSE(d
.received_data_before_response());
6599 EXPECT_EQ(0, d
.bytes_received());
6601 // Send no certificate.
6602 // TODO(davidben): Get temporary client cert import (with keys) working on
6603 // all platforms so we can test sending a cert as well.
6604 r
.ContinueWithCertificate(NULL
);
6606 base::RunLoop().Run();
6608 EXPECT_EQ(1, d
.response_started_count());
6609 EXPECT_FALSE(d
.received_data_before_response());
6610 EXPECT_NE(0, d
.bytes_received());
6614 TEST_F(HTTPSRequestTest
, ResumeTest
) {
6615 // Test that we attempt a session resume when making two connections to the
6617 SpawnedTestServer::SSLOptions ssl_options
;
6618 ssl_options
.record_resume
= true;
6619 SpawnedTestServer
test_server(
6620 SpawnedTestServer::TYPE_HTTPS
,
6622 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6623 ASSERT_TRUE(test_server
.Start());
6625 SSLClientSocket::ClearSessionCache();
6629 URLRequest
r(test_server
.GetURL("ssl-session-cache"),
6635 EXPECT_TRUE(r
.is_pending());
6637 base::RunLoop().Run();
6639 EXPECT_EQ(1, d
.response_started_count());
6642 reinterpret_cast<HttpCache
*>(default_context_
.http_transaction_factory())->
6643 CloseAllConnections();
6647 URLRequest
r(test_server
.GetURL("ssl-session-cache"),
6653 EXPECT_TRUE(r
.is_pending());
6655 base::RunLoop().Run();
6657 // The response will look like;
6662 // With a newline at the end which makes the split think that there are
6665 EXPECT_EQ(1, d
.response_started_count());
6666 std::vector
<std::string
> lines
;
6667 base::SplitString(d
.data_received(), '\n', &lines
);
6668 ASSERT_EQ(4u, lines
.size()) << d
.data_received();
6670 std::string session_id
;
6672 for (size_t i
= 0; i
< 2; i
++) {
6673 std::vector
<std::string
> parts
;
6674 base::SplitString(lines
[i
], '\t', &parts
);
6675 ASSERT_EQ(2u, parts
.size());
6677 EXPECT_EQ("insert", parts
[0]);
6678 session_id
= parts
[1];
6680 EXPECT_EQ("lookup", parts
[0]);
6681 EXPECT_EQ(session_id
, parts
[1]);
6687 TEST_F(HTTPSRequestTest
, SSLSessionCacheShardTest
) {
6688 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
6690 SpawnedTestServer::SSLOptions ssl_options
;
6691 ssl_options
.record_resume
= true;
6692 SpawnedTestServer
test_server(
6693 SpawnedTestServer::TYPE_HTTPS
,
6695 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6696 ASSERT_TRUE(test_server
.Start());
6698 SSLClientSocket::ClearSessionCache();
6702 URLRequest
r(test_server
.GetURL("ssl-session-cache"),
6708 EXPECT_TRUE(r
.is_pending());
6710 base::RunLoop().Run();
6712 EXPECT_EQ(1, d
.response_started_count());
6715 // Now create a new HttpCache with a different ssl_session_cache_shard value.
6716 HttpNetworkSession::Params params
;
6717 params
.host_resolver
= default_context_
.host_resolver();
6718 params
.cert_verifier
= default_context_
.cert_verifier();
6719 params
.transport_security_state
= default_context_
.transport_security_state();
6720 params
.proxy_service
= default_context_
.proxy_service();
6721 params
.ssl_config_service
= default_context_
.ssl_config_service();
6722 params
.http_auth_handler_factory
=
6723 default_context_
.http_auth_handler_factory();
6724 params
.network_delegate
= &default_network_delegate_
;
6725 params
.http_server_properties
= default_context_
.http_server_properties();
6726 params
.ssl_session_cache_shard
= "alternate";
6728 scoped_ptr
<net::HttpCache
> cache(new net::HttpCache(
6729 new net::HttpNetworkSession(params
),
6730 net::HttpCache::DefaultBackend::InMemory(0)));
6732 default_context_
.set_http_transaction_factory(cache
.get());
6736 URLRequest
r(test_server
.GetURL("ssl-session-cache"),
6742 EXPECT_TRUE(r
.is_pending());
6744 base::RunLoop().Run();
6746 // The response will look like;
6750 // With a newline at the end which makes the split think that there are
6753 EXPECT_EQ(1, d
.response_started_count());
6754 std::vector
<std::string
> lines
;
6755 base::SplitString(d
.data_received(), '\n', &lines
);
6756 ASSERT_EQ(3u, lines
.size());
6758 std::string session_id
;
6759 for (size_t i
= 0; i
< 2; i
++) {
6760 std::vector
<std::string
> parts
;
6761 base::SplitString(lines
[i
], '\t', &parts
);
6762 ASSERT_EQ(2u, parts
.size());
6763 EXPECT_EQ("insert", parts
[0]);
6765 session_id
= parts
[1];
6767 EXPECT_NE(session_id
, parts
[1]);
6773 class HTTPSSessionTest
: public testing::Test
{
6775 HTTPSSessionTest() : default_context_(true) {
6776 cert_verifier_
.set_default_result(net::OK
);
6778 default_context_
.set_network_delegate(&default_network_delegate_
);
6779 default_context_
.set_cert_verifier(&cert_verifier_
);
6780 default_context_
.Init();
6782 virtual ~HTTPSSessionTest() {}
6785 MockCertVerifier cert_verifier_
;
6786 TestNetworkDelegate default_network_delegate_
; // Must outlive URLRequest.
6787 TestURLRequestContext default_context_
;
6790 // Tests that session resumption is not attempted if an invalid certificate
6792 TEST_F(HTTPSSessionTest
, DontResumeSessionsForInvalidCertificates
) {
6793 SpawnedTestServer::SSLOptions ssl_options
;
6794 ssl_options
.record_resume
= true;
6795 SpawnedTestServer
test_server(
6796 SpawnedTestServer::TYPE_HTTPS
,
6798 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6799 ASSERT_TRUE(test_server
.Start());
6801 SSLClientSocket::ClearSessionCache();
6803 // Simulate the certificate being expired and attempt a connection.
6804 cert_verifier_
.set_default_result(net::ERR_CERT_DATE_INVALID
);
6807 URLRequest
r(test_server
.GetURL("ssl-session-cache"),
6813 EXPECT_TRUE(r
.is_pending());
6815 base::RunLoop().Run();
6817 EXPECT_EQ(1, d
.response_started_count());
6820 reinterpret_cast<HttpCache
*>(default_context_
.http_transaction_factory())->
6821 CloseAllConnections();
6823 // Now change the certificate to be acceptable (so that the response is
6824 // loaded), and ensure that no session id is presented to the peer.
6825 cert_verifier_
.set_default_result(net::OK
);
6828 URLRequest
r(test_server
.GetURL("ssl-session-cache"),
6834 EXPECT_TRUE(r
.is_pending());
6836 base::RunLoop().Run();
6838 // The response will look like;
6842 // With a newline at the end which makes the split think that there are
6845 // If a session was presented (eg: a bug), then the response would look
6851 EXPECT_EQ(1, d
.response_started_count());
6852 std::vector
<std::string
> lines
;
6853 base::SplitString(d
.data_received(), '\n', &lines
);
6854 ASSERT_EQ(3u, lines
.size()) << d
.data_received();
6856 std::string session_id
;
6857 for (size_t i
= 0; i
< 2; i
++) {
6858 std::vector
<std::string
> parts
;
6859 base::SplitString(lines
[i
], '\t', &parts
);
6860 ASSERT_EQ(2u, parts
.size());
6861 EXPECT_EQ("insert", parts
[0]);
6863 session_id
= parts
[1];
6865 EXPECT_NE(session_id
, parts
[1]);
6871 class TestSSLConfigService
: public SSLConfigService
{
6873 TestSSLConfigService(bool ev_enabled
,
6874 bool online_rev_checking
,
6875 bool rev_checking_required_local_anchors
)
6876 : ev_enabled_(ev_enabled
),
6877 online_rev_checking_(online_rev_checking
),
6878 rev_checking_required_local_anchors_(
6879 rev_checking_required_local_anchors
) {}
6881 // SSLConfigService:
6882 virtual void GetSSLConfig(SSLConfig
* config
) OVERRIDE
{
6883 *config
= SSLConfig();
6884 config
->rev_checking_enabled
= online_rev_checking_
;
6885 config
->verify_ev_cert
= ev_enabled_
;
6886 config
->rev_checking_required_local_anchors
=
6887 rev_checking_required_local_anchors_
;
6891 virtual ~TestSSLConfigService() {}
6894 const bool ev_enabled_
;
6895 const bool online_rev_checking_
;
6896 const bool rev_checking_required_local_anchors_
;
6899 // This the fingerprint of the "Testing CA" certificate used by the testserver.
6900 // See net/data/ssl/certificates/ocsp-test-root.pem.
6901 static const SHA1HashValue kOCSPTestCertFingerprint
=
6902 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
6903 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
6905 // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
6907 static const SHA256HashValue kOCSPTestCertSPKI
= { {
6908 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
6909 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
6910 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
6911 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
6914 // This is the policy OID contained in the certificates that testserver
6916 static const char kOCSPTestCertPolicy
[] = "1.3.6.1.4.1.11129.2.4.1";
6918 class HTTPSOCSPTest
: public HTTPSRequestTest
{
6923 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
6924 kOCSPTestCertFingerprint
,
6925 kOCSPTestCertPolicy
)) {
6928 virtual void SetUp() OVERRIDE
{
6929 SetupContext(&context_
);
6932 scoped_refptr
<net::X509Certificate
> root_cert
=
6933 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
6934 CHECK_NE(static_cast<X509Certificate
*>(NULL
), root_cert
);
6935 test_root_
.reset(new ScopedTestRoot(root_cert
.get()));
6937 #if defined(USE_NSS) || defined(OS_IOS)
6938 SetURLRequestContextForNSSHttpIO(&context_
);
6939 EnsureNSSHttpIOInit();
6943 void DoConnection(const SpawnedTestServer::SSLOptions
& ssl_options
,
6944 CertStatus
* out_cert_status
) {
6945 // We always overwrite out_cert_status.
6946 *out_cert_status
= 0;
6947 SpawnedTestServer
test_server(
6948 SpawnedTestServer::TYPE_HTTPS
,
6950 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
6951 ASSERT_TRUE(test_server
.Start());
6954 d
.set_allow_certificate_errors(true);
6956 test_server
.GetURL(std::string()), DEFAULT_PRIORITY
, &d
, &context_
);
6959 base::RunLoop().Run();
6961 EXPECT_EQ(1, d
.response_started_count());
6962 *out_cert_status
= r
.ssl_info().cert_status
;
6965 virtual ~HTTPSOCSPTest() {
6966 #if defined(USE_NSS) || defined(OS_IOS)
6967 ShutdownNSSHttpIO();
6972 // SetupContext configures the URLRequestContext that will be used for making
6973 // connetions to testserver. This can be overridden in test subclasses for
6974 // different behaviour.
6975 virtual void SetupContext(URLRequestContext
* context
) {
6976 context
->set_ssl_config_service(
6977 new TestSSLConfigService(true /* check for EV */,
6978 true /* online revocation checking */,
6979 false /* require rev. checking for local
6983 scoped_ptr
<ScopedTestRoot
> test_root_
;
6984 TestURLRequestContext context_
;
6985 scoped_ptr
<ScopedTestEVPolicy
> ev_test_policy_
;
6988 static CertStatus
ExpectedCertStatusForFailedOnlineRevocationCheck() {
6990 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
6991 // have that ability on other platforms.
6992 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION
;
6998 // SystemSupportsHardFailRevocationChecking returns true iff the current
6999 // operating system supports revocation checking and can distinguish between
7000 // situations where a given certificate lacks any revocation information (eg:
7001 // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
7002 // revocation information cannot be obtained (eg: the CRL was unreachable).
7003 // If it does not, then tests which rely on 'hard fail' behaviour should be
7005 static bool SystemSupportsHardFailRevocationChecking() {
7006 #if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS)
7013 // SystemUsesChromiumEVMetadata returns true iff the current operating system
7014 // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
7015 // several tests are effected because our testing EV certificate won't be
7016 // recognised as EV.
7017 static bool SystemUsesChromiumEVMetadata() {
7018 #if defined(USE_OPENSSL)
7019 // http://crbug.com/117478 - OpenSSL does not support EV validation.
7021 #elif defined(OS_MACOSX) && !defined(OS_IOS)
7022 // On OS X, we use the system to tell us whether a certificate is EV or not
7023 // and the system won't recognise our testing root.
7030 static bool SystemSupportsOCSP() {
7031 #if defined(USE_OPENSSL)
7032 // http://crbug.com/117478 - OpenSSL does not support OCSP.
7034 #elif defined(OS_WIN)
7035 return base::win::GetVersion() >= base::win::VERSION_VISTA
;
7036 #elif defined(OS_ANDROID)
7037 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
7044 TEST_F(HTTPSOCSPTest
, Valid
) {
7045 if (!SystemSupportsOCSP()) {
7046 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7050 SpawnedTestServer::SSLOptions
ssl_options(
7051 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7052 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_OK
;
7054 CertStatus cert_status
;
7055 DoConnection(ssl_options
, &cert_status
);
7057 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
7059 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7060 static_cast<bool>(cert_status
& CERT_STATUS_IS_EV
));
7062 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
7065 TEST_F(HTTPSOCSPTest
, Revoked
) {
7066 if (!SystemSupportsOCSP()) {
7067 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7071 SpawnedTestServer::SSLOptions
ssl_options(
7072 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7073 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_REVOKED
;
7075 CertStatus cert_status
;
7076 DoConnection(ssl_options
, &cert_status
);
7078 #if !(defined(OS_MACOSX) && !defined(OS_IOS))
7079 // Doesn't pass on OS X yet for reasons that need to be investigated.
7080 EXPECT_EQ(CERT_STATUS_REVOKED
, cert_status
& CERT_STATUS_ALL_ERRORS
);
7082 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7083 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
7086 TEST_F(HTTPSOCSPTest
, Invalid
) {
7087 if (!SystemSupportsOCSP()) {
7088 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7092 SpawnedTestServer::SSLOptions
ssl_options(
7093 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7094 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
7096 CertStatus cert_status
;
7097 DoConnection(ssl_options
, &cert_status
);
7099 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7100 cert_status
& CERT_STATUS_ALL_ERRORS
);
7102 // Without a positive OCSP response, we shouldn't show the EV status.
7103 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7104 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
7107 class HTTPSHardFailTest
: public HTTPSOCSPTest
{
7109 virtual void SetupContext(URLRequestContext
* context
) OVERRIDE
{
7110 context
->set_ssl_config_service(
7111 new TestSSLConfigService(false /* check for EV */,
7112 false /* online revocation checking */,
7113 true /* require rev. checking for local
7119 TEST_F(HTTPSHardFailTest
, FailsOnOCSPInvalid
) {
7120 if (!SystemSupportsOCSP()) {
7121 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7125 if (!SystemSupportsHardFailRevocationChecking()) {
7126 LOG(WARNING
) << "Skipping test because system doesn't support hard fail "
7127 << "revocation checking";
7131 SpawnedTestServer::SSLOptions
ssl_options(
7132 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7133 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
7135 CertStatus cert_status
;
7136 DoConnection(ssl_options
, &cert_status
);
7138 EXPECT_EQ(CERT_STATUS_REVOKED
,
7139 cert_status
& CERT_STATUS_REVOKED
);
7141 // Without a positive OCSP response, we shouldn't show the EV status.
7142 EXPECT_TRUE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
7145 class HTTPSEVCRLSetTest
: public HTTPSOCSPTest
{
7147 virtual void SetupContext(URLRequestContext
* context
) OVERRIDE
{
7148 context
->set_ssl_config_service(
7149 new TestSSLConfigService(true /* check for EV */,
7150 false /* online revocation checking */,
7151 false /* require rev. checking for local
7156 TEST_F(HTTPSEVCRLSetTest
, MissingCRLSetAndInvalidOCSP
) {
7157 if (!SystemSupportsOCSP()) {
7158 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7162 SpawnedTestServer::SSLOptions
ssl_options(
7163 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7164 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
7165 SSLConfigService::SetCRLSet(scoped_refptr
<CRLSet
>());
7167 CertStatus cert_status
;
7168 DoConnection(ssl_options
, &cert_status
);
7170 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7171 cert_status
& CERT_STATUS_ALL_ERRORS
);
7173 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7174 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7175 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
7178 TEST_F(HTTPSEVCRLSetTest
, MissingCRLSetAndRevokedOCSP
) {
7179 if (!SystemSupportsOCSP()) {
7180 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7184 SpawnedTestServer::SSLOptions
ssl_options(
7185 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7186 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_REVOKED
;
7187 SSLConfigService::SetCRLSet(scoped_refptr
<CRLSet
>());
7189 CertStatus cert_status
;
7190 DoConnection(ssl_options
, &cert_status
);
7192 // Currently only works for Windows. When using NSS or OS X, it's not
7193 // possible to determine whether the check failed because of actual
7194 // revocation or because there was an OCSP failure.
7196 EXPECT_EQ(CERT_STATUS_REVOKED
, cert_status
& CERT_STATUS_ALL_ERRORS
);
7198 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
7201 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7202 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7203 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
7206 TEST_F(HTTPSEVCRLSetTest
, MissingCRLSetAndGoodOCSP
) {
7207 if (!SystemSupportsOCSP()) {
7208 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7212 SpawnedTestServer::SSLOptions
ssl_options(
7213 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7214 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_OK
;
7215 SSLConfigService::SetCRLSet(scoped_refptr
<CRLSet
>());
7217 CertStatus cert_status
;
7218 DoConnection(ssl_options
, &cert_status
);
7220 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
7222 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7223 static_cast<bool>(cert_status
& CERT_STATUS_IS_EV
));
7224 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7225 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
7228 TEST_F(HTTPSEVCRLSetTest
, ExpiredCRLSet
) {
7229 if (!SystemSupportsOCSP()) {
7230 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7234 SpawnedTestServer::SSLOptions
ssl_options(
7235 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7236 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
7237 SSLConfigService::SetCRLSet(
7238 scoped_refptr
<CRLSet
>(CRLSet::ExpiredCRLSetForTesting()));
7240 CertStatus cert_status
;
7241 DoConnection(ssl_options
, &cert_status
);
7243 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7244 cert_status
& CERT_STATUS_ALL_ERRORS
);
7246 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7247 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7248 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
7251 TEST_F(HTTPSEVCRLSetTest
, FreshCRLSetCovered
) {
7252 if (!SystemSupportsOCSP()) {
7253 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7257 SpawnedTestServer::SSLOptions
ssl_options(
7258 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7259 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
7260 SSLConfigService::SetCRLSet(
7261 scoped_refptr
<CRLSet
>(CRLSet::ForTesting(
7262 false, &kOCSPTestCertSPKI
, "")));
7264 CertStatus cert_status
;
7265 DoConnection(ssl_options
, &cert_status
);
7267 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
7268 // revocation check for EV.
7269 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
7270 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7271 static_cast<bool>(cert_status
& CERT_STATUS_IS_EV
));
7273 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
7276 TEST_F(HTTPSEVCRLSetTest
, FreshCRLSetNotCovered
) {
7277 if (!SystemSupportsOCSP()) {
7278 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7282 SpawnedTestServer::SSLOptions
ssl_options(
7283 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7284 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
7285 SSLConfigService::SetCRLSet(
7286 scoped_refptr
<CRLSet
>(CRLSet::EmptyCRLSetForTesting()));
7288 CertStatus cert_status
= 0;
7289 DoConnection(ssl_options
, &cert_status
);
7291 // Even with a fresh CRLSet, we should still do online revocation checks when
7292 // the certificate chain isn't covered by the CRLSet, which it isn't in this
7294 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
7295 cert_status
& CERT_STATUS_ALL_ERRORS
);
7297 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7298 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
7299 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
7302 TEST_F(HTTPSEVCRLSetTest
, ExpiredCRLSetAndRevokedNonEVCert
) {
7303 // Test that when EV verification is requested, but online revocation
7304 // checking is disabled, and the leaf certificate is not in fact EV, that
7305 // no revocation checking actually happens.
7306 if (!SystemSupportsOCSP()) {
7307 LOG(WARNING
) << "Skipping test because system doesn't support OCSP";
7311 // Unmark the certificate's OID as EV, which should disable revocation
7312 // checking (as per the user preference)
7313 ev_test_policy_
.reset();
7315 SpawnedTestServer::SSLOptions
ssl_options(
7316 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7317 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_REVOKED
;
7318 SSLConfigService::SetCRLSet(
7319 scoped_refptr
<CRLSet
>(CRLSet::ExpiredCRLSetForTesting()));
7321 CertStatus cert_status
;
7322 DoConnection(ssl_options
, &cert_status
);
7324 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
7326 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7327 EXPECT_FALSE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
7330 class HTTPSCRLSetTest
: public HTTPSOCSPTest
{
7332 virtual void SetupContext(URLRequestContext
* context
) OVERRIDE
{
7333 context
->set_ssl_config_service(
7334 new TestSSLConfigService(false /* check for EV */,
7335 false /* online revocation checking */,
7336 false /* require rev. checking for local
7341 TEST_F(HTTPSCRLSetTest
, ExpiredCRLSet
) {
7342 SpawnedTestServer::SSLOptions
ssl_options(
7343 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7344 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_INVALID
;
7345 SSLConfigService::SetCRLSet(
7346 scoped_refptr
<CRLSet
>(CRLSet::ExpiredCRLSetForTesting()));
7348 CertStatus cert_status
;
7349 DoConnection(ssl_options
, &cert_status
);
7351 // If we're not trying EV verification then, even if the CRLSet has expired,
7352 // we don't fall back to online revocation checks.
7353 EXPECT_EQ(0u, cert_status
& CERT_STATUS_ALL_ERRORS
);
7354 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7355 EXPECT_FALSE(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
);
7358 TEST_F(HTTPSCRLSetTest
, CRLSetRevoked
) {
7359 #if defined(USE_OPENSSL)
7360 LOG(WARNING
) << "Skipping test because system doesn't support CRLSets";
7364 SpawnedTestServer::SSLOptions
ssl_options(
7365 SpawnedTestServer::SSLOptions::CERT_AUTO
);
7366 ssl_options
.ocsp_status
= SpawnedTestServer::SSLOptions::OCSP_OK
;
7367 ssl_options
.cert_serial
= 10;
7368 SSLConfigService::SetCRLSet(
7369 scoped_refptr
<CRLSet
>(CRLSet::ForTesting(
7370 false, &kOCSPTestCertSPKI
, "\x0a")));
7372 CertStatus cert_status
= 0;
7373 DoConnection(ssl_options
, &cert_status
);
7375 // If the certificate is recorded as revoked in the CRLSet, that should be
7376 // reflected without online revocation checking.
7377 EXPECT_EQ(CERT_STATUS_REVOKED
, cert_status
& CERT_STATUS_ALL_ERRORS
);
7378 EXPECT_FALSE(cert_status
& CERT_STATUS_IS_EV
);
7380 static_cast<bool>(cert_status
& CERT_STATUS_REV_CHECKING_ENABLED
));
7382 #endif // !defined(OS_IOS)
7384 #if !defined(DISABLE_FTP_SUPPORT)
7385 class URLRequestTestFTP
: public URLRequestTest
{
7388 : test_server_(SpawnedTestServer::TYPE_FTP
, SpawnedTestServer::kLocalhost
,
7393 SpawnedTestServer test_server_
;
7396 // Make sure an FTP request using an unsafe ports fails.
7397 TEST_F(URLRequestTestFTP
, UnsafePort
) {
7398 ASSERT_TRUE(test_server_
.Start());
7400 URLRequestJobFactoryImpl job_factory
;
7401 FtpNetworkLayer
ftp_transaction_factory(default_context_
.host_resolver());
7403 GURL
url("ftp://127.0.0.1:7");
7404 job_factory
.SetProtocolHandler(
7406 new FtpProtocolHandler(&ftp_transaction_factory
));
7407 default_context_
.set_job_factory(&job_factory
);
7411 URLRequest
r(url
, DEFAULT_PRIORITY
, &d
, &default_context_
);
7413 EXPECT_TRUE(r
.is_pending());
7415 base::RunLoop().Run();
7417 EXPECT_FALSE(r
.is_pending());
7418 EXPECT_EQ(URLRequestStatus::FAILED
, r
.status().status());
7419 EXPECT_EQ(ERR_UNSAFE_PORT
, r
.status().error());
7423 // Flaky, see http://crbug.com/25045.
7424 TEST_F(URLRequestTestFTP
, DISABLED_FTPDirectoryListing
) {
7425 ASSERT_TRUE(test_server_
.Start());
7430 test_server_
.GetURL("/"), DEFAULT_PRIORITY
, &d
, &default_context_
);
7432 EXPECT_TRUE(r
.is_pending());
7434 base::RunLoop().Run();
7436 EXPECT_FALSE(r
.is_pending());
7437 EXPECT_EQ(1, d
.response_started_count());
7438 EXPECT_FALSE(d
.received_data_before_response());
7439 EXPECT_LT(0, d
.bytes_received());
7440 EXPECT_EQ(test_server_
.host_port_pair().host(),
7441 r
.GetSocketAddress().host());
7442 EXPECT_EQ(test_server_
.host_port_pair().port(),
7443 r
.GetSocketAddress().port());
7447 // Flaky, see http://crbug.com/25045.
7448 TEST_F(URLRequestTestFTP
, DISABLED_FTPGetTestAnonymous
) {
7449 ASSERT_TRUE(test_server_
.Start());
7451 base::FilePath app_path
;
7452 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
7453 app_path
= app_path
.AppendASCII("LICENSE");
7456 URLRequest
r(test_server_
.GetURL("/LICENSE"),
7461 EXPECT_TRUE(r
.is_pending());
7463 base::RunLoop().Run();
7465 int64 file_size
= 0;
7466 base::GetFileSize(app_path
, &file_size
);
7468 EXPECT_FALSE(r
.is_pending());
7469 EXPECT_EQ(1, d
.response_started_count());
7470 EXPECT_FALSE(d
.received_data_before_response());
7471 EXPECT_EQ(d
.bytes_received(), static_cast<int>(file_size
));
7472 EXPECT_EQ(test_server_
.host_port_pair().host(),
7473 r
.GetSocketAddress().host());
7474 EXPECT_EQ(test_server_
.host_port_pair().port(),
7475 r
.GetSocketAddress().port());
7479 // Flaky, see http://crbug.com/25045.
7480 TEST_F(URLRequestTestFTP
, DISABLED_FTPGetTest
) {
7481 ASSERT_TRUE(test_server_
.Start());
7483 base::FilePath app_path
;
7484 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
7485 app_path
= app_path
.AppendASCII("LICENSE");
7489 test_server_
.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
7494 EXPECT_TRUE(r
.is_pending());
7496 base::RunLoop().Run();
7498 int64 file_size
= 0;
7499 base::GetFileSize(app_path
, &file_size
);
7501 EXPECT_FALSE(r
.is_pending());
7502 EXPECT_EQ(test_server_
.host_port_pair().host(),
7503 r
.GetSocketAddress().host());
7504 EXPECT_EQ(test_server_
.host_port_pair().port(),
7505 r
.GetSocketAddress().port());
7506 EXPECT_EQ(1, d
.response_started_count());
7507 EXPECT_FALSE(d
.received_data_before_response());
7508 EXPECT_EQ(d
.bytes_received(), static_cast<int>(file_size
));
7510 LoadTimingInfo load_timing_info
;
7511 r
.GetLoadTimingInfo(&load_timing_info
);
7512 TestLoadTimingNoHttpResponse(load_timing_info
);
7516 // Flaky, see http://crbug.com/25045.
7517 TEST_F(URLRequestTestFTP
, DISABLED_FTPCheckWrongPassword
) {
7518 ASSERT_TRUE(test_server_
.Start());
7520 base::FilePath app_path
;
7521 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
7522 app_path
= app_path
.AppendASCII("LICENSE");
7525 URLRequest
r(test_server_
.GetURLWithUserAndPassword(
7526 "/LICENSE", "chrome", "wrong_password"),
7531 EXPECT_TRUE(r
.is_pending());
7533 base::RunLoop().Run();
7535 int64 file_size
= 0;
7536 base::GetFileSize(app_path
, &file_size
);
7538 EXPECT_FALSE(r
.is_pending());
7539 EXPECT_EQ(1, d
.response_started_count());
7540 EXPECT_FALSE(d
.received_data_before_response());
7541 EXPECT_EQ(d
.bytes_received(), 0);
7545 // Flaky, see http://crbug.com/25045.
7546 TEST_F(URLRequestTestFTP
, DISABLED_FTPCheckWrongPasswordRestart
) {
7547 ASSERT_TRUE(test_server_
.Start());
7549 base::FilePath app_path
;
7550 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
7551 app_path
= app_path
.AppendASCII("LICENSE");
7553 // Set correct login credentials. The delegate will be asked for them when
7554 // the initial login with wrong credentials will fail.
7555 d
.set_credentials(AuthCredentials(kChrome
, kChrome
));
7557 URLRequest
r(test_server_
.GetURLWithUserAndPassword(
7558 "/LICENSE", "chrome", "wrong_password"),
7563 EXPECT_TRUE(r
.is_pending());
7565 base::RunLoop().Run();
7567 int64 file_size
= 0;
7568 base::GetFileSize(app_path
, &file_size
);
7570 EXPECT_FALSE(r
.is_pending());
7571 EXPECT_EQ(1, d
.response_started_count());
7572 EXPECT_FALSE(d
.received_data_before_response());
7573 EXPECT_EQ(d
.bytes_received(), static_cast<int>(file_size
));
7577 // Flaky, see http://crbug.com/25045.
7578 TEST_F(URLRequestTestFTP
, DISABLED_FTPCheckWrongUser
) {
7579 ASSERT_TRUE(test_server_
.Start());
7581 base::FilePath app_path
;
7582 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
7583 app_path
= app_path
.AppendASCII("LICENSE");
7586 URLRequest
r(test_server_
.GetURLWithUserAndPassword(
7587 "/LICENSE", "wrong_user", "chrome"),
7592 EXPECT_TRUE(r
.is_pending());
7594 base::RunLoop().Run();
7596 int64 file_size
= 0;
7597 base::GetFileSize(app_path
, &file_size
);
7599 EXPECT_FALSE(r
.is_pending());
7600 EXPECT_EQ(1, d
.response_started_count());
7601 EXPECT_FALSE(d
.received_data_before_response());
7602 EXPECT_EQ(d
.bytes_received(), 0);
7606 // Flaky, see http://crbug.com/25045.
7607 TEST_F(URLRequestTestFTP
, DISABLED_FTPCheckWrongUserRestart
) {
7608 ASSERT_TRUE(test_server_
.Start());
7610 base::FilePath app_path
;
7611 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
7612 app_path
= app_path
.AppendASCII("LICENSE");
7614 // Set correct login credentials. The delegate will be asked for them when
7615 // the initial login with wrong credentials will fail.
7616 d
.set_credentials(AuthCredentials(kChrome
, kChrome
));
7618 URLRequest
r(test_server_
.GetURLWithUserAndPassword(
7619 "/LICENSE", "wrong_user", "chrome"),
7624 EXPECT_TRUE(r
.is_pending());
7626 base::RunLoop().Run();
7628 int64 file_size
= 0;
7629 base::GetFileSize(app_path
, &file_size
);
7631 EXPECT_FALSE(r
.is_pending());
7632 EXPECT_EQ(1, d
.response_started_count());
7633 EXPECT_FALSE(d
.received_data_before_response());
7634 EXPECT_EQ(d
.bytes_received(), static_cast<int>(file_size
));
7638 // Flaky, see http://crbug.com/25045.
7639 TEST_F(URLRequestTestFTP
, DISABLED_FTPCacheURLCredentials
) {
7640 ASSERT_TRUE(test_server_
.Start());
7642 base::FilePath app_path
;
7643 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
7644 app_path
= app_path
.AppendASCII("LICENSE");
7646 scoped_ptr
<TestDelegate
> d(new TestDelegate
);
7648 // Pass correct login identity in the URL.
7650 test_server_
.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
7655 EXPECT_TRUE(r
.is_pending());
7657 base::RunLoop().Run();
7659 int64 file_size
= 0;
7660 base::GetFileSize(app_path
, &file_size
);
7662 EXPECT_FALSE(r
.is_pending());
7663 EXPECT_EQ(1, d
->response_started_count());
7664 EXPECT_FALSE(d
->received_data_before_response());
7665 EXPECT_EQ(d
->bytes_received(), static_cast<int>(file_size
));
7668 d
.reset(new TestDelegate
);
7670 // This request should use cached identity from previous request.
7671 URLRequest
r(test_server_
.GetURL("/LICENSE"),
7676 EXPECT_TRUE(r
.is_pending());
7678 base::RunLoop().Run();
7680 int64 file_size
= 0;
7681 base::GetFileSize(app_path
, &file_size
);
7683 EXPECT_FALSE(r
.is_pending());
7684 EXPECT_EQ(1, d
->response_started_count());
7685 EXPECT_FALSE(d
->received_data_before_response());
7686 EXPECT_EQ(d
->bytes_received(), static_cast<int>(file_size
));
7690 // Flaky, see http://crbug.com/25045.
7691 TEST_F(URLRequestTestFTP
, DISABLED_FTPCacheLoginBoxCredentials
) {
7692 ASSERT_TRUE(test_server_
.Start());
7694 base::FilePath app_path
;
7695 PathService::Get(base::DIR_SOURCE_ROOT
, &app_path
);
7696 app_path
= app_path
.AppendASCII("LICENSE");
7698 scoped_ptr
<TestDelegate
> d(new TestDelegate
);
7699 // Set correct login credentials. The delegate will be asked for them when
7700 // the initial login with wrong credentials will fail.
7701 d
->set_credentials(AuthCredentials(kChrome
, kChrome
));
7703 URLRequest
r(test_server_
.GetURLWithUserAndPassword(
7704 "/LICENSE", "chrome", "wrong_password"),
7709 EXPECT_TRUE(r
.is_pending());
7711 base::RunLoop().Run();
7713 int64 file_size
= 0;
7714 base::GetFileSize(app_path
, &file_size
);
7716 EXPECT_FALSE(r
.is_pending());
7717 EXPECT_EQ(1, d
->response_started_count());
7718 EXPECT_FALSE(d
->received_data_before_response());
7719 EXPECT_EQ(d
->bytes_received(), static_cast<int>(file_size
));
7722 // Use a new delegate without explicit credentials. The cached ones should be
7724 d
.reset(new TestDelegate
);
7726 // Don't pass wrong credentials in the URL, they would override valid cached
7728 URLRequest
r(test_server_
.GetURL("/LICENSE"),
7733 EXPECT_TRUE(r
.is_pending());
7735 base::RunLoop().Run();
7737 int64 file_size
= 0;
7738 base::GetFileSize(app_path
, &file_size
);
7740 EXPECT_FALSE(r
.is_pending());
7741 EXPECT_EQ(1, d
->response_started_count());
7742 EXPECT_FALSE(d
->received_data_before_response());
7743 EXPECT_EQ(d
->bytes_received(), static_cast<int>(file_size
));
7746 #endif // !defined(DISABLE_FTP_SUPPORT)