Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / url_request / url_request_unittest.cc
blob18ec0f397a58654fbcf038740bd0aa47a9035fba
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"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #include <shlobj.h>
10 #endif
12 #include <algorithm>
14 #include "base/basictypes.h"
15 #include "base/bind.h"
16 #include "base/compiler_specific.h"
17 #include "base/files/file_util.h"
18 #include "base/files/scoped_temp_dir.h"
19 #include "base/format_macros.h"
20 #include "base/memory/scoped_ptr.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/chunked_upload_data_stream.h"
33 #include "net/base/elements_upload_data_stream.h"
34 #include "net/base/load_flags.h"
35 #include "net/base/load_timing_info.h"
36 #include "net/base/load_timing_info_test_util.h"
37 #include "net/base/net_errors.h"
38 #include "net/base/net_module.h"
39 #include "net/base/net_util.h"
40 #include "net/base/request_priority.h"
41 #include "net/base/test_data_directory.h"
42 #include "net/base/upload_bytes_element_reader.h"
43 #include "net/base/upload_data_stream.h"
44 #include "net/base/upload_file_element_reader.h"
45 #include "net/cert/ev_root_ca_metadata.h"
46 #include "net/cert/mock_cert_verifier.h"
47 #include "net/cert/test_root_certs.h"
48 #include "net/cert_net/nss_ocsp.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/log/net_log.h"
62 #include "net/log/net_log_unittest.h"
63 #include "net/log/test_net_log.h"
64 #include "net/proxy/proxy_service.h"
65 #include "net/socket/ssl_client_socket.h"
66 #include "net/ssl/ssl_cipher_suite_names.h"
67 #include "net/ssl/ssl_connection_status_flags.h"
68 #include "net/test/cert_test_util.h"
69 #include "net/test/spawned_test_server/spawned_test_server.h"
70 #include "net/url_request/data_protocol_handler.h"
71 #include "net/url_request/static_http_user_agent_settings.h"
72 #include "net/url_request/url_request.h"
73 #include "net/url_request/url_request_http_job.h"
74 #include "net/url_request/url_request_intercepting_job_factory.h"
75 #include "net/url_request/url_request_interceptor.h"
76 #include "net/url_request/url_request_job_factory_impl.h"
77 #include "net/url_request/url_request_redirect_job.h"
78 #include "net/url_request/url_request_test_job.h"
79 #include "net/url_request/url_request_test_util.h"
80 #include "testing/gtest/include/gtest/gtest.h"
81 #include "testing/platform_test.h"
83 #if !defined(DISABLE_FILE_SUPPORT)
84 #include "net/base/filename_util.h"
85 #include "net/url_request/file_protocol_handler.h"
86 #include "net/url_request/url_request_file_dir_job.h"
87 #endif
89 #if !defined(DISABLE_FTP_SUPPORT)
90 #include "net/url_request/ftp_protocol_handler.h"
91 #endif
93 #if defined(OS_WIN)
94 #include "base/win/scoped_com_initializer.h"
95 #include "base/win/scoped_comptr.h"
96 #include "base/win/windows_version.h"
97 #endif
99 using base::ASCIIToUTF16;
100 using base::Time;
101 using std::string;
103 namespace net {
105 namespace {
107 const base::string16 kChrome(ASCIIToUTF16("chrome"));
108 const base::string16 kSecret(ASCIIToUTF16("secret"));
109 const base::string16 kUser(ASCIIToUTF16("user"));
111 // Tests load timing information in the case a fresh connection was used, with
112 // no proxy.
113 void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info,
114 int connect_timing_flags) {
115 EXPECT_FALSE(load_timing_info.socket_reused);
116 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
118 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
119 EXPECT_FALSE(load_timing_info.request_start.is_null());
121 EXPECT_LE(load_timing_info.request_start,
122 load_timing_info.connect_timing.connect_start);
123 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
124 connect_timing_flags);
125 EXPECT_LE(load_timing_info.connect_timing.connect_end,
126 load_timing_info.send_start);
127 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
128 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
130 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
131 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
134 // Same as above, but with proxy times.
135 void TestLoadTimingNotReusedWithProxy(
136 const LoadTimingInfo& load_timing_info,
137 int connect_timing_flags) {
138 EXPECT_FALSE(load_timing_info.socket_reused);
139 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
141 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
142 EXPECT_FALSE(load_timing_info.request_start.is_null());
144 EXPECT_LE(load_timing_info.request_start,
145 load_timing_info.proxy_resolve_start);
146 EXPECT_LE(load_timing_info.proxy_resolve_start,
147 load_timing_info.proxy_resolve_end);
148 EXPECT_LE(load_timing_info.proxy_resolve_end,
149 load_timing_info.connect_timing.connect_start);
150 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
151 connect_timing_flags);
152 EXPECT_LE(load_timing_info.connect_timing.connect_end,
153 load_timing_info.send_start);
154 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
155 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
158 // Same as above, but with a reused socket and proxy times.
159 void TestLoadTimingReusedWithProxy(
160 const LoadTimingInfo& load_timing_info) {
161 EXPECT_TRUE(load_timing_info.socket_reused);
162 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
164 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
165 EXPECT_FALSE(load_timing_info.request_start.is_null());
167 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
169 EXPECT_LE(load_timing_info.request_start,
170 load_timing_info.proxy_resolve_start);
171 EXPECT_LE(load_timing_info.proxy_resolve_start,
172 load_timing_info.proxy_resolve_end);
173 EXPECT_LE(load_timing_info.proxy_resolve_end,
174 load_timing_info.send_start);
175 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
176 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
179 #if !defined(DISABLE_FILE_SUPPORT)
180 // Tests load timing information in the case of a cache hit, when no cache
181 // validation request was sent over the wire.
182 base::StringPiece TestNetResourceProvider(int key) {
183 return "header";
186 void FillBuffer(char* buffer, size_t len) {
187 static bool called = false;
188 if (!called) {
189 called = true;
190 int seed = static_cast<int>(Time::Now().ToInternalValue());
191 srand(seed);
194 for (size_t i = 0; i < len; i++) {
195 buffer[i] = static_cast<char>(rand());
196 if (!buffer[i])
197 buffer[i] = 'g';
200 #endif
202 #if !defined(OS_IOS)
203 void TestLoadTimingCacheHitNoNetwork(
204 const LoadTimingInfo& load_timing_info) {
205 EXPECT_FALSE(load_timing_info.socket_reused);
206 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
208 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
209 EXPECT_FALSE(load_timing_info.request_start.is_null());
211 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
212 EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start);
213 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end);
214 EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end);
216 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
217 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
220 #if !defined(DISABLE_FTP_SUPPORT)
221 // Tests load timing in the case that there is no HTTP response. This can be
222 // used to test in the case of errors or non-HTTP requests.
223 void TestLoadTimingNoHttpResponse(
224 const LoadTimingInfo& load_timing_info) {
225 EXPECT_FALSE(load_timing_info.socket_reused);
226 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
228 // Only the request times should be non-null.
229 EXPECT_FALSE(load_timing_info.request_start_time.is_null());
230 EXPECT_FALSE(load_timing_info.request_start.is_null());
232 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
234 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null());
235 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null());
236 EXPECT_TRUE(load_timing_info.send_start.is_null());
237 EXPECT_TRUE(load_timing_info.send_end.is_null());
238 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null());
240 #endif
242 // Do a case-insensitive search through |haystack| for |needle|.
243 bool ContainsString(const std::string& haystack, const char* needle) {
244 std::string::const_iterator it =
245 std::search(haystack.begin(),
246 haystack.end(),
247 needle,
248 needle + strlen(needle),
249 base::CaseInsensitiveCompare<char>());
250 return it != haystack.end();
253 scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) {
254 scoped_ptr<UploadElementReader> reader(
255 new UploadBytesElementReader(data, strlen(data)));
256 return ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0);
259 // Verify that the SSLInfo of a successful SSL connection has valid values.
260 void CheckSSLInfo(const SSLInfo& ssl_info) {
261 // -1 means unknown. 0 means no encryption.
262 EXPECT_GT(ssl_info.security_bits, 0);
264 // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated.
265 uint16 cipher_suite = SSLConnectionStatusToCipherSuite(
266 ssl_info.connection_status);
267 EXPECT_NE(0U, cipher_suite);
270 void CheckFullRequestHeaders(const HttpRequestHeaders& headers,
271 const GURL& host_url) {
272 std::string sent_value;
274 EXPECT_TRUE(headers.GetHeader("Host", &sent_value));
275 EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value);
277 EXPECT_TRUE(headers.GetHeader("Connection", &sent_value));
278 EXPECT_EQ("keep-alive", sent_value);
281 bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) {
282 size_t size = a.size();
284 if (size != b.size())
285 return false;
287 for (size_t i = 0; i < size; ++i) {
288 if (!a[i].Equals(b[i]))
289 return false;
292 return true;
294 #endif // !defined(OS_IOS)
296 // A network delegate that allows the user to choose a subset of request stages
297 // to block in. When blocking, the delegate can do one of the following:
298 // * synchronously return a pre-specified error code, or
299 // * asynchronously return that value via an automatically called callback,
300 // or
301 // * block and wait for the user to do a callback.
302 // Additionally, the user may also specify a redirect URL -- then each request
303 // with the current URL different from the redirect target will be redirected
304 // to that target, in the on-before-URL-request stage, independent of whether
305 // the delegate blocks in ON_BEFORE_URL_REQUEST or not.
306 class BlockingNetworkDelegate : public TestNetworkDelegate {
307 public:
308 // Stages in which the delegate can block.
309 enum Stage {
310 NOT_BLOCKED = 0,
311 ON_BEFORE_URL_REQUEST = 1 << 0,
312 ON_BEFORE_SEND_HEADERS = 1 << 1,
313 ON_HEADERS_RECEIVED = 1 << 2,
314 ON_AUTH_REQUIRED = 1 << 3
317 // Behavior during blocked stages. During other stages, just
318 // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION.
319 enum BlockMode {
320 SYNCHRONOUS, // No callback, returns specified return values.
321 AUTO_CALLBACK, // |this| posts a task to run the callback using the
322 // specified return codes.
323 USER_CALLBACK, // User takes care of doing a callback. |retval_| and
324 // |auth_retval_| are ignored. In every blocking stage the
325 // message loop is quit.
328 // Creates a delegate which does not block at all.
329 explicit BlockingNetworkDelegate(BlockMode block_mode);
331 // For users to trigger a callback returning |response|.
332 // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks.
333 // Only call if |block_mode_| == USER_CALLBACK.
334 void DoCallback(int response);
335 void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response);
337 // Setters.
338 void set_retval(int retval) {
339 ASSERT_NE(USER_CALLBACK, block_mode_);
340 ASSERT_NE(ERR_IO_PENDING, retval);
341 ASSERT_NE(OK, retval);
342 retval_ = retval;
345 // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then
346 // |auth_credentials_| will be passed with the response.
347 void set_auth_retval(AuthRequiredResponse auth_retval) {
348 ASSERT_NE(USER_CALLBACK, block_mode_);
349 ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval);
350 auth_retval_ = auth_retval;
352 void set_auth_credentials(const AuthCredentials& auth_credentials) {
353 auth_credentials_ = auth_credentials;
356 void set_redirect_url(const GURL& url) {
357 redirect_url_ = url;
360 void set_block_on(int block_on) {
361 block_on_ = block_on;
364 // Allows the user to check in which state did we block.
365 Stage stage_blocked_for_callback() const {
366 EXPECT_EQ(USER_CALLBACK, block_mode_);
367 return stage_blocked_for_callback_;
370 private:
371 void RunCallback(int response, const CompletionCallback& callback);
372 void RunAuthCallback(AuthRequiredResponse response,
373 const AuthCallback& callback);
375 // TestNetworkDelegate implementation.
376 int OnBeforeURLRequest(URLRequest* request,
377 const CompletionCallback& callback,
378 GURL* new_url) override;
380 int OnBeforeSendHeaders(URLRequest* request,
381 const CompletionCallback& callback,
382 HttpRequestHeaders* headers) override;
384 int OnHeadersReceived(
385 URLRequest* request,
386 const CompletionCallback& callback,
387 const HttpResponseHeaders* original_response_headers,
388 scoped_refptr<HttpResponseHeaders>* override_response_headers,
389 GURL* allowed_unsafe_redirect_url) override;
391 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
392 URLRequest* request,
393 const AuthChallengeInfo& auth_info,
394 const AuthCallback& callback,
395 AuthCredentials* credentials) override;
397 // Resets the callbacks and |stage_blocked_for_callback_|.
398 void Reset();
400 // Checks whether we should block in |stage|. If yes, returns an error code
401 // and optionally sets up callback based on |block_mode_|. If no, returns OK.
402 int MaybeBlockStage(Stage stage, const CompletionCallback& callback);
404 // Configuration parameters, can be adjusted by public methods:
405 const BlockMode block_mode_;
407 // Values returned on blocking stages when mode is SYNCHRONOUS or
408 // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING.
409 int retval_; // To be returned in non-auth stages.
410 AuthRequiredResponse auth_retval_;
412 GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest.
413 int block_on_; // Bit mask: in which stages to block.
415 // |auth_credentials_| will be copied to |*target_auth_credential_| on
416 // callback.
417 AuthCredentials auth_credentials_;
418 AuthCredentials* target_auth_credentials_;
420 // Internal variables, not set by not the user:
421 // Last blocked stage waiting for user callback (unused if |block_mode_| !=
422 // USER_CALLBACK).
423 Stage stage_blocked_for_callback_;
425 // Callback objects stored during blocking stages.
426 CompletionCallback callback_;
427 AuthCallback auth_callback_;
429 base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_;
431 DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate);
434 BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode)
435 : block_mode_(block_mode),
436 retval_(OK),
437 auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION),
438 block_on_(0),
439 target_auth_credentials_(NULL),
440 stage_blocked_for_callback_(NOT_BLOCKED),
441 weak_factory_(this) {
444 void BlockingNetworkDelegate::DoCallback(int response) {
445 ASSERT_EQ(USER_CALLBACK, block_mode_);
446 ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
447 ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
448 CompletionCallback callback = callback_;
449 Reset();
450 RunCallback(response, callback);
453 void BlockingNetworkDelegate::DoAuthCallback(
454 NetworkDelegate::AuthRequiredResponse response) {
455 ASSERT_EQ(USER_CALLBACK, block_mode_);
456 ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_);
457 AuthCallback auth_callback = auth_callback_;
458 Reset();
459 RunAuthCallback(response, auth_callback);
462 void BlockingNetworkDelegate::RunCallback(int response,
463 const CompletionCallback& callback) {
464 callback.Run(response);
467 void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response,
468 const AuthCallback& callback) {
469 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) {
470 ASSERT_TRUE(target_auth_credentials_ != NULL);
471 *target_auth_credentials_ = auth_credentials_;
473 callback.Run(response);
476 int BlockingNetworkDelegate::OnBeforeURLRequest(
477 URLRequest* request,
478 const CompletionCallback& callback,
479 GURL* new_url) {
480 if (redirect_url_ == request->url())
481 return OK; // We've already seen this request and redirected elsewhere.
483 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
485 if (!redirect_url_.is_empty())
486 *new_url = redirect_url_;
488 return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback);
491 int BlockingNetworkDelegate::OnBeforeSendHeaders(
492 URLRequest* request,
493 const CompletionCallback& callback,
494 HttpRequestHeaders* headers) {
495 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
497 return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback);
500 int BlockingNetworkDelegate::OnHeadersReceived(
501 URLRequest* request,
502 const CompletionCallback& callback,
503 const HttpResponseHeaders* original_response_headers,
504 scoped_refptr<HttpResponseHeaders>* override_response_headers,
505 GURL* allowed_unsafe_redirect_url) {
506 TestNetworkDelegate::OnHeadersReceived(request,
507 callback,
508 original_response_headers,
509 override_response_headers,
510 allowed_unsafe_redirect_url);
512 return MaybeBlockStage(ON_HEADERS_RECEIVED, callback);
515 NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired(
516 URLRequest* request,
517 const AuthChallengeInfo& auth_info,
518 const AuthCallback& callback,
519 AuthCredentials* credentials) {
520 TestNetworkDelegate::OnAuthRequired(request, auth_info, callback,
521 credentials);
522 // Check that the user has provided callback for the previous blocked stage.
523 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
525 if ((block_on_ & ON_AUTH_REQUIRED) == 0) {
526 return AUTH_REQUIRED_RESPONSE_NO_ACTION;
529 target_auth_credentials_ = credentials;
531 switch (block_mode_) {
532 case SYNCHRONOUS:
533 if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH)
534 *target_auth_credentials_ = auth_credentials_;
535 return auth_retval_;
537 case AUTO_CALLBACK:
538 base::MessageLoop::current()->PostTask(
539 FROM_HERE,
540 base::Bind(&BlockingNetworkDelegate::RunAuthCallback,
541 weak_factory_.GetWeakPtr(), auth_retval_, callback));
542 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
544 case USER_CALLBACK:
545 auth_callback_ = callback;
546 stage_blocked_for_callback_ = ON_AUTH_REQUIRED;
547 base::MessageLoop::current()->PostTask(FROM_HERE,
548 base::MessageLoop::QuitClosure());
549 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
551 NOTREACHED();
552 return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value.
555 void BlockingNetworkDelegate::Reset() {
556 EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_);
557 stage_blocked_for_callback_ = NOT_BLOCKED;
558 callback_.Reset();
559 auth_callback_.Reset();
562 int BlockingNetworkDelegate::MaybeBlockStage(
563 BlockingNetworkDelegate::Stage stage,
564 const CompletionCallback& callback) {
565 // Check that the user has provided callback for the previous blocked stage.
566 EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_);
568 if ((block_on_ & stage) == 0) {
569 return OK;
572 switch (block_mode_) {
573 case SYNCHRONOUS:
574 EXPECT_NE(OK, retval_);
575 return retval_;
577 case AUTO_CALLBACK:
578 base::MessageLoop::current()->PostTask(
579 FROM_HERE,
580 base::Bind(&BlockingNetworkDelegate::RunCallback,
581 weak_factory_.GetWeakPtr(), retval_, callback));
582 return ERR_IO_PENDING;
584 case USER_CALLBACK:
585 callback_ = callback;
586 stage_blocked_for_callback_ = stage;
587 base::MessageLoop::current()->PostTask(FROM_HERE,
588 base::MessageLoop::QuitClosure());
589 return ERR_IO_PENDING;
591 NOTREACHED();
592 return 0;
595 class TestURLRequestContextWithProxy : public TestURLRequestContext {
596 public:
597 // Does not own |delegate|.
598 TestURLRequestContextWithProxy(const std::string& proxy,
599 NetworkDelegate* delegate)
600 : TestURLRequestContext(true) {
601 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy));
602 set_network_delegate(delegate);
603 Init();
605 ~TestURLRequestContextWithProxy() override {}
608 } // namespace
610 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.
611 class URLRequestTest : public PlatformTest {
612 public:
613 URLRequestTest() : default_context_(true) {
614 default_context_.set_network_delegate(&default_network_delegate_);
615 default_context_.set_net_log(&net_log_);
616 job_factory_impl_ = new URLRequestJobFactoryImpl();
617 job_factory_.reset(job_factory_impl_);
620 ~URLRequestTest() override {
621 // URLRequestJobs may post clean-up tasks on destruction.
622 base::RunLoop().RunUntilIdle();
625 void SetUp() override {
626 SetUpFactory();
627 default_context_.set_job_factory(job_factory_.get());
628 default_context_.Init();
629 PlatformTest::SetUp();
632 virtual void SetUpFactory() {
633 job_factory_impl_->SetProtocolHandler("data", new DataProtocolHandler);
634 #if !defined(DISABLE_FILE_SUPPORT)
635 job_factory_impl_->SetProtocolHandler(
636 "file", new FileProtocolHandler(base::MessageLoopProxy::current()));
637 #endif
640 TestNetworkDelegate* default_network_delegate() {
641 return &default_network_delegate_;
644 const TestURLRequestContext& default_context() const {
645 return default_context_;
649 // Adds the TestJobInterceptor to the default context.
650 TestJobInterceptor* AddTestInterceptor() {
651 TestJobInterceptor* protocol_handler_ = new TestJobInterceptor();
652 job_factory_impl_->SetProtocolHandler("http", NULL);
653 job_factory_impl_->SetProtocolHandler("http", protocol_handler_);
654 return protocol_handler_;
657 protected:
658 TestNetLog net_log_;
659 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
660 URLRequestJobFactoryImpl* job_factory_impl_;
661 scoped_ptr<URLRequestJobFactory> job_factory_;
662 TestURLRequestContext default_context_;
665 TEST_F(URLRequestTest, AboutBlankTest) {
666 TestDelegate d;
668 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
669 GURL("about:blank"), DEFAULT_PRIORITY, &d));
671 r->Start();
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(), 0);
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, DataURLImageTest) {
688 TestDelegate d;
690 // Use our nice little Chrome logo.
691 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
692 GURL(
693 "data:image/png;base64,"
694 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB"
695 "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ"
696 "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI"
697 "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz"
698 "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck"
699 "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0"
700 "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H"
701 "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk"
702 "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6"
703 "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ"
704 "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ"
705 "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz"
706 "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC"
707 "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40"
708 "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4"
709 "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O"
710 "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm"
711 "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV"
712 "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="),
713 DEFAULT_PRIORITY, &d));
715 r->Start();
716 EXPECT_TRUE(r->is_pending());
718 base::RunLoop().Run();
720 EXPECT_TRUE(!r->is_pending());
721 EXPECT_FALSE(d.received_data_before_response());
722 EXPECT_EQ(d.bytes_received(), 911);
723 EXPECT_EQ("", r->GetSocketAddress().host());
724 EXPECT_EQ(0, r->GetSocketAddress().port());
726 HttpRequestHeaders headers;
727 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
731 #if !defined(DISABLE_FILE_SUPPORT)
732 TEST_F(URLRequestTest, FileTest) {
733 base::FilePath app_path;
734 PathService::Get(base::FILE_EXE, &app_path);
735 GURL app_url = FilePathToFileURL(app_path);
737 TestDelegate d;
739 scoped_ptr<URLRequest> r(
740 default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d));
742 r->Start();
743 EXPECT_TRUE(r->is_pending());
745 base::RunLoop().Run();
747 int64 file_size = -1;
748 EXPECT_TRUE(base::GetFileSize(app_path, &file_size));
750 EXPECT_TRUE(!r->is_pending());
751 EXPECT_EQ(1, d.response_started_count());
752 EXPECT_FALSE(d.received_data_before_response());
753 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
754 EXPECT_EQ("", r->GetSocketAddress().host());
755 EXPECT_EQ(0, r->GetSocketAddress().port());
757 HttpRequestHeaders headers;
758 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
762 TEST_F(URLRequestTest, FileTestCancel) {
763 base::FilePath app_path;
764 PathService::Get(base::FILE_EXE, &app_path);
765 GURL app_url = FilePathToFileURL(app_path);
767 TestDelegate d;
769 scoped_ptr<URLRequest> r(
770 default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d));
772 r->Start();
773 EXPECT_TRUE(r->is_pending());
774 r->Cancel();
776 // Async cancellation should be safe even when URLRequest has been already
777 // destroyed.
778 base::RunLoop().RunUntilIdle();
781 TEST_F(URLRequestTest, FileTestFullSpecifiedRange) {
782 const size_t buffer_size = 4000;
783 scoped_ptr<char[]> buffer(new char[buffer_size]);
784 FillBuffer(buffer.get(), buffer_size);
786 base::FilePath temp_path;
787 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
788 GURL temp_url = FilePathToFileURL(temp_path);
789 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
791 int64 file_size;
792 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
794 const size_t first_byte_position = 500;
795 const size_t last_byte_position = buffer_size - first_byte_position;
796 const size_t content_length = last_byte_position - first_byte_position + 1;
797 std::string partial_buffer_string(buffer.get() + first_byte_position,
798 buffer.get() + last_byte_position + 1);
800 TestDelegate d;
802 scoped_ptr<URLRequest> r(
803 default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d));
805 HttpRequestHeaders headers;
806 headers.SetHeader(
807 HttpRequestHeaders::kRange,
808 HttpByteRange::Bounded(
809 first_byte_position, last_byte_position).GetHeaderValue());
810 r->SetExtraRequestHeaders(headers);
811 r->Start();
812 EXPECT_TRUE(r->is_pending());
814 base::RunLoop().Run();
815 EXPECT_TRUE(!r->is_pending());
816 EXPECT_EQ(1, d.response_started_count());
817 EXPECT_FALSE(d.received_data_before_response());
818 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
819 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
820 EXPECT_TRUE(partial_buffer_string == d.data_received());
823 EXPECT_TRUE(base::DeleteFile(temp_path, false));
826 TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) {
827 const size_t buffer_size = 4000;
828 scoped_ptr<char[]> buffer(new char[buffer_size]);
829 FillBuffer(buffer.get(), buffer_size);
831 base::FilePath temp_path;
832 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
833 GURL temp_url = FilePathToFileURL(temp_path);
834 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
836 int64 file_size;
837 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
839 const size_t first_byte_position = 500;
840 const size_t last_byte_position = buffer_size - 1;
841 const size_t content_length = last_byte_position - first_byte_position + 1;
842 std::string partial_buffer_string(buffer.get() + first_byte_position,
843 buffer.get() + last_byte_position + 1);
845 TestDelegate d;
847 scoped_ptr<URLRequest> r(
848 default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d));
850 HttpRequestHeaders headers;
851 headers.SetHeader(HttpRequestHeaders::kRange,
852 HttpByteRange::RightUnbounded(
853 first_byte_position).GetHeaderValue());
854 r->SetExtraRequestHeaders(headers);
855 r->Start();
856 EXPECT_TRUE(r->is_pending());
858 base::RunLoop().Run();
859 EXPECT_TRUE(!r->is_pending());
860 EXPECT_EQ(1, d.response_started_count());
861 EXPECT_FALSE(d.received_data_before_response());
862 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received());
863 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed.
864 EXPECT_TRUE(partial_buffer_string == d.data_received());
867 EXPECT_TRUE(base::DeleteFile(temp_path, false));
870 TEST_F(URLRequestTest, FileTestMultipleRanges) {
871 const size_t buffer_size = 400000;
872 scoped_ptr<char[]> buffer(new char[buffer_size]);
873 FillBuffer(buffer.get(), buffer_size);
875 base::FilePath temp_path;
876 EXPECT_TRUE(base::CreateTemporaryFile(&temp_path));
877 GURL temp_url = FilePathToFileURL(temp_path);
878 EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size));
880 int64 file_size;
881 EXPECT_TRUE(base::GetFileSize(temp_path, &file_size));
883 TestDelegate d;
885 scoped_ptr<URLRequest> r(
886 default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d));
888 HttpRequestHeaders headers;
889 headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300");
890 r->SetExtraRequestHeaders(headers);
891 r->Start();
892 EXPECT_TRUE(r->is_pending());
894 base::RunLoop().Run();
895 EXPECT_TRUE(d.request_failed());
898 EXPECT_TRUE(base::DeleteFile(temp_path, false));
901 TEST_F(URLRequestTest, AllowFileURLs) {
902 base::ScopedTempDir temp_dir;
903 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
904 base::FilePath test_file;
905 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file));
906 std::string test_data("monkey");
907 base::WriteFile(test_file, test_data.data(), test_data.size());
908 GURL test_file_url = FilePathToFileURL(test_file);
911 TestDelegate d;
912 TestNetworkDelegate network_delegate;
913 network_delegate.set_can_access_files(true);
914 default_context_.set_network_delegate(&network_delegate);
915 scoped_ptr<URLRequest> r(
916 default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d));
917 r->Start();
918 base::RunLoop().Run();
919 EXPECT_FALSE(d.request_failed());
920 EXPECT_EQ(test_data, d.data_received());
924 TestDelegate d;
925 TestNetworkDelegate network_delegate;
926 network_delegate.set_can_access_files(false);
927 default_context_.set_network_delegate(&network_delegate);
928 scoped_ptr<URLRequest> r(
929 default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d));
930 r->Start();
931 base::RunLoop().Run();
932 EXPECT_TRUE(d.request_failed());
933 EXPECT_EQ("", d.data_received());
938 TEST_F(URLRequestTest, FileDirCancelTest) {
939 // Put in mock resource provider.
940 NetModule::SetResourceProvider(TestNetResourceProvider);
942 TestDelegate d;
944 base::FilePath file_path;
945 PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
946 file_path = file_path.Append(FILE_PATH_LITERAL("net"));
947 file_path = file_path.Append(FILE_PATH_LITERAL("data"));
949 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
950 FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d));
951 req->Start();
952 EXPECT_TRUE(req->is_pending());
954 d.set_cancel_in_received_data_pending(true);
956 base::RunLoop().Run();
959 // Take out mock resource provider.
960 NetModule::SetResourceProvider(NULL);
963 TEST_F(URLRequestTest, FileDirOutputSanity) {
964 // Verify the general sanity of the the output of the file:
965 // directory lister by checking for the output of a known existing
966 // file.
967 const char sentinel_name[] = "filedir-sentinel";
969 base::FilePath path;
970 PathService::Get(base::DIR_SOURCE_ROOT, &path);
971 path = path.Append(FILE_PATH_LITERAL("net"));
972 path = path.Append(FILE_PATH_LITERAL("data"));
973 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
975 TestDelegate d;
976 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
977 FilePathToFileURL(path), DEFAULT_PRIORITY, &d));
978 req->Start();
979 base::RunLoop().Run();
981 // Generate entry for the sentinel file.
982 base::FilePath sentinel_path = path.AppendASCII(sentinel_name);
983 base::File::Info info;
984 EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info));
985 EXPECT_GT(info.size, 0);
986 std::string sentinel_output = GetDirectoryListingEntry(
987 base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)),
988 std::string(sentinel_name),
989 false /* is_dir */,
990 info.size,
991 info.last_modified);
993 ASSERT_LT(0, d.bytes_received());
994 ASSERT_FALSE(d.request_failed());
995 ASSERT_TRUE(req->status().is_success());
996 // Check for the entry generated for the "sentinel" file.
997 const std::string& data = d.data_received();
998 ASSERT_NE(data.find(sentinel_output), std::string::npos);
1001 TEST_F(URLRequestTest, FileDirRedirectNoCrash) {
1002 // There is an implicit redirect when loading a file path that matches a
1003 // directory and does not end with a slash. Ensure that following such
1004 // redirects does not crash. See http://crbug.com/18686.
1006 base::FilePath path;
1007 PathService::Get(base::DIR_SOURCE_ROOT, &path);
1008 path = path.Append(FILE_PATH_LITERAL("net"));
1009 path = path.Append(FILE_PATH_LITERAL("data"));
1010 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
1012 TestDelegate d;
1013 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1014 FilePathToFileURL(path), DEFAULT_PRIORITY, &d));
1015 req->Start();
1016 base::RunLoop().Run();
1018 ASSERT_EQ(1, d.received_redirect_count());
1019 ASSERT_LT(0, d.bytes_received());
1020 ASSERT_FALSE(d.request_failed());
1021 ASSERT_TRUE(req->status().is_success());
1024 #if defined(OS_WIN)
1025 // Don't accept the url "file:///" on windows. See http://crbug.com/1474.
1026 TEST_F(URLRequestTest, FileDirRedirectSingleSlash) {
1027 TestDelegate d;
1028 scoped_ptr<URLRequest> req(
1029 default_context_.CreateRequest(GURL("file:///"), DEFAULT_PRIORITY, &d));
1030 req->Start();
1031 base::RunLoop().Run();
1033 ASSERT_EQ(1, d.received_redirect_count());
1034 ASSERT_FALSE(req->status().is_success());
1036 #endif // defined(OS_WIN)
1038 #endif // !defined(DISABLE_FILE_SUPPORT)
1040 TEST_F(URLRequestTest, InvalidUrlTest) {
1041 TestDelegate d;
1043 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
1044 GURL("invalid url"), DEFAULT_PRIORITY, &d));
1046 r->Start();
1047 EXPECT_TRUE(r->is_pending());
1049 base::RunLoop().Run();
1050 EXPECT_TRUE(d.request_failed());
1054 TEST_F(URLRequestTest, InvalidReferrerTest) {
1055 TestURLRequestContext context;
1056 TestNetworkDelegate network_delegate;
1057 network_delegate.set_cancel_request_with_policy_violating_referrer(true);
1058 context.set_network_delegate(&network_delegate);
1059 TestDelegate d;
1060 scoped_ptr<URLRequest> req(
1061 context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d));
1062 req->SetReferrer("https://somewhere.com/");
1064 req->Start();
1065 base::RunLoop().Run();
1066 EXPECT_TRUE(d.request_failed());
1069 #if defined(OS_WIN)
1070 TEST_F(URLRequestTest, ResolveShortcutTest) {
1071 base::FilePath app_path;
1072 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
1073 app_path = app_path.AppendASCII("net");
1074 app_path = app_path.AppendASCII("data");
1075 app_path = app_path.AppendASCII("url_request_unittest");
1076 app_path = app_path.AppendASCII("with-headers.html");
1078 std::wstring lnk_path = app_path.value() + L".lnk";
1080 base::win::ScopedCOMInitializer com_initializer;
1082 // Temporarily create a shortcut for test
1084 base::win::ScopedComPtr<IShellLink> shell;
1085 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL,
1086 CLSCTX_INPROC_SERVER)));
1087 base::win::ScopedComPtr<IPersistFile> persist;
1088 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive())));
1089 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str())));
1090 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest")));
1091 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE)));
1094 TestDelegate d;
1096 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
1097 FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d));
1099 r->Start();
1100 EXPECT_TRUE(r->is_pending());
1102 base::RunLoop().Run();
1104 WIN32_FILE_ATTRIBUTE_DATA data;
1105 GetFileAttributesEx(app_path.value().c_str(),
1106 GetFileExInfoStandard, &data);
1107 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ,
1108 FILE_SHARE_READ, NULL, OPEN_EXISTING,
1109 FILE_ATTRIBUTE_NORMAL, NULL);
1110 EXPECT_NE(INVALID_HANDLE_VALUE, file);
1111 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]);
1112 DWORD read_size;
1113 BOOL result;
1114 result = ReadFile(file, buffer.get(), data.nFileSizeLow,
1115 &read_size, NULL);
1116 std::string content(buffer.get(), read_size);
1117 CloseHandle(file);
1119 EXPECT_TRUE(!r->is_pending());
1120 EXPECT_EQ(1, d.received_redirect_count());
1121 EXPECT_EQ(content, d.data_received());
1124 // Clean the shortcut
1125 DeleteFile(lnk_path.c_str());
1127 #endif // defined(OS_WIN)
1129 // Custom URLRequestJobs for use with interceptor tests
1130 class RestartTestJob : public URLRequestTestJob {
1131 public:
1132 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1133 : URLRequestTestJob(request, network_delegate, true) {}
1134 protected:
1135 void StartAsync() override { this->NotifyRestartRequired(); }
1136 private:
1137 ~RestartTestJob() override {}
1140 class CancelTestJob : public URLRequestTestJob {
1141 public:
1142 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate)
1143 : URLRequestTestJob(request, network_delegate, true) {}
1144 protected:
1145 void StartAsync() override { request_->Cancel(); }
1146 private:
1147 ~CancelTestJob() override {}
1150 class CancelThenRestartTestJob : public URLRequestTestJob {
1151 public:
1152 explicit CancelThenRestartTestJob(URLRequest* request,
1153 NetworkDelegate* network_delegate)
1154 : URLRequestTestJob(request, network_delegate, true) {
1156 protected:
1157 void StartAsync() override {
1158 request_->Cancel();
1159 this->NotifyRestartRequired();
1161 private:
1162 ~CancelThenRestartTestJob() override {}
1165 // An Interceptor for use with interceptor tests.
1166 class MockURLRequestInterceptor : public URLRequestInterceptor {
1167 public:
1168 // Static getters for canned response header and data strings.
1169 static std::string ok_data() {
1170 return URLRequestTestJob::test_data_1();
1173 static std::string ok_headers() {
1174 return URLRequestTestJob::test_headers();
1177 static std::string redirect_data() {
1178 return std::string();
1181 static std::string redirect_headers() {
1182 return URLRequestTestJob::test_redirect_headers();
1185 static std::string error_data() {
1186 return std::string("ohhh nooooo mr. bill!");
1189 static std::string error_headers() {
1190 return URLRequestTestJob::test_error_headers();
1193 MockURLRequestInterceptor()
1194 : intercept_main_request_(false), restart_main_request_(false),
1195 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1196 simulate_main_network_error_(false),
1197 intercept_redirect_(false), cancel_redirect_request_(false),
1198 intercept_final_response_(false), cancel_final_request_(false),
1199 use_url_request_http_job_(false),
1200 did_intercept_main_(false), did_restart_main_(false),
1201 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1202 did_simulate_error_main_(false),
1203 did_intercept_redirect_(false), did_cancel_redirect_(false),
1204 did_intercept_final_(false), did_cancel_final_(false) {
1207 ~MockURLRequestInterceptor() override {
1210 // URLRequestInterceptor implementation:
1211 URLRequestJob* MaybeInterceptRequest(
1212 URLRequest* request,
1213 NetworkDelegate* network_delegate) const override {
1214 if (restart_main_request_) {
1215 restart_main_request_ = false;
1216 did_restart_main_ = true;
1217 return new RestartTestJob(request, network_delegate);
1219 if (cancel_main_request_) {
1220 cancel_main_request_ = false;
1221 did_cancel_main_ = true;
1222 return new CancelTestJob(request, network_delegate);
1224 if (cancel_then_restart_main_request_) {
1225 cancel_then_restart_main_request_ = false;
1226 did_cancel_then_restart_main_ = true;
1227 return new CancelThenRestartTestJob(request, network_delegate);
1229 if (simulate_main_network_error_) {
1230 simulate_main_network_error_ = false;
1231 did_simulate_error_main_ = true;
1232 if (use_url_request_http_job_) {
1233 return URLRequestHttpJob::Factory(request, network_delegate, "http");
1235 // This job will result in error since the requested URL is not one of the
1236 // URLs supported by these tests.
1237 return new URLRequestTestJob(request, network_delegate, true);
1239 if (!intercept_main_request_)
1240 return nullptr;
1241 intercept_main_request_ = false;
1242 did_intercept_main_ = true;
1243 URLRequestTestJob* job = new URLRequestTestJob(request,
1244 network_delegate,
1245 main_headers_,
1246 main_data_,
1247 true);
1248 job->set_load_timing_info(main_request_load_timing_info_);
1249 return job;
1252 URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
1253 NetworkDelegate* network_delegate,
1254 const GURL& location) const override {
1255 if (cancel_redirect_request_) {
1256 cancel_redirect_request_ = false;
1257 did_cancel_redirect_ = true;
1258 return new CancelTestJob(request, network_delegate);
1260 if (!intercept_redirect_)
1261 return nullptr;
1262 intercept_redirect_ = false;
1263 did_intercept_redirect_ = true;
1264 if (use_url_request_http_job_) {
1265 return URLRequestHttpJob::Factory(request, network_delegate, "http");
1267 return new URLRequestTestJob(request,
1268 network_delegate,
1269 redirect_headers_,
1270 redirect_data_,
1271 true);
1274 URLRequestJob* MaybeInterceptResponse(
1275 URLRequest* request,
1276 NetworkDelegate* network_delegate) const override {
1277 if (cancel_final_request_) {
1278 cancel_final_request_ = false;
1279 did_cancel_final_ = true;
1280 return new CancelTestJob(request, network_delegate);
1282 if (!intercept_final_response_)
1283 return nullptr;
1284 intercept_final_response_ = false;
1285 did_intercept_final_ = true;
1286 if (use_url_request_http_job_) {
1287 return URLRequestHttpJob::Factory(request, network_delegate, "http");
1289 return new URLRequestTestJob(request,
1290 network_delegate,
1291 final_headers_,
1292 final_data_,
1293 true);
1296 void set_intercept_main_request(bool intercept_main_request) {
1297 intercept_main_request_ = intercept_main_request;
1300 void set_main_headers(const std::string& main_headers) {
1301 main_headers_ = main_headers;
1304 void set_main_data(const std::string& main_data) {
1305 main_data_ = main_data;
1308 void set_main_request_load_timing_info(
1309 const LoadTimingInfo& main_request_load_timing_info) {
1310 main_request_load_timing_info_ = main_request_load_timing_info;
1313 void set_restart_main_request(bool restart_main_request) {
1314 restart_main_request_ = restart_main_request;
1317 void set_cancel_main_request(bool cancel_main_request) {
1318 cancel_main_request_ = cancel_main_request;
1321 void set_cancel_then_restart_main_request(
1322 bool cancel_then_restart_main_request) {
1323 cancel_then_restart_main_request_ = cancel_then_restart_main_request;
1326 void set_simulate_main_network_error(bool simulate_main_network_error) {
1327 simulate_main_network_error_ = simulate_main_network_error;
1330 void set_intercept_redirect(bool intercept_redirect) {
1331 intercept_redirect_ = intercept_redirect;
1334 void set_redirect_headers(const std::string& redirect_headers) {
1335 redirect_headers_ = redirect_headers;
1338 void set_redirect_data(const std::string& redirect_data) {
1339 redirect_data_ = redirect_data;
1342 void set_cancel_redirect_request(bool cancel_redirect_request) {
1343 cancel_redirect_request_ = cancel_redirect_request;
1346 void set_intercept_final_response(bool intercept_final_response) {
1347 intercept_final_response_ = intercept_final_response;
1350 void set_final_headers(const std::string& final_headers) {
1351 final_headers_ = final_headers;
1354 void set_final_data(const std::string& final_data) {
1355 final_data_ = final_data;
1358 void set_cancel_final_request(bool cancel_final_request) {
1359 cancel_final_request_ = cancel_final_request;
1362 void set_use_url_request_http_job(bool use_url_request_http_job) {
1363 use_url_request_http_job_ = use_url_request_http_job;
1366 bool did_intercept_main() const {
1367 return did_intercept_main_;
1370 bool did_restart_main() const {
1371 return did_restart_main_;
1374 bool did_cancel_main() const {
1375 return did_cancel_main_;
1378 bool did_cancel_then_restart_main() const {
1379 return did_cancel_then_restart_main_;
1382 bool did_simulate_error_main() const {
1383 return did_simulate_error_main_;
1386 bool did_intercept_redirect() const {
1387 return did_intercept_redirect_;
1390 bool did_cancel_redirect() const {
1391 return did_cancel_redirect_;
1394 bool did_intercept_final() const {
1395 return did_intercept_final_;
1398 bool did_cancel_final() const {
1399 return did_cancel_final_;
1402 private:
1403 // Indicate whether to intercept the main request, and if so specify the
1404 // response to return and the LoadTimingInfo to use.
1405 mutable bool intercept_main_request_;
1406 mutable std::string main_headers_;
1407 mutable std::string main_data_;
1408 mutable LoadTimingInfo main_request_load_timing_info_;
1410 // These indicate actions that can be taken within MaybeInterceptRequest.
1411 mutable bool restart_main_request_;
1412 mutable bool cancel_main_request_;
1413 mutable bool cancel_then_restart_main_request_;
1414 mutable bool simulate_main_network_error_;
1416 // Indicate whether to intercept redirects, and if so specify the response to
1417 // return.
1418 mutable bool intercept_redirect_;
1419 mutable std::string redirect_headers_;
1420 mutable std::string redirect_data_;
1422 // Cancel the request within MaybeInterceptRedirect.
1423 mutable bool cancel_redirect_request_;
1425 // Indicate whether to intercept the final response, and if so specify the
1426 // response to return.
1427 mutable bool intercept_final_response_;
1428 mutable std::string final_headers_;
1429 mutable std::string final_data_;
1431 // Cancel the final request within MaybeInterceptResponse.
1432 mutable bool cancel_final_request_;
1434 // Instruct the interceptor to use a real URLRequestHTTPJob.
1435 mutable bool use_url_request_http_job_;
1437 // These indicate if the interceptor did something or not.
1438 mutable bool did_intercept_main_;
1439 mutable bool did_restart_main_;
1440 mutable bool did_cancel_main_;
1441 mutable bool did_cancel_then_restart_main_;
1442 mutable bool did_simulate_error_main_;
1443 mutable bool did_intercept_redirect_;
1444 mutable bool did_cancel_redirect_;
1445 mutable bool did_intercept_final_;
1446 mutable bool did_cancel_final_;
1449 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.
1450 class URLRequestInterceptorTest : public URLRequestTest {
1451 public:
1452 URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) {
1455 ~URLRequestInterceptorTest() override {
1456 // URLRequestJobs may post clean-up tasks on destruction.
1457 base::RunLoop().RunUntilIdle();
1460 void SetUpFactory() override {
1461 interceptor_ = new MockURLRequestInterceptor();
1462 job_factory_.reset(new URLRequestInterceptingJobFactory(
1463 job_factory_.Pass(), make_scoped_ptr(interceptor_)));
1466 MockURLRequestInterceptor* interceptor() const {
1467 return interceptor_;
1470 private:
1471 MockURLRequestInterceptor* interceptor_;
1474 TEST_F(URLRequestInterceptorTest, Intercept) {
1475 // Intercept the main request and respond with a simple response.
1476 interceptor()->set_intercept_main_request(true);
1477 interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers());
1478 interceptor()->set_main_data(MockURLRequestInterceptor::ok_data());
1479 TestDelegate d;
1480 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1481 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1482 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1483 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1484 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1485 req->SetUserData(nullptr, user_data0);
1486 req->SetUserData(&user_data1, user_data1);
1487 req->SetUserData(&user_data2, user_data2);
1488 req->set_method("GET");
1489 req->Start();
1490 base::RunLoop().Run();
1492 // Make sure we can retrieve our specific user data.
1493 EXPECT_EQ(user_data0, req->GetUserData(nullptr));
1494 EXPECT_EQ(user_data1, req->GetUserData(&user_data1));
1495 EXPECT_EQ(user_data2, req->GetUserData(&user_data2));
1497 // Check that we got one good response.
1498 EXPECT_TRUE(req->status().is_success());
1499 EXPECT_EQ(200, req->response_headers()->response_code());
1500 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1501 EXPECT_EQ(1, d.response_started_count());
1502 EXPECT_EQ(0, d.received_redirect_count());
1505 TEST_F(URLRequestInterceptorTest, InterceptRedirect) {
1506 // Intercept the main request and respond with a redirect.
1507 interceptor()->set_intercept_main_request(true);
1508 interceptor()->set_main_headers(
1509 MockURLRequestInterceptor::redirect_headers());
1510 interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data());
1512 // Intercept that redirect and respond with a final OK response.
1513 interceptor()->set_intercept_redirect(true);
1514 interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers());
1515 interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data());
1517 TestDelegate d;
1518 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1519 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1520 req->set_method("GET");
1521 req->Start();
1522 base::RunLoop().Run();
1524 // Check that the interceptor got called as expected.
1525 EXPECT_TRUE(interceptor()->did_intercept_main());
1526 EXPECT_TRUE(interceptor()->did_intercept_redirect());
1528 // Check that we got one good response.
1529 EXPECT_TRUE(req->status().is_success());
1530 if (req->status().is_success())
1531 EXPECT_EQ(200, req->response_headers()->response_code());
1533 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1534 EXPECT_EQ(1, d.response_started_count());
1535 EXPECT_EQ(0, d.received_redirect_count());
1538 TEST_F(URLRequestInterceptorTest, InterceptServerError) {
1539 // Intercept the main request to generate a server error response.
1540 interceptor()->set_intercept_main_request(true);
1541 interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers());
1542 interceptor()->set_main_data(MockURLRequestInterceptor::error_data());
1544 // Intercept that error and respond with an OK response.
1545 interceptor()->set_intercept_final_response(true);
1546 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1547 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1549 TestDelegate d;
1550 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1551 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1552 req->set_method("GET");
1553 req->Start();
1554 base::RunLoop().Run();
1556 // Check that the interceptor got called as expected.
1557 EXPECT_TRUE(interceptor()->did_intercept_main());
1558 EXPECT_TRUE(interceptor()->did_intercept_final());
1560 // Check that we got one good response.
1561 EXPECT_TRUE(req->status().is_success());
1562 EXPECT_EQ(200, req->response_headers()->response_code());
1563 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1564 EXPECT_EQ(1, d.response_started_count());
1565 EXPECT_EQ(0, d.received_redirect_count());
1568 TEST_F(URLRequestInterceptorTest, InterceptNetworkError) {
1569 // Intercept the main request to simulate a network error.
1570 interceptor()->set_simulate_main_network_error(true);
1572 // Intercept that error and respond with an OK response.
1573 interceptor()->set_intercept_final_response(true);
1574 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1575 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1577 TestDelegate d;
1578 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1579 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1580 req->set_method("GET");
1581 req->Start();
1582 base::RunLoop().Run();
1584 // Check that the interceptor got called as expected.
1585 EXPECT_TRUE(interceptor()->did_simulate_error_main());
1586 EXPECT_TRUE(interceptor()->did_intercept_final());
1588 // Check that we received one good response.
1589 EXPECT_TRUE(req->status().is_success());
1590 EXPECT_EQ(200, req->response_headers()->response_code());
1591 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1592 EXPECT_EQ(1, d.response_started_count());
1593 EXPECT_EQ(0, d.received_redirect_count());
1596 TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) {
1597 // Restart the main request.
1598 interceptor()->set_restart_main_request(true);
1600 // then intercept the new main request and respond with an OK response
1601 interceptor()->set_intercept_main_request(true);
1602 interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers());
1603 interceptor()->set_main_data(MockURLRequestInterceptor::ok_data());
1605 TestDelegate d;
1606 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1607 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1608 req->set_method("GET");
1609 req->Start();
1610 base::RunLoop().Run();
1612 // Check that the interceptor got called as expected.
1613 EXPECT_TRUE(interceptor()->did_restart_main());
1614 EXPECT_TRUE(interceptor()->did_intercept_main());
1616 // Check that we received one good response.
1617 EXPECT_TRUE(req->status().is_success());
1618 if (req->status().is_success())
1619 EXPECT_EQ(200, req->response_headers()->response_code());
1621 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
1622 EXPECT_EQ(1, d.response_started_count());
1623 EXPECT_EQ(0, d.received_redirect_count());
1626 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) {
1627 // Intercept the main request and cancel from within the restarted job.
1628 interceptor()->set_cancel_main_request(true);
1630 // Set up to intercept the final response and override it with an OK response.
1631 interceptor()->set_intercept_final_response(true);
1632 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1633 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1635 TestDelegate d;
1636 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1637 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1638 req->set_method("GET");
1639 req->Start();
1640 base::RunLoop().Run();
1642 // Check that the interceptor got called as expected.
1643 EXPECT_TRUE(interceptor()->did_cancel_main());
1644 EXPECT_FALSE(interceptor()->did_intercept_final());
1646 // Check that we see a canceled request.
1647 EXPECT_FALSE(req->status().is_success());
1648 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1651 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) {
1652 // Intercept the main request and respond with a redirect.
1653 interceptor()->set_intercept_main_request(true);
1654 interceptor()->set_main_headers(
1655 MockURLRequestInterceptor::redirect_headers());
1656 interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data());
1658 // Intercept the redirect and cancel from within that job.
1659 interceptor()->set_cancel_redirect_request(true);
1661 // Set up to intercept the final response and override it with an OK response.
1662 interceptor()->set_intercept_final_response(true);
1663 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1664 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1666 TestDelegate d;
1667 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1668 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1669 req->set_method("GET");
1670 req->Start();
1671 base::RunLoop().Run();
1673 // Check that the interceptor got called as expected.
1674 EXPECT_TRUE(interceptor()->did_intercept_main());
1675 EXPECT_TRUE(interceptor()->did_cancel_redirect());
1676 EXPECT_FALSE(interceptor()->did_intercept_final());
1678 // Check that we see a canceled request.
1679 EXPECT_FALSE(req->status().is_success());
1680 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1683 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) {
1684 // Intercept the main request to simulate a network error.
1685 interceptor()->set_simulate_main_network_error(true);
1687 // Set up to intercept final the response and cancel from within that job.
1688 interceptor()->set_cancel_final_request(true);
1690 TestDelegate d;
1691 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1692 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1693 req->set_method("GET");
1694 req->Start();
1695 base::RunLoop().Run();
1697 // Check that the interceptor got called as expected.
1698 EXPECT_TRUE(interceptor()->did_simulate_error_main());
1699 EXPECT_TRUE(interceptor()->did_cancel_final());
1701 // Check that we see a canceled request.
1702 EXPECT_FALSE(req->status().is_success());
1703 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1706 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) {
1707 // Intercept the main request and cancel then restart from within that job.
1708 interceptor()->set_cancel_then_restart_main_request(true);
1710 // Set up to intercept the final response and override it with an OK response.
1711 interceptor()->set_intercept_final_response(true);
1712 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
1713 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
1715 TestDelegate d;
1716 scoped_ptr<URLRequest> req(default_context().CreateRequest(
1717 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1718 req->set_method("GET");
1719 req->Start();
1720 base::RunLoop().Run();
1722 // Check that the interceptor got called as expected.
1723 EXPECT_TRUE(interceptor()->did_cancel_then_restart_main());
1724 EXPECT_FALSE(interceptor()->did_intercept_final());
1726 // Check that we see a canceled request.
1727 EXPECT_FALSE(req->status().is_success());
1728 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1731 // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not
1732 // reused. |connect_time_flags| is used to indicate if there should be dns
1733 // or SSL times, and |used_proxy| is used for proxy times.
1734 LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now,
1735 int connect_time_flags,
1736 bool used_proxy) {
1737 LoadTimingInfo load_timing;
1738 load_timing.socket_log_id = 1;
1740 if (used_proxy) {
1741 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1742 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1745 LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing;
1746 if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) {
1747 connect_timing.dns_start = now + base::TimeDelta::FromDays(3);
1748 connect_timing.dns_end = now + base::TimeDelta::FromDays(4);
1750 connect_timing.connect_start = now + base::TimeDelta::FromDays(5);
1751 if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) {
1752 connect_timing.ssl_start = now + base::TimeDelta::FromDays(6);
1753 connect_timing.ssl_end = now + base::TimeDelta::FromDays(7);
1755 connect_timing.connect_end = now + base::TimeDelta::FromDays(8);
1757 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1758 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1759 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1760 return load_timing;
1763 // Same as above, but in the case of a reused socket.
1764 LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now,
1765 bool used_proxy) {
1766 LoadTimingInfo load_timing;
1767 load_timing.socket_log_id = 1;
1768 load_timing.socket_reused = true;
1770 if (used_proxy) {
1771 load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1);
1772 load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2);
1775 load_timing.send_start = now + base::TimeDelta::FromDays(9);
1776 load_timing.send_end = now + base::TimeDelta::FromDays(10);
1777 load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11);
1778 return load_timing;
1781 LoadTimingInfo RunURLRequestInterceptorLoadTimingTest(
1782 const LoadTimingInfo& job_load_timing,
1783 const URLRequestContext& context,
1784 MockURLRequestInterceptor* interceptor) {
1785 interceptor->set_intercept_main_request(true);
1786 interceptor->set_main_request_load_timing_info(job_load_timing);
1787 TestDelegate d;
1788 scoped_ptr<URLRequest> req(context.CreateRequest(
1789 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
1790 req->Start();
1791 base::RunLoop().Run();
1793 LoadTimingInfo resulting_load_timing;
1794 req->GetLoadTimingInfo(&resulting_load_timing);
1796 // None of these should be modified by the URLRequest.
1797 EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused);
1798 EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id);
1799 EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start);
1800 EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end);
1801 EXPECT_EQ(job_load_timing.receive_headers_end,
1802 resulting_load_timing.receive_headers_end);
1804 return resulting_load_timing;
1807 // Basic test that the intercept + load timing tests work.
1808 TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) {
1809 base::TimeTicks now = base::TimeTicks::Now();
1810 LoadTimingInfo job_load_timing =
1811 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false);
1813 LoadTimingInfo load_timing_result =
1814 RunURLRequestInterceptorLoadTimingTest(
1815 job_load_timing, default_context(), interceptor());
1817 // Nothing should have been changed by the URLRequest.
1818 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1819 load_timing_result.proxy_resolve_start);
1820 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1821 load_timing_result.proxy_resolve_end);
1822 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1823 load_timing_result.connect_timing.dns_start);
1824 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1825 load_timing_result.connect_timing.dns_end);
1826 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1827 load_timing_result.connect_timing.connect_start);
1828 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1829 load_timing_result.connect_timing.connect_end);
1830 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1831 load_timing_result.connect_timing.ssl_start);
1832 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1833 load_timing_result.connect_timing.ssl_end);
1835 // Redundant sanity check.
1836 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES);
1839 // Another basic test, with proxy and SSL times, but no DNS times.
1840 TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) {
1841 base::TimeTicks now = base::TimeTicks::Now();
1842 LoadTimingInfo job_load_timing =
1843 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true);
1845 LoadTimingInfo load_timing_result =
1846 RunURLRequestInterceptorLoadTimingTest(
1847 job_load_timing, default_context(), interceptor());
1849 // Nothing should have been changed by the URLRequest.
1850 EXPECT_EQ(job_load_timing.proxy_resolve_start,
1851 load_timing_result.proxy_resolve_start);
1852 EXPECT_EQ(job_load_timing.proxy_resolve_end,
1853 load_timing_result.proxy_resolve_end);
1854 EXPECT_EQ(job_load_timing.connect_timing.dns_start,
1855 load_timing_result.connect_timing.dns_start);
1856 EXPECT_EQ(job_load_timing.connect_timing.dns_end,
1857 load_timing_result.connect_timing.dns_end);
1858 EXPECT_EQ(job_load_timing.connect_timing.connect_start,
1859 load_timing_result.connect_timing.connect_start);
1860 EXPECT_EQ(job_load_timing.connect_timing.connect_end,
1861 load_timing_result.connect_timing.connect_end);
1862 EXPECT_EQ(job_load_timing.connect_timing.ssl_start,
1863 load_timing_result.connect_timing.ssl_start);
1864 EXPECT_EQ(job_load_timing.connect_timing.ssl_end,
1865 load_timing_result.connect_timing.ssl_end);
1867 // Redundant sanity check.
1868 TestLoadTimingNotReusedWithProxy(load_timing_result,
1869 CONNECT_TIMING_HAS_SSL_TIMES);
1872 // Make sure that URLRequest correctly adjusts proxy times when they're before
1873 // |request_start|, due to already having a connected socket. This happens in
1874 // the case of reusing a SPDY session. The connected socket is not considered
1875 // reused in this test (May be a preconnect).
1877 // To mix things up from the test above, assumes DNS times but no SSL times.
1878 TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) {
1879 base::TimeTicks now = base::TimeTicks::Now();
1880 LoadTimingInfo job_load_timing =
1881 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true);
1882 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6);
1883 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5);
1884 job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4);
1885 job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3);
1886 job_load_timing.connect_timing.connect_start =
1887 now - base::TimeDelta::FromDays(2);
1888 job_load_timing.connect_timing.connect_end =
1889 now - base::TimeDelta::FromDays(1);
1891 LoadTimingInfo load_timing_result =
1892 RunURLRequestInterceptorLoadTimingTest(
1893 job_load_timing, default_context(), interceptor());
1895 // Proxy times, connect times, and DNS times should all be replaced with
1896 // request_start.
1897 EXPECT_EQ(load_timing_result.request_start,
1898 load_timing_result.proxy_resolve_start);
1899 EXPECT_EQ(load_timing_result.request_start,
1900 load_timing_result.proxy_resolve_end);
1901 EXPECT_EQ(load_timing_result.request_start,
1902 load_timing_result.connect_timing.dns_start);
1903 EXPECT_EQ(load_timing_result.request_start,
1904 load_timing_result.connect_timing.dns_end);
1905 EXPECT_EQ(load_timing_result.request_start,
1906 load_timing_result.connect_timing.connect_start);
1907 EXPECT_EQ(load_timing_result.request_start,
1908 load_timing_result.connect_timing.connect_end);
1910 // Other times should have been left null.
1911 TestLoadTimingNotReusedWithProxy(load_timing_result,
1912 CONNECT_TIMING_HAS_DNS_TIMES);
1915 // Same as above, but in the reused case.
1916 TEST_F(URLRequestInterceptorTest,
1917 InterceptLoadTimingEarlyProxyResolutionReused) {
1918 base::TimeTicks now = base::TimeTicks::Now();
1919 LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true);
1920 job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4);
1921 job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3);
1923 LoadTimingInfo load_timing_result =
1924 RunURLRequestInterceptorLoadTimingTest(
1925 job_load_timing, default_context(), interceptor());
1927 // Proxy times and connect times should all be replaced with request_start.
1928 EXPECT_EQ(load_timing_result.request_start,
1929 load_timing_result.proxy_resolve_start);
1930 EXPECT_EQ(load_timing_result.request_start,
1931 load_timing_result.proxy_resolve_end);
1933 // Other times should have been left null.
1934 TestLoadTimingReusedWithProxy(load_timing_result);
1937 // Make sure that URLRequest correctly adjusts connect times when they're before
1938 // |request_start|, due to reusing a connected socket. The connected socket is
1939 // not considered reused in this test (May be a preconnect).
1941 // To mix things up, the request has SSL times, but no DNS times.
1942 TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) {
1943 base::TimeTicks now = base::TimeTicks::Now();
1944 LoadTimingInfo job_load_timing =
1945 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false);
1946 job_load_timing.connect_timing.connect_start =
1947 now - base::TimeDelta::FromDays(1);
1948 job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2);
1949 job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3);
1950 job_load_timing.connect_timing.connect_end =
1951 now - base::TimeDelta::FromDays(4);
1953 LoadTimingInfo load_timing_result =
1954 RunURLRequestInterceptorLoadTimingTest(
1955 job_load_timing, default_context(), interceptor());
1957 // Connect times, and SSL times should be replaced with request_start.
1958 EXPECT_EQ(load_timing_result.request_start,
1959 load_timing_result.connect_timing.connect_start);
1960 EXPECT_EQ(load_timing_result.request_start,
1961 load_timing_result.connect_timing.ssl_start);
1962 EXPECT_EQ(load_timing_result.request_start,
1963 load_timing_result.connect_timing.ssl_end);
1964 EXPECT_EQ(load_timing_result.request_start,
1965 load_timing_result.connect_timing.connect_end);
1967 // Other times should have been left null.
1968 TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES);
1971 // Make sure that URLRequest correctly adjusts connect times when they're before
1972 // |request_start|, due to reusing a connected socket in the case that there
1973 // are also proxy times. The connected socket is not considered reused in this
1974 // test (May be a preconnect).
1976 // In this test, there are no SSL or DNS times.
1977 TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) {
1978 base::TimeTicks now = base::TimeTicks::Now();
1979 LoadTimingInfo job_load_timing =
1980 NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true);
1981 job_load_timing.connect_timing.connect_start =
1982 now - base::TimeDelta::FromDays(1);
1983 job_load_timing.connect_timing.connect_end =
1984 now - base::TimeDelta::FromDays(2);
1986 LoadTimingInfo load_timing_result =
1987 RunURLRequestInterceptorLoadTimingTest(
1988 job_load_timing, default_context(), interceptor());
1990 // Connect times should be replaced with proxy_resolve_end.
1991 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1992 load_timing_result.connect_timing.connect_start);
1993 EXPECT_EQ(load_timing_result.proxy_resolve_end,
1994 load_timing_result.connect_timing.connect_end);
1996 // Other times should have been left null.
1997 TestLoadTimingNotReusedWithProxy(load_timing_result,
1998 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
2001 // Check that two different URL requests have different identifiers.
2002 TEST_F(URLRequestTest, Identifiers) {
2003 TestDelegate d;
2004 TestURLRequestContext context;
2005 scoped_ptr<URLRequest> req(
2006 context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d));
2007 scoped_ptr<URLRequest> other_req(
2008 context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d));
2010 ASSERT_NE(req->identifier(), other_req->identifier());
2013 // Check that a failure to connect to the proxy is reported to the network
2014 // delegate.
2015 TEST_F(URLRequestTest, NetworkDelegateProxyError) {
2016 MockHostResolver host_resolver;
2017 host_resolver.rules()->AddSimulatedFailure("*");
2019 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
2020 TestURLRequestContextWithProxy context("myproxy:70", &network_delegate);
2022 TestDelegate d;
2023 scoped_ptr<URLRequest> req(
2024 context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d));
2025 req->set_method("GET");
2027 req->Start();
2028 base::RunLoop().Run();
2030 // Check we see a failed request.
2031 EXPECT_FALSE(req->status().is_success());
2032 // The proxy server is not set before failure.
2033 EXPECT_TRUE(req->proxy_server().IsEmpty());
2034 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
2035 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error());
2037 EXPECT_EQ(1, network_delegate.error_count());
2038 EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error());
2039 EXPECT_EQ(1, network_delegate.completed_requests());
2042 // Make sure that NetworkDelegate::NotifyCompleted is called if
2043 // content is empty.
2044 TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) {
2045 TestDelegate d;
2046 scoped_ptr<URLRequest> req(
2047 default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d));
2048 req->Start();
2049 base::RunLoop().Run();
2050 EXPECT_EQ("", d.data_received());
2051 EXPECT_EQ(1, default_network_delegate_.completed_requests());
2054 // Make sure that SetPriority actually sets the URLRequest's priority
2055 // correctly, both before and after start.
2056 TEST_F(URLRequestTest, SetPriorityBasic) {
2057 TestDelegate d;
2058 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2059 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
2060 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
2062 req->SetPriority(LOW);
2063 EXPECT_EQ(LOW, req->priority());
2065 req->Start();
2066 EXPECT_EQ(LOW, req->priority());
2068 req->SetPriority(MEDIUM);
2069 EXPECT_EQ(MEDIUM, req->priority());
2072 // Make sure that URLRequest calls SetPriority on a job before calling
2073 // Start on it.
2074 TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) {
2075 TestDelegate d;
2076 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2077 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
2078 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
2080 scoped_refptr<URLRequestTestJob> job =
2081 new URLRequestTestJob(req.get(), &default_network_delegate_);
2082 AddTestInterceptor()->set_main_intercept_job(job.get());
2083 EXPECT_EQ(DEFAULT_PRIORITY, job->priority());
2085 req->SetPriority(LOW);
2087 req->Start();
2088 EXPECT_EQ(LOW, job->priority());
2091 // Make sure that URLRequest passes on its priority updates to its
2092 // job.
2093 TEST_F(URLRequestTest, SetJobPriority) {
2094 TestDelegate d;
2095 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2096 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d));
2098 scoped_refptr<URLRequestTestJob> job =
2099 new URLRequestTestJob(req.get(), &default_network_delegate_);
2100 AddTestInterceptor()->set_main_intercept_job(job.get());
2102 req->SetPriority(LOW);
2103 req->Start();
2104 EXPECT_EQ(LOW, job->priority());
2106 req->SetPriority(MEDIUM);
2107 EXPECT_EQ(MEDIUM, req->priority());
2108 EXPECT_EQ(MEDIUM, job->priority());
2111 // Setting the IGNORE_LIMITS load flag should be okay if the priority
2112 // is MAXIMUM_PRIORITY.
2113 TEST_F(URLRequestTest, PriorityIgnoreLimits) {
2114 TestDelegate d;
2115 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2116 GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d));
2117 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
2119 scoped_refptr<URLRequestTestJob> job =
2120 new URLRequestTestJob(req.get(), &default_network_delegate_);
2121 AddTestInterceptor()->set_main_intercept_job(job.get());
2123 req->SetLoadFlags(LOAD_IGNORE_LIMITS);
2124 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
2126 req->SetPriority(MAXIMUM_PRIORITY);
2127 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
2129 req->Start();
2130 EXPECT_EQ(MAXIMUM_PRIORITY, req->priority());
2131 EXPECT_EQ(MAXIMUM_PRIORITY, job->priority());
2134 // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666).
2135 #if !defined(OS_IOS)
2136 // A subclass of SpawnedTestServer that uses a statically-configured hostname.
2137 // This is to work around mysterious failures in chrome_frame_net_tests. See:
2138 // http://crbug.com/114369
2139 // TODO(erikwright): remove or update as needed; see http://crbug.com/334634.
2140 class LocalHttpTestServer : public SpawnedTestServer {
2141 public:
2142 explicit LocalHttpTestServer(const base::FilePath& document_root)
2143 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
2144 ScopedCustomUrlRequestTestHttpHost::value(),
2145 document_root) {}
2146 LocalHttpTestServer()
2147 : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP,
2148 ScopedCustomUrlRequestTestHttpHost::value(),
2149 base::FilePath()) {}
2152 TEST_F(URLRequestTest, DelayedCookieCallback) {
2153 LocalHttpTestServer test_server;
2154 ASSERT_TRUE(test_server.Start());
2156 TestURLRequestContext context;
2157 scoped_refptr<DelayedCookieMonster> delayed_cm =
2158 new DelayedCookieMonster();
2159 scoped_refptr<CookieStore> cookie_store = delayed_cm;
2160 context.set_cookie_store(delayed_cm.get());
2162 // Set up a cookie.
2164 TestNetworkDelegate network_delegate;
2165 context.set_network_delegate(&network_delegate);
2166 TestDelegate d;
2167 scoped_ptr<URLRequest> req(context.CreateRequest(
2168 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2169 &d));
2170 req->Start();
2171 base::RunLoop().Run();
2172 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2173 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2174 EXPECT_EQ(1, network_delegate.set_cookie_count());
2177 // Verify that the cookie is set.
2179 TestNetworkDelegate network_delegate;
2180 context.set_network_delegate(&network_delegate);
2181 TestDelegate d;
2182 scoped_ptr<URLRequest> req(context.CreateRequest(
2183 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2184 req->Start();
2185 base::RunLoop().Run();
2187 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2188 != std::string::npos);
2189 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2190 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2194 TEST_F(URLRequestTest, DoNotSendCookies) {
2195 LocalHttpTestServer test_server;
2196 ASSERT_TRUE(test_server.Start());
2198 // Set up a cookie.
2200 TestNetworkDelegate network_delegate;
2201 default_context_.set_network_delegate(&network_delegate);
2202 TestDelegate d;
2203 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2204 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2205 &d));
2206 req->Start();
2207 base::RunLoop().Run();
2208 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2209 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2212 // Verify that the cookie is set.
2214 TestNetworkDelegate network_delegate;
2215 default_context_.set_network_delegate(&network_delegate);
2216 TestDelegate d;
2217 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2218 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2219 req->Start();
2220 base::RunLoop().Run();
2222 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2223 != std::string::npos);
2224 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2225 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2228 // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set.
2230 TestNetworkDelegate network_delegate;
2231 default_context_.set_network_delegate(&network_delegate);
2232 TestDelegate d;
2233 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2234 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2235 req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES);
2236 req->Start();
2237 base::RunLoop().Run();
2239 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2240 == std::string::npos);
2242 // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies.
2243 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2244 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2248 TEST_F(URLRequestTest, DoNotSaveCookies) {
2249 LocalHttpTestServer test_server;
2250 ASSERT_TRUE(test_server.Start());
2252 // Set up a cookie.
2254 TestNetworkDelegate network_delegate;
2255 default_context_.set_network_delegate(&network_delegate);
2256 TestDelegate d;
2257 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2258 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2259 &d));
2260 req->Start();
2261 base::RunLoop().Run();
2263 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2264 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2265 EXPECT_EQ(1, network_delegate.set_cookie_count());
2268 // Try to set-up another cookie and update the previous cookie.
2270 TestNetworkDelegate network_delegate;
2271 default_context_.set_network_delegate(&network_delegate);
2272 TestDelegate d;
2273 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2274 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2275 DEFAULT_PRIORITY, &d));
2276 req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES);
2277 req->Start();
2279 base::RunLoop().Run();
2281 // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie.
2282 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2283 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2284 EXPECT_EQ(0, network_delegate.set_cookie_count());
2287 // Verify the cookies weren't saved or updated.
2289 TestNetworkDelegate network_delegate;
2290 default_context_.set_network_delegate(&network_delegate);
2291 TestDelegate d;
2292 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2293 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2294 req->Start();
2295 base::RunLoop().Run();
2297 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2298 == std::string::npos);
2299 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2300 != std::string::npos);
2302 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2303 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2304 EXPECT_EQ(0, network_delegate.set_cookie_count());
2308 TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) {
2309 LocalHttpTestServer test_server;
2310 ASSERT_TRUE(test_server.Start());
2312 // Set up a cookie.
2314 TestNetworkDelegate network_delegate;
2315 default_context_.set_network_delegate(&network_delegate);
2316 TestDelegate d;
2317 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2318 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2319 &d));
2320 req->Start();
2321 base::RunLoop().Run();
2323 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2324 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2327 // Verify that the cookie is set.
2329 TestNetworkDelegate network_delegate;
2330 default_context_.set_network_delegate(&network_delegate);
2331 TestDelegate d;
2332 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2333 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2334 req->Start();
2335 base::RunLoop().Run();
2337 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2338 != std::string::npos);
2340 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2341 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2344 // Verify that the cookie isn't sent.
2346 TestNetworkDelegate network_delegate;
2347 default_context_.set_network_delegate(&network_delegate);
2348 TestDelegate d;
2349 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2350 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2351 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2352 req->Start();
2353 base::RunLoop().Run();
2355 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2356 == std::string::npos);
2358 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2359 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2363 TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) {
2364 LocalHttpTestServer test_server;
2365 ASSERT_TRUE(test_server.Start());
2367 // Set up a cookie.
2369 TestNetworkDelegate network_delegate;
2370 default_context_.set_network_delegate(&network_delegate);
2371 TestDelegate d;
2372 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2373 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2374 &d));
2375 req->Start();
2376 base::RunLoop().Run();
2378 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2379 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2382 // Try to set-up another cookie and update the previous cookie.
2384 TestNetworkDelegate network_delegate;
2385 default_context_.set_network_delegate(&network_delegate);
2386 TestDelegate d;
2387 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2388 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2389 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2390 DEFAULT_PRIORITY, &d));
2391 req->Start();
2393 base::RunLoop().Run();
2395 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2396 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2399 // Verify the cookies weren't saved or updated.
2401 TestNetworkDelegate network_delegate;
2402 default_context_.set_network_delegate(&network_delegate);
2403 TestDelegate d;
2404 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2405 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2406 req->Start();
2407 base::RunLoop().Run();
2409 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2410 == std::string::npos);
2411 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2412 != std::string::npos);
2414 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2415 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2419 TEST_F(URLRequestTest, DoNotSaveEmptyCookies) {
2420 LocalHttpTestServer test_server;
2421 ASSERT_TRUE(test_server.Start());
2423 // Set up an empty cookie.
2425 TestNetworkDelegate network_delegate;
2426 default_context_.set_network_delegate(&network_delegate);
2427 TestDelegate d;
2428 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2429 test_server.GetURL("set-cookie"), DEFAULT_PRIORITY, &d));
2430 req->Start();
2431 base::RunLoop().Run();
2433 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2434 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2435 EXPECT_EQ(0, network_delegate.set_cookie_count());
2439 TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) {
2440 LocalHttpTestServer test_server;
2441 ASSERT_TRUE(test_server.Start());
2443 // Set up a cookie.
2445 TestNetworkDelegate network_delegate;
2446 default_context_.set_network_delegate(&network_delegate);
2447 TestDelegate d;
2448 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2449 test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY,
2450 &d));
2451 req->Start();
2452 base::RunLoop().Run();
2454 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2455 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2458 // Verify that the cookie is set.
2460 TestNetworkDelegate network_delegate;
2461 default_context_.set_network_delegate(&network_delegate);
2462 TestDelegate d;
2463 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2464 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2465 req->Start();
2466 base::RunLoop().Run();
2468 EXPECT_TRUE(d.data_received().find("CookieToNotSend=1")
2469 != std::string::npos);
2471 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2472 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2475 // Verify that the cookie isn't sent.
2477 TestNetworkDelegate network_delegate;
2478 default_context_.set_network_delegate(&network_delegate);
2479 TestDelegate d;
2480 network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES);
2481 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2482 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2483 req->Start();
2484 base::RunLoop().Run();
2486 EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1")
2487 == std::string::npos);
2489 EXPECT_EQ(1, network_delegate.blocked_get_cookies_count());
2490 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2494 TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) {
2495 LocalHttpTestServer test_server;
2496 ASSERT_TRUE(test_server.Start());
2498 // Set up a cookie.
2500 TestNetworkDelegate network_delegate;
2501 default_context_.set_network_delegate(&network_delegate);
2502 TestDelegate d;
2503 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2504 test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY,
2505 &d));
2506 req->Start();
2507 base::RunLoop().Run();
2509 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2510 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2513 // Try to set-up another cookie and update the previous cookie.
2515 TestNetworkDelegate network_delegate;
2516 default_context_.set_network_delegate(&network_delegate);
2517 TestDelegate d;
2518 network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE);
2519 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2520 test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"),
2521 DEFAULT_PRIORITY, &d));
2522 req->Start();
2524 base::RunLoop().Run();
2526 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2527 EXPECT_EQ(2, network_delegate.blocked_set_cookie_count());
2530 // Verify the cookies weren't saved or updated.
2532 TestNetworkDelegate network_delegate;
2533 default_context_.set_network_delegate(&network_delegate);
2534 TestDelegate d;
2535 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2536 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2537 req->Start();
2538 base::RunLoop().Run();
2540 EXPECT_TRUE(d.data_received().find("CookieToNotSave=1")
2541 == std::string::npos);
2542 EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2")
2543 != std::string::npos);
2545 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2546 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2550 TEST_F(URLRequestTest, FirstPartyOnlyCookiesEnabled) {
2551 LocalHttpTestServer test_server;
2552 ASSERT_TRUE(test_server.Start());
2554 // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where
2555 // LocalHttpTestServer points).
2557 TestNetworkDelegate network_delegate;
2558 network_delegate.set_first_party_only_cookies_enabled(true);
2559 default_context_.set_network_delegate(&network_delegate);
2561 TestDelegate d;
2562 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2563 test_server.GetURL(
2564 "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"),
2565 DEFAULT_PRIORITY, &d));
2566 req->Start();
2567 base::RunLoop().Run();
2568 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2569 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2570 EXPECT_EQ(1, network_delegate.set_cookie_count());
2573 // Verify that the cookie is sent for first-party requests.
2575 TestNetworkDelegate network_delegate;
2576 network_delegate.set_first_party_only_cookies_enabled(true);
2577 default_context_.set_network_delegate(&network_delegate);
2578 TestDelegate d;
2579 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2580 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2581 req->set_first_party_for_cookies(test_server.GetURL(""));
2582 req->Start();
2583 base::RunLoop().Run();
2585 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") !=
2586 std::string::npos);
2587 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2588 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2591 // Verify that the cookie is not-sent for non-first-party requests.
2593 TestNetworkDelegate network_delegate;
2594 network_delegate.set_first_party_only_cookies_enabled(true);
2595 default_context_.set_network_delegate(&network_delegate);
2596 TestDelegate d;
2597 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2598 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2599 req->set_first_party_for_cookies(GURL("http://third-party.test/"));
2600 req->Start();
2601 base::RunLoop().Run();
2603 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") ==
2604 std::string::npos);
2605 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2606 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2610 TEST_F(URLRequestTest, FirstPartyOnlyCookiesDisabled) {
2611 LocalHttpTestServer test_server;
2612 ASSERT_TRUE(test_server.Start());
2614 // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where
2615 // LocalHttpTestServer points).
2617 TestNetworkDelegate network_delegate;
2618 network_delegate.set_first_party_only_cookies_enabled(false);
2619 default_context_.set_network_delegate(&network_delegate);
2621 TestDelegate d;
2622 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2623 test_server.GetURL(
2624 "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"),
2625 DEFAULT_PRIORITY, &d));
2626 req->Start();
2627 base::RunLoop().Run();
2628 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2629 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2630 EXPECT_EQ(1, network_delegate.set_cookie_count());
2633 // Verify that the cookie is sent for first-party requests.
2635 TestNetworkDelegate network_delegate;
2636 network_delegate.set_first_party_only_cookies_enabled(false);
2637 default_context_.set_network_delegate(&network_delegate);
2638 TestDelegate d;
2639 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2640 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2641 req->set_first_party_for_cookies(test_server.GetURL(""));
2642 req->Start();
2643 base::RunLoop().Run();
2645 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") !=
2646 std::string::npos);
2647 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2648 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2651 // Verify that the cookie is also sent for non-first-party requests.
2653 TestNetworkDelegate network_delegate;
2654 network_delegate.set_first_party_only_cookies_enabled(false);
2655 default_context_.set_network_delegate(&network_delegate);
2656 TestDelegate d;
2657 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2658 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2659 req->set_first_party_for_cookies(GURL("http://third-party.test/"));
2660 req->Start();
2661 base::RunLoop().Run();
2663 EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") !=
2664 std::string::npos);
2665 EXPECT_EQ(0, network_delegate.blocked_get_cookies_count());
2666 EXPECT_EQ(0, network_delegate.blocked_set_cookie_count());
2670 // FixedDateNetworkDelegate swaps out the server's HTTP Date response header
2671 // value for the |fixed_date| argument given to the constructor.
2672 class FixedDateNetworkDelegate : public TestNetworkDelegate {
2673 public:
2674 explicit FixedDateNetworkDelegate(const std::string& fixed_date)
2675 : fixed_date_(fixed_date) {}
2676 ~FixedDateNetworkDelegate() override {}
2678 // NetworkDelegate implementation
2679 int OnHeadersReceived(
2680 URLRequest* request,
2681 const CompletionCallback& callback,
2682 const HttpResponseHeaders* original_response_headers,
2683 scoped_refptr<HttpResponseHeaders>* override_response_headers,
2684 GURL* allowed_unsafe_redirect_url) override;
2686 private:
2687 std::string fixed_date_;
2689 DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate);
2692 int FixedDateNetworkDelegate::OnHeadersReceived(
2693 URLRequest* request,
2694 const CompletionCallback& callback,
2695 const HttpResponseHeaders* original_response_headers,
2696 scoped_refptr<HttpResponseHeaders>* override_response_headers,
2697 GURL* allowed_unsafe_redirect_url) {
2698 HttpResponseHeaders* new_response_headers =
2699 new HttpResponseHeaders(original_response_headers->raw_headers());
2701 new_response_headers->RemoveHeader("Date");
2702 new_response_headers->AddHeader("Date: " + fixed_date_);
2704 *override_response_headers = new_response_headers;
2705 return TestNetworkDelegate::OnHeadersReceived(request,
2706 callback,
2707 original_response_headers,
2708 override_response_headers,
2709 allowed_unsafe_redirect_url);
2712 // Test that cookie expiration times are adjusted for server/client clock
2713 // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date
2714 // headers by defaulting to GMT. (crbug.com/135131)
2715 TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) {
2716 LocalHttpTestServer test_server;
2717 ASSERT_TRUE(test_server.Start());
2719 // Set up an expired cookie.
2721 TestNetworkDelegate network_delegate;
2722 default_context_.set_network_delegate(&network_delegate);
2723 TestDelegate d;
2724 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2725 test_server.GetURL(
2726 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2727 DEFAULT_PRIORITY, &d));
2728 req->Start();
2729 base::RunLoop().Run();
2731 // Verify that the cookie is not set.
2733 TestNetworkDelegate network_delegate;
2734 default_context_.set_network_delegate(&network_delegate);
2735 TestDelegate d;
2736 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2737 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2738 req->Start();
2739 base::RunLoop().Run();
2741 EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos);
2743 // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier.
2745 FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC");
2746 default_context_.set_network_delegate(&network_delegate);
2747 TestDelegate d;
2748 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2749 test_server.GetURL(
2750 "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"),
2751 DEFAULT_PRIORITY, &d));
2752 req->Start();
2753 base::RunLoop().Run();
2755 // Verify that the cookie is set.
2757 TestNetworkDelegate network_delegate;
2758 default_context_.set_network_delegate(&network_delegate);
2759 TestDelegate d;
2760 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2761 test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d));
2762 req->Start();
2763 base::RunLoop().Run();
2765 EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos);
2770 // Check that it is impossible to change the referrer in the extra headers of
2771 // an URLRequest.
2772 TEST_F(URLRequestTest, DoNotOverrideReferrer) {
2773 LocalHttpTestServer test_server;
2774 ASSERT_TRUE(test_server.Start());
2776 // If extra headers contain referer and the request contains a referer,
2777 // only the latter shall be respected.
2779 TestDelegate d;
2780 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2781 test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
2782 req->SetReferrer("http://foo.com/");
2784 HttpRequestHeaders headers;
2785 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2786 req->SetExtraRequestHeaders(headers);
2788 req->Start();
2789 base::RunLoop().Run();
2791 EXPECT_EQ("http://foo.com/", d.data_received());
2794 // If extra headers contain a referer but the request does not, no referer
2795 // shall be sent in the header.
2797 TestDelegate d;
2798 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2799 test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
2801 HttpRequestHeaders headers;
2802 headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/");
2803 req->SetExtraRequestHeaders(headers);
2804 req->SetLoadFlags(LOAD_VALIDATE_CACHE);
2806 req->Start();
2807 base::RunLoop().Run();
2809 EXPECT_EQ("None", d.data_received());
2813 class URLRequestTestHTTP : public URLRequestTest {
2814 public:
2815 URLRequestTestHTTP()
2816 : test_server_(base::FilePath(FILE_PATH_LITERAL(
2817 "net/data/url_request_unittest"))) {
2820 protected:
2821 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2822 // |request_method| is the method to use for the initial request.
2823 // |redirect_method| is the method that is expected to be used for the second
2824 // request, after redirection.
2825 // If |include_data| is true, data is uploaded with the request. The
2826 // response body is expected to match it exactly, if and only if
2827 // |request_method| == |redirect_method|.
2828 void HTTPRedirectMethodTest(const GURL& redirect_url,
2829 const std::string& request_method,
2830 const std::string& redirect_method,
2831 bool include_data) {
2832 static const char kData[] = "hello world";
2833 TestDelegate d;
2834 scoped_ptr<URLRequest> req(
2835 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
2836 req->set_method(request_method);
2837 if (include_data) {
2838 req->set_upload(CreateSimpleUploadData(kData));
2839 HttpRequestHeaders headers;
2840 headers.SetHeader(HttpRequestHeaders::kContentLength,
2841 base::UintToString(arraysize(kData) - 1));
2842 req->SetExtraRequestHeaders(headers);
2844 req->Start();
2845 base::RunLoop().Run();
2846 EXPECT_EQ(redirect_method, req->method());
2847 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
2848 EXPECT_EQ(OK, req->status().error());
2849 if (include_data) {
2850 if (request_method == redirect_method) {
2851 EXPECT_EQ(kData, d.data_received());
2852 } else {
2853 EXPECT_NE(kData, d.data_received());
2856 if (HasFailure())
2857 LOG(WARNING) << "Request method was: " << request_method;
2860 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2861 // |request_method| is the method to use for the initial request.
2862 // |redirect_method| is the method that is expected to be used for the second
2863 // request, after redirection.
2864 // |origin_value| is the expected value for the Origin header after
2865 // redirection. If empty, expects that there will be no Origin header.
2866 void HTTPRedirectOriginHeaderTest(const GURL& redirect_url,
2867 const std::string& request_method,
2868 const std::string& redirect_method,
2869 const std::string& origin_value) {
2870 TestDelegate d;
2871 scoped_ptr<URLRequest> req(
2872 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
2873 req->set_method(request_method);
2874 req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin,
2875 redirect_url.GetOrigin().spec(), false);
2876 req->Start();
2878 base::RunLoop().Run();
2880 EXPECT_EQ(redirect_method, req->method());
2881 // Note that there is no check for request success here because, for
2882 // purposes of testing, the request very well may fail. For example, if the
2883 // test redirects to an HTTPS server from an HTTP origin, thus it is cross
2884 // origin, there is not an HTTPS server in this unit test framework, so the
2885 // request would fail. However, that's fine, as long as the request headers
2886 // are in order and pass the checks below.
2887 if (origin_value.empty()) {
2888 EXPECT_FALSE(
2889 req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin));
2890 } else {
2891 std::string origin_header;
2892 EXPECT_TRUE(req->extra_request_headers().GetHeader(
2893 HttpRequestHeaders::kOrigin, &origin_header));
2894 EXPECT_EQ(origin_value, origin_header);
2898 void HTTPUploadDataOperationTest(const std::string& method) {
2899 const int kMsgSize = 20000; // multiple of 10
2900 const int kIterations = 50;
2901 char* uploadBytes = new char[kMsgSize+1];
2902 char* ptr = uploadBytes;
2903 char marker = 'a';
2904 for (int idx = 0; idx < kMsgSize/10; idx++) {
2905 memcpy(ptr, "----------", 10);
2906 ptr += 10;
2907 if (idx % 100 == 0) {
2908 ptr--;
2909 *ptr++ = marker;
2910 if (++marker > 'z')
2911 marker = 'a';
2914 uploadBytes[kMsgSize] = '\0';
2916 for (int i = 0; i < kIterations; ++i) {
2917 TestDelegate d;
2918 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
2919 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
2920 r->set_method(method.c_str());
2922 r->set_upload(CreateSimpleUploadData(uploadBytes));
2924 r->Start();
2925 EXPECT_TRUE(r->is_pending());
2927 base::RunLoop().Run();
2929 ASSERT_EQ(1, d.response_started_count())
2930 << "request failed: " << r->status().status()
2931 << ", os error: " << r->status().error();
2933 EXPECT_FALSE(d.received_data_before_response());
2934 EXPECT_EQ(uploadBytes, d.data_received());
2936 delete[] uploadBytes;
2939 void AddChunksToUpload(URLRequest* r) {
2940 r->AppendChunkToUpload("a", 1, false);
2941 r->AppendChunkToUpload("bcd", 3, false);
2942 r->AppendChunkToUpload("this is a longer chunk than before.", 35, false);
2943 r->AppendChunkToUpload("\r\n\r\n", 4, false);
2944 r->AppendChunkToUpload("0", 1, false);
2945 r->AppendChunkToUpload("2323", 4, true);
2948 void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) {
2949 // This should match the chunks sent by AddChunksToUpload().
2950 const std::string expected_data =
2951 "abcdthis is a longer chunk than before.\r\n\r\n02323";
2953 ASSERT_EQ(1, d->response_started_count())
2954 << "request failed: " << r->status().status()
2955 << ", os error: " << r->status().error();
2957 EXPECT_FALSE(d->received_data_before_response());
2959 EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received()));
2960 EXPECT_EQ(expected_data, d->data_received());
2963 bool DoManyCookiesRequest(int num_cookies) {
2964 TestDelegate d;
2965 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
2966 test_server_.GetURL("set-many-cookies?" +
2967 base::IntToString(num_cookies)),
2968 DEFAULT_PRIORITY, &d));
2970 r->Start();
2971 EXPECT_TRUE(r->is_pending());
2973 base::RunLoop().Run();
2975 bool is_success = r->status().is_success();
2977 if (!is_success) {
2978 EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG);
2979 // The test server appears to be unable to handle subsequent requests
2980 // after this error is triggered. Force it to restart.
2981 EXPECT_TRUE(test_server_.Stop());
2982 EXPECT_TRUE(test_server_.Start());
2985 return is_success;
2988 LocalHttpTestServer* test_server() {
2989 return &test_server_;
2992 protected:
2993 LocalHttpTestServer test_server_;
2996 // In this unit test, we're using the HTTPTestServer as a proxy server and
2997 // issuing a CONNECT request with the magic host name "www.redirect.com".
2998 // The HTTPTestServer will return a 302 response, which we should not
2999 // follow.
3000 TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) {
3001 ASSERT_TRUE(test_server_.Start());
3003 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
3004 TestURLRequestContextWithProxy context(
3005 test_server_.host_port_pair().ToString(), &network_delegate);
3007 TestDelegate d;
3009 scoped_ptr<URLRequest> r(context.CreateRequest(
3010 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d));
3011 r->Start();
3012 EXPECT_TRUE(r->is_pending());
3014 base::RunLoop().Run();
3016 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
3017 // The proxy server is not set before failure.
3018 EXPECT_TRUE(r->proxy_server().IsEmpty());
3019 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
3020 EXPECT_EQ(1, d.response_started_count());
3021 // We should not have followed the redirect.
3022 EXPECT_EQ(0, d.received_redirect_count());
3026 // This is the same as the previous test, but checks that the network delegate
3027 // registers the error.
3028 TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) {
3029 ASSERT_TRUE(test_server_.Start());
3031 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
3032 TestURLRequestContextWithProxy context(
3033 test_server_.host_port_pair().ToString(), &network_delegate);
3035 TestDelegate d;
3037 scoped_ptr<URLRequest> r(context.CreateRequest(
3038 GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d));
3039 r->Start();
3040 EXPECT_TRUE(r->is_pending());
3042 base::RunLoop().Run();
3044 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
3045 // The proxy server is not set before failure.
3046 EXPECT_TRUE(r->proxy_server().IsEmpty());
3047 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
3048 EXPECT_EQ(1, d.response_started_count());
3049 // We should not have followed the redirect.
3050 EXPECT_EQ(0, d.received_redirect_count());
3052 EXPECT_EQ(1, network_delegate.error_count());
3053 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error());
3057 // Tests that we can block and asynchronously return OK in various stages.
3058 TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) {
3059 static const BlockingNetworkDelegate::Stage blocking_stages[] = {
3060 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3061 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3062 BlockingNetworkDelegate::ON_HEADERS_RECEIVED
3064 static const size_t blocking_stages_length = arraysize(blocking_stages);
3066 ASSERT_TRUE(test_server_.Start());
3068 TestDelegate d;
3069 BlockingNetworkDelegate network_delegate(
3070 BlockingNetworkDelegate::USER_CALLBACK);
3071 network_delegate.set_block_on(
3072 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST |
3073 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS |
3074 BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3076 TestURLRequestContext context(true);
3077 context.set_network_delegate(&network_delegate);
3078 context.Init();
3081 scoped_ptr<URLRequest> r(context.CreateRequest(
3082 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d));
3084 r->Start();
3085 for (size_t i = 0; i < blocking_stages_length; ++i) {
3086 base::RunLoop().Run();
3087 EXPECT_EQ(blocking_stages[i],
3088 network_delegate.stage_blocked_for_callback());
3089 network_delegate.DoCallback(OK);
3091 base::RunLoop().Run();
3092 EXPECT_EQ(200, r->GetResponseCode());
3093 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3094 EXPECT_EQ(1, network_delegate.created_requests());
3095 EXPECT_EQ(0, network_delegate.destroyed_requests());
3097 EXPECT_EQ(1, network_delegate.destroyed_requests());
3100 // Tests that the network delegate can block and cancel a request.
3101 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) {
3102 ASSERT_TRUE(test_server_.Start());
3104 TestDelegate d;
3105 BlockingNetworkDelegate network_delegate(
3106 BlockingNetworkDelegate::AUTO_CALLBACK);
3107 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3108 network_delegate.set_retval(ERR_EMPTY_RESPONSE);
3110 TestURLRequestContextWithProxy context(
3111 test_server_.host_port_pair().ToString(), &network_delegate);
3114 scoped_ptr<URLRequest> r(context.CreateRequest(
3115 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
3117 r->Start();
3118 base::RunLoop().Run();
3120 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
3121 // The proxy server is not set before cancellation.
3122 EXPECT_TRUE(r->proxy_server().IsEmpty());
3123 EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error());
3124 EXPECT_EQ(1, network_delegate.created_requests());
3125 EXPECT_EQ(0, network_delegate.destroyed_requests());
3127 EXPECT_EQ(1, network_delegate.destroyed_requests());
3130 // Helper function for NetworkDelegateCancelRequestAsynchronously and
3131 // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network
3132 // delegate operating in |block_mode| and a request for |url|. It blocks the
3133 // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT.
3134 void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode,
3135 BlockingNetworkDelegate::Stage stage,
3136 const GURL& url) {
3137 TestDelegate d;
3138 BlockingNetworkDelegate network_delegate(block_mode);
3139 network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT);
3140 network_delegate.set_block_on(stage);
3142 TestURLRequestContext context(true);
3143 context.set_network_delegate(&network_delegate);
3144 context.Init();
3147 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3149 r->Start();
3150 base::RunLoop().Run();
3152 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
3153 // The proxy server is not set before cancellation.
3154 EXPECT_TRUE(r->proxy_server().IsEmpty());
3155 EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error());
3156 EXPECT_EQ(1, network_delegate.created_requests());
3157 EXPECT_EQ(0, network_delegate.destroyed_requests());
3159 EXPECT_EQ(1, network_delegate.destroyed_requests());
3162 // The following 3 tests check that the network delegate can cancel a request
3163 // synchronously in various stages of the request.
3164 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) {
3165 ASSERT_TRUE(test_server_.Start());
3166 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
3167 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3168 test_server_.GetURL(std::string()));
3171 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) {
3172 ASSERT_TRUE(test_server_.Start());
3173 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
3174 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3175 test_server_.GetURL(std::string()));
3178 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) {
3179 ASSERT_TRUE(test_server_.Start());
3180 NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS,
3181 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3182 test_server_.GetURL(std::string()));
3185 // The following 3 tests check that the network delegate can cancel a request
3186 // asynchronously in various stages of the request.
3187 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) {
3188 ASSERT_TRUE(test_server_.Start());
3189 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
3190 BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3191 test_server_.GetURL(std::string()));
3194 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) {
3195 ASSERT_TRUE(test_server_.Start());
3196 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
3197 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3198 test_server_.GetURL(std::string()));
3201 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) {
3202 ASSERT_TRUE(test_server_.Start());
3203 NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK,
3204 BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3205 test_server_.GetURL(std::string()));
3208 // Tests that the network delegate can block and redirect a request to a new
3209 // URL.
3210 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) {
3211 ASSERT_TRUE(test_server_.Start());
3213 TestDelegate d;
3214 BlockingNetworkDelegate network_delegate(
3215 BlockingNetworkDelegate::AUTO_CALLBACK);
3216 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3217 GURL redirect_url(test_server_.GetURL("simple.html"));
3218 network_delegate.set_redirect_url(redirect_url);
3220 TestURLRequestContextWithProxy context(
3221 test_server_.host_port_pair().ToString(), &network_delegate);
3224 GURL original_url(test_server_.GetURL("empty.html"));
3225 scoped_ptr<URLRequest> r(
3226 context.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
3228 // Quit after hitting the redirect, so can check the headers.
3229 d.set_quit_on_redirect(true);
3230 r->Start();
3231 base::RunLoop().Run();
3233 // Check headers from URLRequestJob.
3234 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3235 EXPECT_EQ(307, r->GetResponseCode());
3236 EXPECT_EQ(307, r->response_headers()->response_code());
3237 std::string location;
3238 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
3239 &location));
3240 EXPECT_EQ(redirect_url, GURL(location));
3242 // Let the request finish.
3243 r->FollowDeferredRedirect();
3244 base::RunLoop().Run();
3245 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3246 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
3247 EXPECT_EQ(
3248 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
3249 EXPECT_TRUE(
3250 network_delegate.last_observed_proxy().Equals(
3251 test_server_.host_port_pair()));
3253 EXPECT_EQ(0, r->status().error());
3254 EXPECT_EQ(redirect_url, r->url());
3255 EXPECT_EQ(original_url, r->original_url());
3256 EXPECT_EQ(2U, r->url_chain().size());
3257 EXPECT_EQ(1, network_delegate.created_requests());
3258 EXPECT_EQ(0, network_delegate.destroyed_requests());
3260 EXPECT_EQ(1, network_delegate.destroyed_requests());
3263 // Tests that the network delegate can block and redirect a request to a new
3264 // URL by setting a redirect_url and returning in OnBeforeURLRequest directly.
3265 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) {
3266 ASSERT_TRUE(test_server_.Start());
3268 TestDelegate d;
3269 BlockingNetworkDelegate network_delegate(
3270 BlockingNetworkDelegate::SYNCHRONOUS);
3271 GURL redirect_url(test_server_.GetURL("simple.html"));
3272 network_delegate.set_redirect_url(redirect_url);
3274 TestURLRequestContextWithProxy context(
3275 test_server_.host_port_pair().ToString(), &network_delegate);
3278 GURL original_url(test_server_.GetURL("empty.html"));
3279 scoped_ptr<URLRequest> r(
3280 context.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
3282 // Quit after hitting the redirect, so can check the headers.
3283 d.set_quit_on_redirect(true);
3284 r->Start();
3285 base::RunLoop().Run();
3287 // Check headers from URLRequestJob.
3288 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3289 EXPECT_EQ(307, r->GetResponseCode());
3290 EXPECT_EQ(307, r->response_headers()->response_code());
3291 std::string location;
3292 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
3293 &location));
3294 EXPECT_EQ(redirect_url, GURL(location));
3296 // Let the request finish.
3297 r->FollowDeferredRedirect();
3298 base::RunLoop().Run();
3300 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3301 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
3302 EXPECT_EQ(
3303 1, network_delegate.observed_before_proxy_headers_sent_callbacks());
3304 EXPECT_TRUE(
3305 network_delegate.last_observed_proxy().Equals(
3306 test_server_.host_port_pair()));
3307 EXPECT_EQ(0, r->status().error());
3308 EXPECT_EQ(redirect_url, r->url());
3309 EXPECT_EQ(original_url, r->original_url());
3310 EXPECT_EQ(2U, r->url_chain().size());
3311 EXPECT_EQ(1, network_delegate.created_requests());
3312 EXPECT_EQ(0, network_delegate.destroyed_requests());
3314 EXPECT_EQ(1, network_delegate.destroyed_requests());
3317 // Tests that redirects caused by the network delegate preserve POST data.
3318 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) {
3319 ASSERT_TRUE(test_server_.Start());
3321 const char kData[] = "hello world";
3323 TestDelegate d;
3324 BlockingNetworkDelegate network_delegate(
3325 BlockingNetworkDelegate::AUTO_CALLBACK);
3326 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3327 GURL redirect_url(test_server_.GetURL("echo"));
3328 network_delegate.set_redirect_url(redirect_url);
3330 TestURLRequestContext context(true);
3331 context.set_network_delegate(&network_delegate);
3332 context.Init();
3335 GURL original_url(test_server_.GetURL("empty.html"));
3336 scoped_ptr<URLRequest> r(
3337 context.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
3338 r->set_method("POST");
3339 r->set_upload(CreateSimpleUploadData(kData));
3340 HttpRequestHeaders headers;
3341 headers.SetHeader(HttpRequestHeaders::kContentLength,
3342 base::UintToString(arraysize(kData) - 1));
3343 r->SetExtraRequestHeaders(headers);
3345 // Quit after hitting the redirect, so can check the headers.
3346 d.set_quit_on_redirect(true);
3347 r->Start();
3348 base::RunLoop().Run();
3350 // Check headers from URLRequestJob.
3351 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3352 EXPECT_EQ(307, r->GetResponseCode());
3353 EXPECT_EQ(307, r->response_headers()->response_code());
3354 std::string location;
3355 ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location",
3356 &location));
3357 EXPECT_EQ(redirect_url, GURL(location));
3359 // Let the request finish.
3360 r->FollowDeferredRedirect();
3361 base::RunLoop().Run();
3363 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3364 EXPECT_EQ(0, r->status().error());
3365 EXPECT_EQ(redirect_url, r->url());
3366 EXPECT_EQ(original_url, r->original_url());
3367 EXPECT_EQ(2U, r->url_chain().size());
3368 EXPECT_EQ(1, network_delegate.created_requests());
3369 EXPECT_EQ(0, network_delegate.destroyed_requests());
3370 EXPECT_EQ("POST", r->method());
3371 EXPECT_EQ(kData, d.data_received());
3373 EXPECT_EQ(1, network_delegate.destroyed_requests());
3376 // Tests that the network delegate can block and redirect a request to a new
3377 // URL during OnHeadersReceived.
3378 TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) {
3379 ASSERT_TRUE(test_server_.Start());
3381 TestDelegate d;
3382 BlockingNetworkDelegate network_delegate(
3383 BlockingNetworkDelegate::AUTO_CALLBACK);
3384 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3385 GURL redirect_url(test_server_.GetURL("simple.html"));
3386 network_delegate.set_redirect_on_headers_received_url(redirect_url);
3388 TestURLRequestContextWithProxy context(
3389 test_server_.host_port_pair().ToString(), &network_delegate);
3392 GURL original_url(test_server_.GetURL("empty.html"));
3393 scoped_ptr<URLRequest> r(
3394 context.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
3396 r->Start();
3397 base::RunLoop().Run();
3399 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3400 EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair()));
3401 EXPECT_EQ(
3402 2, network_delegate.observed_before_proxy_headers_sent_callbacks());
3403 EXPECT_TRUE(
3404 network_delegate.last_observed_proxy().Equals(
3405 test_server_.host_port_pair()));
3407 EXPECT_EQ(OK, r->status().error());
3408 EXPECT_EQ(redirect_url, r->url());
3409 EXPECT_EQ(original_url, r->original_url());
3410 EXPECT_EQ(2U, r->url_chain().size());
3411 EXPECT_EQ(2, network_delegate.created_requests());
3412 EXPECT_EQ(0, network_delegate.destroyed_requests());
3414 EXPECT_EQ(1, network_delegate.destroyed_requests());
3417 // Tests that the network delegate can synchronously complete OnAuthRequired
3418 // by taking no action. This indicates that the NetworkDelegate does not want to
3419 // handle the challenge, and is passing the buck along to the
3420 // URLRequest::Delegate.
3421 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) {
3422 ASSERT_TRUE(test_server_.Start());
3424 TestDelegate d;
3425 BlockingNetworkDelegate network_delegate(
3426 BlockingNetworkDelegate::SYNCHRONOUS);
3428 TestURLRequestContext context(true);
3429 context.set_network_delegate(&network_delegate);
3430 context.Init();
3432 d.set_credentials(AuthCredentials(kUser, kSecret));
3435 GURL url(test_server_.GetURL("auth-basic"));
3436 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3437 r->Start();
3439 base::RunLoop().Run();
3441 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3442 EXPECT_EQ(0, r->status().error());
3443 EXPECT_EQ(200, r->GetResponseCode());
3444 EXPECT_TRUE(d.auth_required_called());
3445 EXPECT_EQ(1, network_delegate.created_requests());
3446 EXPECT_EQ(0, network_delegate.destroyed_requests());
3448 EXPECT_EQ(1, network_delegate.destroyed_requests());
3451 TEST_F(URLRequestTestHTTP,
3452 NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) {
3453 ASSERT_TRUE(test_server_.Start());
3455 TestDelegate d;
3456 BlockingNetworkDelegate network_delegate(
3457 BlockingNetworkDelegate::SYNCHRONOUS);
3459 TestURLRequestContext context(true);
3460 context.set_network_delegate(&network_delegate);
3461 context.Init();
3463 d.set_credentials(AuthCredentials(kUser, kSecret));
3466 GURL url(test_server_.GetURL("auth-basic"));
3467 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3468 r->Start();
3471 HttpRequestHeaders headers;
3472 EXPECT_TRUE(r->GetFullRequestHeaders(&headers));
3473 EXPECT_FALSE(headers.HasHeader("Authorization"));
3476 base::RunLoop().Run();
3478 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3479 EXPECT_EQ(0, r->status().error());
3480 EXPECT_EQ(200, r->GetResponseCode());
3481 EXPECT_TRUE(d.auth_required_called());
3482 EXPECT_EQ(1, network_delegate.created_requests());
3483 EXPECT_EQ(0, network_delegate.destroyed_requests());
3485 EXPECT_EQ(1, network_delegate.destroyed_requests());
3488 // Tests that the network delegate can synchronously complete OnAuthRequired
3489 // by setting credentials.
3490 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) {
3491 ASSERT_TRUE(test_server_.Start());
3493 TestDelegate d;
3494 BlockingNetworkDelegate network_delegate(
3495 BlockingNetworkDelegate::SYNCHRONOUS);
3496 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3497 network_delegate.set_auth_retval(
3498 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3500 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3502 TestURLRequestContext context(true);
3503 context.set_network_delegate(&network_delegate);
3504 context.Init();
3507 GURL url(test_server_.GetURL("auth-basic"));
3508 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3509 r->Start();
3510 base::RunLoop().Run();
3512 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3513 EXPECT_EQ(0, r->status().error());
3514 EXPECT_EQ(200, r->GetResponseCode());
3515 EXPECT_FALSE(d.auth_required_called());
3516 EXPECT_EQ(1, network_delegate.created_requests());
3517 EXPECT_EQ(0, network_delegate.destroyed_requests());
3519 EXPECT_EQ(1, network_delegate.destroyed_requests());
3522 // Same as above, but also tests that GetFullRequestHeaders returns the proper
3523 // headers (for the first or second request) when called at the proper times.
3524 TEST_F(URLRequestTestHTTP,
3525 NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) {
3526 ASSERT_TRUE(test_server_.Start());
3528 TestDelegate d;
3529 BlockingNetworkDelegate network_delegate(
3530 BlockingNetworkDelegate::SYNCHRONOUS);
3531 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3532 network_delegate.set_auth_retval(
3533 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3535 network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret));
3537 TestURLRequestContext context(true);
3538 context.set_network_delegate(&network_delegate);
3539 context.Init();
3542 GURL url(test_server_.GetURL("auth-basic"));
3543 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3544 r->Start();
3545 base::RunLoop().Run();
3547 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3548 EXPECT_EQ(0, r->status().error());
3549 EXPECT_EQ(200, r->GetResponseCode());
3550 EXPECT_FALSE(d.auth_required_called());
3551 EXPECT_EQ(1, network_delegate.created_requests());
3552 EXPECT_EQ(0, network_delegate.destroyed_requests());
3555 HttpRequestHeaders headers;
3556 EXPECT_TRUE(r->GetFullRequestHeaders(&headers));
3557 EXPECT_TRUE(headers.HasHeader("Authorization"));
3560 EXPECT_EQ(1, network_delegate.destroyed_requests());
3563 // Tests that the network delegate can synchronously complete OnAuthRequired
3564 // by cancelling authentication.
3565 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) {
3566 ASSERT_TRUE(test_server_.Start());
3568 TestDelegate d;
3569 BlockingNetworkDelegate network_delegate(
3570 BlockingNetworkDelegate::SYNCHRONOUS);
3571 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3572 network_delegate.set_auth_retval(
3573 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3575 TestURLRequestContext context(true);
3576 context.set_network_delegate(&network_delegate);
3577 context.Init();
3580 GURL url(test_server_.GetURL("auth-basic"));
3581 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3582 r->Start();
3583 base::RunLoop().Run();
3585 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3586 EXPECT_EQ(OK, r->status().error());
3587 EXPECT_EQ(401, r->GetResponseCode());
3588 EXPECT_FALSE(d.auth_required_called());
3589 EXPECT_EQ(1, network_delegate.created_requests());
3590 EXPECT_EQ(0, network_delegate.destroyed_requests());
3592 EXPECT_EQ(1, network_delegate.destroyed_requests());
3595 // Tests that the network delegate can asynchronously complete OnAuthRequired
3596 // by taking no action. This indicates that the NetworkDelegate does not want
3597 // to handle the challenge, and is passing the buck along to the
3598 // URLRequest::Delegate.
3599 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) {
3600 ASSERT_TRUE(test_server_.Start());
3602 TestDelegate d;
3603 BlockingNetworkDelegate network_delegate(
3604 BlockingNetworkDelegate::AUTO_CALLBACK);
3605 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3607 TestURLRequestContext context(true);
3608 context.set_network_delegate(&network_delegate);
3609 context.Init();
3611 d.set_credentials(AuthCredentials(kUser, kSecret));
3614 GURL url(test_server_.GetURL("auth-basic"));
3615 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3616 r->Start();
3617 base::RunLoop().Run();
3619 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3620 EXPECT_EQ(0, r->status().error());
3621 EXPECT_EQ(200, r->GetResponseCode());
3622 EXPECT_TRUE(d.auth_required_called());
3623 EXPECT_EQ(1, network_delegate.created_requests());
3624 EXPECT_EQ(0, network_delegate.destroyed_requests());
3626 EXPECT_EQ(1, network_delegate.destroyed_requests());
3629 // Tests that the network delegate can asynchronously complete OnAuthRequired
3630 // by setting credentials.
3631 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) {
3632 ASSERT_TRUE(test_server_.Start());
3634 TestDelegate d;
3635 BlockingNetworkDelegate network_delegate(
3636 BlockingNetworkDelegate::AUTO_CALLBACK);
3637 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3638 network_delegate.set_auth_retval(
3639 NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
3641 AuthCredentials auth_credentials(kUser, kSecret);
3642 network_delegate.set_auth_credentials(auth_credentials);
3644 TestURLRequestContext context(true);
3645 context.set_network_delegate(&network_delegate);
3646 context.Init();
3649 GURL url(test_server_.GetURL("auth-basic"));
3650 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3651 r->Start();
3652 base::RunLoop().Run();
3654 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3655 EXPECT_EQ(0, r->status().error());
3657 EXPECT_EQ(200, r->GetResponseCode());
3658 EXPECT_FALSE(d.auth_required_called());
3659 EXPECT_EQ(1, network_delegate.created_requests());
3660 EXPECT_EQ(0, network_delegate.destroyed_requests());
3662 EXPECT_EQ(1, network_delegate.destroyed_requests());
3665 // Tests that the network delegate can asynchronously complete OnAuthRequired
3666 // by cancelling authentication.
3667 TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) {
3668 ASSERT_TRUE(test_server_.Start());
3670 TestDelegate d;
3671 BlockingNetworkDelegate network_delegate(
3672 BlockingNetworkDelegate::AUTO_CALLBACK);
3673 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3674 network_delegate.set_auth_retval(
3675 NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH);
3677 TestURLRequestContext context(true);
3678 context.set_network_delegate(&network_delegate);
3679 context.Init();
3682 GURL url(test_server_.GetURL("auth-basic"));
3683 scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d));
3684 r->Start();
3685 base::RunLoop().Run();
3687 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
3688 EXPECT_EQ(OK, r->status().error());
3689 EXPECT_EQ(401, r->GetResponseCode());
3690 EXPECT_FALSE(d.auth_required_called());
3691 EXPECT_EQ(1, network_delegate.created_requests());
3692 EXPECT_EQ(0, network_delegate.destroyed_requests());
3694 EXPECT_EQ(1, network_delegate.destroyed_requests());
3697 // Tests that we can handle when a network request was canceled while we were
3698 // waiting for the network delegate.
3699 // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback.
3700 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) {
3701 ASSERT_TRUE(test_server_.Start());
3703 TestDelegate d;
3704 BlockingNetworkDelegate network_delegate(
3705 BlockingNetworkDelegate::USER_CALLBACK);
3706 network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST);
3708 TestURLRequestContext context(true);
3709 context.set_network_delegate(&network_delegate);
3710 context.Init();
3713 scoped_ptr<URLRequest> r(context.CreateRequest(
3714 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
3716 r->Start();
3717 base::RunLoop().Run();
3718 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST,
3719 network_delegate.stage_blocked_for_callback());
3720 EXPECT_EQ(0, network_delegate.completed_requests());
3721 // Cancel before callback.
3722 r->Cancel();
3723 // Ensure that network delegate is notified.
3724 EXPECT_EQ(1, network_delegate.completed_requests());
3725 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3726 EXPECT_EQ(ERR_ABORTED, r->status().error());
3727 EXPECT_EQ(1, network_delegate.created_requests());
3728 EXPECT_EQ(0, network_delegate.destroyed_requests());
3730 EXPECT_EQ(1, network_delegate.destroyed_requests());
3733 // Tests that we can handle when a network request was canceled while we were
3734 // waiting for the network delegate.
3735 // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback.
3736 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) {
3737 ASSERT_TRUE(test_server_.Start());
3739 TestDelegate d;
3740 BlockingNetworkDelegate network_delegate(
3741 BlockingNetworkDelegate::USER_CALLBACK);
3742 network_delegate.set_block_on(
3743 BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS);
3745 TestURLRequestContext context(true);
3746 context.set_network_delegate(&network_delegate);
3747 context.Init();
3750 scoped_ptr<URLRequest> r(context.CreateRequest(
3751 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
3753 r->Start();
3754 base::RunLoop().Run();
3755 EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS,
3756 network_delegate.stage_blocked_for_callback());
3757 EXPECT_EQ(0, network_delegate.completed_requests());
3758 // Cancel before callback.
3759 r->Cancel();
3760 // Ensure that network delegate is notified.
3761 EXPECT_EQ(1, network_delegate.completed_requests());
3762 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3763 EXPECT_EQ(ERR_ABORTED, r->status().error());
3764 EXPECT_EQ(1, network_delegate.created_requests());
3765 EXPECT_EQ(0, network_delegate.destroyed_requests());
3767 EXPECT_EQ(1, network_delegate.destroyed_requests());
3770 // Tests that we can handle when a network request was canceled while we were
3771 // waiting for the network delegate.
3772 // Part 3: Request is cancelled while waiting for OnHeadersReceived callback.
3773 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) {
3774 ASSERT_TRUE(test_server_.Start());
3776 TestDelegate d;
3777 BlockingNetworkDelegate network_delegate(
3778 BlockingNetworkDelegate::USER_CALLBACK);
3779 network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED);
3781 TestURLRequestContext context(true);
3782 context.set_network_delegate(&network_delegate);
3783 context.Init();
3786 scoped_ptr<URLRequest> r(context.CreateRequest(
3787 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
3789 r->Start();
3790 base::RunLoop().Run();
3791 EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED,
3792 network_delegate.stage_blocked_for_callback());
3793 EXPECT_EQ(0, network_delegate.completed_requests());
3794 // Cancel before callback.
3795 r->Cancel();
3796 // Ensure that network delegate is notified.
3797 EXPECT_EQ(1, network_delegate.completed_requests());
3798 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3799 EXPECT_EQ(ERR_ABORTED, r->status().error());
3800 EXPECT_EQ(1, network_delegate.created_requests());
3801 EXPECT_EQ(0, network_delegate.destroyed_requests());
3803 EXPECT_EQ(1, network_delegate.destroyed_requests());
3806 // Tests that we can handle when a network request was canceled while we were
3807 // waiting for the network delegate.
3808 // Part 4: Request is cancelled while waiting for OnAuthRequired callback.
3809 TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) {
3810 ASSERT_TRUE(test_server_.Start());
3812 TestDelegate d;
3813 BlockingNetworkDelegate network_delegate(
3814 BlockingNetworkDelegate::USER_CALLBACK);
3815 network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED);
3817 TestURLRequestContext context(true);
3818 context.set_network_delegate(&network_delegate);
3819 context.Init();
3822 scoped_ptr<URLRequest> r(context.CreateRequest(
3823 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
3825 r->Start();
3826 base::RunLoop().Run();
3827 EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED,
3828 network_delegate.stage_blocked_for_callback());
3829 EXPECT_EQ(0, network_delegate.completed_requests());
3830 // Cancel before callback.
3831 r->Cancel();
3832 // Ensure that network delegate is notified.
3833 EXPECT_EQ(1, network_delegate.completed_requests());
3834 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
3835 EXPECT_EQ(ERR_ABORTED, r->status().error());
3836 EXPECT_EQ(1, network_delegate.created_requests());
3837 EXPECT_EQ(0, network_delegate.destroyed_requests());
3839 EXPECT_EQ(1, network_delegate.destroyed_requests());
3842 // In this unit test, we're using the HTTPTestServer as a proxy server and
3843 // issuing a CONNECT request with the magic host name "www.server-auth.com".
3844 // The HTTPTestServer will return a 401 response, which we should balk at.
3845 TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) {
3846 ASSERT_TRUE(test_server_.Start());
3848 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
3849 TestURLRequestContextWithProxy context(
3850 test_server_.host_port_pair().ToString(), &network_delegate);
3852 TestDelegate d;
3854 scoped_ptr<URLRequest> r(context.CreateRequest(
3855 GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d));
3857 r->Start();
3858 EXPECT_TRUE(r->is_pending());
3860 base::RunLoop().Run();
3862 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
3863 // The proxy server is not set before failure.
3864 EXPECT_TRUE(r->proxy_server().IsEmpty());
3865 EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error());
3869 TEST_F(URLRequestTestHTTP, GetTest_NoCache) {
3870 ASSERT_TRUE(test_server_.Start());
3872 TestDelegate d;
3874 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3875 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
3877 r->Start();
3878 EXPECT_TRUE(r->is_pending());
3880 base::RunLoop().Run();
3882 EXPECT_EQ(1, d.response_started_count());
3883 EXPECT_FALSE(d.received_data_before_response());
3884 EXPECT_NE(0, d.bytes_received());
3885 EXPECT_EQ(test_server_.host_port_pair().host(),
3886 r->GetSocketAddress().host());
3887 EXPECT_EQ(test_server_.host_port_pair().port(),
3888 r->GetSocketAddress().port());
3890 // TODO(eroman): Add back the NetLog tests...
3894 // This test has the server send a large number of cookies to the client.
3895 // To ensure that no number of cookies causes a crash, a galloping binary
3896 // search is used to estimate that maximum number of cookies that are accepted
3897 // by the browser. Beyond the maximum number, the request will fail with
3898 // ERR_RESPONSE_HEADERS_TOO_BIG.
3899 #if defined(OS_WIN)
3900 // http://crbug.com/177916
3901 #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies
3902 #else
3903 #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies
3904 #endif // defined(OS_WIN)
3905 TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) {
3906 ASSERT_TRUE(test_server_.Start());
3908 int lower_bound = 0;
3909 int upper_bound = 1;
3911 // Double the number of cookies until the response header limits are
3912 // exceeded.
3913 while (DoManyCookiesRequest(upper_bound)) {
3914 lower_bound = upper_bound;
3915 upper_bound *= 2;
3916 ASSERT_LT(upper_bound, 1000000);
3919 int tolerance = static_cast<int>(upper_bound * 0.005);
3920 if (tolerance < 2)
3921 tolerance = 2;
3923 // Perform a binary search to find the highest possible number of cookies,
3924 // within the desired tolerance.
3925 while (upper_bound - lower_bound >= tolerance) {
3926 int num_cookies = (lower_bound + upper_bound) / 2;
3928 if (DoManyCookiesRequest(num_cookies))
3929 lower_bound = num_cookies;
3930 else
3931 upper_bound = num_cookies;
3933 // Success: the test did not crash.
3936 TEST_F(URLRequestTestHTTP, GetTest) {
3937 ASSERT_TRUE(test_server_.Start());
3939 TestDelegate d;
3941 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3942 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
3944 r->Start();
3945 EXPECT_TRUE(r->is_pending());
3947 base::RunLoop().Run();
3949 EXPECT_EQ(1, d.response_started_count());
3950 EXPECT_FALSE(d.received_data_before_response());
3951 EXPECT_NE(0, d.bytes_received());
3952 EXPECT_EQ(test_server_.host_port_pair().host(),
3953 r->GetSocketAddress().host());
3954 EXPECT_EQ(test_server_.host_port_pair().port(),
3955 r->GetSocketAddress().port());
3959 TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) {
3960 ASSERT_TRUE(test_server_.Start());
3962 TestDelegate d;
3964 GURL test_url(test_server_.GetURL(std::string()));
3965 scoped_ptr<URLRequest> r(
3966 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
3968 HttpRequestHeaders headers;
3969 EXPECT_FALSE(r->GetFullRequestHeaders(&headers));
3971 r->Start();
3972 EXPECT_TRUE(r->is_pending());
3974 base::RunLoop().Run();
3976 EXPECT_EQ(1, d.response_started_count());
3977 EXPECT_FALSE(d.received_data_before_response());
3978 EXPECT_NE(0, d.bytes_received());
3979 EXPECT_EQ(test_server_.host_port_pair().host(),
3980 r->GetSocketAddress().host());
3981 EXPECT_EQ(test_server_.host_port_pair().port(),
3982 r->GetSocketAddress().port());
3984 EXPECT_TRUE(d.have_full_request_headers());
3985 CheckFullRequestHeaders(d.full_request_headers(), test_url);
3989 TEST_F(URLRequestTestHTTP, GetTestLoadTiming) {
3990 ASSERT_TRUE(test_server_.Start());
3992 TestDelegate d;
3994 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
3995 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
3997 r->Start();
3998 EXPECT_TRUE(r->is_pending());
4000 base::RunLoop().Run();
4002 LoadTimingInfo load_timing_info;
4003 r->GetLoadTimingInfo(&load_timing_info);
4004 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4006 EXPECT_EQ(1, d.response_started_count());
4007 EXPECT_FALSE(d.received_data_before_response());
4008 EXPECT_NE(0, d.bytes_received());
4009 EXPECT_EQ(test_server_.host_port_pair().host(),
4010 r->GetSocketAddress().host());
4011 EXPECT_EQ(test_server_.host_port_pair().port(),
4012 r->GetSocketAddress().port());
4016 TEST_F(URLRequestTestHTTP, GetZippedTest) {
4017 ASSERT_TRUE(test_server_.Start());
4019 // Parameter that specifies the Content-Length field in the response:
4020 // C - Compressed length.
4021 // U - Uncompressed length.
4022 // L - Large length (larger than both C & U).
4023 // M - Medium length (between C & U).
4024 // S - Small length (smaller than both C & U).
4025 const char test_parameters[] = "CULMS";
4026 const int num_tests = arraysize(test_parameters)- 1; // Skip NULL.
4027 // C & U should be OK.
4028 // L & M are larger than the data sent, and show an error.
4029 // S has too little data, but we seem to accept it.
4030 const bool test_expect_success[num_tests] =
4031 { true, true, false, false, true };
4033 for (int i = 0; i < num_tests ; i++) {
4034 TestDelegate d;
4036 std::string test_file =
4037 base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c",
4038 test_parameters[i]);
4040 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
4041 TestURLRequestContext context(true);
4042 context.set_network_delegate(&network_delegate);
4043 context.Init();
4045 scoped_ptr<URLRequest> r(context.CreateRequest(
4046 test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d));
4047 r->Start();
4048 EXPECT_TRUE(r->is_pending());
4050 base::RunLoop().Run();
4052 EXPECT_EQ(1, d.response_started_count());
4053 EXPECT_FALSE(d.received_data_before_response());
4054 VLOG(1) << " Received " << d.bytes_received() << " bytes"
4055 << " status = " << r->status().status()
4056 << " error = " << r->status().error();
4057 if (test_expect_success[i]) {
4058 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status())
4059 << " Parameter = \"" << test_file << "\"";
4060 } else {
4061 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
4062 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error())
4063 << " Parameter = \"" << test_file << "\"";
4069 TEST_F(URLRequestTestHTTP, RedirectLoadTiming) {
4070 ASSERT_TRUE(test_server_.Start());
4072 GURL destination_url = test_server_.GetURL(std::string());
4073 GURL original_url =
4074 test_server_.GetURL("server-redirect?" + destination_url.spec());
4075 TestDelegate d;
4076 scoped_ptr<URLRequest> req(
4077 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
4078 req->Start();
4079 base::RunLoop().Run();
4081 EXPECT_EQ(1, d.response_started_count());
4082 EXPECT_EQ(1, d.received_redirect_count());
4083 EXPECT_EQ(destination_url, req->url());
4084 EXPECT_EQ(original_url, req->original_url());
4085 ASSERT_EQ(2U, req->url_chain().size());
4086 EXPECT_EQ(original_url, req->url_chain()[0]);
4087 EXPECT_EQ(destination_url, req->url_chain()[1]);
4089 LoadTimingInfo load_timing_info_before_redirect;
4090 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect(
4091 &load_timing_info_before_redirect));
4092 TestLoadTimingNotReused(load_timing_info_before_redirect,
4093 CONNECT_TIMING_HAS_DNS_TIMES);
4095 LoadTimingInfo load_timing_info;
4096 req->GetLoadTimingInfo(&load_timing_info);
4097 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
4099 // Check that a new socket was used on redirect, since the server does not
4100 // supposed keep-alive sockets, and that the times before the redirect are
4101 // before the ones recorded for the second request.
4102 EXPECT_NE(load_timing_info_before_redirect.socket_log_id,
4103 load_timing_info.socket_log_id);
4104 EXPECT_LE(load_timing_info_before_redirect.receive_headers_end,
4105 load_timing_info.connect_timing.connect_start);
4108 TEST_F(URLRequestTestHTTP, MultipleRedirectTest) {
4109 ASSERT_TRUE(test_server_.Start());
4111 GURL destination_url = test_server_.GetURL(std::string());
4112 GURL middle_redirect_url =
4113 test_server_.GetURL("server-redirect?" + destination_url.spec());
4114 GURL original_url = test_server_.GetURL(
4115 "server-redirect?" + middle_redirect_url.spec());
4116 TestDelegate d;
4117 scoped_ptr<URLRequest> req(
4118 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
4119 req->Start();
4120 base::RunLoop().Run();
4122 EXPECT_EQ(1, d.response_started_count());
4123 EXPECT_EQ(2, d.received_redirect_count());
4124 EXPECT_EQ(destination_url, req->url());
4125 EXPECT_EQ(original_url, req->original_url());
4126 ASSERT_EQ(3U, req->url_chain().size());
4127 EXPECT_EQ(original_url, req->url_chain()[0]);
4128 EXPECT_EQ(middle_redirect_url, req->url_chain()[1]);
4129 EXPECT_EQ(destination_url, req->url_chain()[2]);
4132 // First and second pieces of information logged by delegates to URLRequests.
4133 const char kFirstDelegateInfo[] = "Wonderful delegate";
4134 const char kSecondDelegateInfo[] = "Exciting delegate";
4136 // Logs delegate information to a URLRequest. The first string is logged
4137 // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is
4138 // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then
4139 // another asynchronous call is used to clear the delegate information
4140 // before calling a callback. The object then deletes itself.
4141 class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> {
4142 public:
4143 typedef base::Callback<void()> Callback;
4145 // Each time delegate information is added to the URLRequest, the resulting
4146 // load state is checked. The expected load state after each request is
4147 // passed in as an argument.
4148 static void Run(URLRequest* url_request,
4149 LoadState expected_first_load_state,
4150 LoadState expected_second_load_state,
4151 LoadState expected_third_load_state,
4152 const Callback& callback) {
4153 AsyncDelegateLogger* logger = new AsyncDelegateLogger(
4154 url_request,
4155 expected_first_load_state,
4156 expected_second_load_state,
4157 expected_third_load_state,
4158 callback);
4159 logger->Start();
4162 // Checks that the log entries, starting with log_position, contain the
4163 // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have
4164 // recorded. Returns the index of entry after the expected number of
4165 // events this logged, or entries.size() if there aren't enough entries.
4166 static size_t CheckDelegateInfo(const TestNetLog::CapturedEntryList& entries,
4167 size_t log_position) {
4168 // There should be 4 DELEGATE_INFO events: Two begins and two ends.
4169 if (log_position + 3 >= entries.size()) {
4170 ADD_FAILURE() << "Not enough log entries";
4171 return entries.size();
4173 std::string delegate_info;
4174 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
4175 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
4176 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
4177 &delegate_info));
4178 EXPECT_EQ(kFirstDelegateInfo, delegate_info);
4180 ++log_position;
4181 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
4182 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4184 ++log_position;
4185 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
4186 EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase);
4187 EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info",
4188 &delegate_info));
4189 EXPECT_EQ(kSecondDelegateInfo, delegate_info);
4191 ++log_position;
4192 EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type);
4193 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4195 return log_position + 1;
4198 // Find delegate request begin and end messages for OnBeforeNetworkStart.
4199 // Returns the position of the end message.
4200 static size_t ExpectBeforeNetworkEvents(
4201 const TestNetLog::CapturedEntryList& entries,
4202 size_t log_position) {
4203 log_position =
4204 ExpectLogContainsSomewhereAfter(entries,
4205 log_position,
4206 NetLog::TYPE_URL_REQUEST_DELEGATE,
4207 NetLog::PHASE_BEGIN);
4208 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE,
4209 entries[log_position + 1].type);
4210 EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase);
4211 return log_position + 1;
4214 private:
4215 friend class base::RefCounted<AsyncDelegateLogger>;
4217 AsyncDelegateLogger(URLRequest* url_request,
4218 LoadState expected_first_load_state,
4219 LoadState expected_second_load_state,
4220 LoadState expected_third_load_state,
4221 const Callback& callback)
4222 : url_request_(url_request),
4223 expected_first_load_state_(expected_first_load_state),
4224 expected_second_load_state_(expected_second_load_state),
4225 expected_third_load_state_(expected_third_load_state),
4226 callback_(callback) {
4229 ~AsyncDelegateLogger() {}
4231 void Start() {
4232 url_request_->LogBlockedBy(kFirstDelegateInfo);
4233 LoadStateWithParam load_state = url_request_->GetLoadState();
4234 EXPECT_EQ(expected_first_load_state_, load_state.state);
4235 EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param);
4236 base::MessageLoop::current()->PostTask(
4237 FROM_HERE,
4238 base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this));
4241 void LogSecondDelegate() {
4242 url_request_->LogAndReportBlockedBy(kSecondDelegateInfo);
4243 LoadStateWithParam load_state = url_request_->GetLoadState();
4244 EXPECT_EQ(expected_second_load_state_, load_state.state);
4245 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) {
4246 EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
4247 } else {
4248 EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param);
4250 base::MessageLoop::current()->PostTask(
4251 FROM_HERE,
4252 base::Bind(&AsyncDelegateLogger::LogComplete, this));
4255 void LogComplete() {
4256 url_request_->LogUnblocked();
4257 LoadStateWithParam load_state = url_request_->GetLoadState();
4258 EXPECT_EQ(expected_third_load_state_, load_state.state);
4259 if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE)
4260 EXPECT_EQ(base::string16(), load_state.param);
4261 callback_.Run();
4264 URLRequest* url_request_;
4265 const int expected_first_load_state_;
4266 const int expected_second_load_state_;
4267 const int expected_third_load_state_;
4268 const Callback callback_;
4270 DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger);
4273 // NetworkDelegate that logs delegate information before a request is started,
4274 // before headers are sent, when headers are read, and when auth information
4275 // is requested. Uses AsyncDelegateLogger.
4276 class AsyncLoggingNetworkDelegate : public TestNetworkDelegate {
4277 public:
4278 AsyncLoggingNetworkDelegate() {}
4279 ~AsyncLoggingNetworkDelegate() override {}
4281 // NetworkDelegate implementation.
4282 int OnBeforeURLRequest(URLRequest* request,
4283 const CompletionCallback& callback,
4284 GURL* new_url) override {
4285 TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url);
4286 return RunCallbackAsynchronously(request, callback);
4289 int OnBeforeSendHeaders(URLRequest* request,
4290 const CompletionCallback& callback,
4291 HttpRequestHeaders* headers) override {
4292 TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers);
4293 return RunCallbackAsynchronously(request, callback);
4296 int OnHeadersReceived(
4297 URLRequest* request,
4298 const CompletionCallback& callback,
4299 const HttpResponseHeaders* original_response_headers,
4300 scoped_refptr<HttpResponseHeaders>* override_response_headers,
4301 GURL* allowed_unsafe_redirect_url) override {
4302 TestNetworkDelegate::OnHeadersReceived(request,
4303 callback,
4304 original_response_headers,
4305 override_response_headers,
4306 allowed_unsafe_redirect_url);
4307 return RunCallbackAsynchronously(request, callback);
4310 NetworkDelegate::AuthRequiredResponse OnAuthRequired(
4311 URLRequest* request,
4312 const AuthChallengeInfo& auth_info,
4313 const AuthCallback& callback,
4314 AuthCredentials* credentials) override {
4315 AsyncDelegateLogger::Run(
4316 request,
4317 LOAD_STATE_WAITING_FOR_DELEGATE,
4318 LOAD_STATE_WAITING_FOR_DELEGATE,
4319 LOAD_STATE_WAITING_FOR_DELEGATE,
4320 base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume,
4321 callback, credentials));
4322 return AUTH_REQUIRED_RESPONSE_IO_PENDING;
4325 private:
4326 static int RunCallbackAsynchronously(
4327 URLRequest* request,
4328 const CompletionCallback& callback) {
4329 AsyncDelegateLogger::Run(
4330 request,
4331 LOAD_STATE_WAITING_FOR_DELEGATE,
4332 LOAD_STATE_WAITING_FOR_DELEGATE,
4333 LOAD_STATE_WAITING_FOR_DELEGATE,
4334 base::Bind(callback, OK));
4335 return ERR_IO_PENDING;
4338 static void SetAuthAndResume(const AuthCallback& callback,
4339 AuthCredentials* credentials) {
4340 *credentials = AuthCredentials(kUser, kSecret);
4341 callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
4344 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate);
4347 // URLRequest::Delegate that logs delegate information when the headers
4348 // are received, when each read completes, and during redirects. Uses
4349 // AsyncDelegateLogger. Can optionally cancel a request in any phase.
4351 // Inherits from TestDelegate to reuse the TestDelegate code to handle
4352 // advancing to the next step in most cases, as well as cancellation.
4353 class AsyncLoggingUrlRequestDelegate : public TestDelegate {
4354 public:
4355 enum CancelStage {
4356 NO_CANCEL = 0,
4357 CANCEL_ON_RECEIVED_REDIRECT,
4358 CANCEL_ON_RESPONSE_STARTED,
4359 CANCEL_ON_READ_COMPLETED
4362 explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage)
4363 : cancel_stage_(cancel_stage) {
4364 if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT)
4365 set_cancel_in_received_redirect(true);
4366 else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED)
4367 set_cancel_in_response_started(true);
4368 else if (cancel_stage == CANCEL_ON_READ_COMPLETED)
4369 set_cancel_in_received_data(true);
4371 ~AsyncLoggingUrlRequestDelegate() override {}
4373 // URLRequest::Delegate implementation:
4374 void OnReceivedRedirect(URLRequest* request,
4375 const RedirectInfo& redirect_info,
4376 bool* defer_redirect) override {
4377 *defer_redirect = true;
4378 AsyncDelegateLogger::Run(
4379 request,
4380 LOAD_STATE_WAITING_FOR_DELEGATE,
4381 LOAD_STATE_WAITING_FOR_DELEGATE,
4382 LOAD_STATE_WAITING_FOR_DELEGATE,
4383 base::Bind(
4384 &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete,
4385 base::Unretained(this), request, redirect_info));
4388 void OnResponseStarted(URLRequest* request) override {
4389 AsyncDelegateLogger::Run(
4390 request,
4391 LOAD_STATE_WAITING_FOR_DELEGATE,
4392 LOAD_STATE_WAITING_FOR_DELEGATE,
4393 LOAD_STATE_WAITING_FOR_DELEGATE,
4394 base::Bind(
4395 &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete,
4396 base::Unretained(this), request));
4399 void OnReadCompleted(URLRequest* request, int bytes_read) override {
4400 AsyncDelegateLogger::Run(
4401 request,
4402 LOAD_STATE_IDLE,
4403 LOAD_STATE_IDLE,
4404 LOAD_STATE_IDLE,
4405 base::Bind(
4406 &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete,
4407 base::Unretained(this), request, bytes_read));
4410 private:
4411 void OnReceivedRedirectLoggingComplete(URLRequest* request,
4412 const RedirectInfo& redirect_info) {
4413 bool defer_redirect = false;
4414 TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect);
4415 // FollowDeferredRedirect should not be called after cancellation.
4416 if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT)
4417 return;
4418 if (!defer_redirect)
4419 request->FollowDeferredRedirect();
4422 void OnResponseStartedLoggingComplete(URLRequest* request) {
4423 // The parent class continues the request.
4424 TestDelegate::OnResponseStarted(request);
4427 void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) {
4428 // The parent class continues the request.
4429 TestDelegate::OnReadCompleted(request, bytes_read);
4432 const CancelStage cancel_stage_;
4434 DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate);
4437 // Tests handling of delegate info before a request starts.
4438 TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) {
4439 ASSERT_TRUE(test_server_.Start());
4441 TestDelegate request_delegate;
4442 TestURLRequestContext context(true);
4443 context.set_network_delegate(NULL);
4444 context.set_net_log(&net_log_);
4445 context.Init();
4448 scoped_ptr<URLRequest> r(
4449 context.CreateRequest(test_server_.GetURL("empty.html"),
4450 DEFAULT_PRIORITY, &request_delegate));
4451 LoadStateWithParam load_state = r->GetLoadState();
4452 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4453 EXPECT_EQ(base::string16(), load_state.param);
4455 AsyncDelegateLogger::Run(
4456 r.get(),
4457 LOAD_STATE_WAITING_FOR_DELEGATE,
4458 LOAD_STATE_WAITING_FOR_DELEGATE,
4459 LOAD_STATE_IDLE,
4460 base::Bind(&URLRequest::Start, base::Unretained(r.get())));
4462 base::RunLoop().Run();
4464 EXPECT_EQ(200, r->GetResponseCode());
4465 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
4468 TestNetLog::CapturedEntryList entries;
4469 net_log_.GetEntries(&entries);
4470 size_t log_position = ExpectLogContainsSomewhereAfter(
4471 entries,
4473 NetLog::TYPE_DELEGATE_INFO,
4474 NetLog::PHASE_BEGIN);
4476 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position);
4478 // Nothing else should add any delegate info to the request.
4479 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4480 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4483 // Tests handling of delegate info from a network delegate.
4484 TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) {
4485 ASSERT_TRUE(test_server_.Start());
4487 TestDelegate request_delegate;
4488 AsyncLoggingNetworkDelegate network_delegate;
4489 TestURLRequestContext context(true);
4490 context.set_network_delegate(&network_delegate);
4491 context.set_net_log(&net_log_);
4492 context.Init();
4495 scoped_ptr<URLRequest> r(
4496 context.CreateRequest(test_server_.GetURL("simple.html"),
4497 DEFAULT_PRIORITY, &request_delegate));
4498 LoadStateWithParam load_state = r->GetLoadState();
4499 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4500 EXPECT_EQ(base::string16(), load_state.param);
4502 r->Start();
4503 base::RunLoop().Run();
4505 EXPECT_EQ(200, r->GetResponseCode());
4506 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
4507 EXPECT_EQ(1, network_delegate.created_requests());
4508 EXPECT_EQ(0, network_delegate.destroyed_requests());
4510 EXPECT_EQ(1, network_delegate.destroyed_requests());
4512 size_t log_position = 0;
4513 TestNetLog::CapturedEntryList entries;
4514 net_log_.GetEntries(&entries);
4515 for (size_t i = 0; i < 3; ++i) {
4516 log_position = ExpectLogContainsSomewhereAfter(
4517 entries,
4518 log_position + 1,
4519 NetLog::TYPE_URL_REQUEST_DELEGATE,
4520 NetLog::PHASE_BEGIN);
4522 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4523 log_position + 1);
4525 ASSERT_LT(log_position, entries.size());
4526 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4527 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4529 if (i == 1) {
4530 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4531 entries, log_position + 1);
4535 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4536 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4539 // Tests handling of delegate info from a network delegate in the case of an
4540 // HTTP redirect.
4541 TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) {
4542 ASSERT_TRUE(test_server_.Start());
4544 TestDelegate request_delegate;
4545 AsyncLoggingNetworkDelegate network_delegate;
4546 TestURLRequestContext context(true);
4547 context.set_network_delegate(&network_delegate);
4548 context.set_net_log(&net_log_);
4549 context.Init();
4552 scoped_ptr<URLRequest> r(context.CreateRequest(
4553 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4554 &request_delegate));
4555 LoadStateWithParam load_state = r->GetLoadState();
4556 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4557 EXPECT_EQ(base::string16(), load_state.param);
4559 r->Start();
4560 base::RunLoop().Run();
4562 EXPECT_EQ(200, r->GetResponseCode());
4563 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
4564 EXPECT_EQ(2, network_delegate.created_requests());
4565 EXPECT_EQ(0, network_delegate.destroyed_requests());
4567 EXPECT_EQ(1, network_delegate.destroyed_requests());
4569 size_t log_position = 0;
4570 TestNetLog::CapturedEntryList entries;
4571 net_log_.GetEntries(&entries);
4572 // The NetworkDelegate logged information in OnBeforeURLRequest,
4573 // OnBeforeSendHeaders, and OnHeadersReceived.
4574 for (size_t i = 0; i < 3; ++i) {
4575 log_position = ExpectLogContainsSomewhereAfter(
4576 entries,
4577 log_position + 1,
4578 NetLog::TYPE_URL_REQUEST_DELEGATE,
4579 NetLog::PHASE_BEGIN);
4581 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4582 log_position + 1);
4584 ASSERT_LT(log_position, entries.size());
4585 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4586 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4588 if (i == 1) {
4589 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4590 entries, log_position + 1);
4594 // The URLRequest::Delegate then gets informed about the redirect.
4595 log_position = ExpectLogContainsSomewhereAfter(
4596 entries,
4597 log_position + 1,
4598 NetLog::TYPE_URL_REQUEST_DELEGATE,
4599 NetLog::PHASE_BEGIN);
4601 // The NetworkDelegate logged information in the same three events as before.
4602 for (size_t i = 0; i < 3; ++i) {
4603 log_position = ExpectLogContainsSomewhereAfter(
4604 entries,
4605 log_position + 1,
4606 NetLog::TYPE_URL_REQUEST_DELEGATE,
4607 NetLog::PHASE_BEGIN);
4609 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4610 log_position + 1);
4612 ASSERT_LT(log_position, entries.size());
4613 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4614 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4617 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4618 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4621 // Tests handling of delegate info from a network delegate in the case of HTTP
4622 // AUTH.
4623 TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) {
4624 ASSERT_TRUE(test_server_.Start());
4626 TestDelegate request_delegate;
4627 AsyncLoggingNetworkDelegate network_delegate;
4628 TestURLRequestContext context(true);
4629 context.set_network_delegate(&network_delegate);
4630 context.set_net_log(&net_log_);
4631 context.Init();
4634 scoped_ptr<URLRequest> r(
4635 context.CreateRequest(test_server_.GetURL("auth-basic"),
4636 DEFAULT_PRIORITY, &request_delegate));
4637 LoadStateWithParam load_state = r->GetLoadState();
4638 EXPECT_EQ(LOAD_STATE_IDLE, load_state.state);
4639 EXPECT_EQ(base::string16(), load_state.param);
4641 r->Start();
4642 base::RunLoop().Run();
4644 EXPECT_EQ(200, r->GetResponseCode());
4645 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
4646 EXPECT_EQ(1, network_delegate.created_requests());
4647 EXPECT_EQ(0, network_delegate.destroyed_requests());
4649 EXPECT_EQ(1, network_delegate.destroyed_requests());
4651 size_t log_position = 0;
4652 TestNetLog::CapturedEntryList entries;
4653 net_log_.GetEntries(&entries);
4654 // The NetworkDelegate should have logged information in OnBeforeURLRequest,
4655 // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in
4656 // OnBeforeURLRequest and OnBeforeSendHeaders.
4657 for (size_t i = 0; i < 6; ++i) {
4658 log_position = ExpectLogContainsSomewhereAfter(
4659 entries,
4660 log_position + 1,
4661 NetLog::TYPE_URL_REQUEST_DELEGATE,
4662 NetLog::PHASE_BEGIN);
4664 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4665 log_position + 1);
4667 ASSERT_LT(log_position, entries.size());
4668 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4669 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4671 if (i == 1) {
4672 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4673 entries, log_position + 1);
4677 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4678 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4681 // Tests handling of delegate info from a URLRequest::Delegate.
4682 TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) {
4683 ASSERT_TRUE(test_server_.Start());
4685 AsyncLoggingUrlRequestDelegate request_delegate(
4686 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4687 TestURLRequestContext context(true);
4688 context.set_network_delegate(NULL);
4689 context.set_net_log(&net_log_);
4690 context.Init();
4693 // A chunked response with delays between chunks is used to make sure that
4694 // attempts by the URLRequest delegate to log information while reading the
4695 // body are ignored. Since they are ignored, this test is robust against
4696 // the possibility of multiple reads being combined in the unlikely event
4697 // that it occurs.
4698 scoped_ptr<URLRequest> r(context.CreateRequest(
4699 test_server_.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY,
4700 &request_delegate));
4701 LoadStateWithParam load_state = r->GetLoadState();
4702 r->Start();
4703 base::RunLoop().Run();
4705 EXPECT_EQ(200, r->GetResponseCode());
4706 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
4709 TestNetLog::CapturedEntryList entries;
4710 net_log_.GetEntries(&entries);
4712 size_t log_position = 0;
4714 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4715 entries, log_position);
4717 // The delegate info should only have been logged on header complete. Other
4718 // times it should silently be ignored.
4719 log_position =
4720 ExpectLogContainsSomewhereAfter(entries,
4721 log_position + 1,
4722 NetLog::TYPE_URL_REQUEST_DELEGATE,
4723 NetLog::PHASE_BEGIN);
4725 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4726 log_position + 1);
4728 ASSERT_LT(log_position, entries.size());
4729 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4730 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4732 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4733 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4734 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4735 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4738 // Tests handling of delegate info from a URLRequest::Delegate in the case of
4739 // an HTTP redirect.
4740 TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) {
4741 ASSERT_TRUE(test_server_.Start());
4743 AsyncLoggingUrlRequestDelegate request_delegate(
4744 AsyncLoggingUrlRequestDelegate::NO_CANCEL);
4745 TestURLRequestContext context(true);
4746 context.set_network_delegate(NULL);
4747 context.set_net_log(&net_log_);
4748 context.Init();
4751 scoped_ptr<URLRequest> r(context.CreateRequest(
4752 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4753 &request_delegate));
4754 LoadStateWithParam load_state = r->GetLoadState();
4755 r->Start();
4756 base::RunLoop().Run();
4758 EXPECT_EQ(200, r->GetResponseCode());
4759 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
4762 TestNetLog::CapturedEntryList entries;
4763 net_log_.GetEntries(&entries);
4765 // Delegate info should only have been logged in OnReceivedRedirect and
4766 // OnResponseStarted.
4767 size_t log_position = 0;
4768 for (int i = 0; i < 2; ++i) {
4769 if (i == 0) {
4770 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4771 entries, log_position) + 1;
4774 log_position = ExpectLogContainsSomewhereAfter(
4775 entries,
4776 log_position,
4777 NetLog::TYPE_URL_REQUEST_DELEGATE,
4778 NetLog::PHASE_BEGIN);
4780 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4781 log_position + 1);
4783 ASSERT_LT(log_position, entries.size());
4784 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4785 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4788 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4789 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4790 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4791 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4794 // Tests handling of delegate info from a URLRequest::Delegate in the case of
4795 // an HTTP redirect, with cancellation at various points.
4796 TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) {
4797 ASSERT_TRUE(test_server_.Start());
4799 const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = {
4800 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT,
4801 AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED,
4802 AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED,
4805 for (size_t test_case = 0; test_case < arraysize(kCancelStages);
4806 ++test_case) {
4807 AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]);
4808 TestURLRequestContext context(true);
4809 TestNetLog net_log;
4810 context.set_network_delegate(NULL);
4811 context.set_net_log(&net_log);
4812 context.Init();
4815 scoped_ptr<URLRequest> r(context.CreateRequest(
4816 test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY,
4817 &request_delegate));
4818 LoadStateWithParam load_state = r->GetLoadState();
4819 r->Start();
4820 base::RunLoop().Run();
4821 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
4824 TestNetLog::CapturedEntryList entries;
4825 net_log.GetEntries(&entries);
4827 // Delegate info is always logged in both OnReceivedRedirect and
4828 // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the
4829 // OnResponseStarted delegate call is after cancellation, but logging is
4830 // still currently supported in that call.
4831 size_t log_position = 0;
4832 for (int i = 0; i < 2; ++i) {
4833 if (i == 0) {
4834 log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents(
4835 entries, log_position) + 1;
4838 log_position = ExpectLogContainsSomewhereAfter(
4839 entries,
4840 log_position,
4841 NetLog::TYPE_URL_REQUEST_DELEGATE,
4842 NetLog::PHASE_BEGIN);
4844 log_position = AsyncDelegateLogger::CheckDelegateInfo(entries,
4845 log_position + 1);
4847 ASSERT_LT(log_position, entries.size());
4848 EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type);
4849 EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase);
4852 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4853 entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO));
4854 EXPECT_FALSE(LogContainsEntryWithTypeAfter(
4855 entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE));
4859 namespace {
4861 const char kExtraHeader[] = "Allow-Snafu";
4862 const char kExtraValue[] = "fubar";
4864 class RedirectWithAdditionalHeadersDelegate : public TestDelegate {
4865 void OnReceivedRedirect(URLRequest* request,
4866 const RedirectInfo& redirect_info,
4867 bool* defer_redirect) override {
4868 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
4869 request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false);
4873 } // namespace
4875 TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) {
4876 ASSERT_TRUE(test_server_.Start());
4878 GURL destination_url = test_server_.GetURL(
4879 "echoheader?" + std::string(kExtraHeader));
4880 GURL original_url = test_server_.GetURL(
4881 "server-redirect?" + destination_url.spec());
4882 RedirectWithAdditionalHeadersDelegate d;
4883 scoped_ptr<URLRequest> req(
4884 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
4885 req->Start();
4886 base::RunLoop().Run();
4888 std::string value;
4889 const HttpRequestHeaders& headers = req->extra_request_headers();
4890 EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value));
4891 EXPECT_EQ(kExtraValue, value);
4892 EXPECT_FALSE(req->is_pending());
4893 EXPECT_FALSE(req->is_redirecting());
4894 EXPECT_EQ(kExtraValue, d.data_received());
4897 namespace {
4899 const char kExtraHeaderToRemove[] = "To-Be-Removed";
4901 class RedirectWithHeaderRemovalDelegate : public TestDelegate {
4902 void OnReceivedRedirect(URLRequest* request,
4903 const RedirectInfo& redirect_info,
4904 bool* defer_redirect) override {
4905 TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect);
4906 request->RemoveRequestHeaderByName(kExtraHeaderToRemove);
4910 } // namespace
4912 TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) {
4913 ASSERT_TRUE(test_server_.Start());
4915 GURL destination_url = test_server_.GetURL(
4916 "echoheader?" + std::string(kExtraHeaderToRemove));
4917 GURL original_url = test_server_.GetURL(
4918 "server-redirect?" + destination_url.spec());
4919 RedirectWithHeaderRemovalDelegate d;
4920 scoped_ptr<URLRequest> req(
4921 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
4922 req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false);
4923 req->Start();
4924 base::RunLoop().Run();
4926 std::string value;
4927 const HttpRequestHeaders& headers = req->extra_request_headers();
4928 EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value));
4929 EXPECT_FALSE(req->is_pending());
4930 EXPECT_FALSE(req->is_redirecting());
4931 EXPECT_EQ("None", d.data_received());
4934 TEST_F(URLRequestTestHTTP, CancelTest) {
4935 TestDelegate d;
4937 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4938 GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d));
4940 r->Start();
4941 EXPECT_TRUE(r->is_pending());
4943 r->Cancel();
4945 base::RunLoop().Run();
4947 // We expect to receive OnResponseStarted even though the request has been
4948 // cancelled.
4949 EXPECT_EQ(1, d.response_started_count());
4950 EXPECT_EQ(0, d.bytes_received());
4951 EXPECT_FALSE(d.received_data_before_response());
4955 TEST_F(URLRequestTestHTTP, CancelTest2) {
4956 ASSERT_TRUE(test_server_.Start());
4958 TestDelegate d;
4960 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4961 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
4963 d.set_cancel_in_response_started(true);
4965 r->Start();
4966 EXPECT_TRUE(r->is_pending());
4968 base::RunLoop().Run();
4970 EXPECT_EQ(1, d.response_started_count());
4971 EXPECT_EQ(0, d.bytes_received());
4972 EXPECT_FALSE(d.received_data_before_response());
4973 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
4977 TEST_F(URLRequestTestHTTP, CancelTest3) {
4978 ASSERT_TRUE(test_server_.Start());
4980 TestDelegate d;
4982 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
4983 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
4985 d.set_cancel_in_received_data(true);
4987 r->Start();
4988 EXPECT_TRUE(r->is_pending());
4990 base::RunLoop().Run();
4992 EXPECT_EQ(1, d.response_started_count());
4993 // There is no guarantee about how much data was received
4994 // before the cancel was issued. It could have been 0 bytes,
4995 // or it could have been all the bytes.
4996 // EXPECT_EQ(0, d.bytes_received());
4997 EXPECT_FALSE(d.received_data_before_response());
4998 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
5002 TEST_F(URLRequestTestHTTP, CancelTest4) {
5003 ASSERT_TRUE(test_server_.Start());
5005 TestDelegate d;
5007 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5008 test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d));
5010 r->Start();
5011 EXPECT_TRUE(r->is_pending());
5013 // The request will be implicitly canceled when it is destroyed. The
5014 // test delegate must not post a quit message when this happens because
5015 // this test doesn't actually have a message loop. The quit message would
5016 // get put on this thread's message queue and the next test would exit
5017 // early, causing problems.
5018 d.set_quit_on_complete(false);
5020 // expect things to just cleanup properly.
5022 // we won't actually get a received reponse here because we've never run the
5023 // message loop
5024 EXPECT_FALSE(d.received_data_before_response());
5025 EXPECT_EQ(0, d.bytes_received());
5028 TEST_F(URLRequestTestHTTP, CancelTest5) {
5029 ASSERT_TRUE(test_server_.Start());
5031 // populate cache
5033 TestDelegate d;
5034 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5035 test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d));
5036 r->Start();
5037 base::RunLoop().Run();
5038 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5041 // cancel read from cache (see bug 990242)
5043 TestDelegate d;
5044 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5045 test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d));
5046 r->Start();
5047 r->Cancel();
5048 base::RunLoop().Run();
5050 EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status());
5051 EXPECT_EQ(1, d.response_started_count());
5052 EXPECT_EQ(0, d.bytes_received());
5053 EXPECT_FALSE(d.received_data_before_response());
5057 TEST_F(URLRequestTestHTTP, PostTest) {
5058 ASSERT_TRUE(test_server_.Start());
5059 HTTPUploadDataOperationTest("POST");
5062 TEST_F(URLRequestTestHTTP, PutTest) {
5063 ASSERT_TRUE(test_server_.Start());
5064 HTTPUploadDataOperationTest("PUT");
5067 TEST_F(URLRequestTestHTTP, PostEmptyTest) {
5068 ASSERT_TRUE(test_server_.Start());
5070 TestDelegate d;
5072 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5073 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
5074 r->set_method("POST");
5076 r->Start();
5077 EXPECT_TRUE(r->is_pending());
5079 base::RunLoop().Run();
5081 ASSERT_EQ(1, d.response_started_count())
5082 << "request failed: " << r->status().status()
5083 << ", error: " << r->status().error();
5085 EXPECT_FALSE(d.received_data_before_response());
5086 EXPECT_TRUE(d.data_received().empty());
5090 TEST_F(URLRequestTestHTTP, PostFileTest) {
5091 ASSERT_TRUE(test_server_.Start());
5093 TestDelegate d;
5095 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5096 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
5097 r->set_method("POST");
5099 base::FilePath dir;
5100 PathService::Get(base::DIR_EXE, &dir);
5101 base::SetCurrentDirectory(dir);
5103 ScopedVector<UploadElementReader> element_readers;
5105 base::FilePath path;
5106 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5107 path = path.Append(FILE_PATH_LITERAL("net"));
5108 path = path.Append(FILE_PATH_LITERAL("data"));
5109 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5110 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5111 element_readers.push_back(
5112 new UploadFileElementReader(base::MessageLoopProxy::current().get(),
5113 path,
5115 kuint64max,
5116 base::Time()));
5117 r->set_upload(make_scoped_ptr<UploadDataStream>(
5118 new ElementsUploadDataStream(element_readers.Pass(), 0)));
5120 r->Start();
5121 EXPECT_TRUE(r->is_pending());
5123 base::RunLoop().Run();
5125 int64 size64 = 0;
5126 ASSERT_EQ(true, base::GetFileSize(path, &size64));
5127 ASSERT_LE(size64, std::numeric_limits<int>::max());
5128 int size = static_cast<int>(size64);
5129 scoped_ptr<char[]> buf(new char[size]);
5131 ASSERT_EQ(size, base::ReadFile(path, buf.get(), size));
5133 ASSERT_EQ(1, d.response_started_count())
5134 << "request failed: " << r->status().status()
5135 << ", error: " << r->status().error();
5137 EXPECT_FALSE(d.received_data_before_response());
5139 EXPECT_EQ(size, d.bytes_received());
5140 EXPECT_EQ(std::string(&buf[0], size), d.data_received());
5144 TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) {
5145 ASSERT_TRUE(test_server_.Start());
5147 TestDelegate d;
5149 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5150 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
5151 r->set_method("POST");
5153 ScopedVector<UploadElementReader> element_readers;
5155 element_readers.push_back(new UploadFileElementReader(
5156 base::MessageLoopProxy::current().get(),
5157 base::FilePath(FILE_PATH_LITERAL(
5158 "c:\\path\\to\\non\\existant\\file.randomness.12345")),
5160 kuint64max,
5161 base::Time()));
5162 r->set_upload(make_scoped_ptr<UploadDataStream>(
5163 new ElementsUploadDataStream(element_readers.Pass(), 0)));
5165 r->Start();
5166 EXPECT_TRUE(r->is_pending());
5168 base::RunLoop().Run();
5170 EXPECT_TRUE(d.request_failed());
5171 EXPECT_FALSE(d.received_data_before_response());
5172 EXPECT_EQ(0, d.bytes_received());
5173 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
5174 EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error());
5178 TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) {
5179 ASSERT_TRUE(test_server_.Start());
5181 TestDelegate d;
5183 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5184 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
5185 r->EnableChunkedUpload();
5186 r->set_method("POST");
5187 AddChunksToUpload(r.get());
5188 r->Start();
5189 EXPECT_TRUE(r->is_pending());
5191 base::RunLoop().Run();
5193 VerifyReceivedDataMatchesChunks(r.get(), &d);
5197 TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) {
5198 ASSERT_TRUE(test_server_.Start());
5200 TestDelegate d;
5202 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5203 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
5204 r->EnableChunkedUpload();
5205 r->set_method("POST");
5206 r->Start();
5207 EXPECT_TRUE(r->is_pending());
5208 AddChunksToUpload(r.get());
5209 base::RunLoop().Run();
5211 VerifyReceivedDataMatchesChunks(r.get(), &d);
5215 TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) {
5216 ASSERT_TRUE(test_server_.Start());
5218 TestDelegate d;
5220 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5221 test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d));
5222 r->EnableChunkedUpload();
5223 r->set_method("POST");
5224 r->Start();
5225 EXPECT_TRUE(r->is_pending());
5227 base::RunLoop().RunUntilIdle();
5228 AddChunksToUpload(r.get());
5229 base::RunLoop().Run();
5231 VerifyReceivedDataMatchesChunks(r.get(), &d);
5235 TEST_F(URLRequestTestHTTP, ResponseHeadersTest) {
5236 ASSERT_TRUE(test_server_.Start());
5238 TestDelegate d;
5239 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5240 test_server_.GetURL("files/with-headers.html"), DEFAULT_PRIORITY, &d));
5241 req->Start();
5242 base::RunLoop().Run();
5244 const HttpResponseHeaders* headers = req->response_headers();
5246 // Simple sanity check that response_info() accesses the same data.
5247 EXPECT_EQ(headers, req->response_info().headers.get());
5249 std::string header;
5250 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header));
5251 EXPECT_EQ("private", header);
5253 header.clear();
5254 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header));
5255 EXPECT_EQ("text/html; charset=ISO-8859-1", header);
5257 // The response has two "X-Multiple-Entries" headers.
5258 // This verfies our output has them concatenated together.
5259 header.clear();
5260 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header));
5261 EXPECT_EQ("a, b", header);
5264 TEST_F(URLRequestTestHTTP, ProcessSTS) {
5265 SpawnedTestServer::SSLOptions ssl_options(
5266 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
5267 SpawnedTestServer https_test_server(
5268 SpawnedTestServer::TYPE_HTTPS,
5269 ssl_options,
5270 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5271 ASSERT_TRUE(https_test_server.Start());
5273 std::string test_server_hostname = https_test_server.GetURL("").host();
5275 TestDelegate d;
5276 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5277 https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY,
5278 &d));
5279 request->Start();
5280 base::RunLoop().Run();
5282 TransportSecurityState* security_state =
5283 default_context_.transport_security_state();
5284 TransportSecurityState::DomainState domain_state;
5285 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5286 &domain_state));
5287 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5288 domain_state.sts.upgrade_mode);
5289 EXPECT_TRUE(domain_state.sts.include_subdomains);
5290 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5291 #if defined(OS_ANDROID)
5292 // Android's CertVerifyProc does not (yet) handle pins.
5293 #else
5294 EXPECT_FALSE(domain_state.HasPublicKeyPins());
5295 #endif
5298 TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) {
5299 SpawnedTestServer https_test_server(
5300 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(),
5301 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5302 ASSERT_TRUE(https_test_server.Start());
5303 // Make sure this test fails if the test server is changed to not
5304 // listen on an IP by default.
5305 ASSERT_TRUE(https_test_server.GetURL("").HostIsIPAddress());
5306 std::string test_server_hostname = https_test_server.GetURL("").host();
5308 TestDelegate d;
5309 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5310 https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY,
5311 &d));
5312 request->Start();
5313 base::RunLoop().Run();
5315 TransportSecurityState* security_state =
5316 default_context_.transport_security_state();
5317 TransportSecurityState::DomainState domain_state;
5318 EXPECT_FALSE(security_state->GetDynamicDomainState(test_server_hostname,
5319 &domain_state));
5322 // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will
5323 // reject HPKP headers, and a test setting only HPKP headers will fail (no
5324 // DomainState present because header rejected).
5325 #if defined(OS_ANDROID)
5326 #define MAYBE_ProcessPKP DISABLED_ProcessPKP
5327 #else
5328 #define MAYBE_ProcessPKP ProcessPKP
5329 #endif
5331 // Tests that enabling HPKP on a domain does not affect the HSTS
5332 // validity/expiration.
5333 TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) {
5334 SpawnedTestServer::SSLOptions ssl_options(
5335 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
5336 SpawnedTestServer https_test_server(
5337 SpawnedTestServer::TYPE_HTTPS,
5338 ssl_options,
5339 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5340 ASSERT_TRUE(https_test_server.Start());
5342 std::string test_server_hostname = https_test_server.GetURL("").host();
5344 TestDelegate d;
5345 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5346 https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY,
5347 &d));
5348 request->Start();
5349 base::RunLoop().Run();
5351 TransportSecurityState* security_state =
5352 default_context_.transport_security_state();
5353 TransportSecurityState::DomainState domain_state;
5354 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5355 &domain_state));
5356 EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT,
5357 domain_state.sts.upgrade_mode);
5358 EXPECT_FALSE(domain_state.sts.include_subdomains);
5359 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5360 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5361 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5364 TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) {
5365 SpawnedTestServer https_test_server(
5366 SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(),
5367 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5368 ASSERT_TRUE(https_test_server.Start());
5369 // Make sure this test fails if the test server is changed to not
5370 // listen on an IP by default.
5371 ASSERT_TRUE(https_test_server.GetURL("").HostIsIPAddress());
5372 std::string test_server_hostname = https_test_server.GetURL("").host();
5374 TestDelegate d;
5375 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5376 https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY,
5377 &d));
5378 request->Start();
5379 base::RunLoop().Run();
5381 TransportSecurityState* security_state =
5382 default_context_.transport_security_state();
5383 TransportSecurityState::DomainState domain_state;
5384 EXPECT_FALSE(security_state->GetDynamicDomainState(test_server_hostname,
5385 &domain_state));
5388 TEST_F(URLRequestTestHTTP, ProcessSTSOnce) {
5389 SpawnedTestServer::SSLOptions ssl_options(
5390 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
5391 SpawnedTestServer https_test_server(
5392 SpawnedTestServer::TYPE_HTTPS,
5393 ssl_options,
5394 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5395 ASSERT_TRUE(https_test_server.Start());
5397 std::string test_server_hostname = https_test_server.GetURL("").host();
5399 TestDelegate d;
5400 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5401 https_test_server.GetURL("files/hsts-multiple-headers.html"),
5402 DEFAULT_PRIORITY, &d));
5403 request->Start();
5404 base::RunLoop().Run();
5406 // We should have set parameters from the first header, not the second.
5407 TransportSecurityState* security_state =
5408 default_context_.transport_security_state();
5409 TransportSecurityState::DomainState domain_state;
5410 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5411 &domain_state));
5412 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5413 domain_state.sts.upgrade_mode);
5414 EXPECT_FALSE(domain_state.sts.include_subdomains);
5415 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5418 TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) {
5419 SpawnedTestServer::SSLOptions ssl_options(
5420 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
5421 SpawnedTestServer https_test_server(
5422 SpawnedTestServer::TYPE_HTTPS,
5423 ssl_options,
5424 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5425 ASSERT_TRUE(https_test_server.Start());
5427 std::string test_server_hostname = https_test_server.GetURL("").host();
5429 TestDelegate d;
5430 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5431 https_test_server.GetURL("files/hsts-and-hpkp-headers.html"),
5432 DEFAULT_PRIORITY, &d));
5433 request->Start();
5434 base::RunLoop().Run();
5436 // We should have set parameters from the first header, not the second.
5437 TransportSecurityState* security_state =
5438 default_context_.transport_security_state();
5439 TransportSecurityState::DomainState domain_state;
5440 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5441 &domain_state));
5442 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5443 domain_state.sts.upgrade_mode);
5444 #if defined(OS_ANDROID)
5445 // Android's CertVerifyProc does not (yet) handle pins.
5446 #else
5447 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5448 #endif
5449 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5451 // Even though there is an HSTS header asserting includeSubdomains, it is
5452 // the *second* such header, and we MUST process only the first.
5453 EXPECT_FALSE(domain_state.sts.include_subdomains);
5454 // includeSubdomains does not occur in the test HPKP header.
5455 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5458 // Tests that when multiple HPKP headers are present, asserting different
5459 // policies, that only the first such policy is processed.
5460 TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) {
5461 SpawnedTestServer::SSLOptions ssl_options(
5462 SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN);
5463 SpawnedTestServer https_test_server(
5464 SpawnedTestServer::TYPE_HTTPS,
5465 ssl_options,
5466 base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest")));
5467 ASSERT_TRUE(https_test_server.Start());
5469 std::string test_server_hostname = https_test_server.GetURL("").host();
5471 TestDelegate d;
5472 scoped_ptr<URLRequest> request(default_context_.CreateRequest(
5473 https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"),
5474 DEFAULT_PRIORITY, &d));
5475 request->Start();
5476 base::RunLoop().Run();
5478 TransportSecurityState* security_state =
5479 default_context_.transport_security_state();
5480 TransportSecurityState::DomainState domain_state;
5481 EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname,
5482 &domain_state));
5483 EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS,
5484 domain_state.sts.upgrade_mode);
5485 #if defined(OS_ANDROID)
5486 // Android's CertVerifyProc does not (yet) handle pins.
5487 #else
5488 EXPECT_TRUE(domain_state.HasPublicKeyPins());
5489 #endif
5490 EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry);
5492 EXPECT_TRUE(domain_state.sts.include_subdomains);
5493 EXPECT_FALSE(domain_state.pkp.include_subdomains);
5496 TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) {
5497 ASSERT_TRUE(test_server_.Start());
5499 TestDelegate d;
5500 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5501 test_server_.GetURL("files/content-type-normalization.html"),
5502 DEFAULT_PRIORITY, &d));
5503 req->Start();
5504 base::RunLoop().Run();
5506 std::string mime_type;
5507 req->GetMimeType(&mime_type);
5508 EXPECT_EQ("text/html", mime_type);
5510 std::string charset;
5511 req->GetCharset(&charset);
5512 EXPECT_EQ("utf-8", charset);
5513 req->Cancel();
5516 TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) {
5517 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5518 GURL data_url("data:,foo");
5519 DataProtocolHandler data_protocol_handler;
5520 EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url));
5522 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5523 EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url));
5526 #if !defined(DISABLE_FILE_SUPPORT)
5527 TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) {
5528 // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget().
5529 GURL file_url("file:///foo.txt");
5530 FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current());
5531 EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url));
5533 // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget().
5534 EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url));
5537 TEST_F(URLRequestTestHTTP, RestrictFileRedirects) {
5538 ASSERT_TRUE(test_server_.Start());
5540 TestDelegate d;
5541 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5542 test_server_.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY,
5543 &d));
5544 req->Start();
5545 base::RunLoop().Run();
5547 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5548 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error());
5550 #endif // !defined(DISABLE_FILE_SUPPORT)
5552 TEST_F(URLRequestTestHTTP, RestrictDataRedirects) {
5553 ASSERT_TRUE(test_server_.Start());
5555 TestDelegate d;
5556 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5557 test_server_.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY,
5558 &d));
5559 req->Start();
5560 base::MessageLoop::current()->Run();
5562 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5563 EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error());
5566 TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) {
5567 ASSERT_TRUE(test_server_.Start());
5569 TestDelegate d;
5570 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5571 test_server_.GetURL("files/redirect-to-invalid-url.html"),
5572 DEFAULT_PRIORITY, &d));
5573 req->Start();
5574 base::RunLoop().Run();
5576 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
5577 EXPECT_EQ(ERR_INVALID_URL, req->status().error());
5580 // Make sure redirects are cached, despite not reading their bodies.
5581 TEST_F(URLRequestTestHTTP, CacheRedirect) {
5582 ASSERT_TRUE(test_server_.Start());
5583 GURL redirect_url =
5584 test_server_.GetURL("files/redirect302-to-echo-cacheable");
5587 TestDelegate d;
5588 scoped_ptr<URLRequest> req(
5589 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
5590 req->Start();
5591 base::RunLoop().Run();
5592 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5593 EXPECT_EQ(1, d.received_redirect_count());
5594 EXPECT_EQ(test_server_.GetURL("echo"), req->url());
5598 TestDelegate d;
5599 d.set_quit_on_redirect(true);
5600 scoped_ptr<URLRequest> req(
5601 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
5602 req->Start();
5603 base::RunLoop().Run();
5605 EXPECT_EQ(1, d.received_redirect_count());
5606 EXPECT_EQ(0, d.response_started_count());
5607 EXPECT_TRUE(req->was_cached());
5609 req->FollowDeferredRedirect();
5610 base::RunLoop().Run();
5611 EXPECT_EQ(1, d.received_redirect_count());
5612 EXPECT_EQ(1, d.response_started_count());
5613 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5614 EXPECT_EQ(test_server_.GetURL("echo"), req->url());
5618 // Make sure a request isn't cached when a NetworkDelegate forces a redirect
5619 // when the headers are read, since the body won't have been read.
5620 TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) {
5621 ASSERT_TRUE(test_server_.Start());
5622 // URL that is normally cached.
5623 GURL initial_url = test_server_.GetURL("cachetime");
5626 // Set up the TestNetworkDelegate tp force a redirect.
5627 GURL redirect_to_url = test_server_.GetURL("echo");
5628 default_network_delegate_.set_redirect_on_headers_received_url(
5629 redirect_to_url);
5631 TestDelegate d;
5632 scoped_ptr<URLRequest> req(
5633 default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d));
5634 req->Start();
5635 base::RunLoop().Run();
5636 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5637 EXPECT_EQ(1, d.received_redirect_count());
5638 EXPECT_EQ(redirect_to_url, req->url());
5642 TestDelegate d;
5643 scoped_ptr<URLRequest> req(
5644 default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d));
5645 req->Start();
5646 base::RunLoop().Run();
5648 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5649 EXPECT_FALSE(req->was_cached());
5650 EXPECT_EQ(0, d.received_redirect_count());
5651 EXPECT_EQ(initial_url, req->url());
5655 // Tests that redirection to an unsafe URL is allowed when it has been marked as
5656 // safe.
5657 TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) {
5658 ASSERT_TRUE(test_server_.Start());
5660 GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url");
5661 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5662 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5664 TestDelegate d;
5666 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5667 test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d));
5669 r->Start();
5670 base::RunLoop().Run();
5672 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5674 EXPECT_EQ(2U, r->url_chain().size());
5675 EXPECT_EQ(OK, r->status().error());
5676 EXPECT_EQ(unsafe_url, r->url());
5677 EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received());
5681 // Tests that a redirect to a different unsafe URL is blocked, even after adding
5682 // some other URL to the whitelist.
5683 TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) {
5684 ASSERT_TRUE(test_server_.Start());
5686 GURL unsafe_url("data:text/html,something");
5687 GURL different_unsafe_url("data:text/html,something-else");
5688 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5689 default_network_delegate_.set_allowed_unsafe_redirect_url(
5690 different_unsafe_url);
5692 TestDelegate d;
5694 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
5695 test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d));
5697 r->Start();
5698 base::RunLoop().Run();
5700 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
5701 EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error());
5705 // Redirects from an URL with fragment to an unsafe URL with fragment should
5706 // be allowed, and the reference fragment of the target URL should be preserved.
5707 TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) {
5708 ASSERT_TRUE(test_server_.Start());
5710 GURL original_url(test_server_.GetURL("original#fragment1"));
5711 GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5712 GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2");
5714 default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url);
5715 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5717 TestDelegate d;
5719 scoped_ptr<URLRequest> r(
5720 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
5722 r->Start();
5723 base::RunLoop().Run();
5725 EXPECT_EQ(2U, r->url_chain().size());
5726 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5727 EXPECT_EQ(OK, r->status().error());
5728 EXPECT_EQ(original_url, r->original_url());
5729 EXPECT_EQ(expected_url, r->url());
5733 // When a delegate has specified a safe redirect URL, but it does not match the
5734 // redirect target, then do not prevent the reference fragment from being added.
5735 TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) {
5736 ASSERT_TRUE(test_server_.Start());
5738 GURL original_url(test_server_.GetURL("original#expected-fragment"));
5739 GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url");
5740 GURL redirect_url(test_server_.GetURL("target"));
5741 GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment"));
5743 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5744 default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url);
5746 TestDelegate d;
5748 scoped_ptr<URLRequest> r(
5749 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
5751 r->Start();
5752 base::RunLoop().Run();
5754 EXPECT_EQ(2U, r->url_chain().size());
5755 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5756 EXPECT_EQ(OK, r->status().error());
5757 EXPECT_EQ(original_url, r->original_url());
5758 EXPECT_EQ(expected_redirect_url, r->url());
5762 // When a delegate has specified a safe redirect URL, assume that the redirect
5763 // URL should not be changed. In particular, the reference fragment should not
5764 // be modified.
5765 TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) {
5766 ASSERT_TRUE(test_server_.Start());
5768 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5769 GURL redirect_url("data:text/html,expect-no-reference-fragment");
5771 default_network_delegate_.set_redirect_on_headers_received_url(redirect_url);
5772 default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url);
5774 TestDelegate d;
5776 scoped_ptr<URLRequest> r(
5777 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
5779 r->Start();
5780 base::RunLoop().Run();
5782 EXPECT_EQ(2U, r->url_chain().size());
5783 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5784 EXPECT_EQ(OK, r->status().error());
5785 EXPECT_EQ(original_url, r->original_url());
5786 EXPECT_EQ(redirect_url, r->url());
5790 // When a URLRequestRedirectJob is created, the redirection must be followed and
5791 // the reference fragment of the target URL must not be modified.
5792 TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) {
5793 ASSERT_TRUE(test_server_.Start());
5795 GURL original_url(test_server_.GetURL("original#should-not-be-appended"));
5796 GURL redirect_url(test_server_.GetURL("echo"));
5798 TestDelegate d;
5799 scoped_ptr<URLRequest> r(
5800 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
5802 URLRequestRedirectJob* job = new URLRequestRedirectJob(
5803 r.get(), &default_network_delegate_, redirect_url,
5804 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
5805 AddTestInterceptor()->set_main_intercept_job(job);
5807 r->Start();
5808 base::RunLoop().Run();
5810 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
5811 EXPECT_EQ(OK, r->status().error());
5812 EXPECT_EQ(original_url, r->original_url());
5813 EXPECT_EQ(redirect_url, r->url());
5816 TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) {
5817 ASSERT_TRUE(test_server_.Start());
5819 TestDelegate d;
5820 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5821 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
5822 req->SetReferrer("http://user:pass@foo.com/");
5823 req->Start();
5824 base::RunLoop().Run();
5826 EXPECT_EQ(std::string("http://foo.com/"), d.data_received());
5829 TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) {
5830 ASSERT_TRUE(test_server_.Start());
5832 TestDelegate d;
5833 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5834 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
5835 req->SetReferrer("http://foo.com/test#fragment");
5836 req->Start();
5837 base::RunLoop().Run();
5839 EXPECT_EQ(std::string("http://foo.com/test"), d.data_received());
5842 TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) {
5843 ASSERT_TRUE(test_server_.Start());
5845 TestDelegate d;
5846 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5847 test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d));
5848 req->SetReferrer("http://foo.com/test#fragment");
5849 req->SetReferrer("");
5850 req->Start();
5851 base::RunLoop().Run();
5853 EXPECT_EQ(std::string("None"), d.data_received());
5856 // Defer network start and then resume, checking that the request was a success
5857 // and bytes were received.
5858 TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) {
5859 ASSERT_TRUE(test_server_.Start());
5861 TestDelegate d;
5863 d.set_quit_on_network_start(true);
5864 GURL test_url(test_server_.GetURL("echo"));
5865 scoped_ptr<URLRequest> req(
5866 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
5868 req->Start();
5869 base::RunLoop().Run();
5871 EXPECT_EQ(1, d.received_before_network_start_count());
5872 EXPECT_EQ(0, d.response_started_count());
5874 req->ResumeNetworkStart();
5875 base::RunLoop().Run();
5877 EXPECT_EQ(1, d.response_started_count());
5878 EXPECT_NE(0, d.bytes_received());
5879 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5883 // Check that OnBeforeNetworkStart is only called once even if there is a
5884 // redirect.
5885 TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) {
5886 ASSERT_TRUE(test_server_.Start());
5888 TestDelegate d;
5890 d.set_quit_on_redirect(true);
5891 d.set_quit_on_network_start(true);
5892 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5893 test_server_.GetURL("server-redirect?echo"), DEFAULT_PRIORITY, &d));
5895 req->Start();
5896 base::RunLoop().Run();
5898 EXPECT_EQ(1, d.received_before_network_start_count());
5899 EXPECT_EQ(0, d.response_started_count());
5900 EXPECT_EQ(0, d.received_redirect_count());
5902 req->ResumeNetworkStart();
5903 base::RunLoop().Run();
5905 EXPECT_EQ(1, d.received_redirect_count());
5906 req->FollowDeferredRedirect();
5907 base::RunLoop().Run();
5909 // Check that the redirect's new network transaction does not get propagated
5910 // to a second OnBeforeNetworkStart() notification.
5911 EXPECT_EQ(1, d.received_before_network_start_count());
5913 EXPECT_EQ(1, d.response_started_count());
5914 EXPECT_NE(0, d.bytes_received());
5915 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5919 // Cancel the request after learning that the request would use the network.
5920 TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) {
5921 ASSERT_TRUE(test_server_.Start());
5923 TestDelegate d;
5925 d.set_quit_on_network_start(true);
5926 GURL test_url(test_server_.GetURL("echo"));
5927 scoped_ptr<URLRequest> req(
5928 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
5930 req->Start();
5931 base::RunLoop().Run();
5933 EXPECT_EQ(1, d.received_before_network_start_count());
5934 EXPECT_EQ(0, d.response_started_count());
5936 req->Cancel();
5937 base::RunLoop().Run();
5939 EXPECT_EQ(1, d.response_started_count());
5940 EXPECT_EQ(0, d.bytes_received());
5941 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
5945 TEST_F(URLRequestTestHTTP, CancelRedirect) {
5946 ASSERT_TRUE(test_server_.Start());
5948 TestDelegate d;
5950 d.set_cancel_in_received_redirect(true);
5951 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
5952 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d));
5953 req->Start();
5954 base::RunLoop().Run();
5956 EXPECT_EQ(1, d.response_started_count());
5957 EXPECT_EQ(0, d.bytes_received());
5958 EXPECT_FALSE(d.received_data_before_response());
5959 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
5963 TEST_F(URLRequestTestHTTP, DeferredRedirect) {
5964 ASSERT_TRUE(test_server_.Start());
5966 TestDelegate d;
5968 d.set_quit_on_redirect(true);
5969 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
5970 scoped_ptr<URLRequest> req(
5971 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
5973 req->Start();
5974 base::RunLoop().Run();
5976 EXPECT_EQ(1, d.received_redirect_count());
5978 req->FollowDeferredRedirect();
5979 base::RunLoop().Run();
5981 EXPECT_EQ(1, d.response_started_count());
5982 EXPECT_FALSE(d.received_data_before_response());
5983 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
5985 base::FilePath path;
5986 PathService::Get(base::DIR_SOURCE_ROOT, &path);
5987 path = path.Append(FILE_PATH_LITERAL("net"));
5988 path = path.Append(FILE_PATH_LITERAL("data"));
5989 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
5990 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
5992 std::string contents;
5993 EXPECT_TRUE(base::ReadFileToString(path, &contents));
5994 EXPECT_EQ(contents, d.data_received());
5998 TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) {
5999 ASSERT_TRUE(test_server_.Start());
6001 TestDelegate d;
6003 d.set_quit_on_redirect(true);
6004 GURL test_url(test_server_.GetURL("files/redirect-test.html"));
6005 scoped_ptr<URLRequest> req(
6006 default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d));
6008 EXPECT_FALSE(d.have_full_request_headers());
6010 req->Start();
6011 base::RunLoop().Run();
6013 EXPECT_EQ(1, d.received_redirect_count());
6014 EXPECT_TRUE(d.have_full_request_headers());
6015 CheckFullRequestHeaders(d.full_request_headers(), test_url);
6016 d.ClearFullRequestHeaders();
6018 req->FollowDeferredRedirect();
6019 base::RunLoop().Run();
6021 GURL target_url(test_server_.GetURL("files/with-headers.html"));
6022 EXPECT_EQ(1, d.response_started_count());
6023 EXPECT_TRUE(d.have_full_request_headers());
6024 CheckFullRequestHeaders(d.full_request_headers(), target_url);
6025 EXPECT_FALSE(d.received_data_before_response());
6026 EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status());
6028 base::FilePath path;
6029 PathService::Get(base::DIR_SOURCE_ROOT, &path);
6030 path = path.Append(FILE_PATH_LITERAL("net"));
6031 path = path.Append(FILE_PATH_LITERAL("data"));
6032 path = path.Append(FILE_PATH_LITERAL("url_request_unittest"));
6033 path = path.Append(FILE_PATH_LITERAL("with-headers.html"));
6035 std::string contents;
6036 EXPECT_TRUE(base::ReadFileToString(path, &contents));
6037 EXPECT_EQ(contents, d.data_received());
6041 TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) {
6042 ASSERT_TRUE(test_server_.Start());
6044 TestDelegate d;
6046 d.set_quit_on_redirect(true);
6047 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6048 test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d));
6049 req->Start();
6050 base::RunLoop().Run();
6052 EXPECT_EQ(1, d.received_redirect_count());
6054 req->Cancel();
6055 base::RunLoop().Run();
6057 EXPECT_EQ(1, d.response_started_count());
6058 EXPECT_EQ(0, d.bytes_received());
6059 EXPECT_FALSE(d.received_data_before_response());
6060 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
6064 TEST_F(URLRequestTestHTTP, VaryHeader) {
6065 ASSERT_TRUE(test_server_.Start());
6067 // Populate the cache.
6069 TestDelegate d;
6070 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6071 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d));
6072 HttpRequestHeaders headers;
6073 headers.SetHeader("foo", "1");
6074 req->SetExtraRequestHeaders(headers);
6075 req->Start();
6076 base::RunLoop().Run();
6078 LoadTimingInfo load_timing_info;
6079 req->GetLoadTimingInfo(&load_timing_info);
6080 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
6083 // Expect a cache hit.
6085 TestDelegate d;
6086 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6087 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d));
6088 HttpRequestHeaders headers;
6089 headers.SetHeader("foo", "1");
6090 req->SetExtraRequestHeaders(headers);
6091 req->Start();
6092 base::RunLoop().Run();
6094 EXPECT_TRUE(req->was_cached());
6096 LoadTimingInfo load_timing_info;
6097 req->GetLoadTimingInfo(&load_timing_info);
6098 TestLoadTimingCacheHitNoNetwork(load_timing_info);
6101 // Expect a cache miss.
6103 TestDelegate d;
6104 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6105 test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d));
6106 HttpRequestHeaders headers;
6107 headers.SetHeader("foo", "2");
6108 req->SetExtraRequestHeaders(headers);
6109 req->Start();
6110 base::RunLoop().Run();
6112 EXPECT_FALSE(req->was_cached());
6114 LoadTimingInfo load_timing_info;
6115 req->GetLoadTimingInfo(&load_timing_info);
6116 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
6120 TEST_F(URLRequestTestHTTP, BasicAuth) {
6121 ASSERT_TRUE(test_server_.Start());
6123 // populate the cache
6125 TestDelegate d;
6126 d.set_credentials(AuthCredentials(kUser, kSecret));
6128 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6129 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
6130 r->Start();
6132 base::RunLoop().Run();
6134 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6137 // repeat request with end-to-end validation. since auth-basic results in a
6138 // cachable page, we expect this test to result in a 304. in which case, the
6139 // response should be fetched from the cache.
6141 TestDelegate d;
6142 d.set_credentials(AuthCredentials(kUser, kSecret));
6144 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6145 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
6146 r->SetLoadFlags(LOAD_VALIDATE_CACHE);
6147 r->Start();
6149 base::RunLoop().Run();
6151 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6153 // Should be the same cached document.
6154 EXPECT_TRUE(r->was_cached());
6158 // Check that Set-Cookie headers in 401 responses are respected.
6159 // http://crbug.com/6450
6160 TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) {
6161 ASSERT_TRUE(test_server_.Start());
6163 GURL url_requiring_auth =
6164 test_server_.GetURL("auth-basic?set-cookie-if-challenged");
6166 // Request a page that will give a 401 containing a Set-Cookie header.
6167 // Verify that when the transaction is restarted, it includes the new cookie.
6169 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6170 TestURLRequestContext context(true);
6171 context.set_network_delegate(&network_delegate);
6172 context.Init();
6174 TestDelegate d;
6175 d.set_credentials(AuthCredentials(kUser, kSecret));
6177 scoped_ptr<URLRequest> r(
6178 context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d));
6179 r->Start();
6181 base::RunLoop().Run();
6183 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6185 // Make sure we sent the cookie in the restarted transaction.
6186 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
6187 != std::string::npos);
6190 // Same test as above, except this time the restart is initiated earlier
6191 // (without user intervention since identity is embedded in the URL).
6193 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
6194 TestURLRequestContext context(true);
6195 context.set_network_delegate(&network_delegate);
6196 context.Init();
6198 TestDelegate d;
6200 GURL::Replacements replacements;
6201 replacements.SetUsernameStr("user2");
6202 replacements.SetPasswordStr("secret");
6203 GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements);
6205 scoped_ptr<URLRequest> r(
6206 context.CreateRequest(url_with_identity, DEFAULT_PRIORITY, &d));
6207 r->Start();
6209 base::RunLoop().Run();
6211 EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos);
6213 // Make sure we sent the cookie in the restarted transaction.
6214 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true")
6215 != std::string::npos);
6219 // Tests that load timing works as expected with auth and the cache.
6220 TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) {
6221 ASSERT_TRUE(test_server_.Start());
6223 // populate the cache
6225 TestDelegate d;
6226 d.set_credentials(AuthCredentials(kUser, kSecret));
6228 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6229 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
6230 r->Start();
6232 base::RunLoop().Run();
6234 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6236 LoadTimingInfo load_timing_info_before_auth;
6237 EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth(
6238 &load_timing_info_before_auth));
6239 TestLoadTimingNotReused(load_timing_info_before_auth,
6240 CONNECT_TIMING_HAS_DNS_TIMES);
6242 LoadTimingInfo load_timing_info;
6243 r->GetLoadTimingInfo(&load_timing_info);
6244 // The test server does not support keep alive sockets, so the second
6245 // request with auth should use a new socket.
6246 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
6247 EXPECT_NE(load_timing_info_before_auth.socket_log_id,
6248 load_timing_info.socket_log_id);
6249 EXPECT_LE(load_timing_info_before_auth.receive_headers_end,
6250 load_timing_info.connect_timing.connect_start);
6253 // Repeat request with end-to-end validation. Since auth-basic results in a
6254 // cachable page, we expect this test to result in a 304. In which case, the
6255 // response should be fetched from the cache.
6257 TestDelegate d;
6258 d.set_credentials(AuthCredentials(kUser, kSecret));
6260 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
6261 test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d));
6262 r->SetLoadFlags(LOAD_VALIDATE_CACHE);
6263 r->Start();
6265 base::RunLoop().Run();
6267 EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos);
6269 // Should be the same cached document.
6270 EXPECT_TRUE(r->was_cached());
6272 // Since there was a request that went over the wire, the load timing
6273 // information should include connection times.
6274 LoadTimingInfo load_timing_info;
6275 r->GetLoadTimingInfo(&load_timing_info);
6276 TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES);
6280 // In this test, we do a POST which the server will 302 redirect.
6281 // The subsequent transaction should use GET, and should not send the
6282 // Content-Type header.
6283 // http://code.google.com/p/chromium/issues/detail?id=843
6284 TEST_F(URLRequestTestHTTP, Post302RedirectGet) {
6285 ASSERT_TRUE(test_server_.Start());
6287 const char kData[] = "hello world";
6289 TestDelegate d;
6290 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6291 test_server_.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY, &d));
6292 req->set_method("POST");
6293 req->set_upload(CreateSimpleUploadData(kData));
6295 // Set headers (some of which are specific to the POST).
6296 HttpRequestHeaders headers;
6297 headers.AddHeadersFromString(
6298 "Content-Type: multipart/form-data; "
6299 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n"
6300 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,"
6301 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n"
6302 "Accept-Language: en-US,en\r\n"
6303 "Accept-Charset: ISO-8859-1,*,utf-8\r\n"
6304 "Content-Length: 11\r\n"
6305 "Origin: http://localhost:1337/");
6306 req->SetExtraRequestHeaders(headers);
6307 req->Start();
6308 base::RunLoop().Run();
6310 std::string mime_type;
6311 req->GetMimeType(&mime_type);
6312 EXPECT_EQ("text/html", mime_type);
6314 const std::string& data = d.data_received();
6316 // Check that the post-specific headers were stripped:
6317 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
6318 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
6319 EXPECT_FALSE(ContainsString(data, "Origin:"));
6321 // These extra request headers should not have been stripped.
6322 EXPECT_TRUE(ContainsString(data, "Accept:"));
6323 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
6324 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
6327 // The following tests check that we handle mutating the request for HTTP
6328 // redirects as expected.
6329 // See https://crbug.com/56373, https://crbug.com/102130, and
6330 // https://crbug.com/465517.
6332 TEST_F(URLRequestTestHTTP, Redirect301Tests) {
6333 ASSERT_TRUE(test_server_.Start());
6335 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
6336 const GURL https_redirect_url =
6337 test_server_.GetURL("files/redirect301-to-https");
6339 HTTPRedirectMethodTest(url, "POST", "GET", true);
6340 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6341 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6343 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6344 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6345 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6346 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6347 std::string());
6350 TEST_F(URLRequestTestHTTP, Redirect302Tests) {
6351 ASSERT_TRUE(test_server_.Start());
6353 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
6354 const GURL https_redirect_url =
6355 test_server_.GetURL("files/redirect302-to-https");
6357 HTTPRedirectMethodTest(url, "POST", "GET", true);
6358 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6359 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6361 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6362 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6363 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6364 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6365 std::string());
6368 TEST_F(URLRequestTestHTTP, Redirect303Tests) {
6369 ASSERT_TRUE(test_server_.Start());
6371 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
6372 const GURL https_redirect_url =
6373 test_server_.GetURL("files/redirect303-to-https");
6375 HTTPRedirectMethodTest(url, "POST", "GET", true);
6376 HTTPRedirectMethodTest(url, "PUT", "GET", true);
6377 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6379 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6380 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6381 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6382 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6383 std::string());
6386 TEST_F(URLRequestTestHTTP, Redirect307Tests) {
6387 ASSERT_TRUE(test_server_.Start());
6389 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
6390 const GURL https_redirect_url =
6391 test_server_.GetURL("files/redirect307-to-https");
6393 HTTPRedirectMethodTest(url, "POST", "POST", true);
6394 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6395 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6397 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6398 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6399 HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec());
6400 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null");
6403 TEST_F(URLRequestTestHTTP, Redirect308Tests) {
6404 ASSERT_TRUE(test_server_.Start());
6406 const GURL url = test_server_.GetURL("files/redirect308-to-echo");
6407 const GURL https_redirect_url =
6408 test_server_.GetURL("files/redirect308-to-https");
6410 HTTPRedirectMethodTest(url, "POST", "POST", true);
6411 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6412 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6414 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6415 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6416 HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec());
6417 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null");
6420 // Make sure that 308 responses without bodies are not treated as redirects.
6421 // Certain legacy apis that pre-date the response code expect this behavior
6422 // (Like Google Drive).
6423 TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) {
6424 ASSERT_TRUE(test_server_.Start());
6426 TestDelegate d;
6427 const GURL url = test_server_.GetURL("files/308-without-location-header");
6429 scoped_ptr<URLRequest> request(
6430 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d));
6432 request->Start();
6433 base::RunLoop().Run();
6434 EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status());
6435 EXPECT_EQ(OK, request->status().error());
6436 EXPECT_EQ(0, d.received_redirect_count());
6437 EXPECT_EQ(308, request->response_headers()->response_code());
6438 EXPECT_EQ("This is not a redirect.", d.data_received());
6441 TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) {
6442 ASSERT_TRUE(test_server_.Start());
6444 GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment"));
6445 GURL expected_url(test_server_.GetURL("echo#fragment"));
6447 TestDelegate d;
6449 scoped_ptr<URLRequest> r(
6450 default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d));
6452 r->Start();
6453 base::RunLoop().Run();
6455 EXPECT_EQ(2U, r->url_chain().size());
6456 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6457 EXPECT_EQ(OK, r->status().error());
6458 EXPECT_EQ(original_url, r->original_url());
6459 EXPECT_EQ(expected_url, r->url());
6463 TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) {
6464 ASSERT_TRUE(test_server_.Start());
6466 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6467 GURL first_party_url("http://example.com");
6469 TestDelegate d;
6471 scoped_ptr<URLRequest> r(
6472 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d));
6473 r->set_first_party_for_cookies(first_party_url);
6475 r->Start();
6476 base::RunLoop().Run();
6478 EXPECT_EQ(2U, r->url_chain().size());
6479 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6480 EXPECT_EQ(OK, r->status().error());
6481 EXPECT_EQ(first_party_url, r->first_party_for_cookies());
6485 TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) {
6486 ASSERT_TRUE(test_server_.Start());
6488 GURL url(test_server_.GetURL("files/redirect302-to-echo"));
6489 GURL original_first_party_url("http://example.com");
6490 GURL expected_first_party_url(test_server_.GetURL("echo"));
6492 TestDelegate d;
6494 scoped_ptr<URLRequest> r(
6495 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d));
6496 r->set_first_party_for_cookies(original_first_party_url);
6497 r->set_first_party_url_policy(
6498 URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT);
6500 r->Start();
6501 base::RunLoop().Run();
6503 EXPECT_EQ(2U, r->url_chain().size());
6504 EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status());
6505 EXPECT_EQ(OK, r->status().error());
6506 EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies());
6510 TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) {
6511 ASSERT_TRUE(test_server_.Start());
6513 const char kData[] = "hello world";
6515 TestDelegate d;
6516 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6517 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d));
6518 req->set_method("POST");
6519 req->set_upload(CreateSimpleUploadData(kData));
6520 HttpRequestHeaders headers;
6521 headers.SetHeader(HttpRequestHeaders::kContentLength,
6522 base::UintToString(arraysize(kData) - 1));
6523 req->SetExtraRequestHeaders(headers);
6525 URLRequestRedirectJob* job = new URLRequestRedirectJob(
6526 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
6527 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
6528 AddTestInterceptor()->set_main_intercept_job(job);
6530 req->Start();
6531 base::RunLoop().Run();
6532 EXPECT_EQ("GET", req->method());
6535 TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) {
6536 ASSERT_TRUE(test_server_.Start());
6538 const char kData[] = "hello world";
6540 TestDelegate d;
6541 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6542 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d));
6543 req->set_method("POST");
6544 req->set_upload(CreateSimpleUploadData(kData));
6545 HttpRequestHeaders headers;
6546 headers.SetHeader(HttpRequestHeaders::kContentLength,
6547 base::UintToString(arraysize(kData) - 1));
6548 req->SetExtraRequestHeaders(headers);
6550 URLRequestRedirectJob* job = new URLRequestRedirectJob(
6551 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
6552 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT,
6553 "Very Good Reason");
6554 AddTestInterceptor()->set_main_intercept_job(job);
6556 req->Start();
6557 base::RunLoop().Run();
6558 EXPECT_EQ("POST", req->method());
6559 EXPECT_EQ(kData, d.data_received());
6562 // Check that default A-L header is sent.
6563 TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) {
6564 ASSERT_TRUE(test_server_.Start());
6566 StaticHttpUserAgentSettings settings("en", std::string());
6567 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
6568 TestURLRequestContext context(true);
6569 context.set_network_delegate(&network_delegate);
6570 context.set_http_user_agent_settings(&settings);
6571 context.Init();
6573 TestDelegate d;
6574 scoped_ptr<URLRequest> req(context.CreateRequest(
6575 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d));
6576 req->Start();
6577 base::RunLoop().Run();
6578 EXPECT_EQ("en", d.data_received());
6581 // Check that an empty A-L header is not sent. http://crbug.com/77365.
6582 TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) {
6583 ASSERT_TRUE(test_server_.Start());
6585 std::string empty_string; // Avoid most vexing parse on line below.
6586 StaticHttpUserAgentSettings settings(empty_string, empty_string);
6587 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
6588 TestURLRequestContext context(true);
6589 context.set_network_delegate(&network_delegate);
6590 context.Init();
6591 // We override the language after initialization because empty entries
6592 // get overridden by Init().
6593 context.set_http_user_agent_settings(&settings);
6595 TestDelegate d;
6596 scoped_ptr<URLRequest> req(context.CreateRequest(
6597 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d));
6598 req->Start();
6599 base::RunLoop().Run();
6600 EXPECT_EQ("None", d.data_received());
6603 // Check that if request overrides the A-L header, the default is not appended.
6604 // See http://crbug.com/20894
6605 TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) {
6606 ASSERT_TRUE(test_server_.Start());
6608 TestDelegate d;
6609 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6610 test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d));
6611 HttpRequestHeaders headers;
6612 headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru");
6613 req->SetExtraRequestHeaders(headers);
6614 req->Start();
6615 base::RunLoop().Run();
6616 EXPECT_EQ(std::string("ru"), d.data_received());
6619 // Check that default A-E header is sent.
6620 TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) {
6621 ASSERT_TRUE(test_server_.Start());
6623 TestDelegate d;
6624 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6625 test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d));
6626 HttpRequestHeaders headers;
6627 req->SetExtraRequestHeaders(headers);
6628 req->Start();
6629 base::RunLoop().Run();
6630 EXPECT_TRUE(ContainsString(d.data_received(), "gzip"));
6633 // Check that if request overrides the A-E header, the default is not appended.
6634 // See http://crbug.com/47381
6635 TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) {
6636 ASSERT_TRUE(test_server_.Start());
6638 TestDelegate d;
6639 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6640 test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d));
6641 HttpRequestHeaders headers;
6642 headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity");
6643 req->SetExtraRequestHeaders(headers);
6644 req->Start();
6645 base::RunLoop().Run();
6646 EXPECT_FALSE(ContainsString(d.data_received(), "gzip"));
6647 EXPECT_TRUE(ContainsString(d.data_received(), "identity"));
6650 // Check that setting the A-C header sends the proper header.
6651 TEST_F(URLRequestTestHTTP, SetAcceptCharset) {
6652 ASSERT_TRUE(test_server_.Start());
6654 TestDelegate d;
6655 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6656 test_server_.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY, &d));
6657 HttpRequestHeaders headers;
6658 headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r");
6659 req->SetExtraRequestHeaders(headers);
6660 req->Start();
6661 base::RunLoop().Run();
6662 EXPECT_EQ(std::string("koi-8r"), d.data_received());
6665 // Check that default User-Agent header is sent.
6666 TEST_F(URLRequestTestHTTP, DefaultUserAgent) {
6667 ASSERT_TRUE(test_server_.Start());
6669 TestDelegate d;
6670 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6671 test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d));
6672 req->Start();
6673 base::RunLoop().Run();
6674 EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(),
6675 d.data_received());
6678 // Check that if request overrides the User-Agent header,
6679 // the default is not appended.
6680 TEST_F(URLRequestTestHTTP, OverrideUserAgent) {
6681 ASSERT_TRUE(test_server_.Start());
6683 TestDelegate d;
6684 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6685 test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d));
6686 HttpRequestHeaders headers;
6687 headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)");
6688 req->SetExtraRequestHeaders(headers);
6689 req->Start();
6690 base::RunLoop().Run();
6691 EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received());
6694 // Check that a NULL HttpUserAgentSettings causes the corresponding empty
6695 // User-Agent header to be sent but does not send the Accept-Language and
6696 // Accept-Charset headers.
6697 TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) {
6698 ASSERT_TRUE(test_server_.Start());
6700 TestNetworkDelegate network_delegate; // Must outlive URLRequests.
6701 TestURLRequestContext context(true);
6702 context.set_network_delegate(&network_delegate);
6703 context.Init();
6704 // We override the HttpUserAgentSettings after initialization because empty
6705 // entries get overridden by Init().
6706 context.set_http_user_agent_settings(NULL);
6708 struct {
6709 const char* request;
6710 const char* expected_response;
6711 } tests[] = { { "echoheader?Accept-Language", "None" },
6712 { "echoheader?Accept-Charset", "None" },
6713 { "echoheader?User-Agent", "" } };
6715 for (size_t i = 0; i < arraysize(tests); i++) {
6716 TestDelegate d;
6717 scoped_ptr<URLRequest> req(context.CreateRequest(
6718 test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d));
6719 req->Start();
6720 base::RunLoop().Run();
6721 EXPECT_EQ(tests[i].expected_response, d.data_received())
6722 << " Request = \"" << tests[i].request << "\"";
6726 // Make sure that URLRequest passes on its priority updates to
6727 // newly-created jobs after the first one.
6728 TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) {
6729 ASSERT_TRUE(test_server_.Start());
6731 TestDelegate d;
6732 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
6733 test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d));
6734 EXPECT_EQ(DEFAULT_PRIORITY, req->priority());
6736 scoped_refptr<URLRequestRedirectJob> redirect_job =
6737 new URLRequestRedirectJob(
6738 req.get(), &default_network_delegate_, test_server_.GetURL("echo"),
6739 URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason");
6740 AddTestInterceptor()->set_main_intercept_job(redirect_job.get());
6742 req->SetPriority(LOW);
6743 req->Start();
6744 EXPECT_TRUE(req->is_pending());
6746 scoped_refptr<URLRequestTestJob> job =
6747 new URLRequestTestJob(req.get(), &default_network_delegate_);
6748 AddTestInterceptor()->set_main_intercept_job(job.get());
6750 // Should trigger |job| to be started.
6751 base::RunLoop().Run();
6752 EXPECT_EQ(LOW, job->priority());
6755 // Check that creating a network request while entering/exiting suspend mode
6756 // fails as it should. This is the only case where an HttpTransactionFactory
6757 // does not return an HttpTransaction.
6758 TEST_F(URLRequestTestHTTP, NetworkSuspendTest) {
6759 // Create a new HttpNetworkLayer that thinks it's suspended.
6760 HttpNetworkSession::Params params;
6761 params.host_resolver = default_context_.host_resolver();
6762 params.cert_verifier = default_context_.cert_verifier();
6763 params.transport_security_state = default_context_.transport_security_state();
6764 params.proxy_service = default_context_.proxy_service();
6765 params.ssl_config_service = default_context_.ssl_config_service();
6766 params.http_auth_handler_factory =
6767 default_context_.http_auth_handler_factory();
6768 params.network_delegate = &default_network_delegate_;
6769 params.http_server_properties = default_context_.http_server_properties();
6770 scoped_ptr<HttpNetworkLayer> network_layer(
6771 new HttpNetworkLayer(new HttpNetworkSession(params)));
6772 network_layer->OnSuspend();
6774 HttpCache http_cache(network_layer.release(), default_context_.net_log(),
6775 HttpCache::DefaultBackend::InMemory(0));
6777 TestURLRequestContext context(true);
6778 context.set_http_transaction_factory(&http_cache);
6779 context.Init();
6781 TestDelegate d;
6782 scoped_ptr<URLRequest> req(
6783 context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d));
6784 req->Start();
6785 base::RunLoop().Run();
6787 EXPECT_TRUE(d.request_failed());
6788 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
6789 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
6792 // Check that creating a network request while entering/exiting suspend mode
6793 // fails as it should in the case there is no cache. This is the only case
6794 // where an HttpTransactionFactory does not return an HttpTransaction.
6795 TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) {
6796 // Create a new HttpNetworkLayer that thinks it's suspended.
6797 HttpNetworkSession::Params params;
6798 params.host_resolver = default_context_.host_resolver();
6799 params.cert_verifier = default_context_.cert_verifier();
6800 params.transport_security_state = default_context_.transport_security_state();
6801 params.proxy_service = default_context_.proxy_service();
6802 params.ssl_config_service = default_context_.ssl_config_service();
6803 params.http_auth_handler_factory =
6804 default_context_.http_auth_handler_factory();
6805 params.network_delegate = &default_network_delegate_;
6806 params.http_server_properties = default_context_.http_server_properties();
6807 HttpNetworkLayer network_layer(new HttpNetworkSession(params));
6808 network_layer.OnSuspend();
6810 TestURLRequestContext context(true);
6811 context.set_http_transaction_factory(&network_layer);
6812 context.Init();
6814 TestDelegate d;
6815 scoped_ptr<URLRequest> req(
6816 context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d));
6817 req->Start();
6818 base::RunLoop().Run();
6820 EXPECT_TRUE(d.request_failed());
6821 EXPECT_EQ(URLRequestStatus::FAILED, req->status().status());
6822 EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error());
6825 class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP {
6826 public:
6827 // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture,
6828 // ideally remove the dependency on URLRequestTestJob, and maybe move these
6829 // tests into the factory tests.
6830 URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) {
6833 void SetUpFactory() override {
6834 interceptor_ = new MockURLRequestInterceptor();
6835 job_factory_.reset(new URLRequestInterceptingJobFactory(
6836 job_factory_.Pass(), make_scoped_ptr(interceptor_)));
6839 MockURLRequestInterceptor* interceptor() const {
6840 return interceptor_;
6843 private:
6844 MockURLRequestInterceptor* interceptor_;
6847 TEST_F(URLRequestInterceptorTestHTTP,
6848 NetworkDelegateNotificationOnRedirectIntercept) {
6849 interceptor()->set_intercept_redirect(true);
6850 interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers());
6851 interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data());
6853 ASSERT_TRUE(test_server()->Start());
6855 TestDelegate d;
6856 scoped_ptr<URLRequest> req(default_context().CreateRequest(
6857 test_server()->GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d));
6858 req->Start();
6859 base::RunLoop().Run();
6861 EXPECT_TRUE(interceptor()->did_intercept_redirect());
6862 // Check we got one good response
6863 EXPECT_TRUE(req->status().is_success());
6864 if (req->status().is_success())
6865 EXPECT_EQ(200, req->response_headers()->response_code());
6867 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
6868 EXPECT_EQ(1, d.response_started_count());
6869 EXPECT_EQ(0, d.received_redirect_count());
6871 EXPECT_EQ(1, default_network_delegate()->created_requests());
6872 EXPECT_EQ(1, default_network_delegate()->before_send_headers_count());
6873 EXPECT_EQ(1, default_network_delegate()->headers_received_count());
6876 TEST_F(URLRequestInterceptorTestHTTP,
6877 NetworkDelegateNotificationOnErrorIntercept) {
6878 // Intercept that error and respond with an OK response.
6879 interceptor()->set_intercept_final_response(true);
6880 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
6881 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
6882 default_network_delegate()->set_can_be_intercepted_on_error(true);
6884 ASSERT_TRUE(test_server()->Start());
6886 TestDelegate d;
6887 scoped_ptr<URLRequest> req(default_context().CreateRequest(
6888 test_server()->GetURL("files/two-content-lengths.html"), DEFAULT_PRIORITY,
6889 &d));
6890 req->set_method("GET");
6891 req->Start();
6892 base::RunLoop().Run();
6894 EXPECT_TRUE(interceptor()->did_intercept_final());
6896 // Check we received one good response.
6897 EXPECT_TRUE(req->status().is_success());
6898 if (req->status().is_success())
6899 EXPECT_EQ(200, req->response_headers()->response_code());
6900 EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received());
6901 EXPECT_EQ(1, d.response_started_count());
6902 EXPECT_EQ(0, d.received_redirect_count());
6904 EXPECT_EQ(1, default_network_delegate()->created_requests());
6905 EXPECT_EQ(1, default_network_delegate()->before_send_headers_count());
6906 EXPECT_EQ(0, default_network_delegate()->headers_received_count());
6909 TEST_F(URLRequestInterceptorTestHTTP,
6910 NetworkDelegateNotificationOnResponseIntercept) {
6911 // Intercept that error and respond with an OK response.
6912 interceptor()->set_intercept_final_response(true);
6914 // Intercept with a real URLRequestHttpJob.
6915 interceptor()->set_use_url_request_http_job(true);
6917 ASSERT_TRUE(test_server()->Start());
6919 TestDelegate d;
6920 scoped_ptr<URLRequest> req(default_context().CreateRequest(
6921 test_server()->GetURL("files/simple.html"), DEFAULT_PRIORITY, &d));
6922 req->set_method("GET");
6923 req->Start();
6924 base::RunLoop().Run();
6926 EXPECT_TRUE(interceptor()->did_intercept_final());
6928 // Check we received one good response.
6929 EXPECT_TRUE(req->status().is_success());
6930 if (req->status().is_success())
6931 EXPECT_EQ(200, req->response_headers()->response_code());
6932 EXPECT_EQ("hello", d.data_received());
6933 EXPECT_EQ(1, d.response_started_count());
6934 EXPECT_EQ(0, d.received_redirect_count());
6936 EXPECT_EQ(1, default_network_delegate()->created_requests());
6937 EXPECT_EQ(2, default_network_delegate()->before_send_headers_count());
6938 EXPECT_EQ(2, default_network_delegate()->headers_received_count());
6941 class URLRequestTestReferrerPolicy : public URLRequestTest {
6942 public:
6943 URLRequestTestReferrerPolicy() {}
6945 void InstantiateSameOriginServers(SpawnedTestServer::Type origin_type) {
6946 origin_server_.reset(new SpawnedTestServer(
6947 origin_type, SpawnedTestServer::kLocalhost,
6948 origin_type == SpawnedTestServer::TYPE_HTTPS
6949 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
6950 : base::FilePath(
6951 FILE_PATH_LITERAL("net/data/url_request_unittest"))));
6952 ASSERT_TRUE(origin_server_->Start());
6955 void InstantiateCrossOriginServers(SpawnedTestServer::Type origin_type,
6956 SpawnedTestServer::Type destination_type) {
6957 origin_server_.reset(new SpawnedTestServer(
6958 origin_type, SpawnedTestServer::kLocalhost,
6959 origin_type == SpawnedTestServer::TYPE_HTTPS
6960 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
6961 : base::FilePath(
6962 FILE_PATH_LITERAL("net/data/url_request_unittest"))));
6963 ASSERT_TRUE(origin_server_->Start());
6965 destination_server_.reset(new SpawnedTestServer(
6966 destination_type, SpawnedTestServer::kLocalhost,
6967 destination_type == SpawnedTestServer::TYPE_HTTPS
6968 ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))
6969 : base::FilePath(
6970 FILE_PATH_LITERAL("net/data/url_request_unittest"))));
6971 ASSERT_TRUE(destination_server_->Start());
6974 void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy,
6975 const GURL& referrer,
6976 const GURL& expected) {
6977 // Create and execute the request: we'll only have a |destination_server_|
6978 // if the origins are meant to be distinct. Otherwise, we'll use the
6979 // |origin_server_| for both endpoints.
6980 GURL destination_url =
6981 destination_server_ ? destination_server_->GetURL("echoheader?Referer")
6982 : origin_server_->GetURL("echoheader?Referer");
6983 GURL origin_url =
6984 origin_server_->GetURL("server-redirect?" + destination_url.spec());
6986 TestDelegate d;
6987 scoped_ptr<URLRequest> req(
6988 default_context_.CreateRequest(origin_url, DEFAULT_PRIORITY, &d));
6989 req->set_referrer_policy(policy);
6990 req->SetReferrer(referrer.spec());
6991 req->Start();
6992 base::RunLoop().Run();
6994 EXPECT_EQ(1, d.response_started_count());
6995 EXPECT_EQ(1, d.received_redirect_count());
6996 EXPECT_EQ(destination_url, req->url());
6997 EXPECT_TRUE(req->status().is_success());
6998 EXPECT_EQ(200, req->response_headers()->response_code());
7000 EXPECT_EQ(expected.spec(), req->referrer());
7001 if (expected.is_empty())
7002 EXPECT_EQ("None", d.data_received());
7003 else
7004 EXPECT_EQ(expected.spec(), d.data_received());
7007 SpawnedTestServer* origin_server() const { return origin_server_.get(); }
7009 private:
7010 scoped_ptr<SpawnedTestServer> origin_server_;
7011 scoped_ptr<SpawnedTestServer> destination_server_;
7014 TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) {
7015 InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTP);
7017 VerifyReferrerAfterRedirect(
7018 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7019 origin_server()->GetURL("path/to/file.html"),
7020 origin_server()->GetURL("path/to/file.html"));
7022 VerifyReferrerAfterRedirect(
7023 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7024 origin_server()->GetURL("path/to/file.html"),
7025 origin_server()->GetURL("path/to/file.html"));
7027 VerifyReferrerAfterRedirect(
7028 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7029 origin_server()->GetURL("path/to/file.html"),
7030 origin_server()->GetURL("path/to/file.html"));
7032 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7033 origin_server()->GetURL("path/to/file.html"),
7034 origin_server()->GetURL("path/to/file.html"));
7037 TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) {
7038 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP,
7039 SpawnedTestServer::TYPE_HTTP);
7041 VerifyReferrerAfterRedirect(
7042 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7043 origin_server()->GetURL("path/to/file.html"),
7044 origin_server()->GetURL("path/to/file.html"));
7046 VerifyReferrerAfterRedirect(
7047 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7048 origin_server()->GetURL("path/to/file.html"),
7049 origin_server()->GetURL(std::string()));
7051 VerifyReferrerAfterRedirect(
7052 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7053 origin_server()->GetURL("path/to/file.html"),
7054 origin_server()->GetURL(std::string()));
7056 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7057 origin_server()->GetURL("path/to/file.html"),
7058 origin_server()->GetURL("path/to/file.html"));
7061 TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) {
7062 InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTPS);
7064 VerifyReferrerAfterRedirect(
7065 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7066 origin_server()->GetURL("path/to/file.html"),
7067 origin_server()->GetURL("path/to/file.html"));
7069 VerifyReferrerAfterRedirect(
7070 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7071 origin_server()->GetURL("path/to/file.html"),
7072 origin_server()->GetURL("path/to/file.html"));
7074 VerifyReferrerAfterRedirect(
7075 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7076 origin_server()->GetURL("path/to/file.html"),
7077 origin_server()->GetURL("path/to/file.html"));
7079 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7080 origin_server()->GetURL("path/to/file.html"),
7081 origin_server()->GetURL("path/to/file.html"));
7084 TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) {
7085 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS,
7086 SpawnedTestServer::TYPE_HTTPS);
7088 VerifyReferrerAfterRedirect(
7089 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7090 origin_server()->GetURL("path/to/file.html"),
7091 origin_server()->GetURL("path/to/file.html"));
7093 VerifyReferrerAfterRedirect(
7094 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7095 origin_server()->GetURL("path/to/file.html"),
7096 origin_server()->GetURL(std::string()));
7098 VerifyReferrerAfterRedirect(
7099 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7100 origin_server()->GetURL("path/to/file.html"),
7101 origin_server()->GetURL(std::string()));
7103 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7104 origin_server()->GetURL("path/to/file.html"),
7105 origin_server()->GetURL("path/to/file.html"));
7108 TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) {
7109 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP,
7110 SpawnedTestServer::TYPE_HTTPS);
7112 VerifyReferrerAfterRedirect(
7113 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7114 origin_server()->GetURL("path/to/file.html"),
7115 origin_server()->GetURL("path/to/file.html"));
7117 VerifyReferrerAfterRedirect(
7118 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7119 origin_server()->GetURL("path/to/file.html"),
7120 origin_server()->GetURL(std::string()));
7122 VerifyReferrerAfterRedirect(
7123 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7124 origin_server()->GetURL("path/to/file.html"),
7125 origin_server()->GetURL(std::string()));
7127 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7128 origin_server()->GetURL("path/to/file.html"),
7129 origin_server()->GetURL("path/to/file.html"));
7132 TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) {
7133 InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS,
7134 SpawnedTestServer::TYPE_HTTP);
7136 VerifyReferrerAfterRedirect(
7137 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
7138 origin_server()->GetURL("path/to/file.html"), GURL());
7140 VerifyReferrerAfterRedirect(
7141 URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
7142 origin_server()->GetURL("path/to/file.html"), GURL());
7144 VerifyReferrerAfterRedirect(
7145 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN,
7146 origin_server()->GetURL("path/to/file.html"),
7147 origin_server()->GetURL(std::string()));
7149 VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER,
7150 origin_server()->GetURL("path/to/file.html"),
7151 origin_server()->GetURL("path/to/file.html"));
7154 class HTTPSRequestTest : public testing::Test {
7155 public:
7156 HTTPSRequestTest() : default_context_(true) {
7157 default_context_.set_network_delegate(&default_network_delegate_);
7158 default_context_.Init();
7160 ~HTTPSRequestTest() override {}
7162 protected:
7163 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
7164 TestURLRequestContext default_context_;
7167 TEST_F(HTTPSRequestTest, HTTPSGetTest) {
7168 SpawnedTestServer test_server(
7169 SpawnedTestServer::TYPE_HTTPS,
7170 SpawnedTestServer::kLocalhost,
7171 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7172 ASSERT_TRUE(test_server.Start());
7174 TestDelegate d;
7176 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7177 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
7178 r->Start();
7179 EXPECT_TRUE(r->is_pending());
7181 base::RunLoop().Run();
7183 EXPECT_EQ(1, d.response_started_count());
7184 EXPECT_FALSE(d.received_data_before_response());
7185 EXPECT_NE(0, d.bytes_received());
7186 CheckSSLInfo(r->ssl_info());
7187 EXPECT_EQ(test_server.host_port_pair().host(),
7188 r->GetSocketAddress().host());
7189 EXPECT_EQ(test_server.host_port_pair().port(),
7190 r->GetSocketAddress().port());
7194 TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) {
7195 SpawnedTestServer::SSLOptions ssl_options(
7196 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
7197 SpawnedTestServer test_server(
7198 SpawnedTestServer::TYPE_HTTPS,
7199 ssl_options,
7200 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7201 ASSERT_TRUE(test_server.Start());
7203 bool err_allowed = true;
7204 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
7205 TestDelegate d;
7207 d.set_allow_certificate_errors(err_allowed);
7208 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7209 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
7211 r->Start();
7212 EXPECT_TRUE(r->is_pending());
7214 base::RunLoop().Run();
7216 EXPECT_EQ(1, d.response_started_count());
7217 EXPECT_FALSE(d.received_data_before_response());
7218 EXPECT_TRUE(d.have_certificate_errors());
7219 if (err_allowed) {
7220 EXPECT_NE(0, d.bytes_received());
7221 CheckSSLInfo(r->ssl_info());
7222 } else {
7223 EXPECT_EQ(0, d.bytes_received());
7229 TEST_F(HTTPSRequestTest, HTTPSExpiredTest) {
7230 SpawnedTestServer::SSLOptions ssl_options(
7231 SpawnedTestServer::SSLOptions::CERT_EXPIRED);
7232 SpawnedTestServer test_server(
7233 SpawnedTestServer::TYPE_HTTPS,
7234 ssl_options,
7235 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7236 ASSERT_TRUE(test_server.Start());
7238 // Iterate from false to true, just so that we do the opposite of the
7239 // previous test in order to increase test coverage.
7240 bool err_allowed = false;
7241 for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) {
7242 TestDelegate d;
7244 d.set_allow_certificate_errors(err_allowed);
7245 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7246 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
7248 r->Start();
7249 EXPECT_TRUE(r->is_pending());
7251 base::RunLoop().Run();
7253 EXPECT_EQ(1, d.response_started_count());
7254 EXPECT_FALSE(d.received_data_before_response());
7255 EXPECT_TRUE(d.have_certificate_errors());
7256 if (err_allowed) {
7257 EXPECT_NE(0, d.bytes_received());
7258 CheckSSLInfo(r->ssl_info());
7259 } else {
7260 EXPECT_EQ(0, d.bytes_received());
7266 // Tests that servers which require a deprecated cipher suite still work.
7267 TEST_F(HTTPSRequestTest, CipherFallbackTest) {
7268 TestNetLog net_log;
7269 default_context_.set_net_log(&net_log);
7271 SpawnedTestServer::SSLOptions ssl_options;
7272 ssl_options.bulk_ciphers = SpawnedTestServer::SSLOptions::BULK_CIPHER_RC4;
7273 SpawnedTestServer test_server(
7274 SpawnedTestServer::TYPE_HTTPS, ssl_options,
7275 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7276 ASSERT_TRUE(test_server.Start());
7278 TestDelegate d;
7279 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7280 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
7281 r->Start();
7282 EXPECT_TRUE(r->is_pending());
7284 base::RunLoop().Run();
7286 EXPECT_EQ(1, d.response_started_count());
7287 EXPECT_FALSE(d.received_data_before_response());
7288 EXPECT_NE(0, d.bytes_received());
7289 CheckSSLInfo(r->ssl_info());
7290 EXPECT_EQ(test_server.host_port_pair().host(), r->GetSocketAddress().host());
7291 EXPECT_EQ(test_server.host_port_pair().port(), r->GetSocketAddress().port());
7293 // No version downgrade should have been necessary.
7294 EXPECT_FALSE(r->ssl_info().connection_status &
7295 SSL_CONNECTION_VERSION_FALLBACK);
7296 int expected_version = SSL_CONNECTION_VERSION_TLS1_2;
7297 if (SSLClientSocket::GetMaxSupportedSSLVersion() <
7298 SSL_PROTOCOL_VERSION_TLS1_2) {
7299 expected_version = SSL_CONNECTION_VERSION_TLS1_1;
7301 EXPECT_EQ(expected_version,
7302 SSLConnectionStatusToVersion(r->ssl_info().connection_status));
7304 TestNetLog::CapturedEntryList entries;
7305 net_log.GetEntries(&entries);
7306 ExpectLogContainsSomewhere(entries, 0, NetLog::TYPE_SSL_CIPHER_FALLBACK,
7307 NetLog::PHASE_NONE);
7310 // This tests that a load of www.google.com with a certificate error sets
7311 // the |certificate_errors_are_fatal| flag correctly. This flag will cause
7312 // the interstitial to be fatal.
7313 TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) {
7314 SpawnedTestServer::SSLOptions ssl_options(
7315 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
7316 SpawnedTestServer test_server(
7317 SpawnedTestServer::TYPE_HTTPS,
7318 ssl_options,
7319 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7320 ASSERT_TRUE(test_server.Start());
7322 // We require that the URL be www.google.com in order to pick up the
7323 // preloaded HSTS entries in the TransportSecurityState. This means that we
7324 // have to use a MockHostResolver in order to direct www.google.com to the
7325 // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
7327 MockHostResolver host_resolver;
7328 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
7329 TestURLRequestContext context(true);
7330 context.set_network_delegate(&network_delegate);
7331 context.set_host_resolver(&host_resolver);
7332 TransportSecurityState transport_security_state;
7333 context.set_transport_security_state(&transport_security_state);
7334 context.Init();
7336 TestDelegate d;
7337 scoped_ptr<URLRequest> r(context.CreateRequest(
7338 GURL(base::StringPrintf("https://www.google.com:%d",
7339 test_server.host_port_pair().port())),
7340 DEFAULT_PRIORITY, &d));
7342 r->Start();
7343 EXPECT_TRUE(r->is_pending());
7345 base::RunLoop().Run();
7347 EXPECT_EQ(1, d.response_started_count());
7348 EXPECT_FALSE(d.received_data_before_response());
7349 EXPECT_TRUE(d.have_certificate_errors());
7350 EXPECT_TRUE(d.certificate_errors_are_fatal());
7353 // This tests that cached HTTPS page loads do not cause any updates to the
7354 // TransportSecurityState.
7355 TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) {
7356 // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't
7357 // matter. It just has to be any error.
7358 SpawnedTestServer::SSLOptions ssl_options(
7359 SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME);
7360 SpawnedTestServer test_server(
7361 SpawnedTestServer::TYPE_HTTPS,
7362 ssl_options,
7363 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7364 ASSERT_TRUE(test_server.Start());
7366 // We require that the URL be www.google.com in order to pick up the static
7367 // and dynamic STS and PKP entries in the TransportSecurityState. This means
7368 // that we have to use a MockHostResolver in order to direct www.google.com to
7369 // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1.
7371 MockHostResolver host_resolver;
7372 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
7373 TestURLRequestContext context(true);
7374 context.set_network_delegate(&network_delegate);
7375 context.set_host_resolver(&host_resolver);
7376 TransportSecurityState transport_security_state;
7378 TransportSecurityState::DomainState static_domain_state;
7379 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
7380 "www.google.com", &static_domain_state));
7381 context.set_transport_security_state(&transport_security_state);
7382 context.Init();
7384 TransportSecurityState::DomainState dynamic_domain_state;
7385 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
7386 "www.google.com", &dynamic_domain_state));
7388 TestDelegate d;
7389 scoped_ptr<URLRequest> r(context.CreateRequest(
7390 GURL(base::StringPrintf("https://www.google.com:%d",
7391 test_server.host_port_pair().port())),
7392 DEFAULT_PRIORITY, &d));
7394 r->Start();
7395 EXPECT_TRUE(r->is_pending());
7397 base::RunLoop().Run();
7399 EXPECT_EQ(1, d.response_started_count());
7400 EXPECT_FALSE(d.received_data_before_response());
7401 EXPECT_TRUE(d.have_certificate_errors());
7402 EXPECT_TRUE(d.certificate_errors_are_fatal());
7404 // Get a fresh copy of the states, and check that they haven't changed.
7405 TransportSecurityState::DomainState new_static_domain_state;
7406 EXPECT_TRUE(transport_security_state.GetStaticDomainState(
7407 "www.google.com", &new_static_domain_state));
7408 TransportSecurityState::DomainState new_dynamic_domain_state;
7409 EXPECT_FALSE(transport_security_state.GetDynamicDomainState(
7410 "www.google.com", &new_dynamic_domain_state));
7412 EXPECT_EQ(new_static_domain_state.sts.upgrade_mode,
7413 static_domain_state.sts.upgrade_mode);
7414 EXPECT_EQ(new_static_domain_state.sts.include_subdomains,
7415 static_domain_state.sts.include_subdomains);
7416 EXPECT_EQ(new_static_domain_state.pkp.include_subdomains,
7417 static_domain_state.pkp.include_subdomains);
7418 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes,
7419 static_domain_state.pkp.spki_hashes));
7420 EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes,
7421 static_domain_state.pkp.bad_spki_hashes));
7424 // Make sure HSTS preserves a POST request's method and body.
7425 TEST_F(HTTPSRequestTest, HSTSPreservesPosts) {
7426 static const char kData[] = "hello world";
7428 SpawnedTestServer::SSLOptions ssl_options(
7429 SpawnedTestServer::SSLOptions::CERT_OK);
7430 SpawnedTestServer test_server(
7431 SpawnedTestServer::TYPE_HTTPS,
7432 ssl_options,
7433 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7434 ASSERT_TRUE(test_server.Start());
7437 // Per spec, TransportSecurityState expects a domain name, rather than an IP
7438 // address, so a MockHostResolver is needed to redirect www.somewhere.com to
7439 // the SpawnedTestServer. By default, MockHostResolver maps all hosts
7440 // to 127.0.0.1.
7441 MockHostResolver host_resolver;
7443 // Force https for www.somewhere.com.
7444 TransportSecurityState transport_security_state;
7445 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000);
7446 bool include_subdomains = false;
7447 transport_security_state.AddHSTS("www.somewhere.com", expiry,
7448 include_subdomains);
7450 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
7452 TestURLRequestContext context(true);
7453 context.set_host_resolver(&host_resolver);
7454 context.set_transport_security_state(&transport_security_state);
7455 context.set_network_delegate(&network_delegate);
7456 context.Init();
7458 TestDelegate d;
7459 // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will
7460 // cause a certificate error. Ignore the error.
7461 d.set_allow_certificate_errors(true);
7463 scoped_ptr<URLRequest> req(context.CreateRequest(
7464 GURL(base::StringPrintf("http://www.somewhere.com:%d/echo",
7465 test_server.host_port_pair().port())),
7466 DEFAULT_PRIORITY, &d));
7467 req->set_method("POST");
7468 req->set_upload(CreateSimpleUploadData(kData));
7470 req->Start();
7471 base::RunLoop().Run();
7473 EXPECT_EQ("https", req->url().scheme());
7474 EXPECT_EQ("POST", req->method());
7475 EXPECT_EQ(kData, d.data_received());
7477 LoadTimingInfo load_timing_info;
7478 network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info);
7479 // LoadTimingInfo of HSTS redirects is similar to that of network cache hits
7480 TestLoadTimingCacheHitNoNetwork(load_timing_info);
7483 // Make sure that the CORS headers are added to cross-origin HSTS redirects.
7484 TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) {
7485 static const char kOriginHeaderValue[] = "http://www.example.com";
7487 SpawnedTestServer::SSLOptions ssl_options(
7488 SpawnedTestServer::SSLOptions::CERT_OK);
7489 SpawnedTestServer test_server(
7490 SpawnedTestServer::TYPE_HTTPS,
7491 ssl_options,
7492 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7493 ASSERT_TRUE(test_server.Start());
7495 // Per spec, TransportSecurityState expects a domain name, rather than an IP
7496 // address, so a MockHostResolver is needed to redirect example.net to the
7497 // SpawnedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by default.
7498 MockHostResolver host_resolver;
7500 TransportSecurityState transport_security_state;
7501 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1);
7502 bool include_subdomains = false;
7503 transport_security_state.AddHSTS("example.net", expiry, include_subdomains);
7505 TestNetworkDelegate network_delegate; // Must outlive URLRequest.
7507 MockCertVerifier cert_verifier;
7508 cert_verifier.set_default_result(OK);
7510 TestURLRequestContext context(true);
7511 context.set_host_resolver(&host_resolver);
7512 context.set_transport_security_state(&transport_security_state);
7513 context.set_network_delegate(&network_delegate);
7514 context.set_cert_verifier(&cert_verifier);
7515 context.Init();
7517 GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite",
7518 test_server.host_port_pair().port()));
7519 url::Replacements<char> replacements;
7520 const char kNewScheme[] = "https";
7521 replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme)));
7522 GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements);
7524 TestDelegate d;
7525 // Quit on redirect to allow response header inspection upon redirect.
7526 d.set_quit_on_redirect(true);
7528 scoped_ptr<URLRequest> req(
7529 context.CreateRequest(hsts_http_url, DEFAULT_PRIORITY, &d));
7530 // Set Origin header to simulate a cross-origin request.
7531 HttpRequestHeaders request_headers;
7532 request_headers.SetHeader("Origin", kOriginHeaderValue);
7533 req->SetExtraRequestHeaders(request_headers);
7535 req->Start();
7536 base::RunLoop().Run();
7538 EXPECT_EQ(1, d.received_redirect_count());
7540 const HttpResponseHeaders* headers = req->response_headers();
7541 std::string redirect_location;
7542 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location));
7543 EXPECT_EQ(hsts_https_url.spec(), redirect_location);
7545 std::string received_cors_header;
7546 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin",
7547 &received_cors_header));
7548 EXPECT_EQ(kOriginHeaderValue, received_cors_header);
7551 // This just tests the behaviour of GetHSTSRedirect(). End-to-end tests of HSTS
7552 // are performed in net/websockets/websocket_end_to_end_test.cc.
7553 TEST(WebSocketURLRequestTest, HSTSApplied) {
7554 TestNetworkDelegate network_delegate;
7555 TransportSecurityState transport_security_state;
7556 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1);
7557 bool include_subdomains = false;
7558 transport_security_state.AddHSTS("example.net", expiry, include_subdomains);
7559 TestURLRequestContext context(true);
7560 context.set_transport_security_state(&transport_security_state);
7561 context.set_network_delegate(&network_delegate);
7562 context.Init();
7563 GURL ws_url("ws://example.net/echo");
7564 TestDelegate delegate;
7565 scoped_ptr<URLRequest> request(
7566 context.CreateRequest(ws_url, DEFAULT_PRIORITY, &delegate));
7567 EXPECT_TRUE(request->GetHSTSRedirect(&ws_url));
7568 EXPECT_TRUE(ws_url.SchemeIs("wss"));
7571 namespace {
7573 class SSLClientAuthTestDelegate : public TestDelegate {
7574 public:
7575 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
7577 void OnCertificateRequested(URLRequest* request,
7578 SSLCertRequestInfo* cert_request_info) override {
7579 on_certificate_requested_count_++;
7580 base::MessageLoop::current()->Quit();
7582 int on_certificate_requested_count() {
7583 return on_certificate_requested_count_;
7585 private:
7586 int on_certificate_requested_count_;
7589 } // namespace
7591 // TODO(davidben): Test the rest of the code. Specifically,
7592 // - Filtering which certificates to select.
7593 // - Sending a certificate back.
7594 // - Getting a certificate request in an SSL renegotiation sending the
7595 // HTTP request.
7596 TEST_F(HTTPSRequestTest, ClientAuthTest) {
7597 SpawnedTestServer::SSLOptions ssl_options;
7598 ssl_options.request_client_certificate = true;
7599 SpawnedTestServer test_server(
7600 SpawnedTestServer::TYPE_HTTPS,
7601 ssl_options,
7602 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7603 ASSERT_TRUE(test_server.Start());
7605 SSLClientAuthTestDelegate d;
7607 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7608 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
7610 r->Start();
7611 EXPECT_TRUE(r->is_pending());
7613 base::RunLoop().Run();
7615 EXPECT_EQ(1, d.on_certificate_requested_count());
7616 EXPECT_FALSE(d.received_data_before_response());
7617 EXPECT_EQ(0, d.bytes_received());
7619 // Send no certificate.
7620 // TODO(davidben): Get temporary client cert import (with keys) working on
7621 // all platforms so we can test sending a cert as well.
7622 r->ContinueWithCertificate(NULL);
7624 base::RunLoop().Run();
7626 EXPECT_EQ(1, d.response_started_count());
7627 EXPECT_FALSE(d.received_data_before_response());
7628 EXPECT_NE(0, d.bytes_received());
7632 TEST_F(HTTPSRequestTest, ResumeTest) {
7633 // Test that we attempt a session resume when making two connections to the
7634 // same host.
7635 SpawnedTestServer::SSLOptions ssl_options;
7636 ssl_options.record_resume = true;
7637 SpawnedTestServer test_server(
7638 SpawnedTestServer::TYPE_HTTPS,
7639 ssl_options,
7640 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7641 ASSERT_TRUE(test_server.Start());
7643 SSLClientSocket::ClearSessionCache();
7646 TestDelegate d;
7647 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7648 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
7650 r->Start();
7651 EXPECT_TRUE(r->is_pending());
7653 base::RunLoop().Run();
7655 EXPECT_EQ(1, d.response_started_count());
7658 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
7659 CloseAllConnections();
7662 TestDelegate d;
7663 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7664 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
7666 r->Start();
7667 EXPECT_TRUE(r->is_pending());
7669 base::RunLoop().Run();
7671 // The response will look like;
7672 // insert abc
7673 // lookup abc
7674 // insert xyz
7676 // With a newline at the end which makes the split think that there are
7677 // four lines.
7679 EXPECT_EQ(1, d.response_started_count());
7680 std::vector<std::string> lines;
7681 base::SplitString(d.data_received(), '\n', &lines);
7682 ASSERT_EQ(4u, lines.size()) << d.data_received();
7684 std::string session_id;
7686 for (size_t i = 0; i < 2; i++) {
7687 std::vector<std::string> parts;
7688 base::SplitString(lines[i], '\t', &parts);
7689 ASSERT_EQ(2u, parts.size());
7690 if (i == 0) {
7691 EXPECT_EQ("insert", parts[0]);
7692 session_id = parts[1];
7693 } else {
7694 EXPECT_EQ("lookup", parts[0]);
7695 EXPECT_EQ(session_id, parts[1]);
7701 // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be
7702 // the result of fetching "ssl-session-cache" from the test server, indicates
7703 // that exactly two different sessions were inserted, with no lookups etc.
7704 static void AssertTwoDistinctSessionsInserted(const string& session_info) {
7705 std::vector<std::string> lines;
7706 base::SplitString(session_info, '\n', &lines);
7707 ASSERT_EQ(3u, lines.size()) << session_info;
7709 std::string session_id;
7710 for (size_t i = 0; i < 2; i++) {
7711 std::vector<std::string> parts;
7712 base::SplitString(lines[i], '\t', &parts);
7713 ASSERT_EQ(2u, parts.size());
7714 EXPECT_EQ("insert", parts[0]);
7715 if (i == 0) {
7716 session_id = parts[1];
7717 } else {
7718 EXPECT_NE(session_id, parts[1]);
7723 TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) {
7724 // Test that sessions aren't resumed when the value of ssl_session_cache_shard
7725 // differs.
7726 SpawnedTestServer::SSLOptions ssl_options;
7727 ssl_options.record_resume = true;
7728 SpawnedTestServer test_server(
7729 SpawnedTestServer::TYPE_HTTPS,
7730 ssl_options,
7731 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7732 ASSERT_TRUE(test_server.Start());
7734 SSLClientSocket::ClearSessionCache();
7737 TestDelegate d;
7738 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7739 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
7741 r->Start();
7742 EXPECT_TRUE(r->is_pending());
7744 base::RunLoop().Run();
7746 EXPECT_EQ(1, d.response_started_count());
7749 // Now create a new HttpCache with a different ssl_session_cache_shard value.
7750 HttpNetworkSession::Params params;
7751 params.host_resolver = default_context_.host_resolver();
7752 params.cert_verifier = default_context_.cert_verifier();
7753 params.transport_security_state = default_context_.transport_security_state();
7754 params.proxy_service = default_context_.proxy_service();
7755 params.ssl_config_service = default_context_.ssl_config_service();
7756 params.http_auth_handler_factory =
7757 default_context_.http_auth_handler_factory();
7758 params.network_delegate = &default_network_delegate_;
7759 params.http_server_properties = default_context_.http_server_properties();
7760 params.ssl_session_cache_shard = "alternate";
7762 scoped_ptr<HttpCache> cache(new HttpCache(
7763 new HttpNetworkSession(params),
7764 HttpCache::DefaultBackend::InMemory(0)));
7766 default_context_.set_http_transaction_factory(cache.get());
7769 TestDelegate d;
7770 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7771 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
7773 r->Start();
7774 EXPECT_TRUE(r->is_pending());
7776 base::RunLoop().Run();
7778 // The response will look like;
7779 // insert abc
7780 // insert xyz
7782 // With a newline at the end which makes the split think that there are
7783 // three lines.
7785 EXPECT_EQ(1, d.response_started_count());
7786 AssertTwoDistinctSessionsInserted(d.data_received());
7790 #if defined(OS_WIN)
7792 namespace {
7794 bool IsECDSACipherSuite(uint16_t cipher_suite) {
7795 const char* key_exchange;
7796 const char* cipher;
7797 const char* mac;
7798 bool is_aead;
7799 SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite);
7800 return std::string(key_exchange).find("ECDSA") != std::string::npos;
7803 } // namespace
7805 // Test that ECDSA is disabled on Windows XP, where ECDSA certificates cannot be
7806 // verified.
7807 TEST_F(HTTPSRequestTest, DisableECDSAOnXP) {
7808 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
7809 LOG(INFO) << "Skipping test on this version.";
7810 return;
7813 SpawnedTestServer test_server(
7814 SpawnedTestServer::TYPE_HTTPS,
7815 SpawnedTestServer::kLocalhost,
7816 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7817 ASSERT_TRUE(test_server.Start());
7819 TestDelegate d;
7820 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
7821 test_server.GetURL("client-cipher-list"), DEFAULT_PRIORITY, &d));
7822 r->Start();
7823 EXPECT_TRUE(r->is_pending());
7825 base::RunLoop().Run();
7827 EXPECT_EQ(1, d.response_started_count());
7828 std::vector<std::string> lines;
7829 base::SplitString(d.data_received(), '\n', &lines);
7831 for (size_t i = 0; i < lines.size(); i++) {
7832 int cipher_suite;
7833 ASSERT_TRUE(base::StringToInt(lines[i], &cipher_suite));
7834 EXPECT_FALSE(IsECDSACipherSuite(cipher_suite))
7835 << "ClientHello advertised " << cipher_suite;
7839 #endif // OS_WIN
7841 class TestSSLConfigService : public SSLConfigService {
7842 public:
7843 TestSSLConfigService(bool ev_enabled,
7844 bool online_rev_checking,
7845 bool rev_checking_required_local_anchors)
7846 : ev_enabled_(ev_enabled),
7847 online_rev_checking_(online_rev_checking),
7848 rev_checking_required_local_anchors_(
7849 rev_checking_required_local_anchors),
7850 min_version_(kDefaultSSLVersionMin),
7851 fallback_min_version_(kDefaultSSLVersionFallbackMin) {}
7853 void set_min_version(uint16 version) {
7854 min_version_ = version;
7857 void set_fallback_min_version(uint16 version) {
7858 fallback_min_version_ = version;
7861 // SSLConfigService:
7862 void GetSSLConfig(SSLConfig* config) override {
7863 *config = SSLConfig();
7864 config->rev_checking_enabled = online_rev_checking_;
7865 config->verify_ev_cert = ev_enabled_;
7866 config->rev_checking_required_local_anchors =
7867 rev_checking_required_local_anchors_;
7868 if (fallback_min_version_) {
7869 config->version_fallback_min = fallback_min_version_;
7871 if (min_version_) {
7872 config->version_min = min_version_;
7876 protected:
7877 ~TestSSLConfigService() override {}
7879 private:
7880 const bool ev_enabled_;
7881 const bool online_rev_checking_;
7882 const bool rev_checking_required_local_anchors_;
7883 uint16 min_version_;
7884 uint16 fallback_min_version_;
7887 class FallbackTestURLRequestContext : public TestURLRequestContext {
7888 public:
7889 explicit FallbackTestURLRequestContext(bool delay_initialization)
7890 : TestURLRequestContext(delay_initialization) {}
7892 void set_fallback_min_version(uint16 version) {
7893 TestSSLConfigService *ssl_config_service =
7894 new TestSSLConfigService(true /* check for EV */,
7895 false /* online revocation checking */,
7896 false /* require rev. checking for local
7897 anchors */);
7898 ssl_config_service->set_min_version(SSL_PROTOCOL_VERSION_SSL3);
7899 ssl_config_service->set_fallback_min_version(version);
7900 set_ssl_config_service(ssl_config_service);
7904 class HTTPSFallbackTest : public testing::Test {
7905 public:
7906 HTTPSFallbackTest() : context_(true) {}
7907 ~HTTPSFallbackTest() override {}
7909 protected:
7910 void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) {
7911 DCHECK(!request_);
7912 context_.Init();
7913 delegate_.set_allow_certificate_errors(true);
7915 SpawnedTestServer test_server(
7916 SpawnedTestServer::TYPE_HTTPS,
7917 ssl_options,
7918 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
7919 ASSERT_TRUE(test_server.Start());
7921 request_ = context_.CreateRequest(test_server.GetURL(std::string()),
7922 DEFAULT_PRIORITY, &delegate_);
7923 request_->Start();
7925 base::RunLoop().Run();
7928 void set_fallback_min_version(uint16 version) {
7929 context_.set_fallback_min_version(version);
7932 void ExpectConnection(int version) {
7933 EXPECT_EQ(1, delegate_.response_started_count());
7934 EXPECT_NE(0, delegate_.bytes_received());
7935 EXPECT_EQ(version, SSLConnectionStatusToVersion(
7936 request_->ssl_info().connection_status));
7937 EXPECT_TRUE(request_->ssl_info().connection_status &
7938 SSL_CONNECTION_VERSION_FALLBACK);
7941 void ExpectFailure(int error) {
7942 EXPECT_EQ(1, delegate_.response_started_count());
7943 EXPECT_FALSE(request_->status().is_success());
7944 EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status());
7945 EXPECT_EQ(error, request_->status().error());
7948 private:
7949 TestDelegate delegate_;
7950 FallbackTestURLRequestContext context_;
7951 scoped_ptr<URLRequest> request_;
7954 // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more
7955 // than necessary.
7956 TEST_F(HTTPSFallbackTest, TLSv1Fallback) {
7957 SpawnedTestServer::SSLOptions ssl_options(
7958 SpawnedTestServer::SSLOptions::CERT_OK);
7959 ssl_options.tls_intolerant =
7960 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7962 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7963 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7966 // This test is disabled on Android because the remote test server doesn't cause
7967 // a TCP reset.
7968 #if !defined(OS_ANDROID)
7969 // Tests fallback to TLS 1.0 on connection reset.
7970 TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) {
7971 SpawnedTestServer::SSLOptions ssl_options(
7972 SpawnedTestServer::SSLOptions::CERT_OK);
7973 ssl_options.tls_intolerant =
7974 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7975 ssl_options.tls_intolerance_type =
7976 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
7978 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7979 ExpectConnection(SSL_CONNECTION_VERSION_TLS1);
7981 #endif // !OS_ANDROID
7983 // Tests that we don't fallback on handshake failure with servers that implement
7984 // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
7985 TEST_F(HTTPSFallbackTest, FallbackSCSV) {
7986 SpawnedTestServer::SSLOptions ssl_options(
7987 SpawnedTestServer::SSLOptions::CERT_OK);
7988 // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger
7989 // a version fallback.
7990 ssl_options.tls_intolerant =
7991 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
7992 // Have the server process TLS_FALLBACK_SCSV so that version fallback
7993 // connections are rejected.
7994 ssl_options.fallback_scsv_enabled = true;
7996 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
7998 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version
7999 // intolerance. If the fallback SCSV is processed when the original error
8000 // that caused the fallback should be returned, which should be
8001 // ERR_SSL_VERSION_OR_CIPHER_MISMATCH.
8002 ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH);
8005 // Tests that we don't fallback on connection closed with servers that implement
8006 // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported.
8007 TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) {
8008 SpawnedTestServer::SSLOptions ssl_options(
8009 SpawnedTestServer::SSLOptions::CERT_OK);
8010 // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger
8011 // a version fallback.
8012 ssl_options.tls_intolerant =
8013 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1;
8014 ssl_options.tls_intolerance_type =
8015 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
8016 // Have the server process TLS_FALLBACK_SCSV so that version fallback
8017 // connections are rejected.
8018 ssl_options.fallback_scsv_enabled = true;
8020 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
8022 // The original error should be replayed on rejected fallback.
8023 ExpectFailure(ERR_CONNECTION_CLOSED);
8026 // Tests that the SSLv3 fallback doesn't happen by default.
8027 TEST_F(HTTPSFallbackTest, SSLv3Fallback) {
8028 SpawnedTestServer::SSLOptions ssl_options(
8029 SpawnedTestServer::SSLOptions::CERT_OK);
8030 ssl_options.tls_intolerant =
8031 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8033 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
8034 ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH);
8037 // Tests that the SSLv3 fallback works when explicitly enabled.
8038 TEST_F(HTTPSFallbackTest, SSLv3FallbackEnabled) {
8039 SpawnedTestServer::SSLOptions ssl_options(
8040 SpawnedTestServer::SSLOptions::CERT_OK);
8041 ssl_options.tls_intolerant =
8042 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8043 set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
8045 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
8046 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
8049 // Tests that the SSLv3 fallback triggers on closed connections when explicitly
8050 // enabled.
8051 TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) {
8052 SpawnedTestServer::SSLOptions ssl_options(
8053 SpawnedTestServer::SSLOptions::CERT_OK);
8054 ssl_options.tls_intolerant =
8055 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8056 ssl_options.tls_intolerance_type =
8057 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
8058 set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
8060 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
8061 ExpectConnection(SSL_CONNECTION_VERSION_SSL3);
8064 // Test that SSLv3 fallback probe connections don't cause sessions to be cached.
8065 TEST_F(HTTPSRequestTest, SSLv3FallbackNoCache) {
8066 SpawnedTestServer::SSLOptions ssl_options(
8067 SpawnedTestServer::SSLOptions::CERT_OK);
8068 ssl_options.tls_intolerant =
8069 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8070 ssl_options.tls_intolerance_type =
8071 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE;
8072 ssl_options.record_resume = true;
8074 SpawnedTestServer test_server(
8075 SpawnedTestServer::TYPE_HTTPS,
8076 ssl_options,
8077 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8078 ASSERT_TRUE(test_server.Start());
8080 SSLClientSocket::ClearSessionCache();
8082 // Make a connection that does a probe fallback to SSLv3 but fails because
8083 // SSLv3 fallback is disabled. We don't wish a session for this connection to
8084 // be inserted locally.
8086 TestDelegate delegate;
8087 FallbackTestURLRequestContext context(true);
8089 context.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1);
8090 context.Init();
8091 scoped_ptr<URLRequest> request(context.CreateRequest(
8092 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate));
8093 request->Start();
8095 base::RunLoop().Run();
8097 EXPECT_EQ(1, delegate.response_started_count());
8098 EXPECT_FALSE(request->status().is_success());
8099 EXPECT_EQ(URLRequestStatus::FAILED, request->status().status());
8100 EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION,
8101 request->status().error());
8104 // Now allow SSLv3 connections and request the session cache log.
8106 TestDelegate delegate;
8107 FallbackTestURLRequestContext context(true);
8108 context.set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3);
8110 context.Init();
8111 scoped_ptr<URLRequest> request(context.CreateRequest(
8112 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &delegate));
8113 request->Start();
8115 base::RunLoop().Run();
8117 EXPECT_EQ(1, delegate.response_started_count());
8118 EXPECT_NE(0, delegate.bytes_received());
8119 EXPECT_EQ(SSL_CONNECTION_VERSION_SSL3, SSLConnectionStatusToVersion(
8120 request->ssl_info().connection_status));
8121 EXPECT_TRUE(request->ssl_info().connection_status &
8122 SSL_CONNECTION_VERSION_FALLBACK);
8124 std::vector<std::string> lines;
8125 // If no sessions were cached then the server should have seen two sessions
8126 // inserted with no lookups.
8127 AssertTwoDistinctSessionsInserted(delegate.data_received());
8131 // This test is disabled on Android because the remote test server doesn't cause
8132 // a TCP reset.
8133 #if !defined(OS_ANDROID)
8134 // Tests that a reset connection does not fallback down to SSL3.
8135 TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) {
8136 SpawnedTestServer::SSLOptions ssl_options(
8137 SpawnedTestServer::SSLOptions::CERT_OK);
8138 ssl_options.tls_intolerant =
8139 SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL;
8140 ssl_options.tls_intolerance_type =
8141 SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET;
8143 ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options));
8144 ExpectFailure(ERR_CONNECTION_RESET);
8146 #endif // !OS_ANDROID
8148 class HTTPSSessionTest : public testing::Test {
8149 public:
8150 HTTPSSessionTest() : default_context_(true) {
8151 cert_verifier_.set_default_result(OK);
8153 default_context_.set_network_delegate(&default_network_delegate_);
8154 default_context_.set_cert_verifier(&cert_verifier_);
8155 default_context_.Init();
8157 ~HTTPSSessionTest() override {}
8159 protected:
8160 MockCertVerifier cert_verifier_;
8161 TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest.
8162 TestURLRequestContext default_context_;
8165 // Tests that session resumption is not attempted if an invalid certificate
8166 // is presented.
8167 TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) {
8168 SpawnedTestServer::SSLOptions ssl_options;
8169 ssl_options.record_resume = true;
8170 SpawnedTestServer test_server(
8171 SpawnedTestServer::TYPE_HTTPS,
8172 ssl_options,
8173 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8174 ASSERT_TRUE(test_server.Start());
8176 SSLClientSocket::ClearSessionCache();
8178 // Simulate the certificate being expired and attempt a connection.
8179 cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID);
8181 TestDelegate d;
8182 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8183 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
8185 r->Start();
8186 EXPECT_TRUE(r->is_pending());
8188 base::RunLoop().Run();
8190 EXPECT_EQ(1, d.response_started_count());
8193 reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())->
8194 CloseAllConnections();
8196 // Now change the certificate to be acceptable (so that the response is
8197 // loaded), and ensure that no session id is presented to the peer.
8198 cert_verifier_.set_default_result(OK);
8200 TestDelegate d;
8201 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8202 test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d));
8204 r->Start();
8205 EXPECT_TRUE(r->is_pending());
8207 base::RunLoop().Run();
8209 // The response will look like;
8210 // insert abc
8211 // insert xyz
8213 // With a newline at the end which makes the split think that there are
8214 // three lines.
8216 // If a session was presented (eg: a bug), then the response would look
8217 // like;
8218 // insert abc
8219 // lookup abc
8220 // insert xyz
8222 EXPECT_EQ(1, d.response_started_count());
8223 AssertTwoDistinctSessionsInserted(d.data_received());
8227 // This the fingerprint of the "Testing CA" certificate used by the testserver.
8228 // See net/data/ssl/certificates/ocsp-test-root.pem.
8229 static const SHA1HashValue kOCSPTestCertFingerprint =
8230 { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24,
8231 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } };
8233 // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the
8234 // testserver.
8235 static const SHA256HashValue kOCSPTestCertSPKI = { {
8236 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e,
8237 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49,
8238 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74,
8239 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28,
8240 } };
8242 // This is the policy OID contained in the certificates that testserver
8243 // generates.
8244 static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1";
8246 class HTTPSOCSPTest : public HTTPSRequestTest {
8247 public:
8248 HTTPSOCSPTest()
8249 : context_(true),
8250 ev_test_policy_(
8251 new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(),
8252 kOCSPTestCertFingerprint,
8253 kOCSPTestCertPolicy)) {
8256 void SetUp() override {
8257 SetupContext(&context_);
8258 context_.Init();
8260 scoped_refptr<X509Certificate> root_cert =
8261 ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem");
8262 CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get());
8263 test_root_.reset(new ScopedTestRoot(root_cert.get()));
8265 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
8266 SetURLRequestContextForNSSHttpIO(&context_);
8267 EnsureNSSHttpIOInit();
8268 #endif
8271 void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options,
8272 CertStatus* out_cert_status) {
8273 // We always overwrite out_cert_status.
8274 *out_cert_status = 0;
8275 SpawnedTestServer test_server(
8276 SpawnedTestServer::TYPE_HTTPS,
8277 ssl_options,
8278 base::FilePath(FILE_PATH_LITERAL("net/data/ssl")));
8279 ASSERT_TRUE(test_server.Start());
8281 TestDelegate d;
8282 d.set_allow_certificate_errors(true);
8283 scoped_ptr<URLRequest> r(context_.CreateRequest(
8284 test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d));
8285 r->Start();
8287 base::RunLoop().Run();
8289 EXPECT_EQ(1, d.response_started_count());
8290 *out_cert_status = r->ssl_info().cert_status;
8293 ~HTTPSOCSPTest() override {
8294 #if defined(USE_NSS_CERTS) || defined(OS_IOS)
8295 ShutdownNSSHttpIO();
8296 #endif
8299 protected:
8300 // SetupContext configures the URLRequestContext that will be used for making
8301 // connetions to testserver. This can be overridden in test subclasses for
8302 // different behaviour.
8303 virtual void SetupContext(URLRequestContext* context) {
8304 context->set_ssl_config_service(
8305 new TestSSLConfigService(true /* check for EV */,
8306 true /* online revocation checking */,
8307 false /* require rev. checking for local
8308 anchors */));
8311 scoped_ptr<ScopedTestRoot> test_root_;
8312 TestURLRequestContext context_;
8313 scoped_ptr<ScopedTestEVPolicy> ev_test_policy_;
8316 static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() {
8317 #if defined(OS_WIN)
8318 // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't
8319 // have that ability on other platforms.
8320 return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION;
8321 #else
8322 return 0;
8323 #endif
8326 // SystemSupportsHardFailRevocationChecking returns true iff the current
8327 // operating system supports revocation checking and can distinguish between
8328 // situations where a given certificate lacks any revocation information (eg:
8329 // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when
8330 // revocation information cannot be obtained (eg: the CRL was unreachable).
8331 // If it does not, then tests which rely on 'hard fail' behaviour should be
8332 // skipped.
8333 static bool SystemSupportsHardFailRevocationChecking() {
8334 #if defined(OS_WIN) || defined(USE_NSS_CERTS) || defined(OS_IOS)
8335 return true;
8336 #else
8337 return false;
8338 #endif
8341 // SystemUsesChromiumEVMetadata returns true iff the current operating system
8342 // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then
8343 // several tests are effected because our testing EV certificate won't be
8344 // recognised as EV.
8345 static bool SystemUsesChromiumEVMetadata() {
8346 #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID)
8347 // http://crbug.com/117478 - OpenSSL does not support EV validation.
8348 return false;
8349 #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
8350 // On OS X and Android, we use the system to tell us whether a certificate is
8351 // EV or not and the system won't recognise our testing root.
8352 return false;
8353 #else
8354 return true;
8355 #endif
8358 static bool SystemSupportsOCSP() {
8359 #if defined(USE_OPENSSL_CERTS)
8360 // http://crbug.com/117478 - OpenSSL does not support OCSP.
8361 return false;
8362 #elif defined(OS_WIN)
8363 return base::win::GetVersion() >= base::win::VERSION_VISTA;
8364 #elif defined(OS_ANDROID)
8365 // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported.
8366 return false;
8367 #else
8368 return true;
8369 #endif
8372 static bool SystemSupportsOCSPStapling() {
8373 #if defined(USE_NSS_CERTS)
8374 return true;
8375 #elif defined(OS_WIN)
8376 return base::win::GetVersion() >= base::win::VERSION_VISTA;
8377 #else
8378 return false;
8379 #endif
8382 TEST_F(HTTPSOCSPTest, Valid) {
8383 if (!SystemSupportsOCSP()) {
8384 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8385 return;
8388 SpawnedTestServer::SSLOptions ssl_options(
8389 SpawnedTestServer::SSLOptions::CERT_AUTO);
8390 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
8392 CertStatus cert_status;
8393 DoConnection(ssl_options, &cert_status);
8395 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8397 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8398 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
8400 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8403 TEST_F(HTTPSOCSPTest, Revoked) {
8404 if (!SystemSupportsOCSP()) {
8405 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8406 return;
8409 SpawnedTestServer::SSLOptions ssl_options(
8410 SpawnedTestServer::SSLOptions::CERT_AUTO);
8411 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
8413 CertStatus cert_status;
8414 DoConnection(ssl_options, &cert_status);
8416 #if !(defined(OS_MACOSX) && !defined(OS_IOS))
8417 // Doesn't pass on OS X yet for reasons that need to be investigated.
8418 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
8419 #endif
8420 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8421 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8424 TEST_F(HTTPSOCSPTest, Invalid) {
8425 if (!SystemSupportsOCSP()) {
8426 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8427 return;
8430 SpawnedTestServer::SSLOptions ssl_options(
8431 SpawnedTestServer::SSLOptions::CERT_AUTO);
8432 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8434 CertStatus cert_status;
8435 DoConnection(ssl_options, &cert_status);
8437 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8438 cert_status & CERT_STATUS_ALL_ERRORS);
8440 // Without a positive OCSP response, we shouldn't show the EV status.
8441 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8442 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8445 TEST_F(HTTPSOCSPTest, ValidStapled) {
8446 if (!SystemSupportsOCSPStapling()) {
8447 LOG(WARNING)
8448 << "Skipping test because system doesn't support OCSP stapling";
8449 return;
8452 SpawnedTestServer::SSLOptions ssl_options(
8453 SpawnedTestServer::SSLOptions::CERT_AUTO);
8454 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
8455 ssl_options.staple_ocsp_response = true;
8456 ssl_options.ocsp_server_unavailable = true;
8458 CertStatus cert_status;
8459 DoConnection(ssl_options, &cert_status);
8461 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8463 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8464 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
8466 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8469 // Disabled on NSS ports. See https://crbug.com/431716.
8470 #if defined(USE_NSS_CERTS)
8471 #define MAYBE_RevokedStapled DISABLED_RevokedStapled
8472 #else
8473 #define MAYBE_RevokedStapled RevokedStapled
8474 #endif
8475 TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) {
8476 if (!SystemSupportsOCSPStapling()) {
8477 LOG(WARNING)
8478 << "Skipping test because system doesn't support OCSP stapling";
8479 return;
8482 SpawnedTestServer::SSLOptions ssl_options(
8483 SpawnedTestServer::SSLOptions::CERT_AUTO);
8484 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
8485 ssl_options.staple_ocsp_response = true;
8486 ssl_options.ocsp_server_unavailable = true;
8488 CertStatus cert_status;
8489 DoConnection(ssl_options, &cert_status);
8491 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
8492 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8493 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8496 class HTTPSHardFailTest : public HTTPSOCSPTest {
8497 protected:
8498 void SetupContext(URLRequestContext* context) override {
8499 context->set_ssl_config_service(
8500 new TestSSLConfigService(false /* check for EV */,
8501 false /* online revocation checking */,
8502 true /* require rev. checking for local
8503 anchors */));
8507 TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) {
8508 if (!SystemSupportsOCSP()) {
8509 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8510 return;
8513 if (!SystemSupportsHardFailRevocationChecking()) {
8514 LOG(WARNING) << "Skipping test because system doesn't support hard fail "
8515 << "revocation checking";
8516 return;
8519 SpawnedTestServer::SSLOptions ssl_options(
8520 SpawnedTestServer::SSLOptions::CERT_AUTO);
8521 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8523 CertStatus cert_status;
8524 DoConnection(ssl_options, &cert_status);
8526 EXPECT_EQ(CERT_STATUS_REVOKED,
8527 cert_status & CERT_STATUS_REVOKED);
8529 // Without a positive OCSP response, we shouldn't show the EV status.
8530 EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8533 class HTTPSEVCRLSetTest : public HTTPSOCSPTest {
8534 protected:
8535 void SetupContext(URLRequestContext* context) override {
8536 context->set_ssl_config_service(
8537 new TestSSLConfigService(true /* check for EV */,
8538 false /* online revocation checking */,
8539 false /* require rev. checking for local
8540 anchors */));
8544 TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) {
8545 if (!SystemSupportsOCSP()) {
8546 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8547 return;
8550 SpawnedTestServer::SSLOptions ssl_options(
8551 SpawnedTestServer::SSLOptions::CERT_AUTO);
8552 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8553 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
8555 CertStatus cert_status;
8556 DoConnection(ssl_options, &cert_status);
8558 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8559 cert_status & CERT_STATUS_ALL_ERRORS);
8561 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8562 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8563 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8566 TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) {
8567 if (!SystemSupportsOCSP()) {
8568 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8569 return;
8572 SpawnedTestServer::SSLOptions ssl_options(
8573 SpawnedTestServer::SSLOptions::CERT_AUTO);
8574 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
8575 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
8577 CertStatus cert_status;
8578 DoConnection(ssl_options, &cert_status);
8580 // Currently only works for Windows. When using NSS or OS X, it's not
8581 // possible to determine whether the check failed because of actual
8582 // revocation or because there was an OCSP failure.
8583 #if defined(OS_WIN)
8584 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
8585 #else
8586 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8587 #endif
8589 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8590 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8591 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8594 TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) {
8595 if (!SystemSupportsOCSP()) {
8596 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8597 return;
8600 SpawnedTestServer::SSLOptions ssl_options(
8601 SpawnedTestServer::SSLOptions::CERT_AUTO);
8602 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
8603 SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>());
8605 CertStatus cert_status;
8606 DoConnection(ssl_options, &cert_status);
8608 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8610 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8611 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
8612 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8613 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8616 TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) {
8617 if (!SystemSupportsOCSP()) {
8618 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8619 return;
8622 SpawnedTestServer::SSLOptions ssl_options(
8623 SpawnedTestServer::SSLOptions::CERT_AUTO);
8624 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8625 SSLConfigService::SetCRLSet(
8626 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
8628 CertStatus cert_status;
8629 DoConnection(ssl_options, &cert_status);
8631 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8632 cert_status & CERT_STATUS_ALL_ERRORS);
8634 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8635 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8636 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8639 TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) {
8640 if (!SystemSupportsOCSP()) {
8641 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8642 return;
8645 SpawnedTestServer::SSLOptions ssl_options(
8646 SpawnedTestServer::SSLOptions::CERT_AUTO);
8647 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8648 SSLConfigService::SetCRLSet(
8649 scoped_refptr<CRLSet>(CRLSet::ForTesting(
8650 false, &kOCSPTestCertSPKI, "")));
8652 CertStatus cert_status;
8653 DoConnection(ssl_options, &cert_status);
8655 // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a
8656 // revocation check for EV.
8657 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8658 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8659 static_cast<bool>(cert_status & CERT_STATUS_IS_EV));
8660 EXPECT_FALSE(
8661 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8664 TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) {
8665 if (!SystemSupportsOCSP()) {
8666 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8667 return;
8670 SpawnedTestServer::SSLOptions ssl_options(
8671 SpawnedTestServer::SSLOptions::CERT_AUTO);
8672 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8673 SSLConfigService::SetCRLSet(
8674 scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting()));
8676 CertStatus cert_status = 0;
8677 DoConnection(ssl_options, &cert_status);
8679 // Even with a fresh CRLSet, we should still do online revocation checks when
8680 // the certificate chain isn't covered by the CRLSet, which it isn't in this
8681 // test.
8682 EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(),
8683 cert_status & CERT_STATUS_ALL_ERRORS);
8685 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8686 EXPECT_EQ(SystemUsesChromiumEVMetadata(),
8687 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8690 TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) {
8691 // Test that when EV verification is requested, but online revocation
8692 // checking is disabled, and the leaf certificate is not in fact EV, that
8693 // no revocation checking actually happens.
8694 if (!SystemSupportsOCSP()) {
8695 LOG(WARNING) << "Skipping test because system doesn't support OCSP";
8696 return;
8699 // Unmark the certificate's OID as EV, which should disable revocation
8700 // checking (as per the user preference)
8701 ev_test_policy_.reset();
8703 SpawnedTestServer::SSLOptions ssl_options(
8704 SpawnedTestServer::SSLOptions::CERT_AUTO);
8705 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED;
8706 SSLConfigService::SetCRLSet(
8707 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
8709 CertStatus cert_status;
8710 DoConnection(ssl_options, &cert_status);
8712 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8714 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8715 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8718 class HTTPSCRLSetTest : public HTTPSOCSPTest {
8719 protected:
8720 void SetupContext(URLRequestContext* context) override {
8721 context->set_ssl_config_service(
8722 new TestSSLConfigService(false /* check for EV */,
8723 false /* online revocation checking */,
8724 false /* require rev. checking for local
8725 anchors */));
8729 TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) {
8730 SpawnedTestServer::SSLOptions ssl_options(
8731 SpawnedTestServer::SSLOptions::CERT_AUTO);
8732 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID;
8733 SSLConfigService::SetCRLSet(
8734 scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting()));
8736 CertStatus cert_status;
8737 DoConnection(ssl_options, &cert_status);
8739 // If we're not trying EV verification then, even if the CRLSet has expired,
8740 // we don't fall back to online revocation checks.
8741 EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS);
8742 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8743 EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED);
8746 TEST_F(HTTPSCRLSetTest, CRLSetRevoked) {
8747 #if defined(OS_ANDROID)
8748 LOG(WARNING) << "Skipping test because system doesn't support CRLSets";
8749 return;
8750 #endif
8752 SpawnedTestServer::SSLOptions ssl_options(
8753 SpawnedTestServer::SSLOptions::CERT_AUTO);
8754 ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK;
8755 ssl_options.cert_serial = 10;
8756 SSLConfigService::SetCRLSet(
8757 scoped_refptr<CRLSet>(CRLSet::ForTesting(
8758 false, &kOCSPTestCertSPKI, "\x0a")));
8760 CertStatus cert_status = 0;
8761 DoConnection(ssl_options, &cert_status);
8763 // If the certificate is recorded as revoked in the CRLSet, that should be
8764 // reflected without online revocation checking.
8765 EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS);
8766 EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV);
8767 EXPECT_FALSE(
8768 static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED));
8770 #endif // !defined(OS_IOS)
8772 #if !defined(DISABLE_FTP_SUPPORT)
8773 class URLRequestTestFTP : public URLRequestTest {
8774 public:
8775 URLRequestTestFTP()
8776 : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost,
8777 base::FilePath()) {
8780 protected:
8781 SpawnedTestServer test_server_;
8784 // Make sure an FTP request using an unsafe ports fails.
8785 TEST_F(URLRequestTestFTP, UnsafePort) {
8786 ASSERT_TRUE(test_server_.Start());
8788 URLRequestJobFactoryImpl job_factory;
8789 FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver());
8791 GURL url("ftp://127.0.0.1:7");
8792 job_factory.SetProtocolHandler(
8793 "ftp",
8794 new FtpProtocolHandler(&ftp_transaction_factory));
8795 default_context_.set_job_factory(&job_factory);
8797 TestDelegate d;
8799 scoped_ptr<URLRequest> r(
8800 default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d));
8801 r->Start();
8802 EXPECT_TRUE(r->is_pending());
8804 base::RunLoop().Run();
8806 EXPECT_FALSE(r->is_pending());
8807 EXPECT_EQ(URLRequestStatus::FAILED, r->status().status());
8808 EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error());
8812 // Flaky, see http://crbug.com/25045.
8813 TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) {
8814 ASSERT_TRUE(test_server_.Start());
8816 TestDelegate d;
8818 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8819 test_server_.GetURL("/"), DEFAULT_PRIORITY, &d));
8820 r->Start();
8821 EXPECT_TRUE(r->is_pending());
8823 base::RunLoop().Run();
8825 EXPECT_FALSE(r->is_pending());
8826 EXPECT_EQ(1, d.response_started_count());
8827 EXPECT_FALSE(d.received_data_before_response());
8828 EXPECT_LT(0, d.bytes_received());
8829 EXPECT_EQ(test_server_.host_port_pair().host(),
8830 r->GetSocketAddress().host());
8831 EXPECT_EQ(test_server_.host_port_pair().port(),
8832 r->GetSocketAddress().port());
8836 // Flaky, see http://crbug.com/25045.
8837 TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) {
8838 ASSERT_TRUE(test_server_.Start());
8840 base::FilePath app_path;
8841 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8842 app_path = app_path.AppendASCII("LICENSE");
8843 TestDelegate d;
8845 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8846 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, &d));
8847 r->Start();
8848 EXPECT_TRUE(r->is_pending());
8850 base::RunLoop().Run();
8852 int64 file_size = 0;
8853 base::GetFileSize(app_path, &file_size);
8855 EXPECT_FALSE(r->is_pending());
8856 EXPECT_EQ(1, d.response_started_count());
8857 EXPECT_FALSE(d.received_data_before_response());
8858 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8859 EXPECT_EQ(test_server_.host_port_pair().host(),
8860 r->GetSocketAddress().host());
8861 EXPECT_EQ(test_server_.host_port_pair().port(),
8862 r->GetSocketAddress().port());
8866 // Flaky, see http://crbug.com/25045.
8867 TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) {
8868 ASSERT_TRUE(test_server_.Start());
8870 base::FilePath app_path;
8871 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8872 app_path = app_path.AppendASCII("LICENSE");
8873 TestDelegate d;
8875 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8876 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
8877 DEFAULT_PRIORITY, &d));
8878 r->Start();
8879 EXPECT_TRUE(r->is_pending());
8881 base::RunLoop().Run();
8883 int64 file_size = 0;
8884 base::GetFileSize(app_path, &file_size);
8886 EXPECT_FALSE(r->is_pending());
8887 EXPECT_EQ(test_server_.host_port_pair().host(),
8888 r->GetSocketAddress().host());
8889 EXPECT_EQ(test_server_.host_port_pair().port(),
8890 r->GetSocketAddress().port());
8891 EXPECT_EQ(1, d.response_started_count());
8892 EXPECT_FALSE(d.received_data_before_response());
8893 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8895 LoadTimingInfo load_timing_info;
8896 r->GetLoadTimingInfo(&load_timing_info);
8897 TestLoadTimingNoHttpResponse(load_timing_info);
8901 // Flaky, see http://crbug.com/25045.
8902 TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) {
8903 ASSERT_TRUE(test_server_.Start());
8905 base::FilePath app_path;
8906 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8907 app_path = app_path.AppendASCII("LICENSE");
8908 TestDelegate d;
8910 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8911 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
8912 "wrong_password"),
8913 DEFAULT_PRIORITY, &d));
8914 r->Start();
8915 EXPECT_TRUE(r->is_pending());
8917 base::RunLoop().Run();
8919 int64 file_size = 0;
8920 base::GetFileSize(app_path, &file_size);
8922 EXPECT_FALSE(r->is_pending());
8923 EXPECT_EQ(1, d.response_started_count());
8924 EXPECT_FALSE(d.received_data_before_response());
8925 EXPECT_EQ(d.bytes_received(), 0);
8929 // Flaky, see http://crbug.com/25045.
8930 TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) {
8931 ASSERT_TRUE(test_server_.Start());
8933 base::FilePath app_path;
8934 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8935 app_path = app_path.AppendASCII("LICENSE");
8936 TestDelegate d;
8937 // Set correct login credentials. The delegate will be asked for them when
8938 // the initial login with wrong credentials will fail.
8939 d.set_credentials(AuthCredentials(kChrome, kChrome));
8941 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
8942 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
8943 "wrong_password"),
8944 DEFAULT_PRIORITY, &d));
8945 r->Start();
8946 EXPECT_TRUE(r->is_pending());
8948 base::RunLoop().Run();
8950 int64 file_size = 0;
8951 base::GetFileSize(app_path, &file_size);
8953 EXPECT_FALSE(r->is_pending());
8954 EXPECT_EQ(1, d.response_started_count());
8955 EXPECT_FALSE(d.received_data_before_response());
8956 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
8960 // Flaky, see http://crbug.com/25045.
8961 TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) {
8962 ASSERT_TRUE(test_server_.Start());
8964 base::FilePath app_path;
8965 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8966 app_path = app_path.AppendASCII("LICENSE");
8967 TestDelegate d;
8969 scoped_ptr<URLRequest> r(
8970 default_context_.CreateRequest(test_server_.GetURLWithUserAndPassword(
8971 "/LICENSE", "wrong_user", "chrome"),
8972 DEFAULT_PRIORITY, &d));
8973 r->Start();
8974 EXPECT_TRUE(r->is_pending());
8976 base::RunLoop().Run();
8978 int64 file_size = 0;
8979 base::GetFileSize(app_path, &file_size);
8981 EXPECT_FALSE(r->is_pending());
8982 EXPECT_EQ(1, d.response_started_count());
8983 EXPECT_FALSE(d.received_data_before_response());
8984 EXPECT_EQ(d.bytes_received(), 0);
8988 // Flaky, see http://crbug.com/25045.
8989 TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) {
8990 ASSERT_TRUE(test_server_.Start());
8992 base::FilePath app_path;
8993 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
8994 app_path = app_path.AppendASCII("LICENSE");
8995 TestDelegate d;
8996 // Set correct login credentials. The delegate will be asked for them when
8997 // the initial login with wrong credentials will fail.
8998 d.set_credentials(AuthCredentials(kChrome, kChrome));
9000 scoped_ptr<URLRequest> r(
9001 default_context_.CreateRequest(test_server_.GetURLWithUserAndPassword(
9002 "/LICENSE", "wrong_user", "chrome"),
9003 DEFAULT_PRIORITY, &d));
9004 r->Start();
9005 EXPECT_TRUE(r->is_pending());
9007 base::RunLoop().Run();
9009 int64 file_size = 0;
9010 base::GetFileSize(app_path, &file_size);
9012 EXPECT_FALSE(r->is_pending());
9013 EXPECT_EQ(1, d.response_started_count());
9014 EXPECT_FALSE(d.received_data_before_response());
9015 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size));
9019 // Flaky, see http://crbug.com/25045.
9020 TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) {
9021 ASSERT_TRUE(test_server_.Start());
9023 base::FilePath app_path;
9024 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
9025 app_path = app_path.AppendASCII("LICENSE");
9027 scoped_ptr<TestDelegate> d(new TestDelegate);
9029 // Pass correct login identity in the URL.
9030 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
9031 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"),
9032 DEFAULT_PRIORITY, d.get()));
9033 r->Start();
9034 EXPECT_TRUE(r->is_pending());
9036 base::RunLoop().Run();
9038 int64 file_size = 0;
9039 base::GetFileSize(app_path, &file_size);
9041 EXPECT_FALSE(r->is_pending());
9042 EXPECT_EQ(1, d->response_started_count());
9043 EXPECT_FALSE(d->received_data_before_response());
9044 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9047 d.reset(new TestDelegate);
9049 // This request should use cached identity from previous request.
9050 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
9051 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get()));
9052 r->Start();
9053 EXPECT_TRUE(r->is_pending());
9055 base::RunLoop().Run();
9057 int64 file_size = 0;
9058 base::GetFileSize(app_path, &file_size);
9060 EXPECT_FALSE(r->is_pending());
9061 EXPECT_EQ(1, d->response_started_count());
9062 EXPECT_FALSE(d->received_data_before_response());
9063 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9067 // Flaky, see http://crbug.com/25045.
9068 TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) {
9069 ASSERT_TRUE(test_server_.Start());
9071 base::FilePath app_path;
9072 PathService::Get(base::DIR_SOURCE_ROOT, &app_path);
9073 app_path = app_path.AppendASCII("LICENSE");
9075 scoped_ptr<TestDelegate> d(new TestDelegate);
9076 // Set correct login credentials. The delegate will be asked for them when
9077 // the initial login with wrong credentials will fail.
9078 d->set_credentials(AuthCredentials(kChrome, kChrome));
9080 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
9081 test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome",
9082 "wrong_password"),
9083 DEFAULT_PRIORITY, d.get()));
9084 r->Start();
9085 EXPECT_TRUE(r->is_pending());
9087 base::RunLoop().Run();
9089 int64 file_size = 0;
9090 base::GetFileSize(app_path, &file_size);
9092 EXPECT_FALSE(r->is_pending());
9093 EXPECT_EQ(1, d->response_started_count());
9094 EXPECT_FALSE(d->received_data_before_response());
9095 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9098 // Use a new delegate without explicit credentials. The cached ones should be
9099 // used.
9100 d.reset(new TestDelegate);
9102 // Don't pass wrong credentials in the URL, they would override valid cached
9103 // ones.
9104 scoped_ptr<URLRequest> r(default_context_.CreateRequest(
9105 test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get()));
9106 r->Start();
9107 EXPECT_TRUE(r->is_pending());
9109 base::RunLoop().Run();
9111 int64 file_size = 0;
9112 base::GetFileSize(app_path, &file_size);
9114 EXPECT_FALSE(r->is_pending());
9115 EXPECT_EQ(1, d->response_started_count());
9116 EXPECT_FALSE(d->received_data_before_response());
9117 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9120 #endif // !defined(DISABLE_FTP_SUPPORT)
9122 } // namespace net