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