[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 7 | #if defined(OS_WIN) |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 8 | #include <windows.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 9 | #include <shlobj.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 10 | #endif |
| 11 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 12 | #include <stdint.h> |
| 13 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 14 | #include <algorithm> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 15 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 16 | #include "base/basictypes.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 17 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 19 | #include "base/files/file_path.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 21 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 22 | #include "base/format_macros.h" |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 23 | #include "base/memory/scoped_ptr.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 24 | #include "base/memory/weak_ptr.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 25 | #include "base/message_loop/message_loop.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 26 | #include "base/message_loop/message_loop_proxy.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 27 | #include "base/path_service.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 28 | #include "base/run_loop.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 29 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 30 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 31 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 32 | #include "base/strings/string_util.h" |
| 33 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 34 | #include "base/strings/utf_string_conversions.h" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 35 | #include "base/test/histogram_tester.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 36 | #include "net/base/chunked_upload_data_stream.h" |
| 37 | #include "net/base/elements_upload_data_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 39 | #include "net/base/load_timing_info.h" |
| 40 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 41 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 42 | #include "net/base/net_module.h" |
| 43 | #include "net/base/net_util.h" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 44 | #include "net/base/network_quality.h" |
| 45 | #include "net/base/network_quality_estimator.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 46 | #include "net/base/request_priority.h" |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 47 | #include "net/base/test_data_directory.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 48 | #include "net/base/upload_bytes_element_reader.h" |
| 49 | #include "net/base/upload_data_stream.h" |
| 50 | #include "net/base/upload_file_element_reader.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 51 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 52 | #include "net/cert/mock_cert_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 53 | #include "net/cert/test_root_certs.h" |
eroman | ed744f3 | 2015-04-09 06:35:49 | [diff] [blame] | 54 | #include "net/cert_net/nss_ocsp.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 55 | #include "net/cookies/cookie_monster.h" |
| 56 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 57 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 58 | #include "net/dns/mock_host_resolver.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 59 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 60 | #include "net/http/http_cache.h" |
| 61 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 62 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 63 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 64 | #include "net/http/http_response_headers.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 65 | #include "net/http/http_util.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 66 | #include "net/log/net_log.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 67 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 68 | #include "net/log/test_net_log_entry.h" |
| 69 | #include "net/log/test_net_log_util.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 70 | #include "net/proxy/proxy_service.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 71 | #include "net/socket/ssl_client_socket.h" |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 72 | #include "net/ssl/ssl_cipher_suite_names.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 73 | #include "net/ssl/ssl_connection_status_flags.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 74 | #include "net/test/cert_test_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 75 | #include "net/test/spawned_test_server/spawned_test_server.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 76 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 77 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 78 | #include "net/url_request/url_request.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 79 | #include "net/url_request/url_request_http_job.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 80 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 81 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 82 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 83 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 84 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 85 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 86 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 87 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 88 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 89 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 90 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 91 | #include "net/url_request/file_protocol_handler.h" |
| 92 | #include "net/url_request/url_request_file_dir_job.h" |
| 93 | #endif |
| 94 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 95 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 96 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 97 | #include "net/url_request/ftp_protocol_handler.h" |
| 98 | #endif |
| 99 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 100 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 101 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 102 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 103 | #include "base/win/windows_version.h" |
| 104 | #endif |
| 105 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 106 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 107 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 108 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 109 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 110 | namespace net { |
| 111 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 112 | namespace { |
| 113 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 114 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 115 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 116 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 117 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 118 | const base::FilePath::CharType kTestFilePath[] = |
| 119 | FILE_PATH_LITERAL("net/data/url_request_unittest"); |
| 120 | |
| 121 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 122 | // Test file used in most FTP tests. |
| 123 | const char kFtpTestFile[] = "BullRunSpeech.txt"; |
| 124 | #endif |
| 125 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 126 | // Tests load timing information in the case a fresh connection was used, with |
| 127 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 128 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 129 | int connect_timing_flags) { |
| 130 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 131 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 132 | |
| 133 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 134 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 135 | |
| 136 | EXPECT_LE(load_timing_info.request_start, |
| 137 | load_timing_info.connect_timing.connect_start); |
| 138 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 139 | connect_timing_flags); |
| 140 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 141 | load_timing_info.send_start); |
| 142 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 143 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 144 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 145 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 146 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 147 | } |
| 148 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 149 | // Same as above, but with proxy times. |
| 150 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 151 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 152 | int connect_timing_flags) { |
| 153 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 154 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 155 | |
| 156 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 157 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 158 | |
| 159 | EXPECT_LE(load_timing_info.request_start, |
| 160 | load_timing_info.proxy_resolve_start); |
| 161 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 162 | load_timing_info.proxy_resolve_end); |
| 163 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 164 | load_timing_info.connect_timing.connect_start); |
| 165 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 166 | connect_timing_flags); |
| 167 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 168 | load_timing_info.send_start); |
| 169 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 170 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 171 | } |
| 172 | |
| 173 | // Same as above, but with a reused socket and proxy times. |
| 174 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 175 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 176 | EXPECT_TRUE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 177 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 178 | |
| 179 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 180 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 181 | |
| 182 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 183 | |
| 184 | EXPECT_LE(load_timing_info.request_start, |
| 185 | load_timing_info.proxy_resolve_start); |
| 186 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 187 | load_timing_info.proxy_resolve_end); |
| 188 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 189 | load_timing_info.send_start); |
| 190 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 191 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 192 | } |
| 193 | |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 194 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 195 | // Tests load timing information in the case of a cache hit, when no cache |
| 196 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 197 | base::StringPiece TestNetResourceProvider(int key) { |
| 198 | return "header"; |
| 199 | } |
| 200 | |
| 201 | void FillBuffer(char* buffer, size_t len) { |
| 202 | static bool called = false; |
| 203 | if (!called) { |
| 204 | called = true; |
| 205 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 206 | srand(seed); |
| 207 | } |
| 208 | |
| 209 | for (size_t i = 0; i < len; i++) { |
| 210 | buffer[i] = static_cast<char>(rand()); |
| 211 | if (!buffer[i]) |
| 212 | buffer[i] = 'g'; |
| 213 | } |
| 214 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 215 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 216 | |
| 217 | #if !defined(OS_IOS) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 218 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 219 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 220 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 221 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 222 | |
| 223 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 224 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 225 | |
| 226 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 227 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 228 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 229 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 230 | |
| 231 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 232 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 233 | } |
| 234 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 235 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 236 | // Tests load timing in the case that there is no HTTP response. This can be |
| 237 | // used to test in the case of errors or non-HTTP requests. |
| 238 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 239 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 240 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 241 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 242 | |
| 243 | // Only the request times should be non-null. |
| 244 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 245 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 246 | |
| 247 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 248 | |
| 249 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 250 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 251 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 252 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 253 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 254 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 255 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 256 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 257 | // Do a case-insensitive search through |haystack| for |needle|. |
| 258 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 259 | std::string::const_iterator it = |
| 260 | std::search(haystack.begin(), |
| 261 | haystack.end(), |
| 262 | needle, |
| 263 | needle + strlen(needle), |
[email protected] | 07f1cee | 2010-11-03 03:53:35 | [diff] [blame] | 264 | base::CaseInsensitiveCompare<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 265 | return it != haystack.end(); |
| 266 | } |
| 267 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 268 | scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 269 | scoped_ptr<UploadElementReader> reader( |
| 270 | new UploadBytesElementReader(data, strlen(data))); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 271 | return ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 274 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 275 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 276 | // -1 means unknown. 0 means no encryption. |
| 277 | EXPECT_GT(ssl_info.security_bits, 0); |
| 278 | |
| 279 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 280 | uint16 cipher_suite = SSLConnectionStatusToCipherSuite( |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 281 | ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 282 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 285 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 286 | const GURL& host_url) { |
| 287 | std::string sent_value; |
| 288 | |
| 289 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 290 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 291 | |
| 292 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 293 | EXPECT_EQ("keep-alive", sent_value); |
| 294 | } |
| 295 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 296 | bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) { |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 297 | size_t size = a.size(); |
| 298 | |
| 299 | if (size != b.size()) |
| 300 | return false; |
| 301 | |
| 302 | for (size_t i = 0; i < size; ++i) { |
| 303 | if (!a[i].Equals(b[i])) |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | return true; |
| 308 | } |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 309 | #endif // !defined(OS_IOS) |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 310 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 311 | // A network delegate that allows the user to choose a subset of request stages |
| 312 | // to block in. When blocking, the delegate can do one of the following: |
| 313 | // * synchronously return a pre-specified error code, or |
| 314 | // * asynchronously return that value via an automatically called callback, |
| 315 | // or |
| 316 | // * block and wait for the user to do a callback. |
| 317 | // Additionally, the user may also specify a redirect URL -- then each request |
| 318 | // with the current URL different from the redirect target will be redirected |
| 319 | // to that target, in the on-before-URL-request stage, independent of whether |
| 320 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 321 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 322 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 323 | // Stages in which the delegate can block. |
| 324 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 325 | NOT_BLOCKED = 0, |
| 326 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 327 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 328 | ON_HEADERS_RECEIVED = 1 << 2, |
| 329 | ON_AUTH_REQUIRED = 1 << 3 |
| 330 | }; |
| 331 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 332 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 333 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 334 | enum BlockMode { |
| 335 | SYNCHRONOUS, // No callback, returns specified return values. |
| 336 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 337 | // specified return codes. |
| 338 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 339 | // |auth_retval_| are ignored. In every blocking stage the |
| 340 | // message loop is quit. |
| 341 | }; |
| 342 | |
| 343 | // Creates a delegate which does not block at all. |
| 344 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 345 | |
| 346 | // For users to trigger a callback returning |response|. |
| 347 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 348 | // Only call if |block_mode_| == USER_CALLBACK. |
| 349 | void DoCallback(int response); |
| 350 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 351 | |
| 352 | // Setters. |
| 353 | void set_retval(int retval) { |
| 354 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 355 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 356 | ASSERT_NE(OK, retval); |
| 357 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 360 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 361 | // |auth_credentials_| will be passed with the response. |
| 362 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 363 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 364 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 365 | auth_retval_ = auth_retval; |
| 366 | } |
| 367 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 368 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 369 | } |
| 370 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 371 | void set_redirect_url(const GURL& url) { |
| 372 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 373 | } |
| 374 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 375 | void set_block_on(int block_on) { |
| 376 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 379 | // Allows the user to check in which state did we block. |
| 380 | Stage stage_blocked_for_callback() const { |
| 381 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 382 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 386 | void RunCallback(int response, const CompletionCallback& callback); |
| 387 | void RunAuthCallback(AuthRequiredResponse response, |
| 388 | const AuthCallback& callback); |
| 389 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 390 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 391 | int OnBeforeURLRequest(URLRequest* request, |
| 392 | const CompletionCallback& callback, |
| 393 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 394 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 395 | int OnBeforeSendHeaders(URLRequest* request, |
| 396 | const CompletionCallback& callback, |
| 397 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 398 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 399 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 400 | URLRequest* request, |
| 401 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 402 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 403 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 404 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 405 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 406 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 407 | URLRequest* request, |
| 408 | const AuthChallengeInfo& auth_info, |
| 409 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 410 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 411 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 412 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 413 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 414 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 415 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 416 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 417 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 418 | |
| 419 | // Configuration parameters, can be adjusted by public methods: |
| 420 | const BlockMode block_mode_; |
| 421 | |
| 422 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 423 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 424 | int retval_; // To be returned in non-auth stages. |
| 425 | AuthRequiredResponse auth_retval_; |
| 426 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 427 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 428 | int block_on_; // Bit mask: in which stages to block. |
| 429 | |
| 430 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 431 | // callback. |
| 432 | AuthCredentials auth_credentials_; |
| 433 | AuthCredentials* target_auth_credentials_; |
| 434 | |
| 435 | // Internal variables, not set by not the user: |
| 436 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 437 | // USER_CALLBACK). |
| 438 | Stage stage_blocked_for_callback_; |
| 439 | |
| 440 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 441 | CompletionCallback callback_; |
| 442 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 443 | |
| 444 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 445 | |
| 446 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 447 | }; |
| 448 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 449 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 450 | : block_mode_(block_mode), |
| 451 | retval_(OK), |
| 452 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 453 | block_on_(0), |
| 454 | target_auth_credentials_(NULL), |
| 455 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 456 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 460 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 461 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 462 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 463 | CompletionCallback callback = callback_; |
| 464 | Reset(); |
| 465 | RunCallback(response, callback); |
| 466 | } |
| 467 | |
| 468 | void BlockingNetworkDelegate::DoAuthCallback( |
| 469 | NetworkDelegate::AuthRequiredResponse response) { |
| 470 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 471 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 472 | AuthCallback auth_callback = auth_callback_; |
| 473 | Reset(); |
| 474 | RunAuthCallback(response, auth_callback); |
| 475 | } |
| 476 | |
| 477 | void BlockingNetworkDelegate::RunCallback(int response, |
| 478 | const CompletionCallback& callback) { |
| 479 | callback.Run(response); |
| 480 | } |
| 481 | |
| 482 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 483 | const AuthCallback& callback) { |
| 484 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 485 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 486 | *target_auth_credentials_ = auth_credentials_; |
| 487 | } |
| 488 | callback.Run(response); |
| 489 | } |
| 490 | |
| 491 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 492 | URLRequest* request, |
| 493 | const CompletionCallback& callback, |
| 494 | GURL* new_url) { |
| 495 | if (redirect_url_ == request->url()) |
| 496 | return OK; // We've already seen this request and redirected elsewhere. |
| 497 | |
| 498 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 499 | |
| 500 | if (!redirect_url_.is_empty()) |
| 501 | *new_url = redirect_url_; |
| 502 | |
| 503 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 504 | } |
| 505 | |
| 506 | int BlockingNetworkDelegate::OnBeforeSendHeaders( |
| 507 | URLRequest* request, |
| 508 | const CompletionCallback& callback, |
| 509 | HttpRequestHeaders* headers) { |
| 510 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 511 | |
| 512 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 513 | } |
| 514 | |
| 515 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 516 | URLRequest* request, |
| 517 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 518 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 519 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 520 | GURL* allowed_unsafe_redirect_url) { |
| 521 | TestNetworkDelegate::OnHeadersReceived(request, |
| 522 | callback, |
| 523 | original_response_headers, |
| 524 | override_response_headers, |
| 525 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 526 | |
| 527 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 528 | } |
| 529 | |
| 530 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 531 | URLRequest* request, |
| 532 | const AuthChallengeInfo& auth_info, |
| 533 | const AuthCallback& callback, |
| 534 | AuthCredentials* credentials) { |
| 535 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 536 | credentials); |
| 537 | // Check that the user has provided callback for the previous blocked stage. |
| 538 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 539 | |
| 540 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 541 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 542 | } |
| 543 | |
| 544 | target_auth_credentials_ = credentials; |
| 545 | |
| 546 | switch (block_mode_) { |
| 547 | case SYNCHRONOUS: |
| 548 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 549 | *target_auth_credentials_ = auth_credentials_; |
| 550 | return auth_retval_; |
| 551 | |
| 552 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 553 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 554 | FROM_HERE, |
| 555 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 556 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 557 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 558 | |
| 559 | case USER_CALLBACK: |
| 560 | auth_callback_ = callback; |
| 561 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 562 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 563 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 564 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 565 | } |
| 566 | NOTREACHED(); |
| 567 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 568 | } |
| 569 | |
| 570 | void BlockingNetworkDelegate::Reset() { |
| 571 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 572 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 573 | callback_.Reset(); |
| 574 | auth_callback_.Reset(); |
| 575 | } |
| 576 | |
| 577 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 578 | BlockingNetworkDelegate::Stage stage, |
| 579 | const CompletionCallback& callback) { |
| 580 | // Check that the user has provided callback for the previous blocked stage. |
| 581 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 582 | |
| 583 | if ((block_on_ & stage) == 0) { |
| 584 | return OK; |
| 585 | } |
| 586 | |
| 587 | switch (block_mode_) { |
| 588 | case SYNCHRONOUS: |
| 589 | EXPECT_NE(OK, retval_); |
| 590 | return retval_; |
| 591 | |
| 592 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 593 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 594 | FROM_HERE, |
| 595 | base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 596 | weak_factory_.GetWeakPtr(), retval_, callback)); |
| 597 | return ERR_IO_PENDING; |
| 598 | |
| 599 | case USER_CALLBACK: |
| 600 | callback_ = callback; |
| 601 | stage_blocked_for_callback_ = stage; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 602 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 603 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 604 | return ERR_IO_PENDING; |
| 605 | } |
| 606 | NOTREACHED(); |
| 607 | return 0; |
| 608 | } |
| 609 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 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(); |
| 619 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 620 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | } // namespace |
| 624 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 625 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 626 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 627 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 628 | URLRequestTest() : default_context_(true) { |
| 629 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 630 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 631 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 632 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 633 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 634 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 635 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 636 | // URLRequestJobs may post clean-up tasks on destruction. |
| 637 | base::RunLoop().RunUntilIdle(); |
| 638 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 639 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 640 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 641 | SetUpFactory(); |
| 642 | default_context_.set_job_factory(job_factory_.get()); |
| 643 | default_context_.Init(); |
| 644 | PlatformTest::SetUp(); |
| 645 | } |
| 646 | |
| 647 | virtual void SetUpFactory() { |
| 648 | job_factory_impl_->SetProtocolHandler("data", new DataProtocolHandler); |
| 649 | #if !defined(DISABLE_FILE_SUPPORT) |
| 650 | job_factory_impl_->SetProtocolHandler( |
| 651 | "file", new FileProtocolHandler(base::MessageLoopProxy::current())); |
| 652 | #endif |
| 653 | } |
| 654 | |
| 655 | TestNetworkDelegate* default_network_delegate() { |
| 656 | return &default_network_delegate_; |
| 657 | } |
| 658 | |
| 659 | const TestURLRequestContext& default_context() const { |
| 660 | return default_context_; |
| 661 | } |
| 662 | |
| 663 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 664 | // Adds the TestJobInterceptor to the default context. |
| 665 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 666 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 667 | job_factory_impl_->SetProtocolHandler("http", NULL); |
| 668 | job_factory_impl_->SetProtocolHandler("http", protocol_handler_); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 669 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 670 | } |
| 671 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 672 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 673 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 674 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 675 | URLRequestJobFactoryImpl* job_factory_impl_; |
| 676 | scoped_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 677 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 678 | }; |
| 679 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 680 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 681 | TestDelegate d; |
| 682 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 683 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 684 | GURL("about:blank"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 685 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 686 | r->Start(); |
| 687 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 688 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 689 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 690 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 691 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 692 | EXPECT_FALSE(d.received_data_before_response()); |
| 693 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 694 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 695 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 696 | |
| 697 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 698 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 703 | TestDelegate d; |
| 704 | { |
| 705 | // Use our nice little Chrome logo. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 706 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 707 | GURL( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 708 | "data:image/png;base64," |
| 709 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 710 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 711 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 712 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 713 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 714 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 715 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 716 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 717 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 718 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 719 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 720 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 721 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 722 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 723 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 724 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 725 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 726 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 727 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
| 728 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 729 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 730 | r->Start(); |
| 731 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 732 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 733 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 734 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 735 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 736 | EXPECT_FALSE(d.received_data_before_response()); |
| 737 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 738 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 739 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 740 | |
| 741 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 742 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 746 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 747 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 748 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 749 | PathService::Get(base::FILE_EXE, &app_path); |
| 750 | GURL app_url = FilePathToFileURL(app_path); |
| 751 | |
| 752 | TestDelegate d; |
| 753 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 754 | scoped_ptr<URLRequest> r( |
| 755 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 756 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 757 | r->Start(); |
| 758 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 759 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 760 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 761 | |
| 762 | int64 file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 763 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 764 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 765 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 766 | EXPECT_EQ(1, d.response_started_count()); |
| 767 | EXPECT_FALSE(d.received_data_before_response()); |
| 768 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 769 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 770 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 771 | |
| 772 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 773 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 777 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 778 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 779 | PathService::Get(base::FILE_EXE, &app_path); |
| 780 | GURL app_url = FilePathToFileURL(app_path); |
| 781 | |
| 782 | TestDelegate d; |
| 783 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 784 | scoped_ptr<URLRequest> r( |
| 785 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 786 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 787 | r->Start(); |
| 788 | EXPECT_TRUE(r->is_pending()); |
| 789 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 790 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 791 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 792 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 793 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 794 | } |
| 795 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 796 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 797 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 798 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 799 | FillBuffer(buffer.get(), buffer_size); |
| 800 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 801 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 802 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 803 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 804 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 805 | |
| 806 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 807 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 808 | |
| 809 | const size_t first_byte_position = 500; |
| 810 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 811 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 812 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 813 | buffer.get() + last_byte_position + 1); |
| 814 | |
| 815 | TestDelegate d; |
| 816 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 817 | scoped_ptr<URLRequest> r( |
| 818 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 819 | |
| 820 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 821 | headers.SetHeader( |
| 822 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 823 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 824 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 825 | r->SetExtraRequestHeaders(headers); |
| 826 | r->Start(); |
| 827 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 828 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 829 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 830 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 831 | EXPECT_EQ(1, d.response_started_count()); |
| 832 | EXPECT_FALSE(d.received_data_before_response()); |
| 833 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 834 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 835 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 836 | } |
| 837 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 838 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 842 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 843 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 844 | FillBuffer(buffer.get(), buffer_size); |
| 845 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 846 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 847 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 848 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 849 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 850 | |
| 851 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 852 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 853 | |
| 854 | const size_t first_byte_position = 500; |
| 855 | const size_t last_byte_position = buffer_size - 1; |
| 856 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 857 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 858 | buffer.get() + last_byte_position + 1); |
| 859 | |
| 860 | TestDelegate d; |
| 861 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 862 | scoped_ptr<URLRequest> r( |
| 863 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 864 | |
| 865 | HttpRequestHeaders headers; |
| 866 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 867 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 868 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 869 | r->SetExtraRequestHeaders(headers); |
| 870 | r->Start(); |
| 871 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 872 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 873 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 874 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 875 | EXPECT_EQ(1, d.response_started_count()); |
| 876 | EXPECT_FALSE(d.received_data_before_response()); |
| 877 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 878 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 879 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 880 | } |
| 881 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 882 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 886 | const size_t buffer_size = 400000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 887 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 888 | FillBuffer(buffer.get(), buffer_size); |
| 889 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 890 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 891 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 892 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 893 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 894 | |
| 895 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 896 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 897 | |
| 898 | TestDelegate d; |
| 899 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 900 | scoped_ptr<URLRequest> r( |
| 901 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 902 | |
| 903 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 904 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 905 | r->SetExtraRequestHeaders(headers); |
| 906 | r->Start(); |
| 907 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 908 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 909 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 910 | EXPECT_TRUE(d.request_failed()); |
| 911 | } |
| 912 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 913 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 914 | } |
| 915 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 916 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 917 | base::ScopedTempDir temp_dir; |
| 918 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 919 | base::FilePath test_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 920 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file)); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 921 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 922 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 923 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 924 | |
| 925 | { |
| 926 | TestDelegate d; |
| 927 | TestNetworkDelegate network_delegate; |
| 928 | network_delegate.set_can_access_files(true); |
| 929 | default_context_.set_network_delegate(&network_delegate); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 930 | scoped_ptr<URLRequest> r( |
| 931 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 932 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 933 | base::RunLoop().Run(); |
| 934 | EXPECT_FALSE(d.request_failed()); |
| 935 | EXPECT_EQ(test_data, d.data_received()); |
| 936 | } |
| 937 | |
| 938 | { |
| 939 | TestDelegate d; |
| 940 | TestNetworkDelegate network_delegate; |
| 941 | network_delegate.set_can_access_files(false); |
| 942 | default_context_.set_network_delegate(&network_delegate); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 943 | scoped_ptr<URLRequest> r( |
| 944 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 945 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 946 | base::RunLoop().Run(); |
| 947 | EXPECT_TRUE(d.request_failed()); |
| 948 | EXPECT_EQ("", d.data_received()); |
| 949 | } |
| 950 | } |
| 951 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 952 | |
| 953 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 954 | // Put in mock resource provider. |
| 955 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 956 | |
| 957 | TestDelegate d; |
| 958 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 959 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 960 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 961 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 962 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 963 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 964 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 965 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 966 | req->Start(); |
| 967 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 968 | |
| 969 | d.set_cancel_in_received_data_pending(true); |
| 970 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 971 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | // Take out mock resource provider. |
| 975 | NetModule::SetResourceProvider(NULL); |
| 976 | } |
| 977 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 978 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 979 | // Verify the general sanity of the the output of the file: |
| 980 | // directory lister by checking for the output of a known existing |
| 981 | // file. |
| 982 | const char sentinel_name[] = "filedir-sentinel"; |
| 983 | |
| 984 | base::FilePath path; |
| 985 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 986 | path = path.Append(kTestFilePath); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 987 | |
| 988 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 989 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 990 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 991 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 992 | base::RunLoop().Run(); |
| 993 | |
| 994 | // Generate entry for the sentinel file. |
| 995 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 996 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 997 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 998 | EXPECT_GT(info.size, 0); |
| 999 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1000 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 1001 | std::string(sentinel_name), |
| 1002 | false /* is_dir */, |
| 1003 | info.size, |
| 1004 | info.last_modified); |
| 1005 | |
| 1006 | ASSERT_LT(0, d.bytes_received()); |
| 1007 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1008 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1009 | // Check for the entry generated for the "sentinel" file. |
| 1010 | const std::string& data = d.data_received(); |
| 1011 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1012 | } |
| 1013 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1014 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1015 | // There is an implicit redirect when loading a file path that matches a |
| 1016 | // directory and does not end with a slash. Ensure that following such |
| 1017 | // redirects does not crash. See http://crbug.com/18686. |
| 1018 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1019 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1020 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 1021 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1022 | |
| 1023 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1024 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1025 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1026 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1027 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1028 | |
| 1029 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1030 | ASSERT_LT(0, d.bytes_received()); |
| 1031 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1032 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | #if defined(OS_WIN) |
| 1036 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1037 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1038 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1039 | scoped_ptr<URLRequest> req( |
| 1040 | default_context_.CreateRequest(GURL("file:///"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1041 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1042 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1043 | |
| 1044 | ASSERT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1045 | ASSERT_FALSE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1046 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1047 | #endif // defined(OS_WIN) |
| 1048 | |
| 1049 | #endif // !defined(DISABLE_FILE_SUPPORT) |
| 1050 | |
| 1051 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1052 | TestDelegate d; |
| 1053 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1054 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1055 | GURL("invalid url"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1056 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1057 | r->Start(); |
| 1058 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1059 | |
| 1060 | base::RunLoop().Run(); |
| 1061 | EXPECT_TRUE(d.request_failed()); |
| 1062 | } |
| 1063 | } |
| 1064 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1065 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1066 | TestURLRequestContext context; |
| 1067 | TestNetworkDelegate network_delegate; |
| 1068 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1069 | context.set_network_delegate(&network_delegate); |
| 1070 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1071 | scoped_ptr<URLRequest> req( |
| 1072 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1073 | req->SetReferrer("https://somewhere.com/"); |
| 1074 | |
| 1075 | req->Start(); |
| 1076 | base::RunLoop().Run(); |
| 1077 | EXPECT_TRUE(d.request_failed()); |
| 1078 | } |
| 1079 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1080 | #if defined(OS_WIN) |
| 1081 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1082 | base::FilePath app_path; |
| 1083 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 1084 | app_path = app_path.Append(kTestFilePath); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1085 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1086 | |
| 1087 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1088 | |
| 1089 | base::win::ScopedCOMInitializer com_initializer; |
| 1090 | |
| 1091 | // Temporarily create a shortcut for test |
| 1092 | { |
| 1093 | base::win::ScopedComPtr<IShellLink> shell; |
| 1094 | ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 1095 | CLSCTX_INPROC_SERVER))); |
| 1096 | base::win::ScopedComPtr<IPersistFile> persist; |
| 1097 | ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 1098 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1099 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1100 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1101 | } |
| 1102 | |
| 1103 | TestDelegate d; |
| 1104 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1105 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1106 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1107 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1108 | r->Start(); |
| 1109 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1110 | |
| 1111 | base::RunLoop().Run(); |
| 1112 | |
| 1113 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1114 | GetFileAttributesEx(app_path.value().c_str(), |
| 1115 | GetFileExInfoStandard, &data); |
| 1116 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1117 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1118 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1119 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 1120 | scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
| 1121 | DWORD read_size; |
| 1122 | BOOL result; |
| 1123 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1124 | &read_size, NULL); |
| 1125 | std::string content(buffer.get(), read_size); |
| 1126 | CloseHandle(file); |
| 1127 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1128 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1129 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1130 | EXPECT_EQ(content, d.data_received()); |
| 1131 | } |
| 1132 | |
| 1133 | // Clean the shortcut |
| 1134 | DeleteFile(lnk_path.c_str()); |
| 1135 | } |
| 1136 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1137 | |
| 1138 | // Custom URLRequestJobs for use with interceptor tests |
| 1139 | class RestartTestJob : public URLRequestTestJob { |
| 1140 | public: |
| 1141 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1142 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1143 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1144 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1145 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1146 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1147 | }; |
| 1148 | |
| 1149 | class CancelTestJob : public URLRequestTestJob { |
| 1150 | public: |
| 1151 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1152 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1153 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1154 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1155 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1156 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1157 | }; |
| 1158 | |
| 1159 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1160 | public: |
| 1161 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1162 | NetworkDelegate* network_delegate) |
| 1163 | : URLRequestTestJob(request, network_delegate, true) { |
| 1164 | } |
| 1165 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1166 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1167 | request_->Cancel(); |
| 1168 | this->NotifyRestartRequired(); |
| 1169 | } |
| 1170 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1171 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1172 | }; |
| 1173 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1174 | // An Interceptor for use with interceptor tests. |
| 1175 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1176 | public: |
| 1177 | // Static getters for canned response header and data strings. |
| 1178 | static std::string ok_data() { |
| 1179 | return URLRequestTestJob::test_data_1(); |
| 1180 | } |
| 1181 | |
| 1182 | static std::string ok_headers() { |
| 1183 | return URLRequestTestJob::test_headers(); |
| 1184 | } |
| 1185 | |
| 1186 | static std::string redirect_data() { |
| 1187 | return std::string(); |
| 1188 | } |
| 1189 | |
| 1190 | static std::string redirect_headers() { |
| 1191 | return URLRequestTestJob::test_redirect_headers(); |
| 1192 | } |
| 1193 | |
| 1194 | static std::string error_data() { |
| 1195 | return std::string("ohhh nooooo mr. bill!"); |
| 1196 | } |
| 1197 | |
| 1198 | static std::string error_headers() { |
| 1199 | return URLRequestTestJob::test_error_headers(); |
| 1200 | } |
| 1201 | |
| 1202 | MockURLRequestInterceptor() |
| 1203 | : intercept_main_request_(false), restart_main_request_(false), |
| 1204 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1205 | simulate_main_network_error_(false), |
| 1206 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1207 | intercept_final_response_(false), cancel_final_request_(false), |
| 1208 | use_url_request_http_job_(false), |
| 1209 | did_intercept_main_(false), did_restart_main_(false), |
| 1210 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1211 | did_simulate_error_main_(false), |
| 1212 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1213 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1214 | } |
| 1215 | |
| 1216 | ~MockURLRequestInterceptor() override { |
| 1217 | } |
| 1218 | |
| 1219 | // URLRequestInterceptor implementation: |
| 1220 | URLRequestJob* MaybeInterceptRequest( |
| 1221 | URLRequest* request, |
| 1222 | NetworkDelegate* network_delegate) const override { |
| 1223 | if (restart_main_request_) { |
| 1224 | restart_main_request_ = false; |
| 1225 | did_restart_main_ = true; |
| 1226 | return new RestartTestJob(request, network_delegate); |
| 1227 | } |
| 1228 | if (cancel_main_request_) { |
| 1229 | cancel_main_request_ = false; |
| 1230 | did_cancel_main_ = true; |
| 1231 | return new CancelTestJob(request, network_delegate); |
| 1232 | } |
| 1233 | if (cancel_then_restart_main_request_) { |
| 1234 | cancel_then_restart_main_request_ = false; |
| 1235 | did_cancel_then_restart_main_ = true; |
| 1236 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1237 | } |
| 1238 | if (simulate_main_network_error_) { |
| 1239 | simulate_main_network_error_ = false; |
| 1240 | did_simulate_error_main_ = true; |
| 1241 | if (use_url_request_http_job_) { |
| 1242 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1243 | } |
| 1244 | // This job will result in error since the requested URL is not one of the |
| 1245 | // URLs supported by these tests. |
| 1246 | return new URLRequestTestJob(request, network_delegate, true); |
| 1247 | } |
| 1248 | if (!intercept_main_request_) |
| 1249 | return nullptr; |
| 1250 | intercept_main_request_ = false; |
| 1251 | did_intercept_main_ = true; |
| 1252 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1253 | network_delegate, |
| 1254 | main_headers_, |
| 1255 | main_data_, |
| 1256 | true); |
| 1257 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1258 | return job; |
| 1259 | } |
| 1260 | |
| 1261 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1262 | NetworkDelegate* network_delegate, |
| 1263 | const GURL& location) const override { |
| 1264 | if (cancel_redirect_request_) { |
| 1265 | cancel_redirect_request_ = false; |
| 1266 | did_cancel_redirect_ = true; |
| 1267 | return new CancelTestJob(request, network_delegate); |
| 1268 | } |
| 1269 | if (!intercept_redirect_) |
| 1270 | return nullptr; |
| 1271 | intercept_redirect_ = false; |
| 1272 | did_intercept_redirect_ = true; |
| 1273 | if (use_url_request_http_job_) { |
| 1274 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1275 | } |
| 1276 | return new URLRequestTestJob(request, |
| 1277 | network_delegate, |
| 1278 | redirect_headers_, |
| 1279 | redirect_data_, |
| 1280 | true); |
| 1281 | } |
| 1282 | |
| 1283 | URLRequestJob* MaybeInterceptResponse( |
| 1284 | URLRequest* request, |
| 1285 | NetworkDelegate* network_delegate) const override { |
| 1286 | if (cancel_final_request_) { |
| 1287 | cancel_final_request_ = false; |
| 1288 | did_cancel_final_ = true; |
| 1289 | return new CancelTestJob(request, network_delegate); |
| 1290 | } |
| 1291 | if (!intercept_final_response_) |
| 1292 | return nullptr; |
| 1293 | intercept_final_response_ = false; |
| 1294 | did_intercept_final_ = true; |
| 1295 | if (use_url_request_http_job_) { |
| 1296 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1297 | } |
| 1298 | return new URLRequestTestJob(request, |
| 1299 | network_delegate, |
| 1300 | final_headers_, |
| 1301 | final_data_, |
| 1302 | true); |
| 1303 | } |
| 1304 | |
| 1305 | void set_intercept_main_request(bool intercept_main_request) { |
| 1306 | intercept_main_request_ = intercept_main_request; |
| 1307 | } |
| 1308 | |
| 1309 | void set_main_headers(const std::string& main_headers) { |
| 1310 | main_headers_ = main_headers; |
| 1311 | } |
| 1312 | |
| 1313 | void set_main_data(const std::string& main_data) { |
| 1314 | main_data_ = main_data; |
| 1315 | } |
| 1316 | |
| 1317 | void set_main_request_load_timing_info( |
| 1318 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1319 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1320 | } |
| 1321 | |
| 1322 | void set_restart_main_request(bool restart_main_request) { |
| 1323 | restart_main_request_ = restart_main_request; |
| 1324 | } |
| 1325 | |
| 1326 | void set_cancel_main_request(bool cancel_main_request) { |
| 1327 | cancel_main_request_ = cancel_main_request; |
| 1328 | } |
| 1329 | |
| 1330 | void set_cancel_then_restart_main_request( |
| 1331 | bool cancel_then_restart_main_request) { |
| 1332 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1333 | } |
| 1334 | |
| 1335 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1336 | simulate_main_network_error_ = simulate_main_network_error; |
| 1337 | } |
| 1338 | |
| 1339 | void set_intercept_redirect(bool intercept_redirect) { |
| 1340 | intercept_redirect_ = intercept_redirect; |
| 1341 | } |
| 1342 | |
| 1343 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1344 | redirect_headers_ = redirect_headers; |
| 1345 | } |
| 1346 | |
| 1347 | void set_redirect_data(const std::string& redirect_data) { |
| 1348 | redirect_data_ = redirect_data; |
| 1349 | } |
| 1350 | |
| 1351 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1352 | cancel_redirect_request_ = cancel_redirect_request; |
| 1353 | } |
| 1354 | |
| 1355 | void set_intercept_final_response(bool intercept_final_response) { |
| 1356 | intercept_final_response_ = intercept_final_response; |
| 1357 | } |
| 1358 | |
| 1359 | void set_final_headers(const std::string& final_headers) { |
| 1360 | final_headers_ = final_headers; |
| 1361 | } |
| 1362 | |
| 1363 | void set_final_data(const std::string& final_data) { |
| 1364 | final_data_ = final_data; |
| 1365 | } |
| 1366 | |
| 1367 | void set_cancel_final_request(bool cancel_final_request) { |
| 1368 | cancel_final_request_ = cancel_final_request; |
| 1369 | } |
| 1370 | |
| 1371 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1372 | use_url_request_http_job_ = use_url_request_http_job; |
| 1373 | } |
| 1374 | |
| 1375 | bool did_intercept_main() const { |
| 1376 | return did_intercept_main_; |
| 1377 | } |
| 1378 | |
| 1379 | bool did_restart_main() const { |
| 1380 | return did_restart_main_; |
| 1381 | } |
| 1382 | |
| 1383 | bool did_cancel_main() const { |
| 1384 | return did_cancel_main_; |
| 1385 | } |
| 1386 | |
| 1387 | bool did_cancel_then_restart_main() const { |
| 1388 | return did_cancel_then_restart_main_; |
| 1389 | } |
| 1390 | |
| 1391 | bool did_simulate_error_main() const { |
| 1392 | return did_simulate_error_main_; |
| 1393 | } |
| 1394 | |
| 1395 | bool did_intercept_redirect() const { |
| 1396 | return did_intercept_redirect_; |
| 1397 | } |
| 1398 | |
| 1399 | bool did_cancel_redirect() const { |
| 1400 | return did_cancel_redirect_; |
| 1401 | } |
| 1402 | |
| 1403 | bool did_intercept_final() const { |
| 1404 | return did_intercept_final_; |
| 1405 | } |
| 1406 | |
| 1407 | bool did_cancel_final() const { |
| 1408 | return did_cancel_final_; |
| 1409 | } |
| 1410 | |
| 1411 | private: |
| 1412 | // Indicate whether to intercept the main request, and if so specify the |
| 1413 | // response to return and the LoadTimingInfo to use. |
| 1414 | mutable bool intercept_main_request_; |
| 1415 | mutable std::string main_headers_; |
| 1416 | mutable std::string main_data_; |
| 1417 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1418 | |
| 1419 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1420 | mutable bool restart_main_request_; |
| 1421 | mutable bool cancel_main_request_; |
| 1422 | mutable bool cancel_then_restart_main_request_; |
| 1423 | mutable bool simulate_main_network_error_; |
| 1424 | |
| 1425 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1426 | // return. |
| 1427 | mutable bool intercept_redirect_; |
| 1428 | mutable std::string redirect_headers_; |
| 1429 | mutable std::string redirect_data_; |
| 1430 | |
| 1431 | // Cancel the request within MaybeInterceptRedirect. |
| 1432 | mutable bool cancel_redirect_request_; |
| 1433 | |
| 1434 | // Indicate whether to intercept the final response, and if so specify the |
| 1435 | // response to return. |
| 1436 | mutable bool intercept_final_response_; |
| 1437 | mutable std::string final_headers_; |
| 1438 | mutable std::string final_data_; |
| 1439 | |
| 1440 | // Cancel the final request within MaybeInterceptResponse. |
| 1441 | mutable bool cancel_final_request_; |
| 1442 | |
| 1443 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1444 | mutable bool use_url_request_http_job_; |
| 1445 | |
| 1446 | // These indicate if the interceptor did something or not. |
| 1447 | mutable bool did_intercept_main_; |
| 1448 | mutable bool did_restart_main_; |
| 1449 | mutable bool did_cancel_main_; |
| 1450 | mutable bool did_cancel_then_restart_main_; |
| 1451 | mutable bool did_simulate_error_main_; |
| 1452 | mutable bool did_intercept_redirect_; |
| 1453 | mutable bool did_cancel_redirect_; |
| 1454 | mutable bool did_intercept_final_; |
| 1455 | mutable bool did_cancel_final_; |
| 1456 | }; |
| 1457 | |
| 1458 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1459 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1460 | public: |
| 1461 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1462 | } |
| 1463 | |
| 1464 | ~URLRequestInterceptorTest() override { |
| 1465 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1466 | base::RunLoop().RunUntilIdle(); |
| 1467 | } |
| 1468 | |
| 1469 | void SetUpFactory() override { |
| 1470 | interceptor_ = new MockURLRequestInterceptor(); |
| 1471 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
| 1472 | job_factory_.Pass(), make_scoped_ptr(interceptor_))); |
| 1473 | } |
| 1474 | |
| 1475 | MockURLRequestInterceptor* interceptor() const { |
| 1476 | return interceptor_; |
| 1477 | } |
| 1478 | |
| 1479 | private: |
| 1480 | MockURLRequestInterceptor* interceptor_; |
| 1481 | }; |
| 1482 | |
| 1483 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1484 | // Intercept the main request and respond with a simple response. |
| 1485 | interceptor()->set_intercept_main_request(true); |
| 1486 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1487 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1488 | TestDelegate d; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1489 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1490 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1491 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1492 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1493 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
| 1494 | req->SetUserData(nullptr, user_data0); |
| 1495 | req->SetUserData(&user_data1, user_data1); |
| 1496 | req->SetUserData(&user_data2, user_data2); |
| 1497 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1498 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1499 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1500 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1501 | // Make sure we can retrieve our specific user data. |
| 1502 | EXPECT_EQ(user_data0, req->GetUserData(nullptr)); |
| 1503 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1504 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1505 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1506 | // Check that we got one good response. |
| 1507 | EXPECT_TRUE(req->status().is_success()); |
| 1508 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1509 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1510 | EXPECT_EQ(1, d.response_started_count()); |
| 1511 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1512 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1513 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1514 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1515 | // Intercept the main request and respond with a redirect. |
| 1516 | interceptor()->set_intercept_main_request(true); |
| 1517 | interceptor()->set_main_headers( |
| 1518 | MockURLRequestInterceptor::redirect_headers()); |
| 1519 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1520 | |
| 1521 | // Intercept that redirect and respond with a final OK response. |
| 1522 | interceptor()->set_intercept_redirect(true); |
| 1523 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1524 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1525 | |
| 1526 | TestDelegate d; |
| 1527 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1528 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1529 | req->set_method("GET"); |
| 1530 | req->Start(); |
| 1531 | base::RunLoop().Run(); |
| 1532 | |
| 1533 | // Check that the interceptor got called as expected. |
| 1534 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1535 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1536 | |
| 1537 | // Check that we got one good response. |
| 1538 | EXPECT_TRUE(req->status().is_success()); |
| 1539 | if (req->status().is_success()) |
| 1540 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1541 | |
| 1542 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1543 | EXPECT_EQ(1, d.response_started_count()); |
| 1544 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1545 | } |
| 1546 | |
| 1547 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1548 | // Intercept the main request to generate a server error response. |
| 1549 | interceptor()->set_intercept_main_request(true); |
| 1550 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1551 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1552 | |
| 1553 | // Intercept that error and respond with an OK response. |
| 1554 | interceptor()->set_intercept_final_response(true); |
| 1555 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1556 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1557 | |
| 1558 | TestDelegate d; |
| 1559 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1560 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1561 | req->set_method("GET"); |
| 1562 | req->Start(); |
| 1563 | base::RunLoop().Run(); |
| 1564 | |
| 1565 | // Check that the interceptor got called as expected. |
| 1566 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1567 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1568 | |
| 1569 | // Check that we got one good response. |
| 1570 | EXPECT_TRUE(req->status().is_success()); |
| 1571 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1572 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1573 | EXPECT_EQ(1, d.response_started_count()); |
| 1574 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1575 | } |
| 1576 | |
| 1577 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1578 | // Intercept the main request to simulate a network error. |
| 1579 | interceptor()->set_simulate_main_network_error(true); |
| 1580 | |
| 1581 | // Intercept that error and respond with an OK response. |
| 1582 | interceptor()->set_intercept_final_response(true); |
| 1583 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1584 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1585 | |
| 1586 | TestDelegate d; |
| 1587 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1588 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1589 | req->set_method("GET"); |
| 1590 | req->Start(); |
| 1591 | base::RunLoop().Run(); |
| 1592 | |
| 1593 | // Check that the interceptor got called as expected. |
| 1594 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1595 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1596 | |
| 1597 | // Check that we received one good response. |
| 1598 | EXPECT_TRUE(req->status().is_success()); |
| 1599 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1600 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1601 | EXPECT_EQ(1, d.response_started_count()); |
| 1602 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1603 | } |
| 1604 | |
| 1605 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1606 | // Restart the main request. |
| 1607 | interceptor()->set_restart_main_request(true); |
| 1608 | |
| 1609 | // then intercept the new main request and respond with an OK response |
| 1610 | interceptor()->set_intercept_main_request(true); |
| 1611 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1612 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1613 | |
| 1614 | TestDelegate d; |
| 1615 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1616 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1617 | req->set_method("GET"); |
| 1618 | req->Start(); |
| 1619 | base::RunLoop().Run(); |
| 1620 | |
| 1621 | // Check that the interceptor got called as expected. |
| 1622 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1623 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1624 | |
| 1625 | // Check that we received one good response. |
| 1626 | EXPECT_TRUE(req->status().is_success()); |
| 1627 | if (req->status().is_success()) |
| 1628 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1629 | |
| 1630 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1631 | EXPECT_EQ(1, d.response_started_count()); |
| 1632 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1633 | } |
| 1634 | |
| 1635 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1636 | // Intercept the main request and cancel from within the restarted job. |
| 1637 | interceptor()->set_cancel_main_request(true); |
| 1638 | |
| 1639 | // Set up to intercept the final response and override it with an OK response. |
| 1640 | interceptor()->set_intercept_final_response(true); |
| 1641 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1642 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1643 | |
| 1644 | TestDelegate d; |
| 1645 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1646 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1647 | req->set_method("GET"); |
| 1648 | req->Start(); |
| 1649 | base::RunLoop().Run(); |
| 1650 | |
| 1651 | // Check that the interceptor got called as expected. |
| 1652 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 1653 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1654 | |
| 1655 | // Check that we see a canceled request. |
| 1656 | EXPECT_FALSE(req->status().is_success()); |
| 1657 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1658 | } |
| 1659 | |
| 1660 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 1661 | // Intercept the main request and respond with a redirect. |
| 1662 | interceptor()->set_intercept_main_request(true); |
| 1663 | interceptor()->set_main_headers( |
| 1664 | MockURLRequestInterceptor::redirect_headers()); |
| 1665 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1666 | |
| 1667 | // Intercept the redirect and cancel from within that job. |
| 1668 | interceptor()->set_cancel_redirect_request(true); |
| 1669 | |
| 1670 | // Set up to intercept the final response and override it with an OK response. |
| 1671 | interceptor()->set_intercept_final_response(true); |
| 1672 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1673 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1674 | |
| 1675 | TestDelegate d; |
| 1676 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1677 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1678 | req->set_method("GET"); |
| 1679 | req->Start(); |
| 1680 | base::RunLoop().Run(); |
| 1681 | |
| 1682 | // Check that the interceptor got called as expected. |
| 1683 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1684 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 1685 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1686 | |
| 1687 | // Check that we see a canceled request. |
| 1688 | EXPECT_FALSE(req->status().is_success()); |
| 1689 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1690 | } |
| 1691 | |
| 1692 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 1693 | // Intercept the main request to simulate a network error. |
| 1694 | interceptor()->set_simulate_main_network_error(true); |
| 1695 | |
| 1696 | // Set up to intercept final the response and cancel from within that job. |
| 1697 | interceptor()->set_cancel_final_request(true); |
| 1698 | |
| 1699 | TestDelegate d; |
| 1700 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1701 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1702 | req->set_method("GET"); |
| 1703 | req->Start(); |
| 1704 | base::RunLoop().Run(); |
| 1705 | |
| 1706 | // Check that the interceptor got called as expected. |
| 1707 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1708 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 1709 | |
| 1710 | // Check that we see a canceled request. |
| 1711 | EXPECT_FALSE(req->status().is_success()); |
| 1712 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1713 | } |
| 1714 | |
| 1715 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 1716 | // Intercept the main request and cancel then restart from within that job. |
| 1717 | interceptor()->set_cancel_then_restart_main_request(true); |
| 1718 | |
| 1719 | // Set up to intercept the final response and override it with an OK response. |
| 1720 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 1721 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1722 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1723 | |
| 1724 | TestDelegate d; |
| 1725 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1726 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1727 | req->set_method("GET"); |
| 1728 | req->Start(); |
| 1729 | base::RunLoop().Run(); |
| 1730 | |
| 1731 | // Check that the interceptor got called as expected. |
| 1732 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 1733 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1734 | |
| 1735 | // Check that we see a canceled request. |
| 1736 | EXPECT_FALSE(req->status().is_success()); |
| 1737 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 1741 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 1742 | // or SSL times, and |used_proxy| is used for proxy times. |
| 1743 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 1744 | int connect_time_flags, |
| 1745 | bool used_proxy) { |
| 1746 | LoadTimingInfo load_timing; |
| 1747 | load_timing.socket_log_id = 1; |
| 1748 | |
| 1749 | if (used_proxy) { |
| 1750 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1751 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1752 | } |
| 1753 | |
| 1754 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 1755 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 1756 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 1757 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 1758 | } |
| 1759 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 1760 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 1761 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 1762 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 1763 | } |
| 1764 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 1765 | |
| 1766 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1767 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1768 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1769 | return load_timing; |
| 1770 | } |
| 1771 | |
| 1772 | // Same as above, but in the case of a reused socket. |
| 1773 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 1774 | bool used_proxy) { |
| 1775 | LoadTimingInfo load_timing; |
| 1776 | load_timing.socket_log_id = 1; |
| 1777 | load_timing.socket_reused = true; |
| 1778 | |
| 1779 | if (used_proxy) { |
| 1780 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1781 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1782 | } |
| 1783 | |
| 1784 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1785 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1786 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1787 | return load_timing; |
| 1788 | } |
| 1789 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1790 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 1791 | const LoadTimingInfo& job_load_timing, |
| 1792 | const URLRequestContext& context, |
| 1793 | MockURLRequestInterceptor* interceptor) { |
| 1794 | interceptor->set_intercept_main_request(true); |
| 1795 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 1796 | TestDelegate d; |
| 1797 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1798 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1799 | req->Start(); |
| 1800 | base::RunLoop().Run(); |
| 1801 | |
| 1802 | LoadTimingInfo resulting_load_timing; |
| 1803 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 1804 | |
| 1805 | // None of these should be modified by the URLRequest. |
| 1806 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1807 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1808 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1809 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1810 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1811 | resulting_load_timing.receive_headers_end); |
| 1812 | |
| 1813 | return resulting_load_timing; |
| 1814 | } |
| 1815 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1816 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1817 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1818 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1819 | LoadTimingInfo job_load_timing = |
| 1820 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1821 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1822 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1823 | RunURLRequestInterceptorLoadTimingTest( |
| 1824 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1825 | |
| 1826 | // Nothing should have been changed by the URLRequest. |
| 1827 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1828 | load_timing_result.proxy_resolve_start); |
| 1829 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1830 | load_timing_result.proxy_resolve_end); |
| 1831 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1832 | load_timing_result.connect_timing.dns_start); |
| 1833 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1834 | load_timing_result.connect_timing.dns_end); |
| 1835 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1836 | load_timing_result.connect_timing.connect_start); |
| 1837 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1838 | load_timing_result.connect_timing.connect_end); |
| 1839 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1840 | load_timing_result.connect_timing.ssl_start); |
| 1841 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1842 | load_timing_result.connect_timing.ssl_end); |
| 1843 | |
| 1844 | // Redundant sanity check. |
| 1845 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1846 | } |
| 1847 | |
| 1848 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1849 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1850 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1851 | LoadTimingInfo job_load_timing = |
| 1852 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1853 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1854 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1855 | RunURLRequestInterceptorLoadTimingTest( |
| 1856 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1857 | |
| 1858 | // Nothing should have been changed by the URLRequest. |
| 1859 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1860 | load_timing_result.proxy_resolve_start); |
| 1861 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1862 | load_timing_result.proxy_resolve_end); |
| 1863 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1864 | load_timing_result.connect_timing.dns_start); |
| 1865 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1866 | load_timing_result.connect_timing.dns_end); |
| 1867 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1868 | load_timing_result.connect_timing.connect_start); |
| 1869 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1870 | load_timing_result.connect_timing.connect_end); |
| 1871 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1872 | load_timing_result.connect_timing.ssl_start); |
| 1873 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1874 | load_timing_result.connect_timing.ssl_end); |
| 1875 | |
| 1876 | // Redundant sanity check. |
| 1877 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1878 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 1879 | } |
| 1880 | |
| 1881 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1882 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 1883 | // the case of reusing a SPDY session. The connected socket is not considered |
| 1884 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1885 | // |
| 1886 | // To mix things up from the test above, assumes DNS times but no SSL times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1887 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1888 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1889 | LoadTimingInfo job_load_timing = |
| 1890 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 1891 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 1892 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 1893 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 1894 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 1895 | job_load_timing.connect_timing.connect_start = |
| 1896 | now - base::TimeDelta::FromDays(2); |
| 1897 | job_load_timing.connect_timing.connect_end = |
| 1898 | now - base::TimeDelta::FromDays(1); |
| 1899 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1900 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1901 | RunURLRequestInterceptorLoadTimingTest( |
| 1902 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1903 | |
| 1904 | // Proxy times, connect times, and DNS times should all be replaced with |
| 1905 | // request_start. |
| 1906 | EXPECT_EQ(load_timing_result.request_start, |
| 1907 | load_timing_result.proxy_resolve_start); |
| 1908 | EXPECT_EQ(load_timing_result.request_start, |
| 1909 | load_timing_result.proxy_resolve_end); |
| 1910 | EXPECT_EQ(load_timing_result.request_start, |
| 1911 | load_timing_result.connect_timing.dns_start); |
| 1912 | EXPECT_EQ(load_timing_result.request_start, |
| 1913 | load_timing_result.connect_timing.dns_end); |
| 1914 | EXPECT_EQ(load_timing_result.request_start, |
| 1915 | load_timing_result.connect_timing.connect_start); |
| 1916 | EXPECT_EQ(load_timing_result.request_start, |
| 1917 | load_timing_result.connect_timing.connect_end); |
| 1918 | |
| 1919 | // Other times should have been left null. |
| 1920 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1921 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 1922 | } |
| 1923 | |
| 1924 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1925 | TEST_F(URLRequestInterceptorTest, |
| 1926 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1927 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1928 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 1929 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 1930 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 1931 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1932 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1933 | RunURLRequestInterceptorLoadTimingTest( |
| 1934 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1935 | |
| 1936 | // Proxy times and connect times should all be replaced with request_start. |
| 1937 | EXPECT_EQ(load_timing_result.request_start, |
| 1938 | load_timing_result.proxy_resolve_start); |
| 1939 | EXPECT_EQ(load_timing_result.request_start, |
| 1940 | load_timing_result.proxy_resolve_end); |
| 1941 | |
| 1942 | // Other times should have been left null. |
| 1943 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 1944 | } |
| 1945 | |
| 1946 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1947 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 1948 | // not considered reused in this test (May be a preconnect). |
| 1949 | // |
| 1950 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1951 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1952 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1953 | LoadTimingInfo job_load_timing = |
| 1954 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 1955 | job_load_timing.connect_timing.connect_start = |
| 1956 | now - base::TimeDelta::FromDays(1); |
| 1957 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 1958 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 1959 | job_load_timing.connect_timing.connect_end = |
| 1960 | now - base::TimeDelta::FromDays(4); |
| 1961 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1962 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1963 | RunURLRequestInterceptorLoadTimingTest( |
| 1964 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1965 | |
| 1966 | // Connect times, and SSL times should be replaced with request_start. |
| 1967 | EXPECT_EQ(load_timing_result.request_start, |
| 1968 | load_timing_result.connect_timing.connect_start); |
| 1969 | EXPECT_EQ(load_timing_result.request_start, |
| 1970 | load_timing_result.connect_timing.ssl_start); |
| 1971 | EXPECT_EQ(load_timing_result.request_start, |
| 1972 | load_timing_result.connect_timing.ssl_end); |
| 1973 | EXPECT_EQ(load_timing_result.request_start, |
| 1974 | load_timing_result.connect_timing.connect_end); |
| 1975 | |
| 1976 | // Other times should have been left null. |
| 1977 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 1978 | } |
| 1979 | |
| 1980 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1981 | // |request_start|, due to reusing a connected socket in the case that there |
| 1982 | // are also proxy times. The connected socket is not considered reused in this |
| 1983 | // test (May be a preconnect). |
| 1984 | // |
| 1985 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1986 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1987 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1988 | LoadTimingInfo job_load_timing = |
| 1989 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 1990 | job_load_timing.connect_timing.connect_start = |
| 1991 | now - base::TimeDelta::FromDays(1); |
| 1992 | job_load_timing.connect_timing.connect_end = |
| 1993 | now - base::TimeDelta::FromDays(2); |
| 1994 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1995 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1996 | RunURLRequestInterceptorLoadTimingTest( |
| 1997 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1998 | |
| 1999 | // Connect times should be replaced with proxy_resolve_end. |
| 2000 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2001 | load_timing_result.connect_timing.connect_start); |
| 2002 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 2003 | load_timing_result.connect_timing.connect_end); |
| 2004 | |
| 2005 | // Other times should have been left null. |
| 2006 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 2007 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 2008 | } |
| 2009 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2010 | // Check that two different URL requests have different identifiers. |
| 2011 | TEST_F(URLRequestTest, Identifiers) { |
| 2012 | TestDelegate d; |
| 2013 | TestURLRequestContext context; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2014 | scoped_ptr<URLRequest> req( |
| 2015 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
| 2016 | scoped_ptr<URLRequest> other_req( |
| 2017 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2018 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2019 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | // Check that a failure to connect to the proxy is reported to the network |
| 2023 | // delegate. |
| 2024 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
| 2025 | MockHostResolver host_resolver; |
| 2026 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2027 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2028 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2029 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2030 | |
| 2031 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2032 | scoped_ptr<URLRequest> req( |
| 2033 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2034 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2035 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2036 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2037 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2038 | |
| 2039 | // Check we see a failed request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2040 | EXPECT_FALSE(req->status().is_success()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2041 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2042 | EXPECT_TRUE(req->proxy_server().IsEmpty()); |
| 2043 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 2044 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2045 | |
| 2046 | EXPECT_EQ(1, network_delegate.error_count()); |
| 2047 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
| 2048 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2049 | } |
| 2050 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2051 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2052 | // content is empty. |
| 2053 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2054 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2055 | scoped_ptr<URLRequest> req( |
| 2056 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2057 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2058 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2059 | EXPECT_EQ("", d.data_received()); |
| 2060 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2061 | } |
| 2062 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2063 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2064 | // correctly, both before and after start. |
| 2065 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2066 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2067 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2068 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2069 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2070 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2071 | req->SetPriority(LOW); |
| 2072 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2073 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2074 | req->Start(); |
| 2075 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2076 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2077 | req->SetPriority(MEDIUM); |
| 2078 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2079 | } |
| 2080 | |
| 2081 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2082 | // Start on it. |
| 2083 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2084 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2085 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2086 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2087 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2088 | |
| 2089 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2090 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2091 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2092 | EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 2093 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2094 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2095 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2096 | req->Start(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2097 | EXPECT_EQ(LOW, job->priority()); |
| 2098 | } |
| 2099 | |
| 2100 | // Make sure that URLRequest passes on its priority updates to its |
| 2101 | // job. |
| 2102 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2103 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2104 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2105 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2106 | |
| 2107 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2108 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2109 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2110 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2111 | req->SetPriority(LOW); |
| 2112 | req->Start(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2113 | EXPECT_EQ(LOW, job->priority()); |
| 2114 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2115 | req->SetPriority(MEDIUM); |
| 2116 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2117 | EXPECT_EQ(MEDIUM, job->priority()); |
| 2118 | } |
| 2119 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2120 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2121 | // is MAXIMUM_PRIORITY. |
| 2122 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2123 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2124 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2125 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2126 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2127 | |
| 2128 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2129 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2130 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
| 2131 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2132 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2133 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2134 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2135 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2136 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2137 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2138 | req->Start(); |
| 2139 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2140 | EXPECT_EQ(MAXIMUM_PRIORITY, job->priority()); |
| 2141 | } |
| 2142 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2143 | // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666). |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2144 | #if !defined(OS_IOS) |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2145 | namespace { |
| 2146 | |
| 2147 | // Less verbose way of running a simple testserver for the tests below. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2148 | class LocalHttpTestServer : public SpawnedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2149 | public: |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 2150 | explicit LocalHttpTestServer(const base::FilePath& document_root) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2151 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2152 | SpawnedTestServer::kLocalhost, |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2153 | document_root) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2154 | LocalHttpTestServer() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2155 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2156 | SpawnedTestServer::kLocalhost, |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2157 | base::FilePath()) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2158 | }; |
| 2159 | |
nick | 5d570de9 | 2015-05-04 20:16:16 | [diff] [blame] | 2160 | } // namespace |
| 2161 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2162 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2163 | LocalHttpTestServer test_server; |
| 2164 | ASSERT_TRUE(test_server.Start()); |
| 2165 | |
| 2166 | TestURLRequestContext context; |
| 2167 | scoped_refptr<DelayedCookieMonster> delayed_cm = |
| 2168 | new DelayedCookieMonster(); |
| 2169 | scoped_refptr<CookieStore> cookie_store = delayed_cm; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2170 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2171 | |
| 2172 | // Set up a cookie. |
| 2173 | { |
| 2174 | TestNetworkDelegate network_delegate; |
| 2175 | context.set_network_delegate(&network_delegate); |
| 2176 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2177 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2178 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2179 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2180 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2181 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2182 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2183 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2184 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2185 | } |
| 2186 | |
| 2187 | // Verify that the cookie is set. |
| 2188 | { |
| 2189 | TestNetworkDelegate network_delegate; |
| 2190 | context.set_network_delegate(&network_delegate); |
| 2191 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2192 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2193 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2194 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2195 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2196 | |
| 2197 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2198 | != std::string::npos); |
| 2199 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2200 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2201 | } |
| 2202 | } |
| 2203 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2204 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2205 | LocalHttpTestServer test_server; |
| 2206 | ASSERT_TRUE(test_server.Start()); |
| 2207 | |
| 2208 | // Set up a cookie. |
| 2209 | { |
| 2210 | TestNetworkDelegate network_delegate; |
| 2211 | default_context_.set_network_delegate(&network_delegate); |
| 2212 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2213 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2214 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2215 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2216 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2217 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2218 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2219 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2220 | } |
| 2221 | |
| 2222 | // Verify that the cookie is set. |
| 2223 | { |
| 2224 | TestNetworkDelegate network_delegate; |
| 2225 | default_context_.set_network_delegate(&network_delegate); |
| 2226 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2227 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2228 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2229 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2230 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2231 | |
| 2232 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2233 | != std::string::npos); |
| 2234 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2235 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2236 | } |
| 2237 | |
| 2238 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2239 | { |
| 2240 | TestNetworkDelegate network_delegate; |
| 2241 | default_context_.set_network_delegate(&network_delegate); |
| 2242 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2243 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2244 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2245 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2246 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2247 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2248 | |
| 2249 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2250 | == std::string::npos); |
| 2251 | |
| 2252 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2253 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2254 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2259 | LocalHttpTestServer test_server; |
| 2260 | ASSERT_TRUE(test_server.Start()); |
| 2261 | |
| 2262 | // Set up a cookie. |
| 2263 | { |
| 2264 | TestNetworkDelegate network_delegate; |
| 2265 | default_context_.set_network_delegate(&network_delegate); |
| 2266 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2267 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2268 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2269 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2270 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2271 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2272 | |
| 2273 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2274 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2275 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2276 | } |
| 2277 | |
| 2278 | // Try to set-up another cookie and update the previous cookie. |
| 2279 | { |
| 2280 | TestNetworkDelegate network_delegate; |
| 2281 | default_context_.set_network_delegate(&network_delegate); |
| 2282 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2283 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2284 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2285 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2286 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2287 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2288 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2289 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2290 | |
| 2291 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2292 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2293 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2294 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2295 | } |
| 2296 | |
| 2297 | // Verify the cookies weren't saved or updated. |
| 2298 | { |
| 2299 | TestNetworkDelegate network_delegate; |
| 2300 | default_context_.set_network_delegate(&network_delegate); |
| 2301 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2302 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2303 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2304 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2305 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2306 | |
| 2307 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2308 | == std::string::npos); |
| 2309 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2310 | != std::string::npos); |
| 2311 | |
| 2312 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2313 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2314 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2319 | LocalHttpTestServer test_server; |
| 2320 | ASSERT_TRUE(test_server.Start()); |
| 2321 | |
| 2322 | // Set up a cookie. |
| 2323 | { |
| 2324 | TestNetworkDelegate network_delegate; |
| 2325 | default_context_.set_network_delegate(&network_delegate); |
| 2326 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2327 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2328 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2329 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2330 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2331 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2332 | |
| 2333 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2334 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2335 | } |
| 2336 | |
| 2337 | // Verify that the cookie is set. |
| 2338 | { |
| 2339 | TestNetworkDelegate network_delegate; |
| 2340 | default_context_.set_network_delegate(&network_delegate); |
| 2341 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2342 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2343 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2344 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2345 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2346 | |
| 2347 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2348 | != std::string::npos); |
| 2349 | |
| 2350 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2351 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2352 | } |
| 2353 | |
| 2354 | // Verify that the cookie isn't sent. |
| 2355 | { |
| 2356 | TestNetworkDelegate network_delegate; |
| 2357 | default_context_.set_network_delegate(&network_delegate); |
| 2358 | TestDelegate d; |
| 2359 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2360 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2361 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2362 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2363 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2364 | |
| 2365 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2366 | == std::string::npos); |
| 2367 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2368 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2369 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2370 | } |
| 2371 | } |
| 2372 | |
| 2373 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2374 | LocalHttpTestServer test_server; |
| 2375 | ASSERT_TRUE(test_server.Start()); |
| 2376 | |
| 2377 | // Set up a cookie. |
| 2378 | { |
| 2379 | TestNetworkDelegate network_delegate; |
| 2380 | default_context_.set_network_delegate(&network_delegate); |
| 2381 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2382 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2383 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2384 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2385 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2386 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2387 | |
| 2388 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2389 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2390 | } |
| 2391 | |
| 2392 | // Try to set-up another cookie and update the previous cookie. |
| 2393 | { |
| 2394 | TestNetworkDelegate network_delegate; |
| 2395 | default_context_.set_network_delegate(&network_delegate); |
| 2396 | TestDelegate d; |
| 2397 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2398 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2399 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2400 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2401 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2402 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2403 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2404 | |
| 2405 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2406 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2407 | } |
| 2408 | |
| 2409 | // Verify the cookies weren't saved or updated. |
| 2410 | { |
| 2411 | TestNetworkDelegate network_delegate; |
| 2412 | default_context_.set_network_delegate(&network_delegate); |
| 2413 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2414 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2415 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2416 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2417 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2418 | |
| 2419 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2420 | == std::string::npos); |
| 2421 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2422 | != std::string::npos); |
| 2423 | |
| 2424 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2425 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2430 | LocalHttpTestServer test_server; |
| 2431 | ASSERT_TRUE(test_server.Start()); |
| 2432 | |
| 2433 | // Set up an empty cookie. |
| 2434 | { |
| 2435 | TestNetworkDelegate network_delegate; |
| 2436 | default_context_.set_network_delegate(&network_delegate); |
| 2437 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2438 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2439 | test_server.GetURL("set-cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2440 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2441 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2442 | |
| 2443 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2444 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2445 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2446 | } |
| 2447 | } |
| 2448 | |
| 2449 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2450 | LocalHttpTestServer test_server; |
| 2451 | ASSERT_TRUE(test_server.Start()); |
| 2452 | |
| 2453 | // Set up a cookie. |
| 2454 | { |
| 2455 | TestNetworkDelegate network_delegate; |
| 2456 | default_context_.set_network_delegate(&network_delegate); |
| 2457 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2458 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2459 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2460 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2461 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2462 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2463 | |
| 2464 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2465 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2466 | } |
| 2467 | |
| 2468 | // Verify that the cookie is set. |
| 2469 | { |
| 2470 | TestNetworkDelegate network_delegate; |
| 2471 | default_context_.set_network_delegate(&network_delegate); |
| 2472 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2473 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2474 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2475 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2476 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2477 | |
| 2478 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2479 | != std::string::npos); |
| 2480 | |
| 2481 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2482 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2483 | } |
| 2484 | |
| 2485 | // Verify that the cookie isn't sent. |
| 2486 | { |
| 2487 | TestNetworkDelegate network_delegate; |
| 2488 | default_context_.set_network_delegate(&network_delegate); |
| 2489 | TestDelegate d; |
| 2490 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2491 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2492 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2493 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2494 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2495 | |
| 2496 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2497 | == std::string::npos); |
| 2498 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2499 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2500 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2505 | LocalHttpTestServer test_server; |
| 2506 | ASSERT_TRUE(test_server.Start()); |
| 2507 | |
| 2508 | // Set up a cookie. |
| 2509 | { |
| 2510 | TestNetworkDelegate network_delegate; |
| 2511 | default_context_.set_network_delegate(&network_delegate); |
| 2512 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2513 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2514 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2515 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2516 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2517 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2518 | |
| 2519 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2520 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2521 | } |
| 2522 | |
| 2523 | // Try to set-up another cookie and update the previous cookie. |
| 2524 | { |
| 2525 | TestNetworkDelegate network_delegate; |
| 2526 | default_context_.set_network_delegate(&network_delegate); |
| 2527 | TestDelegate d; |
| 2528 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2529 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2530 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2531 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2532 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2533 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2534 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2535 | |
| 2536 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2537 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2538 | } |
| 2539 | |
| 2540 | // Verify the cookies weren't saved or updated. |
| 2541 | { |
| 2542 | TestNetworkDelegate network_delegate; |
| 2543 | default_context_.set_network_delegate(&network_delegate); |
| 2544 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2545 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2546 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2547 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2548 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2549 | |
| 2550 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2551 | == std::string::npos); |
| 2552 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2553 | != std::string::npos); |
| 2554 | |
| 2555 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2556 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2557 | } |
| 2558 | } |
| 2559 | |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2560 | TEST_F(URLRequestTest, FirstPartyOnlyCookiesEnabled) { |
| 2561 | LocalHttpTestServer test_server; |
| 2562 | ASSERT_TRUE(test_server.Start()); |
| 2563 | |
| 2564 | // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where |
| 2565 | // LocalHttpTestServer points). |
| 2566 | { |
| 2567 | TestNetworkDelegate network_delegate; |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2568 | network_delegate.set_first_party_only_cookies_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2569 | default_context_.set_network_delegate(&network_delegate); |
| 2570 | |
| 2571 | TestDelegate d; |
| 2572 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2573 | test_server.GetURL( |
| 2574 | "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2575 | DEFAULT_PRIORITY, &d)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2576 | req->Start(); |
| 2577 | base::RunLoop().Run(); |
| 2578 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2579 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2580 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2581 | } |
| 2582 | |
| 2583 | // Verify that the cookie is sent for first-party requests. |
| 2584 | { |
| 2585 | TestNetworkDelegate network_delegate; |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2586 | network_delegate.set_first_party_only_cookies_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2587 | default_context_.set_network_delegate(&network_delegate); |
| 2588 | TestDelegate d; |
| 2589 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2590 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | 92df3fd | 2015-04-06 19:57:47 | [diff] [blame] | 2591 | req->set_first_party_for_cookies(test_server.GetURL("")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2592 | req->Start(); |
| 2593 | base::RunLoop().Run(); |
| 2594 | |
| 2595 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
| 2596 | std::string::npos); |
| 2597 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2598 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2599 | } |
| 2600 | |
| 2601 | // Verify that the cookie is not-sent for non-first-party requests. |
| 2602 | { |
| 2603 | TestNetworkDelegate network_delegate; |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2604 | network_delegate.set_first_party_only_cookies_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2605 | default_context_.set_network_delegate(&network_delegate); |
| 2606 | TestDelegate d; |
| 2607 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2608 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2609 | req->set_first_party_for_cookies(GURL("http://third-party.test/")); |
| 2610 | req->Start(); |
| 2611 | base::RunLoop().Run(); |
| 2612 | |
| 2613 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") == |
| 2614 | std::string::npos); |
| 2615 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2616 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2617 | } |
| 2618 | } |
| 2619 | |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2620 | TEST_F(URLRequestTest, FirstPartyOnlyCookiesDisabled) { |
| 2621 | LocalHttpTestServer test_server; |
| 2622 | ASSERT_TRUE(test_server.Start()); |
| 2623 | |
| 2624 | // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where |
| 2625 | // LocalHttpTestServer points). |
| 2626 | { |
| 2627 | TestNetworkDelegate network_delegate; |
| 2628 | network_delegate.set_first_party_only_cookies_enabled(false); |
| 2629 | default_context_.set_network_delegate(&network_delegate); |
| 2630 | |
| 2631 | TestDelegate d; |
| 2632 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2633 | test_server.GetURL( |
| 2634 | "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"), |
| 2635 | DEFAULT_PRIORITY, &d)); |
| 2636 | req->Start(); |
| 2637 | base::RunLoop().Run(); |
| 2638 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2639 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2640 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2641 | } |
| 2642 | |
| 2643 | // Verify that the cookie is sent for first-party requests. |
| 2644 | { |
| 2645 | TestNetworkDelegate network_delegate; |
| 2646 | network_delegate.set_first_party_only_cookies_enabled(false); |
| 2647 | default_context_.set_network_delegate(&network_delegate); |
| 2648 | TestDelegate d; |
| 2649 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2650 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | 92df3fd | 2015-04-06 19:57:47 | [diff] [blame] | 2651 | req->set_first_party_for_cookies(test_server.GetURL("")); |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2652 | req->Start(); |
| 2653 | base::RunLoop().Run(); |
| 2654 | |
| 2655 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
| 2656 | std::string::npos); |
| 2657 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2658 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2659 | } |
| 2660 | |
| 2661 | // Verify that the cookie is also sent for non-first-party requests. |
| 2662 | { |
| 2663 | TestNetworkDelegate network_delegate; |
| 2664 | network_delegate.set_first_party_only_cookies_enabled(false); |
| 2665 | default_context_.set_network_delegate(&network_delegate); |
| 2666 | TestDelegate d; |
| 2667 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2668 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2669 | req->set_first_party_for_cookies(GURL("http://third-party.test/")); |
| 2670 | req->Start(); |
| 2671 | base::RunLoop().Run(); |
| 2672 | |
| 2673 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
| 2674 | std::string::npos); |
| 2675 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2676 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2677 | } |
| 2678 | } |
| 2679 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2680 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 2681 | // value for the |fixed_date| argument given to the constructor. |
| 2682 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 2683 | public: |
| 2684 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 2685 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 2686 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2687 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2688 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 2689 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2690 | URLRequest* request, |
| 2691 | const CompletionCallback& callback, |
| 2692 | const HttpResponseHeaders* original_response_headers, |
| 2693 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 2694 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2695 | |
| 2696 | private: |
| 2697 | std::string fixed_date_; |
| 2698 | |
| 2699 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 2700 | }; |
| 2701 | |
| 2702 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2703 | URLRequest* request, |
| 2704 | const CompletionCallback& callback, |
| 2705 | const HttpResponseHeaders* original_response_headers, |
| 2706 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2707 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2708 | HttpResponseHeaders* new_response_headers = |
| 2709 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2710 | |
| 2711 | new_response_headers->RemoveHeader("Date"); |
| 2712 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 2713 | |
| 2714 | *override_response_headers = new_response_headers; |
| 2715 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 2716 | callback, |
| 2717 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2718 | override_response_headers, |
| 2719 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2720 | } |
| 2721 | |
| 2722 | // Test that cookie expiration times are adjusted for server/client clock |
| 2723 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 2724 | // headers by defaulting to GMT. (crbug.com/135131) |
| 2725 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 2726 | LocalHttpTestServer test_server; |
| 2727 | ASSERT_TRUE(test_server.Start()); |
| 2728 | |
| 2729 | // Set up an expired cookie. |
| 2730 | { |
| 2731 | TestNetworkDelegate network_delegate; |
| 2732 | default_context_.set_network_delegate(&network_delegate); |
| 2733 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2734 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2735 | test_server.GetURL( |
| 2736 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2737 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2738 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2739 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2740 | } |
| 2741 | // Verify that the cookie is not set. |
| 2742 | { |
| 2743 | TestNetworkDelegate network_delegate; |
| 2744 | default_context_.set_network_delegate(&network_delegate); |
| 2745 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2746 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2747 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2748 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2749 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2750 | |
| 2751 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 2752 | } |
| 2753 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 2754 | { |
| 2755 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 2756 | default_context_.set_network_delegate(&network_delegate); |
| 2757 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2758 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2759 | test_server.GetURL( |
| 2760 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2761 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2762 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2763 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2764 | } |
| 2765 | // Verify that the cookie is set. |
| 2766 | { |
| 2767 | TestNetworkDelegate network_delegate; |
| 2768 | default_context_.set_network_delegate(&network_delegate); |
| 2769 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2770 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2771 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2772 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2773 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2774 | |
| 2775 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2780 | // Check that it is impossible to change the referrer in the extra headers of |
| 2781 | // an URLRequest. |
| 2782 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 2783 | LocalHttpTestServer test_server; |
| 2784 | ASSERT_TRUE(test_server.Start()); |
| 2785 | |
| 2786 | // If extra headers contain referer and the request contains a referer, |
| 2787 | // only the latter shall be respected. |
| 2788 | { |
| 2789 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2790 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2791 | test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2792 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2793 | |
| 2794 | HttpRequestHeaders headers; |
| 2795 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2796 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2797 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2798 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2799 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2800 | |
| 2801 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 2802 | } |
| 2803 | |
| 2804 | // If extra headers contain a referer but the request does not, no referer |
| 2805 | // shall be sent in the header. |
| 2806 | { |
| 2807 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2808 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2809 | test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2810 | |
| 2811 | HttpRequestHeaders headers; |
| 2812 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2813 | req->SetExtraRequestHeaders(headers); |
| 2814 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2815 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2816 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2817 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2818 | |
| 2819 | EXPECT_EQ("None", d.data_received()); |
| 2820 | } |
| 2821 | } |
| 2822 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2823 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2824 | public: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 2825 | URLRequestTestHTTP() : test_server_(base::FilePath(kTestFilePath)) {} |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2826 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2827 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2828 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 2829 | // |request_method| is the method to use for the initial request. |
| 2830 | // |redirect_method| is the method that is expected to be used for the second |
| 2831 | // request, after redirection. |
| 2832 | // If |include_data| is true, data is uploaded with the request. The |
| 2833 | // response body is expected to match it exactly, if and only if |
| 2834 | // |request_method| == |redirect_method|. |
| 2835 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 2836 | const std::string& request_method, |
| 2837 | const std::string& redirect_method, |
| 2838 | bool include_data) { |
| 2839 | static const char kData[] = "hello world"; |
| 2840 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2841 | scoped_ptr<URLRequest> req( |
| 2842 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2843 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2844 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 2845 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2846 | HttpRequestHeaders headers; |
| 2847 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 2848 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2849 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2850 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2851 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2852 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2853 | EXPECT_EQ(redirect_method, req->method()); |
| 2854 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 2855 | EXPECT_EQ(OK, req->status().error()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2856 | if (include_data) { |
| 2857 | if (request_method == redirect_method) { |
| 2858 | EXPECT_EQ(kData, d.data_received()); |
| 2859 | } else { |
| 2860 | EXPECT_NE(kData, d.data_received()); |
| 2861 | } |
| 2862 | } |
| 2863 | if (HasFailure()) |
| 2864 | LOG(WARNING) << "Request method was: " << request_method; |
| 2865 | } |
| 2866 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 2867 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 2868 | // |request_method| is the method to use for the initial request. |
| 2869 | // |redirect_method| is the method that is expected to be used for the second |
| 2870 | // request, after redirection. |
| 2871 | // |origin_value| is the expected value for the Origin header after |
| 2872 | // redirection. If empty, expects that there will be no Origin header. |
| 2873 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 2874 | const std::string& request_method, |
| 2875 | const std::string& redirect_method, |
| 2876 | const std::string& origin_value) { |
| 2877 | TestDelegate d; |
| 2878 | scoped_ptr<URLRequest> req( |
| 2879 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 2880 | req->set_method(request_method); |
| 2881 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 2882 | redirect_url.GetOrigin().spec(), false); |
| 2883 | req->Start(); |
| 2884 | |
| 2885 | base::RunLoop().Run(); |
| 2886 | |
| 2887 | EXPECT_EQ(redirect_method, req->method()); |
| 2888 | // Note that there is no check for request success here because, for |
| 2889 | // purposes of testing, the request very well may fail. For example, if the |
| 2890 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 2891 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 2892 | // request would fail. However, that's fine, as long as the request headers |
| 2893 | // are in order and pass the checks below. |
| 2894 | if (origin_value.empty()) { |
| 2895 | EXPECT_FALSE( |
| 2896 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 2897 | } else { |
| 2898 | std::string origin_header; |
| 2899 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 2900 | HttpRequestHeaders::kOrigin, &origin_header)); |
| 2901 | EXPECT_EQ(origin_value, origin_header); |
| 2902 | } |
| 2903 | } |
| 2904 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2905 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2906 | const int kMsgSize = 20000; // multiple of 10 |
| 2907 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 2908 | char* uploadBytes = new char[kMsgSize+1]; |
| 2909 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2910 | char marker = 'a'; |
| 2911 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 2912 | memcpy(ptr, "----------", 10); |
| 2913 | ptr += 10; |
| 2914 | if (idx % 100 == 0) { |
| 2915 | ptr--; |
| 2916 | *ptr++ = marker; |
| 2917 | if (++marker > 'z') |
| 2918 | marker = 'a'; |
| 2919 | } |
| 2920 | } |
| 2921 | uploadBytes[kMsgSize] = '\0'; |
| 2922 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2923 | for (int i = 0; i < kIterations; ++i) { |
| 2924 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2925 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2926 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2927 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2928 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 2929 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2930 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2931 | r->Start(); |
| 2932 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2933 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2934 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2935 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2936 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2937 | << "request failed: " << r->status().status() |
| 2938 | << ", os error: " << r->status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2939 | |
| 2940 | EXPECT_FALSE(d.received_data_before_response()); |
| 2941 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2942 | } |
| 2943 | delete[] uploadBytes; |
| 2944 | } |
| 2945 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2946 | void AddChunksToUpload(URLRequest* r) { |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 2947 | r->AppendChunkToUpload("a", 1, false); |
| 2948 | r->AppendChunkToUpload("bcd", 3, false); |
| 2949 | r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); |
| 2950 | r->AppendChunkToUpload("\r\n\r\n", 4, false); |
| 2951 | r->AppendChunkToUpload("0", 1, false); |
| 2952 | r->AppendChunkToUpload("2323", 4, true); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2953 | } |
| 2954 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2955 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2956 | // This should match the chunks sent by AddChunksToUpload(). |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2957 | const std::string expected_data = |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2958 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 2959 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2960 | ASSERT_EQ(1, d->response_started_count()) |
| 2961 | << "request failed: " << r->status().status() |
| 2962 | << ", os error: " << r->status().error(); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2963 | |
| 2964 | EXPECT_FALSE(d->received_data_before_response()); |
| 2965 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2966 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 2967 | EXPECT_EQ(expected_data, d->data_received()); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2968 | } |
| 2969 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2970 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2971 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2972 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 2973 | test_server_.GetURL("set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2974 | base::IntToString(num_cookies)), |
| 2975 | DEFAULT_PRIORITY, &d)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2976 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2977 | r->Start(); |
| 2978 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2979 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2980 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2981 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2982 | bool is_success = r->status().is_success(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2983 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2984 | if (!is_success) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2985 | EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2986 | // The test server appears to be unable to handle subsequent requests |
| 2987 | // after this error is triggered. Force it to restart. |
| 2988 | EXPECT_TRUE(test_server_.Stop()); |
| 2989 | EXPECT_TRUE(test_server_.Start()); |
| 2990 | } |
| 2991 | |
| 2992 | return is_success; |
| 2993 | } |
| 2994 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2995 | LocalHttpTestServer* test_server() { |
| 2996 | return &test_server_; |
| 2997 | } |
| 2998 | |
| 2999 | protected: |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 3000 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3001 | }; |
| 3002 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3003 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 3004 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 3005 | // The HTTPTestServer will return a 302 response, which we should not |
| 3006 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3007 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3008 | ASSERT_TRUE(test_server_.Start()); |
| 3009 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3010 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3011 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3012 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3013 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3014 | TestDelegate d; |
| 3015 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3016 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3017 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3018 | r->Start(); |
| 3019 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3020 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3021 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3022 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3023 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3024 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3025 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3026 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3027 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3028 | // We should not have followed the redirect. |
| 3029 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3030 | } |
| 3031 | } |
| 3032 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3033 | // This is the same as the previous test, but checks that the network delegate |
| 3034 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3035 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3036 | ASSERT_TRUE(test_server_.Start()); |
| 3037 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3038 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3039 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3040 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3041 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3042 | TestDelegate d; |
| 3043 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3044 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3045 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3046 | r->Start(); |
| 3047 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3048 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3049 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3050 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3051 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3052 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3053 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3054 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3055 | EXPECT_EQ(1, d.response_started_count()); |
| 3056 | // We should not have followed the redirect. |
| 3057 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3058 | |
| 3059 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 3060 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3061 | } |
| 3062 | } |
| 3063 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3064 | // Tests that we can block and asynchronously return OK in various stages. |
| 3065 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 3066 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 3067 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3068 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3069 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 3070 | }; |
| 3071 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 3072 | |
| 3073 | ASSERT_TRUE(test_server_.Start()); |
| 3074 | |
| 3075 | TestDelegate d; |
| 3076 | BlockingNetworkDelegate network_delegate( |
| 3077 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3078 | network_delegate.set_block_on( |
| 3079 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 3080 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 3081 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3082 | |
| 3083 | TestURLRequestContext context(true); |
| 3084 | context.set_network_delegate(&network_delegate); |
| 3085 | context.Init(); |
| 3086 | |
| 3087 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3088 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3089 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3090 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3091 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3092 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3093 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3094 | EXPECT_EQ(blocking_stages[i], |
| 3095 | network_delegate.stage_blocked_for_callback()); |
| 3096 | network_delegate.DoCallback(OK); |
| 3097 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3098 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3099 | EXPECT_EQ(200, r->GetResponseCode()); |
| 3100 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3101 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3102 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3103 | } |
| 3104 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3105 | } |
| 3106 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3107 | // Tests that the network delegate can block and cancel a request. |
| 3108 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
| 3109 | ASSERT_TRUE(test_server_.Start()); |
| 3110 | |
| 3111 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3112 | BlockingNetworkDelegate network_delegate( |
| 3113 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3114 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 3115 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3116 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3117 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3118 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3119 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3120 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3121 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3122 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3123 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3124 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3125 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3126 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3127 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3128 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3129 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3130 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3131 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3132 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3133 | } |
| 3134 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3135 | } |
| 3136 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3137 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 3138 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 3139 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 3140 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 3141 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 3142 | BlockingNetworkDelegate::Stage stage, |
| 3143 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3144 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3145 | BlockingNetworkDelegate network_delegate(block_mode); |
| 3146 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 3147 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3148 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3149 | TestURLRequestContext context(true); |
| 3150 | context.set_network_delegate(&network_delegate); |
| 3151 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3152 | |
| 3153 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3154 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3155 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3156 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3157 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3158 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3159 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3160 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3161 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3162 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3163 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3164 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3165 | } |
| 3166 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3167 | } |
| 3168 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3169 | // The following 3 tests check that the network delegate can cancel a request |
| 3170 | // synchronously in various stages of the request. |
| 3171 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
| 3172 | ASSERT_TRUE(test_server_.Start()); |
| 3173 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3174 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3175 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
| 3179 | ASSERT_TRUE(test_server_.Start()); |
| 3180 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3181 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3182 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
| 3186 | ASSERT_TRUE(test_server_.Start()); |
| 3187 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3188 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3189 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | // The following 3 tests check that the network delegate can cancel a request |
| 3193 | // asynchronously in various stages of the request. |
| 3194 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
| 3195 | ASSERT_TRUE(test_server_.Start()); |
| 3196 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3197 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3198 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
| 3202 | ASSERT_TRUE(test_server_.Start()); |
| 3203 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3204 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3205 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3206 | } |
| 3207 | |
| 3208 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
| 3209 | ASSERT_TRUE(test_server_.Start()); |
| 3210 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3211 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3212 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3213 | } |
| 3214 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3215 | // Tests that the network delegate can block and redirect a request to a new |
| 3216 | // URL. |
| 3217 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
| 3218 | ASSERT_TRUE(test_server_.Start()); |
| 3219 | |
| 3220 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3221 | BlockingNetworkDelegate network_delegate( |
| 3222 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3223 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3224 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 3225 | network_delegate.set_redirect_url(redirect_url); |
| 3226 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3227 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3228 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3229 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3230 | { |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3231 | GURL original_url(test_server_.GetURL("empty.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3232 | scoped_ptr<URLRequest> r( |
| 3233 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3234 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3235 | // Quit after hitting the redirect, so can check the headers. |
| 3236 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3237 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3238 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3239 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3240 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3241 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3242 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3243 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3244 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3245 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3246 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3247 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3248 | |
| 3249 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3250 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3251 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3252 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3253 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3254 | EXPECT_EQ( |
| 3255 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 3256 | EXPECT_TRUE( |
| 3257 | network_delegate.last_observed_proxy().Equals( |
| 3258 | test_server_.host_port_pair())); |
| 3259 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3260 | EXPECT_EQ(0, r->status().error()); |
| 3261 | EXPECT_EQ(redirect_url, r->url()); |
| 3262 | EXPECT_EQ(original_url, r->original_url()); |
| 3263 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3264 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3265 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3266 | } |
| 3267 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3268 | } |
| 3269 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3270 | // Tests that the network delegate can block and redirect a request to a new |
| 3271 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 3272 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
| 3273 | ASSERT_TRUE(test_server_.Start()); |
| 3274 | |
| 3275 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3276 | BlockingNetworkDelegate network_delegate( |
| 3277 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3278 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 3279 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3280 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3281 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3282 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3283 | |
| 3284 | { |
| 3285 | GURL original_url(test_server_.GetURL("empty.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3286 | scoped_ptr<URLRequest> r( |
| 3287 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3288 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3289 | // Quit after hitting the redirect, so can check the headers. |
| 3290 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3291 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3292 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3293 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3294 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3295 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3296 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3297 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3298 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3299 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3300 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3301 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3302 | |
| 3303 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3304 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3305 | base::RunLoop().Run(); |
| 3306 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3307 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3308 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3309 | EXPECT_EQ( |
| 3310 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 3311 | EXPECT_TRUE( |
| 3312 | network_delegate.last_observed_proxy().Equals( |
| 3313 | test_server_.host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3314 | EXPECT_EQ(0, r->status().error()); |
| 3315 | EXPECT_EQ(redirect_url, r->url()); |
| 3316 | EXPECT_EQ(original_url, r->original_url()); |
| 3317 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3318 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3319 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3320 | } |
| 3321 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3322 | } |
| 3323 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3324 | // Tests that redirects caused by the network delegate preserve POST data. |
| 3325 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 3326 | ASSERT_TRUE(test_server_.Start()); |
| 3327 | |
| 3328 | const char kData[] = "hello world"; |
| 3329 | |
| 3330 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3331 | BlockingNetworkDelegate network_delegate( |
| 3332 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3333 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3334 | GURL redirect_url(test_server_.GetURL("echo")); |
| 3335 | network_delegate.set_redirect_url(redirect_url); |
| 3336 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3337 | TestURLRequestContext context(true); |
| 3338 | context.set_network_delegate(&network_delegate); |
| 3339 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3340 | |
| 3341 | { |
| 3342 | GURL original_url(test_server_.GetURL("empty.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3343 | scoped_ptr<URLRequest> r( |
| 3344 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3345 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3346 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3347 | HttpRequestHeaders headers; |
| 3348 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3349 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3350 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3351 | |
| 3352 | // Quit after hitting the redirect, so can check the headers. |
| 3353 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3354 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3355 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3356 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3357 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3358 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3359 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3360 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3361 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3362 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3363 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3364 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3365 | |
| 3366 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3367 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3368 | base::RunLoop().Run(); |
| 3369 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3370 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3371 | EXPECT_EQ(0, r->status().error()); |
| 3372 | EXPECT_EQ(redirect_url, r->url()); |
| 3373 | EXPECT_EQ(original_url, r->original_url()); |
| 3374 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3375 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3376 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3377 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3378 | EXPECT_EQ(kData, d.data_received()); |
| 3379 | } |
| 3380 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3381 | } |
| 3382 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3383 | // Tests that the network delegate can block and redirect a request to a new |
| 3384 | // URL during OnHeadersReceived. |
| 3385 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
| 3386 | ASSERT_TRUE(test_server_.Start()); |
| 3387 | |
| 3388 | TestDelegate d; |
| 3389 | BlockingNetworkDelegate network_delegate( |
| 3390 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3391 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3392 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 3393 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 3394 | |
| 3395 | TestURLRequestContextWithProxy context( |
| 3396 | test_server_.host_port_pair().ToString(), &network_delegate); |
| 3397 | |
| 3398 | { |
| 3399 | GURL original_url(test_server_.GetURL("empty.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3400 | scoped_ptr<URLRequest> r( |
| 3401 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3402 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3403 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3404 | base::RunLoop().Run(); |
| 3405 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3406 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3407 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3408 | EXPECT_EQ( |
| 3409 | 2, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 3410 | EXPECT_TRUE( |
| 3411 | network_delegate.last_observed_proxy().Equals( |
| 3412 | test_server_.host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3413 | |
| 3414 | EXPECT_EQ(OK, r->status().error()); |
| 3415 | EXPECT_EQ(redirect_url, r->url()); |
| 3416 | EXPECT_EQ(original_url, r->original_url()); |
| 3417 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3418 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 3419 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3420 | } |
| 3421 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3422 | } |
| 3423 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3424 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3425 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 3426 | // handle the challenge, and is passing the buck along to the |
| 3427 | // URLRequest::Delegate. |
| 3428 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 3429 | ASSERT_TRUE(test_server_.Start()); |
| 3430 | |
| 3431 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3432 | BlockingNetworkDelegate network_delegate( |
| 3433 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3434 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3435 | TestURLRequestContext context(true); |
| 3436 | context.set_network_delegate(&network_delegate); |
| 3437 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3438 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3439 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3440 | |
| 3441 | { |
| 3442 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3443 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3444 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3445 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3446 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3447 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3448 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3449 | EXPECT_EQ(0, r->status().error()); |
| 3450 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3451 | EXPECT_TRUE(d.auth_required_called()); |
| 3452 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3453 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3454 | } |
| 3455 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3456 | } |
| 3457 | |
| 3458 | TEST_F(URLRequestTestHTTP, |
| 3459 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
| 3460 | ASSERT_TRUE(test_server_.Start()); |
| 3461 | |
| 3462 | TestDelegate d; |
| 3463 | BlockingNetworkDelegate network_delegate( |
| 3464 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3465 | |
| 3466 | TestURLRequestContext context(true); |
| 3467 | context.set_network_delegate(&network_delegate); |
| 3468 | context.Init(); |
| 3469 | |
| 3470 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3471 | |
| 3472 | { |
| 3473 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3474 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3475 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3476 | |
| 3477 | { |
| 3478 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3479 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3480 | EXPECT_FALSE(headers.HasHeader("Authorization")); |
| 3481 | } |
| 3482 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3483 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3484 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3485 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3486 | EXPECT_EQ(0, r->status().error()); |
| 3487 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3488 | EXPECT_TRUE(d.auth_required_called()); |
| 3489 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3490 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3491 | } |
| 3492 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3493 | } |
| 3494 | |
| 3495 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 3496 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3497 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 3498 | ASSERT_TRUE(test_server_.Start()); |
| 3499 | |
| 3500 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3501 | BlockingNetworkDelegate network_delegate( |
| 3502 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3503 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3504 | network_delegate.set_auth_retval( |
| 3505 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3506 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3507 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3508 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3509 | TestURLRequestContext context(true); |
| 3510 | context.set_network_delegate(&network_delegate); |
| 3511 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3512 | |
| 3513 | { |
| 3514 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3515 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3516 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3517 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3518 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3519 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3520 | EXPECT_EQ(0, r->status().error()); |
| 3521 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3522 | EXPECT_FALSE(d.auth_required_called()); |
| 3523 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3524 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3525 | } |
| 3526 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3527 | } |
| 3528 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3529 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 3530 | // headers (for the first or second request) when called at the proper times. |
| 3531 | TEST_F(URLRequestTestHTTP, |
| 3532 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
| 3533 | ASSERT_TRUE(test_server_.Start()); |
| 3534 | |
| 3535 | TestDelegate d; |
| 3536 | BlockingNetworkDelegate network_delegate( |
| 3537 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3538 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 3539 | network_delegate.set_auth_retval( |
| 3540 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3541 | |
| 3542 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 3543 | |
| 3544 | TestURLRequestContext context(true); |
| 3545 | context.set_network_delegate(&network_delegate); |
| 3546 | context.Init(); |
| 3547 | |
| 3548 | { |
| 3549 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3550 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3551 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3552 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3553 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3554 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3555 | EXPECT_EQ(0, r->status().error()); |
| 3556 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3557 | EXPECT_FALSE(d.auth_required_called()); |
| 3558 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3559 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3560 | |
| 3561 | { |
| 3562 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3563 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3564 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 3565 | } |
| 3566 | } |
| 3567 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3568 | } |
| 3569 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3570 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3571 | // by cancelling authentication. |
| 3572 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 3573 | ASSERT_TRUE(test_server_.Start()); |
| 3574 | |
| 3575 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3576 | BlockingNetworkDelegate network_delegate( |
| 3577 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3578 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3579 | network_delegate.set_auth_retval( |
| 3580 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3581 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3582 | TestURLRequestContext context(true); |
| 3583 | context.set_network_delegate(&network_delegate); |
| 3584 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3585 | |
| 3586 | { |
| 3587 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3588 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3589 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3590 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3591 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3592 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3593 | EXPECT_EQ(OK, r->status().error()); |
| 3594 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3595 | EXPECT_FALSE(d.auth_required_called()); |
| 3596 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3597 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3598 | } |
| 3599 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3600 | } |
| 3601 | |
| 3602 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3603 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 3604 | // to handle the challenge, and is passing the buck along to the |
| 3605 | // URLRequest::Delegate. |
| 3606 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 3607 | ASSERT_TRUE(test_server_.Start()); |
| 3608 | |
| 3609 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3610 | BlockingNetworkDelegate network_delegate( |
| 3611 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3612 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3613 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3614 | TestURLRequestContext context(true); |
| 3615 | context.set_network_delegate(&network_delegate); |
| 3616 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3617 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3618 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3619 | |
| 3620 | { |
| 3621 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3622 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3623 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3624 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3625 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3626 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3627 | EXPECT_EQ(0, r->status().error()); |
| 3628 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3629 | EXPECT_TRUE(d.auth_required_called()); |
| 3630 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3631 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3632 | } |
| 3633 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3634 | } |
| 3635 | |
| 3636 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3637 | // by setting credentials. |
| 3638 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 3639 | ASSERT_TRUE(test_server_.Start()); |
| 3640 | |
| 3641 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3642 | BlockingNetworkDelegate network_delegate( |
| 3643 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3644 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3645 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3646 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3647 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3648 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3649 | network_delegate.set_auth_credentials(auth_credentials); |
| 3650 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3651 | TestURLRequestContext context(true); |
| 3652 | context.set_network_delegate(&network_delegate); |
| 3653 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3654 | |
| 3655 | { |
| 3656 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3657 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3658 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3659 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3660 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3661 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3662 | EXPECT_EQ(0, r->status().error()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3663 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3664 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3665 | EXPECT_FALSE(d.auth_required_called()); |
| 3666 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3667 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3668 | } |
| 3669 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3670 | } |
| 3671 | |
| 3672 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3673 | // by cancelling authentication. |
| 3674 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 3675 | ASSERT_TRUE(test_server_.Start()); |
| 3676 | |
| 3677 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3678 | BlockingNetworkDelegate network_delegate( |
| 3679 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3680 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3681 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3682 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3683 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3684 | TestURLRequestContext context(true); |
| 3685 | context.set_network_delegate(&network_delegate); |
| 3686 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3687 | |
| 3688 | { |
| 3689 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3690 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3691 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3692 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3693 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3694 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3695 | EXPECT_EQ(OK, r->status().error()); |
| 3696 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3697 | EXPECT_FALSE(d.auth_required_called()); |
| 3698 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3699 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3700 | } |
| 3701 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3702 | } |
| 3703 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3704 | // Tests that we can handle when a network request was canceled while we were |
| 3705 | // waiting for the network delegate. |
| 3706 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 3707 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 3708 | ASSERT_TRUE(test_server_.Start()); |
| 3709 | |
| 3710 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3711 | BlockingNetworkDelegate network_delegate( |
| 3712 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3713 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3714 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3715 | TestURLRequestContext context(true); |
| 3716 | context.set_network_delegate(&network_delegate); |
| 3717 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3718 | |
| 3719 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3720 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3721 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3722 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3723 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3724 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3725 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3726 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3727 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3728 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3729 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3730 | // Ensure that network delegate is notified. |
| 3731 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3732 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3733 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3734 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3735 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3736 | } |
| 3737 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3738 | } |
| 3739 | |
| 3740 | // Tests that we can handle when a network request was canceled while we were |
| 3741 | // waiting for the network delegate. |
| 3742 | // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 3743 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
| 3744 | ASSERT_TRUE(test_server_.Start()); |
| 3745 | |
| 3746 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3747 | BlockingNetworkDelegate network_delegate( |
| 3748 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3749 | network_delegate.set_block_on( |
| 3750 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3751 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3752 | TestURLRequestContext context(true); |
| 3753 | context.set_network_delegate(&network_delegate); |
| 3754 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3755 | |
| 3756 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3757 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3758 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3759 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3760 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3761 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3762 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3763 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3764 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3765 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3766 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3767 | // Ensure that network delegate is notified. |
| 3768 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3769 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3770 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3771 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3772 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3773 | } |
| 3774 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3775 | } |
| 3776 | |
| 3777 | // Tests that we can handle when a network request was canceled while we were |
| 3778 | // waiting for the network delegate. |
| 3779 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 3780 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 3781 | ASSERT_TRUE(test_server_.Start()); |
| 3782 | |
| 3783 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3784 | BlockingNetworkDelegate network_delegate( |
| 3785 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3786 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3787 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3788 | TestURLRequestContext context(true); |
| 3789 | context.set_network_delegate(&network_delegate); |
| 3790 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3791 | |
| 3792 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3793 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3794 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3795 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3796 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3797 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3798 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 3799 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3800 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3801 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3802 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3803 | // Ensure that network delegate is notified. |
| 3804 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3805 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3806 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3807 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3808 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3809 | } |
| 3810 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3811 | } |
| 3812 | |
| 3813 | // Tests that we can handle when a network request was canceled while we were |
| 3814 | // waiting for the network delegate. |
| 3815 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 3816 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3817 | ASSERT_TRUE(test_server_.Start()); |
| 3818 | |
| 3819 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3820 | BlockingNetworkDelegate network_delegate( |
| 3821 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3822 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3823 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3824 | TestURLRequestContext context(true); |
| 3825 | context.set_network_delegate(&network_delegate); |
| 3826 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3827 | |
| 3828 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3829 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3830 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3831 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3832 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3833 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3834 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 3835 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3836 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3837 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3838 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3839 | // Ensure that network delegate is notified. |
| 3840 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3841 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3842 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3843 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3844 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3845 | } |
| 3846 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3847 | } |
| 3848 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3849 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 3850 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 3851 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3852 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3853 | ASSERT_TRUE(test_server_.Start()); |
| 3854 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3855 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3856 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3857 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3858 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3859 | TestDelegate d; |
| 3860 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3861 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3862 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3863 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3864 | r->Start(); |
| 3865 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3866 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3867 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3868 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3869 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3870 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3871 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3872 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3873 | } |
| 3874 | } |
| 3875 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3876 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3877 | ASSERT_TRUE(test_server_.Start()); |
| 3878 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3879 | TestDelegate d; |
| 3880 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3881 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3882 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3883 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3884 | r->Start(); |
| 3885 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3886 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3887 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3888 | |
| 3889 | EXPECT_EQ(1, d.response_started_count()); |
| 3890 | EXPECT_FALSE(d.received_data_before_response()); |
| 3891 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3892 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3893 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3894 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3895 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 3896 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 3897 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3898 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3899 | } |
| 3900 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3901 | // This test has the server send a large number of cookies to the client. |
| 3902 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 3903 | // search is used to estimate that maximum number of cookies that are accepted |
| 3904 | // by the browser. Beyond the maximum number, the request will fail with |
| 3905 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 3906 | #if defined(OS_WIN) |
| 3907 | // http://crbug.com/177916 |
| 3908 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 3909 | #else |
| 3910 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 3911 | #endif // defined(OS_WIN) |
| 3912 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3913 | ASSERT_TRUE(test_server_.Start()); |
| 3914 | |
| 3915 | int lower_bound = 0; |
| 3916 | int upper_bound = 1; |
| 3917 | |
| 3918 | // Double the number of cookies until the response header limits are |
| 3919 | // exceeded. |
| 3920 | while (DoManyCookiesRequest(upper_bound)) { |
| 3921 | lower_bound = upper_bound; |
| 3922 | upper_bound *= 2; |
| 3923 | ASSERT_LT(upper_bound, 1000000); |
| 3924 | } |
| 3925 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 3926 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3927 | if (tolerance < 2) |
| 3928 | tolerance = 2; |
| 3929 | |
| 3930 | // Perform a binary search to find the highest possible number of cookies, |
| 3931 | // within the desired tolerance. |
| 3932 | while (upper_bound - lower_bound >= tolerance) { |
| 3933 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 3934 | |
| 3935 | if (DoManyCookiesRequest(num_cookies)) |
| 3936 | lower_bound = num_cookies; |
| 3937 | else |
| 3938 | upper_bound = num_cookies; |
| 3939 | } |
| 3940 | // Success: the test did not crash. |
| 3941 | } |
| 3942 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3943 | TEST_F(URLRequestTestHTTP, GetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3944 | ASSERT_TRUE(test_server_.Start()); |
| 3945 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3946 | TestDelegate d; |
| 3947 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3948 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3949 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3950 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3951 | r->Start(); |
| 3952 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3953 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3954 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3955 | |
| 3956 | EXPECT_EQ(1, d.response_started_count()); |
| 3957 | EXPECT_FALSE(d.received_data_before_response()); |
| 3958 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3959 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3960 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3961 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3962 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3963 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 3964 | } |
| 3965 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3966 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
| 3967 | ASSERT_TRUE(test_server_.Start()); |
| 3968 | |
| 3969 | TestDelegate d; |
| 3970 | { |
| 3971 | GURL test_url(test_server_.GetURL(std::string())); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3972 | scoped_ptr<URLRequest> r( |
| 3973 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3974 | |
| 3975 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3976 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3977 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3978 | r->Start(); |
| 3979 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3980 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3981 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3982 | |
| 3983 | EXPECT_EQ(1, d.response_started_count()); |
| 3984 | EXPECT_FALSE(d.received_data_before_response()); |
| 3985 | EXPECT_NE(0, d.bytes_received()); |
| 3986 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3987 | r->GetSocketAddress().host()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3988 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3989 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3990 | |
| 3991 | EXPECT_TRUE(d.have_full_request_headers()); |
| 3992 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 3993 | } |
| 3994 | } |
| 3995 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3996 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
| 3997 | ASSERT_TRUE(test_server_.Start()); |
| 3998 | |
| 3999 | TestDelegate d; |
| 4000 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4001 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4002 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4003 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4004 | r->Start(); |
| 4005 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4006 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4007 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4008 | |
| 4009 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4010 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4011 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4012 | |
| 4013 | EXPECT_EQ(1, d.response_started_count()); |
| 4014 | EXPECT_FALSE(d.received_data_before_response()); |
| 4015 | EXPECT_NE(0, d.bytes_received()); |
| 4016 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4017 | r->GetSocketAddress().host()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4018 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4019 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4020 | } |
| 4021 | } |
| 4022 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4023 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
| 4024 | ASSERT_TRUE(test_server_.Start()); |
| 4025 | |
| 4026 | // Parameter that specifies the Content-Length field in the response: |
| 4027 | // C - Compressed length. |
| 4028 | // U - Uncompressed length. |
| 4029 | // L - Large length (larger than both C & U). |
| 4030 | // M - Medium length (between C & U). |
| 4031 | // S - Small length (smaller than both C & U). |
| 4032 | const char test_parameters[] = "CULMS"; |
| 4033 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 4034 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 4035 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4036 | // S has too little data, but we seem to accept it. |
| 4037 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 4038 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4039 | |
| 4040 | for (int i = 0; i < num_tests ; i++) { |
| 4041 | TestDelegate d; |
| 4042 | { |
| 4043 | std::string test_file = |
| 4044 | base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c", |
| 4045 | test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4046 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4047 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4048 | TestURLRequestContext context(true); |
| 4049 | context.set_network_delegate(&network_delegate); |
| 4050 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4051 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4052 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4053 | test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4054 | r->Start(); |
| 4055 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4056 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4057 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4058 | |
| 4059 | EXPECT_EQ(1, d.response_started_count()); |
| 4060 | EXPECT_FALSE(d.received_data_before_response()); |
| 4061 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4062 | << " status = " << r->status().status() |
| 4063 | << " error = " << r->status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4064 | if (test_expect_success[i]) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4065 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4066 | << " Parameter = \"" << test_file << "\""; |
| 4067 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4068 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 4069 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4070 | << " Parameter = \"" << test_file << "\""; |
| 4071 | } |
| 4072 | } |
| 4073 | } |
| 4074 | } |
| 4075 | |
tbansal | a1caf961 | 2015-06-01 20:24:18 | [diff] [blame] | 4076 | TEST_F(URLRequestTestHTTP, NetworkQualityEstimator) { |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 4077 | ASSERT_TRUE(test_server_.Start()); |
| 4078 | // Enable requests to local host to be used for network quality estimation. |
| 4079 | NetworkQualityEstimator estimator(true); |
| 4080 | |
| 4081 | TestDelegate d; |
| 4082 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 4083 | TestURLRequestContext context(true); |
| 4084 | context.set_network_quality_estimator(&estimator); |
| 4085 | context.set_network_delegate(&network_delegate); |
| 4086 | context.Init(); |
| 4087 | |
| 4088 | uint64_t min_transfer_size_in_bytes = |
| 4089 | NetworkQualityEstimator::kMinTransferSizeInBytes; |
| 4090 | // Create a long enough URL such that response size exceeds network quality |
| 4091 | // estimator's minimum transfer size. |
| 4092 | std::string url = "echo.html?"; |
| 4093 | url.append(min_transfer_size_in_bytes, 'x'); |
| 4094 | |
| 4095 | scoped_ptr<URLRequest> r( |
| 4096 | context.CreateRequest(test_server_.GetURL(url), DEFAULT_PRIORITY, &d)); |
| 4097 | int sleep_duration_milliseconds = 1; |
| 4098 | base::PlatformThread::Sleep( |
| 4099 | base::TimeDelta::FromMilliseconds(sleep_duration_milliseconds)); |
| 4100 | r->Start(); |
| 4101 | |
| 4102 | base::RunLoop().Run(); |
| 4103 | |
| 4104 | NetworkQuality network_quality = |
| 4105 | context.network_quality_estimator()->GetEstimate(); |
| 4106 | EXPECT_GE(network_quality.fastest_rtt, |
| 4107 | base::TimeDelta::FromMilliseconds(sleep_duration_milliseconds)); |
| 4108 | EXPECT_GT(network_quality.fastest_rtt_confidence, 0); |
| 4109 | EXPECT_GT(network_quality.peak_throughput_kbps, uint64_t(0)); |
| 4110 | EXPECT_GT(network_quality.peak_throughput_kbps_confidence, 0); |
| 4111 | |
| 4112 | // Verify that histograms are not populated. They should populate only when |
| 4113 | // there is a change in ConnectionType. |
| 4114 | base::HistogramTester histogram_tester; |
| 4115 | histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 0); |
| 4116 | histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 0); |
| 4117 | |
| 4118 | NetworkChangeNotifier::NotifyObserversOfConnectionTypeChangeForTests( |
| 4119 | NetworkChangeNotifier::ConnectionType::CONNECTION_WIFI); |
| 4120 | base::MessageLoop::current()->RunUntilIdle(); |
| 4121 | histogram_tester.ExpectTotalCount("NQE.PeakKbps.Unknown", 1); |
| 4122 | histogram_tester.ExpectTotalCount("NQE.FastestRTT.Unknown", 1); |
| 4123 | } |
| 4124 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4125 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
| 4126 | ASSERT_TRUE(test_server_.Start()); |
| 4127 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4128 | GURL destination_url = test_server_.GetURL(std::string()); |
| 4129 | GURL original_url = |
| 4130 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4131 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4132 | scoped_ptr<URLRequest> req( |
| 4133 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4134 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4135 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4136 | |
| 4137 | EXPECT_EQ(1, d.response_started_count()); |
| 4138 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4139 | EXPECT_EQ(destination_url, req->url()); |
| 4140 | EXPECT_EQ(original_url, req->original_url()); |
| 4141 | ASSERT_EQ(2U, req->url_chain().size()); |
| 4142 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4143 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4144 | |
| 4145 | LoadTimingInfo load_timing_info_before_redirect; |
| 4146 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 4147 | &load_timing_info_before_redirect)); |
| 4148 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 4149 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 4150 | |
| 4151 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4152 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4153 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4154 | |
| 4155 | // Check that a new socket was used on redirect, since the server does not |
| 4156 | // supposed keep-alive sockets, and that the times before the redirect are |
| 4157 | // before the ones recorded for the second request. |
| 4158 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 4159 | load_timing_info.socket_log_id); |
| 4160 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 4161 | load_timing_info.connect_timing.connect_start); |
| 4162 | } |
| 4163 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4164 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
| 4165 | ASSERT_TRUE(test_server_.Start()); |
| 4166 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4167 | GURL destination_url = test_server_.GetURL(std::string()); |
| 4168 | GURL middle_redirect_url = |
| 4169 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4170 | GURL original_url = test_server_.GetURL( |
| 4171 | "server-redirect?" + middle_redirect_url.spec()); |
| 4172 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4173 | scoped_ptr<URLRequest> req( |
| 4174 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4175 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4176 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4177 | |
| 4178 | EXPECT_EQ(1, d.response_started_count()); |
| 4179 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4180 | EXPECT_EQ(destination_url, req->url()); |
| 4181 | EXPECT_EQ(original_url, req->original_url()); |
| 4182 | ASSERT_EQ(3U, req->url_chain().size()); |
| 4183 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4184 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 4185 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4186 | } |
| 4187 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4188 | // First and second pieces of information logged by delegates to URLRequests. |
| 4189 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 4190 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 4191 | |
| 4192 | // Logs delegate information to a URLRequest. The first string is logged |
| 4193 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 4194 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 4195 | // another asynchronous call is used to clear the delegate information |
| 4196 | // before calling a callback. The object then deletes itself. |
| 4197 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 4198 | public: |
| 4199 | typedef base::Callback<void()> Callback; |
| 4200 | |
| 4201 | // Each time delegate information is added to the URLRequest, the resulting |
| 4202 | // load state is checked. The expected load state after each request is |
| 4203 | // passed in as an argument. |
| 4204 | static void Run(URLRequest* url_request, |
| 4205 | LoadState expected_first_load_state, |
| 4206 | LoadState expected_second_load_state, |
| 4207 | LoadState expected_third_load_state, |
| 4208 | const Callback& callback) { |
| 4209 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 4210 | url_request, |
| 4211 | expected_first_load_state, |
| 4212 | expected_second_load_state, |
| 4213 | expected_third_load_state, |
| 4214 | callback); |
| 4215 | logger->Start(); |
| 4216 | } |
| 4217 | |
| 4218 | // Checks that the log entries, starting with log_position, contain the |
| 4219 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 4220 | // recorded. Returns the index of entry after the expected number of |
| 4221 | // events this logged, or entries.size() if there aren't enough entries. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4222 | static size_t CheckDelegateInfo(const TestNetLogEntry::List& entries, |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4223 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4224 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 4225 | if (log_position + 3 >= entries.size()) { |
| 4226 | ADD_FAILURE() << "Not enough log entries"; |
| 4227 | return entries.size(); |
| 4228 | } |
| 4229 | std::string delegate_info; |
| 4230 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4231 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4232 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4233 | &delegate_info)); |
| 4234 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 4235 | |
| 4236 | ++log_position; |
| 4237 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4238 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4239 | |
| 4240 | ++log_position; |
| 4241 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4242 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4243 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4244 | &delegate_info)); |
| 4245 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 4246 | |
| 4247 | ++log_position; |
| 4248 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4249 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4250 | |
| 4251 | return log_position + 1; |
| 4252 | } |
| 4253 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4254 | // Find delegate request begin and end messages for OnBeforeNetworkStart. |
| 4255 | // Returns the position of the end message. |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4256 | static size_t ExpectBeforeNetworkEvents(const TestNetLogEntry::List& entries, |
| 4257 | size_t log_position) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4258 | log_position = |
| 4259 | ExpectLogContainsSomewhereAfter(entries, |
| 4260 | log_position, |
| 4261 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4262 | NetLog::PHASE_BEGIN); |
| 4263 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4264 | entries[log_position + 1].type); |
| 4265 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase); |
| 4266 | return log_position + 1; |
| 4267 | } |
| 4268 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4269 | private: |
| 4270 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 4271 | |
| 4272 | AsyncDelegateLogger(URLRequest* url_request, |
| 4273 | LoadState expected_first_load_state, |
| 4274 | LoadState expected_second_load_state, |
| 4275 | LoadState expected_third_load_state, |
| 4276 | const Callback& callback) |
| 4277 | : url_request_(url_request), |
| 4278 | expected_first_load_state_(expected_first_load_state), |
| 4279 | expected_second_load_state_(expected_second_load_state), |
| 4280 | expected_third_load_state_(expected_third_load_state), |
| 4281 | callback_(callback) { |
| 4282 | } |
| 4283 | |
| 4284 | ~AsyncDelegateLogger() {} |
| 4285 | |
| 4286 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4287 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4288 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4289 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 4290 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
| 4291 | base::MessageLoop::current()->PostTask( |
| 4292 | FROM_HERE, |
| 4293 | base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
| 4294 | } |
| 4295 | |
| 4296 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4297 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4298 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4299 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 4300 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 4301 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4302 | } else { |
| 4303 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4304 | } |
| 4305 | base::MessageLoop::current()->PostTask( |
| 4306 | FROM_HERE, |
| 4307 | base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
| 4308 | } |
| 4309 | |
| 4310 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4311 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4312 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4313 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 4314 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4315 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4316 | callback_.Run(); |
| 4317 | } |
| 4318 | |
| 4319 | URLRequest* url_request_; |
| 4320 | const int expected_first_load_state_; |
| 4321 | const int expected_second_load_state_; |
| 4322 | const int expected_third_load_state_; |
| 4323 | const Callback callback_; |
| 4324 | |
| 4325 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 4326 | }; |
| 4327 | |
| 4328 | // NetworkDelegate that logs delegate information before a request is started, |
| 4329 | // before headers are sent, when headers are read, and when auth information |
| 4330 | // is requested. Uses AsyncDelegateLogger. |
| 4331 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 4332 | public: |
| 4333 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4334 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4335 | |
| 4336 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4337 | int OnBeforeURLRequest(URLRequest* request, |
| 4338 | const CompletionCallback& callback, |
| 4339 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4340 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 4341 | return RunCallbackAsynchronously(request, callback); |
| 4342 | } |
| 4343 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4344 | int OnBeforeSendHeaders(URLRequest* request, |
| 4345 | const CompletionCallback& callback, |
| 4346 | HttpRequestHeaders* headers) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4347 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 4348 | return RunCallbackAsynchronously(request, callback); |
| 4349 | } |
| 4350 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4351 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4352 | URLRequest* request, |
| 4353 | const CompletionCallback& callback, |
| 4354 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4355 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4356 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4357 | TestNetworkDelegate::OnHeadersReceived(request, |
| 4358 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4359 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4360 | override_response_headers, |
| 4361 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4362 | return RunCallbackAsynchronously(request, callback); |
| 4363 | } |
| 4364 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4365 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4366 | URLRequest* request, |
| 4367 | const AuthChallengeInfo& auth_info, |
| 4368 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4369 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4370 | AsyncDelegateLogger::Run( |
| 4371 | request, |
| 4372 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4373 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4374 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4375 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 4376 | callback, credentials)); |
| 4377 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 4378 | } |
| 4379 | |
| 4380 | private: |
| 4381 | static int RunCallbackAsynchronously( |
| 4382 | URLRequest* request, |
| 4383 | const CompletionCallback& callback) { |
| 4384 | AsyncDelegateLogger::Run( |
| 4385 | request, |
| 4386 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4387 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4388 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4389 | base::Bind(callback, OK)); |
| 4390 | return ERR_IO_PENDING; |
| 4391 | } |
| 4392 | |
| 4393 | static void SetAuthAndResume(const AuthCallback& callback, |
| 4394 | AuthCredentials* credentials) { |
| 4395 | *credentials = AuthCredentials(kUser, kSecret); |
| 4396 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4397 | } |
| 4398 | |
| 4399 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 4400 | }; |
| 4401 | |
| 4402 | // URLRequest::Delegate that logs delegate information when the headers |
| 4403 | // are received, when each read completes, and during redirects. Uses |
| 4404 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 4405 | // |
| 4406 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 4407 | // advancing to the next step in most cases, as well as cancellation. |
| 4408 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 4409 | public: |
| 4410 | enum CancelStage { |
| 4411 | NO_CANCEL = 0, |
| 4412 | CANCEL_ON_RECEIVED_REDIRECT, |
| 4413 | CANCEL_ON_RESPONSE_STARTED, |
| 4414 | CANCEL_ON_READ_COMPLETED |
| 4415 | }; |
| 4416 | |
| 4417 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 4418 | : cancel_stage_(cancel_stage) { |
| 4419 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 4420 | set_cancel_in_received_redirect(true); |
| 4421 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 4422 | set_cancel_in_response_started(true); |
| 4423 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 4424 | set_cancel_in_received_data(true); |
| 4425 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4426 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4427 | |
| 4428 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4429 | void OnReceivedRedirect(URLRequest* request, |
| 4430 | const RedirectInfo& redirect_info, |
| 4431 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4432 | *defer_redirect = true; |
| 4433 | AsyncDelegateLogger::Run( |
| 4434 | request, |
| 4435 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4436 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4437 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4438 | base::Bind( |
| 4439 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4440 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4441 | } |
| 4442 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4443 | void OnResponseStarted(URLRequest* request) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4444 | AsyncDelegateLogger::Run( |
| 4445 | request, |
| 4446 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4447 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4448 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4449 | base::Bind( |
| 4450 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 4451 | base::Unretained(this), request)); |
| 4452 | } |
| 4453 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4454 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4455 | AsyncDelegateLogger::Run( |
| 4456 | request, |
| 4457 | LOAD_STATE_IDLE, |
| 4458 | LOAD_STATE_IDLE, |
| 4459 | LOAD_STATE_IDLE, |
| 4460 | base::Bind( |
| 4461 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 4462 | base::Unretained(this), request, bytes_read)); |
| 4463 | } |
| 4464 | |
| 4465 | private: |
| 4466 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4467 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4468 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4469 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4470 | // FollowDeferredRedirect should not be called after cancellation. |
| 4471 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 4472 | return; |
| 4473 | if (!defer_redirect) |
| 4474 | request->FollowDeferredRedirect(); |
| 4475 | } |
| 4476 | |
| 4477 | void OnResponseStartedLoggingComplete(URLRequest* request) { |
| 4478 | // The parent class continues the request. |
| 4479 | TestDelegate::OnResponseStarted(request); |
| 4480 | } |
| 4481 | |
| 4482 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 4483 | // The parent class continues the request. |
| 4484 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 4485 | } |
| 4486 | |
| 4487 | const CancelStage cancel_stage_; |
| 4488 | |
| 4489 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 4490 | }; |
| 4491 | |
| 4492 | // Tests handling of delegate info before a request starts. |
| 4493 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
| 4494 | ASSERT_TRUE(test_server_.Start()); |
| 4495 | |
| 4496 | TestDelegate request_delegate; |
| 4497 | TestURLRequestContext context(true); |
| 4498 | context.set_network_delegate(NULL); |
| 4499 | context.set_net_log(&net_log_); |
| 4500 | context.Init(); |
| 4501 | |
| 4502 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4503 | scoped_ptr<URLRequest> r( |
| 4504 | context.CreateRequest(test_server_.GetURL("empty.html"), |
| 4505 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4506 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4507 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4508 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4509 | |
| 4510 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4511 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4512 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4513 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4514 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4515 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4516 | |
| 4517 | base::RunLoop().Run(); |
| 4518 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4519 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4520 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4521 | } |
| 4522 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4523 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4524 | net_log_.GetEntries(&entries); |
| 4525 | size_t log_position = ExpectLogContainsSomewhereAfter( |
| 4526 | entries, |
| 4527 | 0, |
| 4528 | NetLog::TYPE_DELEGATE_INFO, |
| 4529 | NetLog::PHASE_BEGIN); |
| 4530 | |
| 4531 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 4532 | |
| 4533 | // Nothing else should add any delegate info to the request. |
| 4534 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4535 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4536 | } |
| 4537 | |
| 4538 | // Tests handling of delegate info from a network delegate. |
| 4539 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
| 4540 | ASSERT_TRUE(test_server_.Start()); |
| 4541 | |
| 4542 | TestDelegate request_delegate; |
| 4543 | AsyncLoggingNetworkDelegate network_delegate; |
| 4544 | TestURLRequestContext context(true); |
| 4545 | context.set_network_delegate(&network_delegate); |
| 4546 | context.set_net_log(&net_log_); |
| 4547 | context.Init(); |
| 4548 | |
| 4549 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4550 | scoped_ptr<URLRequest> r( |
| 4551 | context.CreateRequest(test_server_.GetURL("simple.html"), |
| 4552 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4553 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4554 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4555 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4556 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4557 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4558 | base::RunLoop().Run(); |
| 4559 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4560 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4561 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4562 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4563 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4564 | } |
| 4565 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4566 | |
| 4567 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4568 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4569 | net_log_.GetEntries(&entries); |
| 4570 | for (size_t i = 0; i < 3; ++i) { |
| 4571 | log_position = ExpectLogContainsSomewhereAfter( |
| 4572 | entries, |
| 4573 | log_position + 1, |
| 4574 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4575 | NetLog::PHASE_BEGIN); |
| 4576 | |
| 4577 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4578 | log_position + 1); |
| 4579 | |
| 4580 | ASSERT_LT(log_position, entries.size()); |
| 4581 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4582 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4583 | |
| 4584 | if (i == 1) { |
| 4585 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4586 | entries, log_position + 1); |
| 4587 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4591 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4592 | } |
| 4593 | |
| 4594 | // Tests handling of delegate info from a network delegate in the case of an |
| 4595 | // HTTP redirect. |
| 4596 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
| 4597 | ASSERT_TRUE(test_server_.Start()); |
| 4598 | |
| 4599 | TestDelegate request_delegate; |
| 4600 | AsyncLoggingNetworkDelegate network_delegate; |
| 4601 | TestURLRequestContext context(true); |
| 4602 | context.set_network_delegate(&network_delegate); |
| 4603 | context.set_net_log(&net_log_); |
| 4604 | context.Init(); |
| 4605 | |
| 4606 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4607 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4608 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4609 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4610 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4611 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4612 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4613 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4614 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4615 | base::RunLoop().Run(); |
| 4616 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4617 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4618 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4619 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4620 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4621 | } |
| 4622 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4623 | |
| 4624 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4625 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4626 | net_log_.GetEntries(&entries); |
| 4627 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
| 4628 | // OnBeforeSendHeaders, and OnHeadersReceived. |
| 4629 | for (size_t i = 0; i < 3; ++i) { |
| 4630 | log_position = ExpectLogContainsSomewhereAfter( |
| 4631 | entries, |
| 4632 | log_position + 1, |
| 4633 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4634 | NetLog::PHASE_BEGIN); |
| 4635 | |
| 4636 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4637 | log_position + 1); |
| 4638 | |
| 4639 | ASSERT_LT(log_position, entries.size()); |
| 4640 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4641 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4642 | |
| 4643 | if (i == 1) { |
| 4644 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4645 | entries, log_position + 1); |
| 4646 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4647 | } |
| 4648 | |
| 4649 | // The URLRequest::Delegate then gets informed about the redirect. |
| 4650 | log_position = ExpectLogContainsSomewhereAfter( |
| 4651 | entries, |
| 4652 | log_position + 1, |
| 4653 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4654 | NetLog::PHASE_BEGIN); |
| 4655 | |
| 4656 | // The NetworkDelegate logged information in the same three events as before. |
| 4657 | for (size_t i = 0; i < 3; ++i) { |
| 4658 | log_position = ExpectLogContainsSomewhereAfter( |
| 4659 | entries, |
| 4660 | log_position + 1, |
| 4661 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4662 | NetLog::PHASE_BEGIN); |
| 4663 | |
| 4664 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4665 | log_position + 1); |
| 4666 | |
| 4667 | ASSERT_LT(log_position, entries.size()); |
| 4668 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4669 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4670 | } |
| 4671 | |
| 4672 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4673 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4674 | } |
| 4675 | |
| 4676 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 4677 | // AUTH. |
| 4678 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
| 4679 | ASSERT_TRUE(test_server_.Start()); |
| 4680 | |
| 4681 | TestDelegate request_delegate; |
| 4682 | AsyncLoggingNetworkDelegate network_delegate; |
| 4683 | TestURLRequestContext context(true); |
| 4684 | context.set_network_delegate(&network_delegate); |
| 4685 | context.set_net_log(&net_log_); |
| 4686 | context.Init(); |
| 4687 | |
| 4688 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4689 | scoped_ptr<URLRequest> r( |
| 4690 | context.CreateRequest(test_server_.GetURL("auth-basic"), |
| 4691 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4692 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4693 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4694 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4695 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4696 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4697 | base::RunLoop().Run(); |
| 4698 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4699 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4700 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4701 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4702 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4703 | } |
| 4704 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4705 | |
| 4706 | size_t log_position = 0; |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4707 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4708 | net_log_.GetEntries(&entries); |
| 4709 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
| 4710 | // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in |
| 4711 | // OnBeforeURLRequest and OnBeforeSendHeaders. |
| 4712 | for (size_t i = 0; i < 6; ++i) { |
| 4713 | log_position = ExpectLogContainsSomewhereAfter( |
| 4714 | entries, |
| 4715 | log_position + 1, |
| 4716 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4717 | NetLog::PHASE_BEGIN); |
| 4718 | |
| 4719 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4720 | log_position + 1); |
| 4721 | |
| 4722 | ASSERT_LT(log_position, entries.size()); |
| 4723 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4724 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4725 | |
| 4726 | if (i == 1) { |
| 4727 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4728 | entries, log_position + 1); |
| 4729 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4730 | } |
| 4731 | |
| 4732 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4733 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4734 | } |
| 4735 | |
| 4736 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 4737 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
| 4738 | ASSERT_TRUE(test_server_.Start()); |
| 4739 | |
| 4740 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4741 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4742 | TestURLRequestContext context(true); |
| 4743 | context.set_network_delegate(NULL); |
| 4744 | context.set_net_log(&net_log_); |
| 4745 | context.Init(); |
| 4746 | |
| 4747 | { |
| 4748 | // A chunked response with delays between chunks is used to make sure that |
| 4749 | // attempts by the URLRequest delegate to log information while reading the |
| 4750 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4751 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4752 | // that it occurs. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4753 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4754 | test_server_.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4755 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4756 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4757 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4758 | base::RunLoop().Run(); |
| 4759 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4760 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4761 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4762 | } |
| 4763 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4764 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4765 | net_log_.GetEntries(&entries); |
| 4766 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4767 | size_t log_position = 0; |
| 4768 | |
| 4769 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4770 | entries, log_position); |
| 4771 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4772 | // The delegate info should only have been logged on header complete. Other |
| 4773 | // times it should silently be ignored. |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4774 | log_position = |
| 4775 | ExpectLogContainsSomewhereAfter(entries, |
| 4776 | log_position + 1, |
| 4777 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4778 | NetLog::PHASE_BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4779 | |
| 4780 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4781 | log_position + 1); |
| 4782 | |
| 4783 | ASSERT_LT(log_position, entries.size()); |
| 4784 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4785 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4786 | |
| 4787 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4788 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4789 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4790 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4791 | } |
| 4792 | |
| 4793 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4794 | // an HTTP redirect. |
| 4795 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
| 4796 | ASSERT_TRUE(test_server_.Start()); |
| 4797 | |
| 4798 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4799 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4800 | TestURLRequestContext context(true); |
| 4801 | context.set_network_delegate(NULL); |
| 4802 | context.set_net_log(&net_log_); |
| 4803 | context.Init(); |
| 4804 | |
| 4805 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4806 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4807 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4808 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4809 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4810 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4811 | base::RunLoop().Run(); |
| 4812 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4813 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4814 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4815 | } |
| 4816 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4817 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4818 | net_log_.GetEntries(&entries); |
| 4819 | |
| 4820 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 4821 | // OnResponseStarted. |
| 4822 | size_t log_position = 0; |
| 4823 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4824 | if (i == 0) { |
| 4825 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4826 | entries, log_position) + 1; |
| 4827 | } |
| 4828 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4829 | log_position = ExpectLogContainsSomewhereAfter( |
| 4830 | entries, |
| 4831 | log_position, |
| 4832 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4833 | NetLog::PHASE_BEGIN); |
| 4834 | |
| 4835 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4836 | log_position + 1); |
| 4837 | |
| 4838 | ASSERT_LT(log_position, entries.size()); |
| 4839 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4840 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4841 | } |
| 4842 | |
| 4843 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4844 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4845 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4846 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4847 | } |
| 4848 | |
| 4849 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4850 | // an HTTP redirect, with cancellation at various points. |
| 4851 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
| 4852 | ASSERT_TRUE(test_server_.Start()); |
| 4853 | |
| 4854 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 4855 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 4856 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 4857 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 4858 | }; |
| 4859 | |
| 4860 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 4861 | ++test_case) { |
| 4862 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
| 4863 | TestURLRequestContext context(true); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4864 | TestNetLog net_log; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4865 | context.set_network_delegate(NULL); |
| 4866 | context.set_net_log(&net_log); |
| 4867 | context.Init(); |
| 4868 | |
| 4869 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4870 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4871 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4872 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4873 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4874 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4875 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4876 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4877 | } |
| 4878 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 4879 | TestNetLogEntry::List entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4880 | net_log.GetEntries(&entries); |
| 4881 | |
| 4882 | // Delegate info is always logged in both OnReceivedRedirect and |
| 4883 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 4884 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 4885 | // still currently supported in that call. |
| 4886 | size_t log_position = 0; |
| 4887 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4888 | if (i == 0) { |
| 4889 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4890 | entries, log_position) + 1; |
| 4891 | } |
| 4892 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4893 | log_position = ExpectLogContainsSomewhereAfter( |
| 4894 | entries, |
| 4895 | log_position, |
| 4896 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4897 | NetLog::PHASE_BEGIN); |
| 4898 | |
| 4899 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4900 | log_position + 1); |
| 4901 | |
| 4902 | ASSERT_LT(log_position, entries.size()); |
| 4903 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4904 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4905 | } |
| 4906 | |
| 4907 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4908 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4909 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4910 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4911 | } |
| 4912 | } |
| 4913 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4914 | namespace { |
| 4915 | |
| 4916 | const char kExtraHeader[] = "Allow-Snafu"; |
| 4917 | const char kExtraValue[] = "fubar"; |
| 4918 | |
| 4919 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4920 | void OnReceivedRedirect(URLRequest* request, |
| 4921 | const RedirectInfo& redirect_info, |
| 4922 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4923 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4924 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 4925 | } |
| 4926 | }; |
| 4927 | |
| 4928 | } // namespace |
| 4929 | |
| 4930 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
| 4931 | ASSERT_TRUE(test_server_.Start()); |
| 4932 | |
| 4933 | GURL destination_url = test_server_.GetURL( |
| 4934 | "echoheader?" + std::string(kExtraHeader)); |
| 4935 | GURL original_url = test_server_.GetURL( |
| 4936 | "server-redirect?" + destination_url.spec()); |
| 4937 | RedirectWithAdditionalHeadersDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4938 | scoped_ptr<URLRequest> req( |
| 4939 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4940 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4941 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4942 | |
| 4943 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4944 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4945 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 4946 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4947 | EXPECT_FALSE(req->is_pending()); |
| 4948 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4949 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 4950 | } |
| 4951 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4952 | namespace { |
| 4953 | |
| 4954 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 4955 | |
| 4956 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4957 | void OnReceivedRedirect(URLRequest* request, |
| 4958 | const RedirectInfo& redirect_info, |
| 4959 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4960 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4961 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 4962 | } |
| 4963 | }; |
| 4964 | |
| 4965 | } // namespace |
| 4966 | |
| 4967 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
| 4968 | ASSERT_TRUE(test_server_.Start()); |
| 4969 | |
| 4970 | GURL destination_url = test_server_.GetURL( |
| 4971 | "echoheader?" + std::string(kExtraHeaderToRemove)); |
| 4972 | GURL original_url = test_server_.GetURL( |
| 4973 | "server-redirect?" + destination_url.spec()); |
| 4974 | RedirectWithHeaderRemovalDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4975 | scoped_ptr<URLRequest> req( |
| 4976 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4977 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 4978 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4979 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4980 | |
| 4981 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4982 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4983 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4984 | EXPECT_FALSE(req->is_pending()); |
| 4985 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4986 | EXPECT_EQ("None", d.data_received()); |
| 4987 | } |
| 4988 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4989 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 4990 | TestDelegate d; |
| 4991 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4992 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4993 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4994 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4995 | r->Start(); |
| 4996 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4997 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4998 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4999 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5000 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5001 | |
| 5002 | // We expect to receive OnResponseStarted even though the request has been |
| 5003 | // cancelled. |
| 5004 | EXPECT_EQ(1, d.response_started_count()); |
| 5005 | EXPECT_EQ(0, d.bytes_received()); |
| 5006 | EXPECT_FALSE(d.received_data_before_response()); |
| 5007 | } |
| 5008 | } |
| 5009 | |
| 5010 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
| 5011 | ASSERT_TRUE(test_server_.Start()); |
| 5012 | |
| 5013 | TestDelegate d; |
| 5014 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5015 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5016 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5017 | |
| 5018 | d.set_cancel_in_response_started(true); |
| 5019 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5020 | r->Start(); |
| 5021 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5022 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5023 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5024 | |
| 5025 | EXPECT_EQ(1, d.response_started_count()); |
| 5026 | EXPECT_EQ(0, d.bytes_received()); |
| 5027 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5028 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5029 | } |
| 5030 | } |
| 5031 | |
| 5032 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
| 5033 | ASSERT_TRUE(test_server_.Start()); |
| 5034 | |
| 5035 | TestDelegate d; |
| 5036 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5037 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5038 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5039 | |
| 5040 | d.set_cancel_in_received_data(true); |
| 5041 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5042 | r->Start(); |
| 5043 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5044 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5045 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5046 | |
| 5047 | EXPECT_EQ(1, d.response_started_count()); |
| 5048 | // There is no guarantee about how much data was received |
| 5049 | // before the cancel was issued. It could have been 0 bytes, |
| 5050 | // or it could have been all the bytes. |
| 5051 | // EXPECT_EQ(0, d.bytes_received()); |
| 5052 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5053 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
| 5058 | ASSERT_TRUE(test_server_.Start()); |
| 5059 | |
| 5060 | TestDelegate d; |
| 5061 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5062 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5063 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5064 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5065 | r->Start(); |
| 5066 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5067 | |
| 5068 | // The request will be implicitly canceled when it is destroyed. The |
| 5069 | // test delegate must not post a quit message when this happens because |
| 5070 | // this test doesn't actually have a message loop. The quit message would |
| 5071 | // get put on this thread's message queue and the next test would exit |
| 5072 | // early, causing problems. |
| 5073 | d.set_quit_on_complete(false); |
| 5074 | } |
| 5075 | // expect things to just cleanup properly. |
| 5076 | |
| 5077 | // we won't actually get a received reponse here because we've never run the |
| 5078 | // message loop |
| 5079 | EXPECT_FALSE(d.received_data_before_response()); |
| 5080 | EXPECT_EQ(0, d.bytes_received()); |
| 5081 | } |
| 5082 | |
| 5083 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
| 5084 | ASSERT_TRUE(test_server_.Start()); |
| 5085 | |
| 5086 | // populate cache |
| 5087 | { |
| 5088 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5089 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5090 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5091 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5092 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5093 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5094 | } |
| 5095 | |
| 5096 | // cancel read from cache (see bug 990242) |
| 5097 | { |
| 5098 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5099 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5100 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5101 | r->Start(); |
| 5102 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5103 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5104 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5105 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5106 | EXPECT_EQ(1, d.response_started_count()); |
| 5107 | EXPECT_EQ(0, d.bytes_received()); |
| 5108 | EXPECT_FALSE(d.received_data_before_response()); |
| 5109 | } |
| 5110 | } |
| 5111 | |
| 5112 | TEST_F(URLRequestTestHTTP, PostTest) { |
| 5113 | ASSERT_TRUE(test_server_.Start()); |
| 5114 | HTTPUploadDataOperationTest("POST"); |
| 5115 | } |
| 5116 | |
| 5117 | TEST_F(URLRequestTestHTTP, PutTest) { |
| 5118 | ASSERT_TRUE(test_server_.Start()); |
| 5119 | HTTPUploadDataOperationTest("PUT"); |
| 5120 | } |
| 5121 | |
| 5122 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
| 5123 | ASSERT_TRUE(test_server_.Start()); |
| 5124 | |
| 5125 | TestDelegate d; |
| 5126 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5127 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5128 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5129 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5130 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5131 | r->Start(); |
| 5132 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5133 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5134 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5135 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5136 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5137 | << "request failed: " << r->status().status() |
| 5138 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5139 | |
| 5140 | EXPECT_FALSE(d.received_data_before_response()); |
| 5141 | EXPECT_TRUE(d.data_received().empty()); |
| 5142 | } |
| 5143 | } |
| 5144 | |
| 5145 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
| 5146 | ASSERT_TRUE(test_server_.Start()); |
| 5147 | |
| 5148 | TestDelegate d; |
| 5149 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5150 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5151 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5152 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5153 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5154 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5155 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 5156 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5157 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 5158 | ScopedVector<UploadElementReader> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5159 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5160 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5161 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 5162 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5163 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 5164 | element_readers.push_back( |
| 5165 | new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 5166 | path, |
| 5167 | 0, |
| 5168 | kuint64max, |
| 5169 | base::Time())); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 5170 | r->set_upload(make_scoped_ptr<UploadDataStream>( |
| 5171 | new ElementsUploadDataStream(element_readers.Pass(), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5172 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5173 | r->Start(); |
| 5174 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5175 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5176 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5177 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5178 | int64 size64 = 0; |
| 5179 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 5180 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 5181 | int size = static_cast<int>(size64); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 5182 | scoped_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5183 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 5184 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5185 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5186 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5187 | << "request failed: " << r->status().status() |
| 5188 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5189 | |
| 5190 | EXPECT_FALSE(d.received_data_before_response()); |
| 5191 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5192 | EXPECT_EQ(size, d.bytes_received()); |
| 5193 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5194 | } |
| 5195 | } |
| 5196 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5197 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
| 5198 | ASSERT_TRUE(test_server_.Start()); |
| 5199 | |
| 5200 | TestDelegate d; |
| 5201 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5202 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5203 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5204 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5205 | |
| 5206 | ScopedVector<UploadElementReader> element_readers; |
| 5207 | |
| 5208 | element_readers.push_back(new UploadFileElementReader( |
| 5209 | base::MessageLoopProxy::current().get(), |
| 5210 | base::FilePath(FILE_PATH_LITERAL( |
| 5211 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
| 5212 | 0, |
| 5213 | kuint64max, |
| 5214 | base::Time())); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 5215 | r->set_upload(make_scoped_ptr<UploadDataStream>( |
| 5216 | new ElementsUploadDataStream(element_readers.Pass(), 0))); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5217 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5218 | r->Start(); |
| 5219 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5220 | |
| 5221 | base::RunLoop().Run(); |
| 5222 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5223 | EXPECT_TRUE(d.request_failed()); |
| 5224 | EXPECT_FALSE(d.received_data_before_response()); |
| 5225 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5226 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 5227 | EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5228 | } |
| 5229 | } |
| 5230 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5231 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
| 5232 | ASSERT_TRUE(test_server_.Start()); |
| 5233 | |
| 5234 | TestDelegate d; |
| 5235 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5236 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5237 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5238 | r->EnableChunkedUpload(); |
| 5239 | r->set_method("POST"); |
| 5240 | AddChunksToUpload(r.get()); |
| 5241 | r->Start(); |
| 5242 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5243 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5244 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5245 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5246 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5247 | } |
| 5248 | } |
| 5249 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5250 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
| 5251 | ASSERT_TRUE(test_server_.Start()); |
| 5252 | |
| 5253 | TestDelegate d; |
| 5254 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5255 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5256 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5257 | r->EnableChunkedUpload(); |
| 5258 | r->set_method("POST"); |
| 5259 | r->Start(); |
| 5260 | EXPECT_TRUE(r->is_pending()); |
| 5261 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5262 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5263 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5264 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5265 | } |
| 5266 | } |
| 5267 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5268 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
| 5269 | ASSERT_TRUE(test_server_.Start()); |
| 5270 | |
| 5271 | TestDelegate d; |
| 5272 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5273 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5274 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5275 | r->EnableChunkedUpload(); |
| 5276 | r->set_method("POST"); |
| 5277 | r->Start(); |
| 5278 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5279 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5280 | base::RunLoop().RunUntilIdle(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5281 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5282 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5283 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5284 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
| 5289 | ASSERT_TRUE(test_server_.Start()); |
| 5290 | |
| 5291 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5292 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5293 | test_server_.GetURL("files/with-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5294 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5295 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5296 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5297 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5298 | |
| 5299 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5300 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5301 | |
| 5302 | std::string header; |
| 5303 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 5304 | EXPECT_EQ("private", header); |
| 5305 | |
| 5306 | header.clear(); |
| 5307 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 5308 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 5309 | |
| 5310 | // The response has two "X-Multiple-Entries" headers. |
| 5311 | // This verfies our output has them concatenated together. |
| 5312 | header.clear(); |
| 5313 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 5314 | EXPECT_EQ("a, b", header); |
| 5315 | } |
| 5316 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5317 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5318 | SpawnedTestServer::SSLOptions ssl_options( |
| 5319 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 5320 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 5321 | ssl_options, |
| 5322 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5323 | ASSERT_TRUE(https_test_server.Start()); |
| 5324 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5325 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5326 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5327 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5328 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5329 | https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY, |
| 5330 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5331 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5332 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5333 | |
| 5334 | TransportSecurityState* security_state = |
| 5335 | default_context_.transport_security_state(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5336 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5337 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5338 | &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5339 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5340 | domain_state.sts.upgrade_mode); |
| 5341 | EXPECT_TRUE(domain_state.sts.include_subdomains); |
| 5342 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5343 | #if defined(OS_ANDROID) |
| 5344 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5345 | #else |
| 5346 | EXPECT_FALSE(domain_state.HasPublicKeyPins()); |
| 5347 | #endif |
| 5348 | } |
| 5349 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5350 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 5351 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 5352 | SpawnedTestServer::SSLOptions(), |
| 5353 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5354 | ASSERT_TRUE(https_test_server.Start()); |
| 5355 | // Make sure this test fails if the test server is changed to not |
| 5356 | // listen on an IP by default. |
| 5357 | ASSERT_TRUE(https_test_server.GetURL("").HostIsIPAddress()); |
| 5358 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5359 | |
| 5360 | TestDelegate d; |
| 5361 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 5362 | https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY, |
| 5363 | &d)); |
| 5364 | request->Start(); |
| 5365 | base::RunLoop().Run(); |
| 5366 | |
| 5367 | TransportSecurityState* security_state = |
| 5368 | default_context_.transport_security_state(); |
| 5369 | TransportSecurityState::DomainState domain_state; |
| 5370 | EXPECT_FALSE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5371 | &domain_state)); |
| 5372 | } |
| 5373 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5374 | // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will |
| 5375 | // reject HPKP headers, and a test setting only HPKP headers will fail (no |
| 5376 | // DomainState present because header rejected). |
| 5377 | #if defined(OS_ANDROID) |
| 5378 | #define MAYBE_ProcessPKP DISABLED_ProcessPKP |
| 5379 | #else |
| 5380 | #define MAYBE_ProcessPKP ProcessPKP |
| 5381 | #endif |
| 5382 | |
| 5383 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 5384 | // validity/expiration. |
| 5385 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5386 | SpawnedTestServer::SSLOptions ssl_options( |
| 5387 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 5388 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 5389 | ssl_options, |
| 5390 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5391 | ASSERT_TRUE(https_test_server.Start()); |
| 5392 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5393 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5394 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5395 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5396 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5397 | https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY, |
| 5398 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5399 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5400 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5401 | |
| 5402 | TransportSecurityState* security_state = |
| 5403 | default_context_.transport_security_state(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5404 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5405 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5406 | &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5407 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5408 | domain_state.sts.upgrade_mode); |
| 5409 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
| 5410 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5411 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5412 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5413 | } |
| 5414 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5415 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 5416 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 5417 | SpawnedTestServer::SSLOptions(), |
| 5418 | base::FilePath(kTestFilePath)); |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5419 | ASSERT_TRUE(https_test_server.Start()); |
| 5420 | // Make sure this test fails if the test server is changed to not |
| 5421 | // listen on an IP by default. |
| 5422 | ASSERT_TRUE(https_test_server.GetURL("").HostIsIPAddress()); |
| 5423 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5424 | |
| 5425 | TestDelegate d; |
| 5426 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 5427 | https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY, |
| 5428 | &d)); |
| 5429 | request->Start(); |
| 5430 | base::RunLoop().Run(); |
| 5431 | |
| 5432 | TransportSecurityState* security_state = |
| 5433 | default_context_.transport_security_state(); |
| 5434 | TransportSecurityState::DomainState domain_state; |
| 5435 | EXPECT_FALSE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5436 | &domain_state)); |
| 5437 | } |
| 5438 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5439 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5440 | SpawnedTestServer::SSLOptions ssl_options( |
| 5441 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 5442 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 5443 | ssl_options, |
| 5444 | base::FilePath(kTestFilePath)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5445 | ASSERT_TRUE(https_test_server.Start()); |
| 5446 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5447 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5448 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5449 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5450 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5451 | https_test_server.GetURL("files/hsts-multiple-headers.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5452 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5453 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5454 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5455 | |
| 5456 | // We should have set parameters from the first header, not the second. |
| 5457 | TransportSecurityState* security_state = |
| 5458 | default_context_.transport_security_state(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5459 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5460 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5461 | &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5462 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5463 | domain_state.sts.upgrade_mode); |
| 5464 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
| 5465 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5466 | } |
| 5467 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5468 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5469 | SpawnedTestServer::SSLOptions ssl_options( |
| 5470 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 5471 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 5472 | ssl_options, |
| 5473 | base::FilePath(kTestFilePath)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5474 | ASSERT_TRUE(https_test_server.Start()); |
| 5475 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5476 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5477 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5478 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5479 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5480 | https_test_server.GetURL("files/hsts-and-hpkp-headers.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5481 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5482 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5483 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5484 | |
| 5485 | // We should have set parameters from the first header, not the second. |
| 5486 | TransportSecurityState* security_state = |
| 5487 | default_context_.transport_security_state(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5488 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5489 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5490 | &domain_state)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5491 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5492 | domain_state.sts.upgrade_mode); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5493 | #if defined(OS_ANDROID) |
| 5494 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5495 | #else |
| 5496 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5497 | #endif |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5498 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5499 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5500 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 5501 | // the *second* such header, and we MUST process only the first. |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5502 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5503 | // includeSubdomains does not occur in the test HPKP header. |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5504 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5505 | } |
| 5506 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5507 | // Tests that when multiple HPKP headers are present, asserting different |
| 5508 | // policies, that only the first such policy is processed. |
| 5509 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5510 | SpawnedTestServer::SSLOptions ssl_options( |
| 5511 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 5512 | SpawnedTestServer https_test_server(SpawnedTestServer::TYPE_HTTPS, |
| 5513 | ssl_options, |
| 5514 | base::FilePath(kTestFilePath)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5515 | ASSERT_TRUE(https_test_server.Start()); |
| 5516 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5517 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5518 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5519 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5520 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5521 | https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5522 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5523 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5524 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5525 | |
| 5526 | TransportSecurityState* security_state = |
| 5527 | default_context_.transport_security_state(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5528 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5529 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5530 | &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5531 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5532 | domain_state.sts.upgrade_mode); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5533 | #if defined(OS_ANDROID) |
| 5534 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5535 | #else |
| 5536 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5537 | #endif |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5538 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5539 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5540 | EXPECT_TRUE(domain_state.sts.include_subdomains); |
| 5541 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5542 | } |
| 5543 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5544 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
| 5545 | ASSERT_TRUE(test_server_.Start()); |
| 5546 | |
| 5547 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5548 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5549 | test_server_.GetURL("files/content-type-normalization.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5550 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5551 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5552 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5553 | |
| 5554 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5555 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5556 | EXPECT_EQ("text/html", mime_type); |
| 5557 | |
| 5558 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5559 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5560 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5561 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5562 | } |
| 5563 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5564 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5565 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5566 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5567 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5568 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5569 | |
| 5570 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 5571 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5572 | } |
| 5573 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5574 | #if !defined(DISABLE_FILE_SUPPORT) |
| 5575 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 5576 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 5577 | GURL file_url("file:///foo.txt"); |
| 5578 | FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current()); |
| 5579 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 5580 | |
| 5581 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 5582 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5583 | } |
| 5584 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5585 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5586 | ASSERT_TRUE(test_server_.Start()); |
| 5587 | |
| 5588 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5589 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5590 | test_server_.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY, |
| 5591 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5592 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5593 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5594 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5595 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5596 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5597 | } |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5598 | #endif // !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5599 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5600 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
| 5601 | ASSERT_TRUE(test_server_.Start()); |
| 5602 | |
| 5603 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5604 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5605 | test_server_.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY, |
| 5606 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5607 | req->Start(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5608 | base::MessageLoop::current()->Run(); |
| 5609 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5610 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5611 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5612 | } |
| 5613 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5614 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
| 5615 | ASSERT_TRUE(test_server_.Start()); |
| 5616 | |
| 5617 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5618 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5619 | test_server_.GetURL("files/redirect-to-invalid-url.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5620 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5621 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5622 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5623 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5624 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5625 | EXPECT_EQ(ERR_INVALID_URL, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5626 | } |
| 5627 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5628 | // Make sure redirects are cached, despite not reading their bodies. |
| 5629 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
| 5630 | ASSERT_TRUE(test_server_.Start()); |
| 5631 | GURL redirect_url = |
| 5632 | test_server_.GetURL("files/redirect302-to-echo-cacheable"); |
| 5633 | |
| 5634 | { |
| 5635 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5636 | scoped_ptr<URLRequest> req( |
| 5637 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5638 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5639 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5640 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5641 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5642 | EXPECT_EQ(test_server_.GetURL("echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5643 | } |
| 5644 | |
| 5645 | { |
| 5646 | TestDelegate d; |
| 5647 | d.set_quit_on_redirect(true); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5648 | scoped_ptr<URLRequest> req( |
| 5649 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5650 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5651 | base::RunLoop().Run(); |
| 5652 | |
| 5653 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5654 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5655 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5656 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5657 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5658 | base::RunLoop().Run(); |
| 5659 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5660 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5661 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 5662 | EXPECT_EQ(test_server_.GetURL("echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5663 | } |
| 5664 | } |
| 5665 | |
| 5666 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 5667 | // when the headers are read, since the body won't have been read. |
| 5668 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
| 5669 | ASSERT_TRUE(test_server_.Start()); |
| 5670 | // URL that is normally cached. |
| 5671 | GURL initial_url = test_server_.GetURL("cachetime"); |
| 5672 | |
| 5673 | { |
| 5674 | // Set up the TestNetworkDelegate tp force a redirect. |
| 5675 | GURL redirect_to_url = test_server_.GetURL("echo"); |
| 5676 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 5677 | redirect_to_url); |
| 5678 | |
| 5679 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5680 | scoped_ptr<URLRequest> req( |
| 5681 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5682 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5683 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5684 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5685 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5686 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5687 | } |
| 5688 | |
| 5689 | { |
| 5690 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5691 | scoped_ptr<URLRequest> req( |
| 5692 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5693 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5694 | base::RunLoop().Run(); |
| 5695 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5696 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 5697 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5698 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5699 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5700 | } |
| 5701 | } |
| 5702 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5703 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 5704 | // safe. |
| 5705 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
| 5706 | ASSERT_TRUE(test_server_.Start()); |
| 5707 | |
| 5708 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 5709 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5710 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5711 | |
| 5712 | TestDelegate d; |
| 5713 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5714 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5715 | test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5716 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5717 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5718 | base::RunLoop().Run(); |
| 5719 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5720 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5721 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5722 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5723 | EXPECT_EQ(OK, r->status().error()); |
| 5724 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5725 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 5726 | } |
| 5727 | } |
| 5728 | |
| 5729 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 5730 | // some other URL to the whitelist. |
| 5731 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
| 5732 | ASSERT_TRUE(test_server_.Start()); |
| 5733 | |
| 5734 | GURL unsafe_url("data:text/html,something"); |
| 5735 | GURL different_unsafe_url("data:text/html,something-else"); |
| 5736 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5737 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 5738 | different_unsafe_url); |
| 5739 | |
| 5740 | TestDelegate d; |
| 5741 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5742 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5743 | test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5744 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5745 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5746 | base::RunLoop().Run(); |
| 5747 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5748 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 5749 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5750 | } |
| 5751 | } |
| 5752 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5753 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 5754 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 5755 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
| 5756 | ASSERT_TRUE(test_server_.Start()); |
| 5757 | |
| 5758 | GURL original_url(test_server_.GetURL("original#fragment1")); |
| 5759 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 5760 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 5761 | |
| 5762 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5763 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5764 | |
| 5765 | TestDelegate d; |
| 5766 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5767 | scoped_ptr<URLRequest> r( |
| 5768 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5769 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5770 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5771 | base::RunLoop().Run(); |
| 5772 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5773 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5774 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5775 | EXPECT_EQ(OK, r->status().error()); |
| 5776 | EXPECT_EQ(original_url, r->original_url()); |
| 5777 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5778 | } |
| 5779 | } |
| 5780 | |
| 5781 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 5782 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5783 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5784 | ASSERT_TRUE(test_server_.Start()); |
| 5785 | |
| 5786 | GURL original_url(test_server_.GetURL("original#expected-fragment")); |
| 5787 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
| 5788 | GURL redirect_url(test_server_.GetURL("target")); |
| 5789 | GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment")); |
| 5790 | |
| 5791 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 5792 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5793 | |
| 5794 | TestDelegate d; |
| 5795 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5796 | scoped_ptr<URLRequest> r( |
| 5797 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5798 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5799 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5800 | base::RunLoop().Run(); |
| 5801 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5802 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5803 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5804 | EXPECT_EQ(OK, r->status().error()); |
| 5805 | EXPECT_EQ(original_url, r->original_url()); |
| 5806 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5807 | } |
| 5808 | } |
| 5809 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5810 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 5811 | // URL should not be changed. In particular, the reference fragment should not |
| 5812 | // be modified. |
| 5813 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
| 5814 | ASSERT_TRUE(test_server_.Start()); |
| 5815 | |
| 5816 | GURL original_url(test_server_.GetURL("original#should-not-be-appended")); |
| 5817 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 5818 | |
| 5819 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 5820 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 5821 | |
| 5822 | TestDelegate d; |
| 5823 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5824 | scoped_ptr<URLRequest> r( |
| 5825 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5826 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5827 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5828 | base::RunLoop().Run(); |
| 5829 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5830 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5831 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5832 | EXPECT_EQ(OK, r->status().error()); |
| 5833 | EXPECT_EQ(original_url, r->original_url()); |
| 5834 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5835 | } |
| 5836 | } |
| 5837 | |
| 5838 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 5839 | // the reference fragment of the target URL must not be modified. |
| 5840 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
| 5841 | ASSERT_TRUE(test_server_.Start()); |
| 5842 | |
| 5843 | GURL original_url(test_server_.GetURL("original#should-not-be-appended")); |
| 5844 | GURL redirect_url(test_server_.GetURL("echo")); |
| 5845 | |
| 5846 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5847 | scoped_ptr<URLRequest> r( |
| 5848 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5849 | |
| 5850 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5851 | r.get(), &default_network_delegate_, redirect_url, |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5852 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
| 5853 | AddTestInterceptor()->set_main_intercept_job(job); |
| 5854 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5855 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5856 | base::RunLoop().Run(); |
| 5857 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5858 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5859 | EXPECT_EQ(OK, r->status().error()); |
| 5860 | EXPECT_EQ(original_url, r->original_url()); |
| 5861 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5862 | } |
| 5863 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5864 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
| 5865 | ASSERT_TRUE(test_server_.Start()); |
| 5866 | |
| 5867 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5868 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5869 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5870 | req->SetReferrer("http://user:[email protected]/"); |
| 5871 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5872 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5873 | |
| 5874 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 5875 | } |
| 5876 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5877 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
| 5878 | ASSERT_TRUE(test_server_.Start()); |
| 5879 | |
| 5880 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5881 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5882 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5883 | req->SetReferrer("http://foo.com/test#fragment"); |
| 5884 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5885 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5886 | |
| 5887 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 5888 | } |
| 5889 | |
| 5890 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
| 5891 | ASSERT_TRUE(test_server_.Start()); |
| 5892 | |
| 5893 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5894 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5895 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5896 | req->SetReferrer("http://foo.com/test#fragment"); |
| 5897 | req->SetReferrer(""); |
| 5898 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5899 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5900 | |
| 5901 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 5902 | } |
| 5903 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5904 | // Defer network start and then resume, checking that the request was a success |
| 5905 | // and bytes were received. |
| 5906 | TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) { |
| 5907 | ASSERT_TRUE(test_server_.Start()); |
| 5908 | |
| 5909 | TestDelegate d; |
| 5910 | { |
| 5911 | d.set_quit_on_network_start(true); |
| 5912 | GURL test_url(test_server_.GetURL("echo")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5913 | scoped_ptr<URLRequest> req( |
| 5914 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5915 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5916 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5917 | base::RunLoop().Run(); |
| 5918 | |
| 5919 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5920 | EXPECT_EQ(0, d.response_started_count()); |
| 5921 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5922 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5923 | base::RunLoop().Run(); |
| 5924 | |
| 5925 | EXPECT_EQ(1, d.response_started_count()); |
| 5926 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5927 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5928 | } |
| 5929 | } |
| 5930 | |
| 5931 | // Check that OnBeforeNetworkStart is only called once even if there is a |
| 5932 | // redirect. |
| 5933 | TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) { |
| 5934 | ASSERT_TRUE(test_server_.Start()); |
| 5935 | |
| 5936 | TestDelegate d; |
| 5937 | { |
| 5938 | d.set_quit_on_redirect(true); |
| 5939 | d.set_quit_on_network_start(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5940 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5941 | test_server_.GetURL("server-redirect?echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5942 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5943 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5944 | base::RunLoop().Run(); |
| 5945 | |
| 5946 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5947 | EXPECT_EQ(0, d.response_started_count()); |
| 5948 | EXPECT_EQ(0, d.received_redirect_count()); |
| 5949 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5950 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5951 | base::RunLoop().Run(); |
| 5952 | |
| 5953 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5954 | req->FollowDeferredRedirect(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5955 | base::RunLoop().Run(); |
| 5956 | |
| 5957 | // Check that the redirect's new network transaction does not get propagated |
| 5958 | // to a second OnBeforeNetworkStart() notification. |
| 5959 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5960 | |
| 5961 | EXPECT_EQ(1, d.response_started_count()); |
| 5962 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5963 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5964 | } |
| 5965 | } |
| 5966 | |
| 5967 | // Cancel the request after learning that the request would use the network. |
| 5968 | TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) { |
| 5969 | ASSERT_TRUE(test_server_.Start()); |
| 5970 | |
| 5971 | TestDelegate d; |
| 5972 | { |
| 5973 | d.set_quit_on_network_start(true); |
| 5974 | GURL test_url(test_server_.GetURL("echo")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5975 | scoped_ptr<URLRequest> req( |
| 5976 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5977 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5978 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5979 | base::RunLoop().Run(); |
| 5980 | |
| 5981 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5982 | EXPECT_EQ(0, d.response_started_count()); |
| 5983 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5984 | req->Cancel(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5985 | base::RunLoop().Run(); |
| 5986 | |
| 5987 | EXPECT_EQ(1, d.response_started_count()); |
| 5988 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5989 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5990 | } |
| 5991 | } |
| 5992 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5993 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
| 5994 | ASSERT_TRUE(test_server_.Start()); |
| 5995 | |
| 5996 | TestDelegate d; |
| 5997 | { |
| 5998 | d.set_cancel_in_received_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5999 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6000 | test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6001 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6002 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6003 | |
| 6004 | EXPECT_EQ(1, d.response_started_count()); |
| 6005 | EXPECT_EQ(0, d.bytes_received()); |
| 6006 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6007 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6008 | } |
| 6009 | } |
| 6010 | |
| 6011 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
| 6012 | ASSERT_TRUE(test_server_.Start()); |
| 6013 | |
| 6014 | TestDelegate d; |
| 6015 | { |
| 6016 | d.set_quit_on_redirect(true); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6017 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6018 | scoped_ptr<URLRequest> req( |
| 6019 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6020 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6021 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6022 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6023 | |
| 6024 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6025 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6026 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6027 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6028 | |
| 6029 | EXPECT_EQ(1, d.response_started_count()); |
| 6030 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6031 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6032 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 6033 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6034 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 6035 | path = path.Append(kTestFilePath); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6036 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 6037 | |
| 6038 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 6039 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6040 | EXPECT_EQ(contents, d.data_received()); |
| 6041 | } |
| 6042 | } |
| 6043 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6044 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
| 6045 | ASSERT_TRUE(test_server_.Start()); |
| 6046 | |
| 6047 | TestDelegate d; |
| 6048 | { |
| 6049 | d.set_quit_on_redirect(true); |
| 6050 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6051 | scoped_ptr<URLRequest> req( |
| 6052 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6053 | |
| 6054 | EXPECT_FALSE(d.have_full_request_headers()); |
| 6055 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6056 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6057 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6058 | |
| 6059 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6060 | EXPECT_TRUE(d.have_full_request_headers()); |
| 6061 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 6062 | d.ClearFullRequestHeaders(); |
| 6063 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6064 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6065 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6066 | |
| 6067 | GURL target_url(test_server_.GetURL("files/with-headers.html")); |
| 6068 | EXPECT_EQ(1, d.response_started_count()); |
| 6069 | EXPECT_TRUE(d.have_full_request_headers()); |
| 6070 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 6071 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6072 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6073 | |
| 6074 | base::FilePath path; |
| 6075 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 6076 | path = path.Append(kTestFilePath); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6077 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 6078 | |
| 6079 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 6080 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6081 | EXPECT_EQ(contents, d.data_received()); |
| 6082 | } |
| 6083 | } |
| 6084 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6085 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
| 6086 | ASSERT_TRUE(test_server_.Start()); |
| 6087 | |
| 6088 | TestDelegate d; |
| 6089 | { |
| 6090 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6091 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6092 | test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6093 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6094 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6095 | |
| 6096 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6097 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6098 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6099 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6100 | |
| 6101 | EXPECT_EQ(1, d.response_started_count()); |
| 6102 | EXPECT_EQ(0, d.bytes_received()); |
| 6103 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6104 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6105 | } |
| 6106 | } |
| 6107 | |
| 6108 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
| 6109 | ASSERT_TRUE(test_server_.Start()); |
| 6110 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6111 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6112 | { |
| 6113 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6114 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6115 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6116 | HttpRequestHeaders headers; |
| 6117 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6118 | req->SetExtraRequestHeaders(headers); |
| 6119 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6120 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6121 | |
| 6122 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6123 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6124 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6125 | } |
| 6126 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6127 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6128 | { |
| 6129 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6130 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6131 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6132 | HttpRequestHeaders headers; |
| 6133 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6134 | req->SetExtraRequestHeaders(headers); |
| 6135 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6136 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6137 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6138 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6139 | |
| 6140 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6141 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6142 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6143 | } |
| 6144 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6145 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6146 | { |
| 6147 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6148 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6149 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6150 | HttpRequestHeaders headers; |
| 6151 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6152 | req->SetExtraRequestHeaders(headers); |
| 6153 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6154 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6155 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6156 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6157 | |
| 6158 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6159 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6160 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6161 | } |
| 6162 | } |
| 6163 | |
| 6164 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
| 6165 | ASSERT_TRUE(test_server_.Start()); |
| 6166 | |
| 6167 | // populate the cache |
| 6168 | { |
| 6169 | TestDelegate d; |
| 6170 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6171 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6172 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6173 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6174 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6175 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6176 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6177 | |
| 6178 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6179 | } |
| 6180 | |
| 6181 | // repeat request with end-to-end validation. since auth-basic results in a |
| 6182 | // cachable page, we expect this test to result in a 304. in which case, the |
| 6183 | // response should be fetched from the cache. |
| 6184 | { |
| 6185 | TestDelegate d; |
| 6186 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6187 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6188 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6189 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6190 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 6191 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6192 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6193 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6194 | |
| 6195 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6196 | |
| 6197 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6198 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6199 | } |
| 6200 | } |
| 6201 | |
| 6202 | // Check that Set-Cookie headers in 401 responses are respected. |
| 6203 | // http://crbug.com/6450 |
| 6204 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
| 6205 | ASSERT_TRUE(test_server_.Start()); |
| 6206 | |
| 6207 | GURL url_requiring_auth = |
| 6208 | test_server_.GetURL("auth-basic?set-cookie-if-challenged"); |
| 6209 | |
| 6210 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 6211 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 6212 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6213 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6214 | TestURLRequestContext context(true); |
| 6215 | context.set_network_delegate(&network_delegate); |
| 6216 | context.Init(); |
| 6217 | |
| 6218 | TestDelegate d; |
| 6219 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6220 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6221 | scoped_ptr<URLRequest> r( |
| 6222 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6223 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6224 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6225 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6226 | |
| 6227 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6228 | |
| 6229 | // Make sure we sent the cookie in the restarted transaction. |
| 6230 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 6231 | != std::string::npos); |
| 6232 | } |
| 6233 | |
| 6234 | // Same test as above, except this time the restart is initiated earlier |
| 6235 | // (without user intervention since identity is embedded in the URL). |
| 6236 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6237 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6238 | TestURLRequestContext context(true); |
| 6239 | context.set_network_delegate(&network_delegate); |
| 6240 | context.Init(); |
| 6241 | |
| 6242 | TestDelegate d; |
| 6243 | |
| 6244 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 6245 | replacements.SetUsernameStr("user2"); |
| 6246 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6247 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 6248 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6249 | scoped_ptr<URLRequest> r( |
| 6250 | context.CreateRequest(url_with_identity, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6251 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6252 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6253 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6254 | |
| 6255 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 6256 | |
| 6257 | // Make sure we sent the cookie in the restarted transaction. |
| 6258 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 6259 | != std::string::npos); |
| 6260 | } |
| 6261 | } |
| 6262 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6263 | // Tests that load timing works as expected with auth and the cache. |
| 6264 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
| 6265 | ASSERT_TRUE(test_server_.Start()); |
| 6266 | |
| 6267 | // populate the cache |
| 6268 | { |
| 6269 | TestDelegate d; |
| 6270 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6271 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6272 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6273 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6274 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6275 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6276 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6277 | |
| 6278 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6279 | |
| 6280 | LoadTimingInfo load_timing_info_before_auth; |
| 6281 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 6282 | &load_timing_info_before_auth)); |
| 6283 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 6284 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 6285 | |
| 6286 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6287 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6288 | // The test server does not support keep alive sockets, so the second |
| 6289 | // request with auth should use a new socket. |
| 6290 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 6291 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 6292 | load_timing_info.socket_log_id); |
| 6293 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 6294 | load_timing_info.connect_timing.connect_start); |
| 6295 | } |
| 6296 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6297 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 6298 | // cachable page, we expect this test to result in a 304. In which case, the |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6299 | // response should be fetched from the cache. |
| 6300 | { |
| 6301 | TestDelegate d; |
| 6302 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6303 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6304 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6305 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6306 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 6307 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6308 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6309 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6310 | |
| 6311 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6312 | |
| 6313 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6314 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6315 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6316 | // Since there was a request that went over the wire, the load timing |
| 6317 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6318 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6319 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6320 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6321 | } |
| 6322 | } |
| 6323 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6324 | // In this test, we do a POST which the server will 302 redirect. |
| 6325 | // The subsequent transaction should use GET, and should not send the |
| 6326 | // Content-Type header. |
| 6327 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 6328 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
| 6329 | ASSERT_TRUE(test_server_.Start()); |
| 6330 | |
| 6331 | const char kData[] = "hello world"; |
| 6332 | |
| 6333 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6334 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6335 | test_server_.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6336 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6337 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6338 | |
| 6339 | // Set headers (some of which are specific to the POST). |
| 6340 | HttpRequestHeaders headers; |
| 6341 | headers.AddHeadersFromString( |
| 6342 | "Content-Type: multipart/form-data; " |
| 6343 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 6344 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 6345 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 6346 | "Accept-Language: en-US,en\r\n" |
| 6347 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 6348 | "Content-Length: 11\r\n" |
| 6349 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6350 | req->SetExtraRequestHeaders(headers); |
| 6351 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6352 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6353 | |
| 6354 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6355 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6356 | EXPECT_EQ("text/html", mime_type); |
| 6357 | |
| 6358 | const std::string& data = d.data_received(); |
| 6359 | |
| 6360 | // Check that the post-specific headers were stripped: |
| 6361 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 6362 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 6363 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 6364 | |
| 6365 | // These extra request headers should not have been stripped. |
| 6366 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 6367 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 6368 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 6369 | } |
| 6370 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6371 | // The following tests check that we handle mutating the request for HTTP |
| 6372 | // redirects as expected. |
| 6373 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 6374 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6375 | |
| 6376 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
| 6377 | ASSERT_TRUE(test_server_.Start()); |
| 6378 | |
| 6379 | const GURL url = test_server_.GetURL("files/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6380 | const GURL https_redirect_url = |
| 6381 | test_server_.GetURL("files/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6382 | |
| 6383 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 6384 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6385 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6386 | |
| 6387 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6388 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6389 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 6390 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 6391 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6392 | } |
| 6393 | |
| 6394 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
| 6395 | ASSERT_TRUE(test_server_.Start()); |
| 6396 | |
| 6397 | const GURL url = test_server_.GetURL("files/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6398 | const GURL https_redirect_url = |
| 6399 | test_server_.GetURL("files/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6400 | |
| 6401 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 6402 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6403 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6404 | |
| 6405 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6406 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6407 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 6408 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 6409 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6410 | } |
| 6411 | |
| 6412 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
| 6413 | ASSERT_TRUE(test_server_.Start()); |
| 6414 | |
| 6415 | const GURL url = test_server_.GetURL("files/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6416 | const GURL https_redirect_url = |
| 6417 | test_server_.GetURL("files/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6418 | |
| 6419 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 6420 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 6421 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6422 | |
| 6423 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6424 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6425 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 6426 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 6427 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6428 | } |
| 6429 | |
| 6430 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
| 6431 | ASSERT_TRUE(test_server_.Start()); |
| 6432 | |
| 6433 | const GURL url = test_server_.GetURL("files/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6434 | const GURL https_redirect_url = |
| 6435 | test_server_.GetURL("files/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6436 | |
| 6437 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 6438 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6439 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6440 | |
| 6441 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6442 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6443 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 6444 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6445 | } |
| 6446 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6447 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
| 6448 | ASSERT_TRUE(test_server_.Start()); |
| 6449 | |
| 6450 | const GURL url = test_server_.GetURL("files/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6451 | const GURL https_redirect_url = |
| 6452 | test_server_.GetURL("files/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6453 | |
| 6454 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 6455 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6456 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6457 | |
| 6458 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6459 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6460 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 6461 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6462 | } |
| 6463 | |
| 6464 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 6465 | // Certain legacy apis that pre-date the response code expect this behavior |
| 6466 | // (Like Google Drive). |
| 6467 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
| 6468 | ASSERT_TRUE(test_server_.Start()); |
| 6469 | |
| 6470 | TestDelegate d; |
| 6471 | const GURL url = test_server_.GetURL("files/308-without-location-header"); |
| 6472 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6473 | scoped_ptr<URLRequest> request( |
| 6474 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6475 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6476 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6477 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6478 | EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
| 6479 | EXPECT_EQ(OK, request->status().error()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6480 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6481 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6482 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 6483 | } |
| 6484 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6485 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
| 6486 | ASSERT_TRUE(test_server_.Start()); |
| 6487 | |
| 6488 | GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment")); |
| 6489 | GURL expected_url(test_server_.GetURL("echo#fragment")); |
| 6490 | |
| 6491 | TestDelegate d; |
| 6492 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6493 | scoped_ptr<URLRequest> r( |
| 6494 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6495 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6496 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6497 | base::RunLoop().Run(); |
| 6498 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6499 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6500 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6501 | EXPECT_EQ(OK, r->status().error()); |
| 6502 | EXPECT_EQ(original_url, r->original_url()); |
| 6503 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6504 | } |
| 6505 | } |
| 6506 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6507 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
| 6508 | ASSERT_TRUE(test_server_.Start()); |
| 6509 | |
| 6510 | GURL url(test_server_.GetURL("files/redirect302-to-echo")); |
| 6511 | GURL first_party_url("http://example.com"); |
| 6512 | |
| 6513 | TestDelegate d; |
| 6514 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6515 | scoped_ptr<URLRequest> r( |
| 6516 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6517 | r->set_first_party_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6518 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6519 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6520 | base::RunLoop().Run(); |
| 6521 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6522 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6523 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6524 | EXPECT_EQ(OK, r->status().error()); |
| 6525 | EXPECT_EQ(first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6526 | } |
| 6527 | } |
| 6528 | |
| 6529 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
| 6530 | ASSERT_TRUE(test_server_.Start()); |
| 6531 | |
| 6532 | GURL url(test_server_.GetURL("files/redirect302-to-echo")); |
| 6533 | GURL original_first_party_url("http://example.com"); |
| 6534 | GURL expected_first_party_url(test_server_.GetURL("echo")); |
| 6535 | |
| 6536 | TestDelegate d; |
| 6537 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6538 | scoped_ptr<URLRequest> r( |
| 6539 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6540 | r->set_first_party_for_cookies(original_first_party_url); |
| 6541 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6542 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 6543 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6544 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6545 | base::RunLoop().Run(); |
| 6546 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6547 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6548 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6549 | EXPECT_EQ(OK, r->status().error()); |
| 6550 | EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6551 | } |
| 6552 | } |
| 6553 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6554 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
| 6555 | ASSERT_TRUE(test_server_.Start()); |
| 6556 | |
| 6557 | const char kData[] = "hello world"; |
| 6558 | |
| 6559 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6560 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6561 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6562 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6563 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6564 | HttpRequestHeaders headers; |
| 6565 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 6566 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6567 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6568 | |
| 6569 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6570 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6571 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6572 | AddTestInterceptor()->set_main_intercept_job(job); |
| 6573 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6574 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6575 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6576 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6577 | } |
| 6578 | |
| 6579 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 6580 | ASSERT_TRUE(test_server_.Start()); |
| 6581 | |
| 6582 | const char kData[] = "hello world"; |
| 6583 | |
| 6584 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6585 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6586 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6587 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6588 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6589 | HttpRequestHeaders headers; |
| 6590 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 6591 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6592 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6593 | |
| 6594 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6595 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6596 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
| 6597 | "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6598 | AddTestInterceptor()->set_main_intercept_job(job); |
| 6599 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6600 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6601 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6602 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6603 | EXPECT_EQ(kData, d.data_received()); |
| 6604 | } |
| 6605 | |
| 6606 | // Check that default A-L header is sent. |
| 6607 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
| 6608 | ASSERT_TRUE(test_server_.Start()); |
| 6609 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 6610 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6611 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6612 | TestURLRequestContext context(true); |
| 6613 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6614 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6615 | context.Init(); |
| 6616 | |
| 6617 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6618 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6619 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6620 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6621 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6622 | EXPECT_EQ("en", d.data_received()); |
| 6623 | } |
| 6624 | |
| 6625 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 6626 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
| 6627 | ASSERT_TRUE(test_server_.Start()); |
| 6628 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 6629 | std::string empty_string; // Avoid most vexing parse on line below. |
| 6630 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6631 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6632 | TestURLRequestContext context(true); |
| 6633 | context.set_network_delegate(&network_delegate); |
| 6634 | context.Init(); |
| 6635 | // We override the language after initialization because empty entries |
| 6636 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6637 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6638 | |
| 6639 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6640 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6641 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6642 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6643 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6644 | EXPECT_EQ("None", d.data_received()); |
| 6645 | } |
| 6646 | |
| 6647 | // Check that if request overrides the A-L header, the default is not appended. |
| 6648 | // See http://crbug.com/20894 |
| 6649 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
| 6650 | ASSERT_TRUE(test_server_.Start()); |
| 6651 | |
| 6652 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6653 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6654 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6655 | HttpRequestHeaders headers; |
| 6656 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6657 | req->SetExtraRequestHeaders(headers); |
| 6658 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6659 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6660 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 6661 | } |
| 6662 | |
| 6663 | // Check that default A-E header is sent. |
| 6664 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
| 6665 | ASSERT_TRUE(test_server_.Start()); |
| 6666 | |
| 6667 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6668 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6669 | test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6670 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6671 | req->SetExtraRequestHeaders(headers); |
| 6672 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6673 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6674 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 6675 | } |
| 6676 | |
| 6677 | // Check that if request overrides the A-E header, the default is not appended. |
| 6678 | // See http://crbug.com/47381 |
| 6679 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
| 6680 | ASSERT_TRUE(test_server_.Start()); |
| 6681 | |
| 6682 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6683 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6684 | test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6685 | HttpRequestHeaders headers; |
| 6686 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6687 | req->SetExtraRequestHeaders(headers); |
| 6688 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6689 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6690 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 6691 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 6692 | } |
| 6693 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 6694 | // Check that setting the A-C header sends the proper header. |
| 6695 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6696 | ASSERT_TRUE(test_server_.Start()); |
| 6697 | |
| 6698 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6699 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6700 | test_server_.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6701 | HttpRequestHeaders headers; |
| 6702 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6703 | req->SetExtraRequestHeaders(headers); |
| 6704 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6705 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6706 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 6707 | } |
| 6708 | |
| 6709 | // Check that default User-Agent header is sent. |
| 6710 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
| 6711 | ASSERT_TRUE(test_server_.Start()); |
| 6712 | |
| 6713 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6714 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6715 | test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6716 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6717 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6718 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 6719 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6720 | } |
| 6721 | |
| 6722 | // Check that if request overrides the User-Agent header, |
| 6723 | // the default is not appended. |
| 6724 | TEST_F(URLRequestTestHTTP, OverrideUserAgent) { |
| 6725 | ASSERT_TRUE(test_server_.Start()); |
| 6726 | |
| 6727 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6728 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6729 | test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6730 | HttpRequestHeaders headers; |
| 6731 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6732 | req->SetExtraRequestHeaders(headers); |
| 6733 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6734 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 6735 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6736 | } |
| 6737 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6738 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 6739 | // User-Agent header to be sent but does not send the Accept-Language and |
| 6740 | // Accept-Charset headers. |
| 6741 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
| 6742 | ASSERT_TRUE(test_server_.Start()); |
| 6743 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6744 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6745 | TestURLRequestContext context(true); |
| 6746 | context.set_network_delegate(&network_delegate); |
| 6747 | context.Init(); |
| 6748 | // We override the HttpUserAgentSettings after initialization because empty |
| 6749 | // entries get overridden by Init(). |
| 6750 | context.set_http_user_agent_settings(NULL); |
| 6751 | |
| 6752 | struct { |
| 6753 | const char* request; |
| 6754 | const char* expected_response; |
| 6755 | } tests[] = { { "echoheader?Accept-Language", "None" }, |
| 6756 | { "echoheader?Accept-Charset", "None" }, |
| 6757 | { "echoheader?User-Agent", "" } }; |
| 6758 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 6759 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6760 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6761 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6762 | test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6763 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6764 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6765 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 6766 | << " Request = \"" << tests[i].request << "\""; |
| 6767 | } |
| 6768 | } |
| 6769 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6770 | // Make sure that URLRequest passes on its priority updates to |
| 6771 | // newly-created jobs after the first one. |
| 6772 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
| 6773 | ASSERT_TRUE(test_server_.Start()); |
| 6774 | |
| 6775 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6776 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6777 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6778 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6779 | |
| 6780 | scoped_refptr<URLRequestRedirectJob> redirect_job = |
| 6781 | new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6782 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6783 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6784 | AddTestInterceptor()->set_main_intercept_job(redirect_job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6785 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6786 | req->SetPriority(LOW); |
| 6787 | req->Start(); |
| 6788 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6789 | |
| 6790 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6791 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6792 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6793 | |
| 6794 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6795 | base::RunLoop().Run(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6796 | EXPECT_EQ(LOW, job->priority()); |
| 6797 | } |
| 6798 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6799 | // Check that creating a network request while entering/exiting suspend mode |
| 6800 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 6801 | // does not return an HttpTransaction. |
| 6802 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 6803 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6804 | HttpNetworkSession::Params params; |
| 6805 | params.host_resolver = default_context_.host_resolver(); |
| 6806 | params.cert_verifier = default_context_.cert_verifier(); |
| 6807 | params.transport_security_state = default_context_.transport_security_state(); |
| 6808 | params.proxy_service = default_context_.proxy_service(); |
| 6809 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6810 | params.http_auth_handler_factory = |
| 6811 | default_context_.http_auth_handler_factory(); |
| 6812 | params.network_delegate = &default_network_delegate_; |
| 6813 | params.http_server_properties = default_context_.http_server_properties(); |
| 6814 | scoped_ptr<HttpNetworkLayer> network_layer( |
| 6815 | new HttpNetworkLayer(new HttpNetworkSession(params))); |
| 6816 | network_layer->OnSuspend(); |
| 6817 | |
| 6818 | HttpCache http_cache(network_layer.release(), default_context_.net_log(), |
| 6819 | HttpCache::DefaultBackend::InMemory(0)); |
| 6820 | |
| 6821 | TestURLRequestContext context(true); |
| 6822 | context.set_http_transaction_factory(&http_cache); |
| 6823 | context.Init(); |
| 6824 | |
| 6825 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6826 | scoped_ptr<URLRequest> req( |
| 6827 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6828 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6829 | base::RunLoop().Run(); |
| 6830 | |
| 6831 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6832 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6833 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6834 | } |
| 6835 | |
| 6836 | // Check that creating a network request while entering/exiting suspend mode |
| 6837 | // fails as it should in the case there is no cache. This is the only case |
| 6838 | // where an HttpTransactionFactory does not return an HttpTransaction. |
| 6839 | TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) { |
| 6840 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6841 | HttpNetworkSession::Params params; |
| 6842 | params.host_resolver = default_context_.host_resolver(); |
| 6843 | params.cert_verifier = default_context_.cert_verifier(); |
| 6844 | params.transport_security_state = default_context_.transport_security_state(); |
| 6845 | params.proxy_service = default_context_.proxy_service(); |
| 6846 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6847 | params.http_auth_handler_factory = |
| 6848 | default_context_.http_auth_handler_factory(); |
| 6849 | params.network_delegate = &default_network_delegate_; |
| 6850 | params.http_server_properties = default_context_.http_server_properties(); |
| 6851 | HttpNetworkLayer network_layer(new HttpNetworkSession(params)); |
| 6852 | network_layer.OnSuspend(); |
| 6853 | |
| 6854 | TestURLRequestContext context(true); |
| 6855 | context.set_http_transaction_factory(&network_layer); |
| 6856 | context.Init(); |
| 6857 | |
| 6858 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6859 | scoped_ptr<URLRequest> req( |
| 6860 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6861 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6862 | base::RunLoop().Run(); |
| 6863 | |
| 6864 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6865 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6866 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6867 | } |
| 6868 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 6869 | TEST_F(URLRequestTestHTTP, NetworkAccessedSetOnNetworkRequest) { |
| 6870 | ASSERT_TRUE(test_server_.Start()); |
| 6871 | |
| 6872 | TestDelegate d; |
| 6873 | GURL test_url(test_server_.GetURL(std::string())); |
| 6874 | scoped_ptr<URLRequest> req( |
| 6875 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
| 6876 | |
| 6877 | req->Start(); |
| 6878 | base::RunLoop().Run(); |
| 6879 | |
| 6880 | EXPECT_TRUE(req->response_info().network_accessed); |
| 6881 | } |
| 6882 | |
| 6883 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnCachedResponse) { |
| 6884 | ASSERT_TRUE(test_server_.Start()); |
| 6885 | |
| 6886 | // Populate the cache. |
| 6887 | TestDelegate d; |
| 6888 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6889 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d)); |
| 6890 | req->Start(); |
| 6891 | base::RunLoop().Run(); |
| 6892 | |
| 6893 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 6894 | EXPECT_TRUE(req->response_info().network_accessed); |
| 6895 | EXPECT_FALSE(req->response_info().was_cached); |
| 6896 | |
| 6897 | req = default_context_.CreateRequest(test_server_.GetURL("cachetime"), |
| 6898 | DEFAULT_PRIORITY, &d); |
| 6899 | req->Start(); |
| 6900 | base::RunLoop().Run(); |
| 6901 | |
| 6902 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 6903 | EXPECT_FALSE(req->response_info().network_accessed); |
| 6904 | EXPECT_TRUE(req->response_info().was_cached); |
| 6905 | } |
| 6906 | |
| 6907 | TEST_F(URLRequestTestHTTP, NetworkAccessedClearOnLoadOnlyFromCache) { |
| 6908 | ASSERT_TRUE(test_server_.Start()); |
| 6909 | |
| 6910 | TestDelegate d; |
| 6911 | GURL test_url(test_server_.GetURL(std::string())); |
| 6912 | scoped_ptr<URLRequest> req( |
| 6913 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
| 6914 | req->SetLoadFlags(LOAD_ONLY_FROM_CACHE); |
| 6915 | |
| 6916 | req->Start(); |
| 6917 | base::RunLoop().Run(); |
| 6918 | |
| 6919 | EXPECT_FALSE(req->response_info().network_accessed); |
| 6920 | } |
| 6921 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6922 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 6923 | public: |
| 6924 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 6925 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 6926 | // tests into the factory tests. |
| 6927 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 6928 | } |
| 6929 | |
| 6930 | void SetUpFactory() override { |
| 6931 | interceptor_ = new MockURLRequestInterceptor(); |
| 6932 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
| 6933 | job_factory_.Pass(), make_scoped_ptr(interceptor_))); |
| 6934 | } |
| 6935 | |
| 6936 | MockURLRequestInterceptor* interceptor() const { |
| 6937 | return interceptor_; |
| 6938 | } |
| 6939 | |
| 6940 | private: |
| 6941 | MockURLRequestInterceptor* interceptor_; |
| 6942 | }; |
| 6943 | |
| 6944 | TEST_F(URLRequestInterceptorTestHTTP, |
| 6945 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 6946 | interceptor()->set_intercept_redirect(true); |
| 6947 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 6948 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 6949 | |
| 6950 | ASSERT_TRUE(test_server()->Start()); |
| 6951 | |
| 6952 | TestDelegate d; |
| 6953 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6954 | test_server()->GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6955 | req->Start(); |
| 6956 | base::RunLoop().Run(); |
| 6957 | |
| 6958 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 6959 | // Check we got one good response |
| 6960 | EXPECT_TRUE(req->status().is_success()); |
| 6961 | if (req->status().is_success()) |
| 6962 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 6963 | |
| 6964 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 6965 | EXPECT_EQ(1, d.response_started_count()); |
| 6966 | EXPECT_EQ(0, d.received_redirect_count()); |
| 6967 | |
| 6968 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 6969 | EXPECT_EQ(1, default_network_delegate()->before_send_headers_count()); |
| 6970 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 6971 | } |
| 6972 | |
| 6973 | TEST_F(URLRequestInterceptorTestHTTP, |
| 6974 | NetworkDelegateNotificationOnErrorIntercept) { |
| 6975 | // Intercept that error and respond with an OK response. |
| 6976 | interceptor()->set_intercept_final_response(true); |
| 6977 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 6978 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 6979 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 6980 | |
| 6981 | ASSERT_TRUE(test_server()->Start()); |
| 6982 | |
| 6983 | TestDelegate d; |
| 6984 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
| 6985 | test_server()->GetURL("files/two-content-lengths.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6986 | &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6987 | req->set_method("GET"); |
| 6988 | req->Start(); |
| 6989 | base::RunLoop().Run(); |
| 6990 | |
| 6991 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 6992 | |
| 6993 | // Check we received one good response. |
| 6994 | EXPECT_TRUE(req->status().is_success()); |
| 6995 | if (req->status().is_success()) |
| 6996 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 6997 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 6998 | EXPECT_EQ(1, d.response_started_count()); |
| 6999 | EXPECT_EQ(0, d.received_redirect_count()); |
| 7000 | |
| 7001 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 7002 | EXPECT_EQ(1, default_network_delegate()->before_send_headers_count()); |
| 7003 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 7004 | } |
| 7005 | |
| 7006 | TEST_F(URLRequestInterceptorTestHTTP, |
| 7007 | NetworkDelegateNotificationOnResponseIntercept) { |
| 7008 | // Intercept that error and respond with an OK response. |
| 7009 | interceptor()->set_intercept_final_response(true); |
| 7010 | |
| 7011 | // Intercept with a real URLRequestHttpJob. |
| 7012 | interceptor()->set_use_url_request_http_job(true); |
| 7013 | |
| 7014 | ASSERT_TRUE(test_server()->Start()); |
| 7015 | |
| 7016 | TestDelegate d; |
| 7017 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7018 | test_server()->GetURL("files/simple.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 7019 | req->set_method("GET"); |
| 7020 | req->Start(); |
| 7021 | base::RunLoop().Run(); |
| 7022 | |
| 7023 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 7024 | |
| 7025 | // Check we received one good response. |
| 7026 | EXPECT_TRUE(req->status().is_success()); |
| 7027 | if (req->status().is_success()) |
| 7028 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7029 | EXPECT_EQ("hello", d.data_received()); |
| 7030 | EXPECT_EQ(1, d.response_started_count()); |
| 7031 | EXPECT_EQ(0, d.received_redirect_count()); |
| 7032 | |
| 7033 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 7034 | EXPECT_EQ(2, default_network_delegate()->before_send_headers_count()); |
| 7035 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 7036 | } |
| 7037 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7038 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 7039 | public: |
| 7040 | URLRequestTestReferrerPolicy() {} |
| 7041 | |
| 7042 | void InstantiateSameOriginServers(SpawnedTestServer::Type origin_type) { |
| 7043 | origin_server_.reset(new SpawnedTestServer( |
| 7044 | origin_type, SpawnedTestServer::kLocalhost, |
| 7045 | origin_type == SpawnedTestServer::TYPE_HTTPS |
| 7046 | ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl")) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 7047 | : base::FilePath(kTestFilePath))); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7048 | ASSERT_TRUE(origin_server_->Start()); |
| 7049 | } |
| 7050 | |
| 7051 | void InstantiateCrossOriginServers(SpawnedTestServer::Type origin_type, |
| 7052 | SpawnedTestServer::Type destination_type) { |
| 7053 | origin_server_.reset(new SpawnedTestServer( |
| 7054 | origin_type, SpawnedTestServer::kLocalhost, |
| 7055 | origin_type == SpawnedTestServer::TYPE_HTTPS |
| 7056 | ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl")) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 7057 | : base::FilePath(kTestFilePath))); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7058 | ASSERT_TRUE(origin_server_->Start()); |
| 7059 | |
| 7060 | destination_server_.reset(new SpawnedTestServer( |
| 7061 | destination_type, SpawnedTestServer::kLocalhost, |
| 7062 | destination_type == SpawnedTestServer::TYPE_HTTPS |
| 7063 | ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl")) |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 7064 | : base::FilePath(kTestFilePath))); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7065 | ASSERT_TRUE(destination_server_->Start()); |
| 7066 | } |
| 7067 | |
| 7068 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 7069 | const GURL& referrer, |
| 7070 | const GURL& expected) { |
| 7071 | // Create and execute the request: we'll only have a |destination_server_| |
| 7072 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 7073 | // |origin_server_| for both endpoints. |
| 7074 | GURL destination_url = |
| 7075 | destination_server_ ? destination_server_->GetURL("echoheader?Referer") |
| 7076 | : origin_server_->GetURL("echoheader?Referer"); |
| 7077 | GURL origin_url = |
| 7078 | origin_server_->GetURL("server-redirect?" + destination_url.spec()); |
| 7079 | |
| 7080 | TestDelegate d; |
| 7081 | scoped_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7082 | default_context_.CreateRequest(origin_url, DEFAULT_PRIORITY, &d)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 7083 | req->set_referrer_policy(policy); |
| 7084 | req->SetReferrer(referrer.spec()); |
| 7085 | req->Start(); |
| 7086 | base::RunLoop().Run(); |
| 7087 | |
| 7088 | EXPECT_EQ(1, d.response_started_count()); |
| 7089 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7090 | EXPECT_EQ(destination_url, req->url()); |
| 7091 | EXPECT_TRUE(req->status().is_success()); |
| 7092 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 7093 | |
| 7094 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 7095 | if (expected.is_empty()) |
| 7096 | EXPECT_EQ("None", d.data_received()); |
| 7097 | else |
| 7098 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 7099 | } |
| 7100 | |
| 7101 | SpawnedTestServer* origin_server() const { return origin_server_.get(); } |
| 7102 | |
| 7103 | private: |
| 7104 | scoped_ptr<SpawnedTestServer> origin_server_; |
| 7105 | scoped_ptr<SpawnedTestServer> destination_server_; |
| 7106 | }; |
| 7107 | |
| 7108 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
| 7109 | InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTP); |
| 7110 | |
| 7111 | VerifyReferrerAfterRedirect( |
| 7112 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7113 | origin_server()->GetURL("path/to/file.html"), |
| 7114 | origin_server()->GetURL("path/to/file.html")); |
| 7115 | |
| 7116 | VerifyReferrerAfterRedirect( |
| 7117 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7118 | origin_server()->GetURL("path/to/file.html"), |
| 7119 | origin_server()->GetURL("path/to/file.html")); |
| 7120 | |
| 7121 | VerifyReferrerAfterRedirect( |
| 7122 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7123 | origin_server()->GetURL("path/to/file.html"), |
| 7124 | origin_server()->GetURL("path/to/file.html")); |
| 7125 | |
| 7126 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7127 | origin_server()->GetURL("path/to/file.html"), |
| 7128 | origin_server()->GetURL("path/to/file.html")); |
| 7129 | } |
| 7130 | |
| 7131 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 7132 | InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP, |
| 7133 | SpawnedTestServer::TYPE_HTTP); |
| 7134 | |
| 7135 | VerifyReferrerAfterRedirect( |
| 7136 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7137 | origin_server()->GetURL("path/to/file.html"), |
| 7138 | origin_server()->GetURL("path/to/file.html")); |
| 7139 | |
| 7140 | VerifyReferrerAfterRedirect( |
| 7141 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7142 | origin_server()->GetURL("path/to/file.html"), |
| 7143 | origin_server()->GetURL(std::string())); |
| 7144 | |
| 7145 | VerifyReferrerAfterRedirect( |
| 7146 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7147 | origin_server()->GetURL("path/to/file.html"), |
| 7148 | origin_server()->GetURL(std::string())); |
| 7149 | |
| 7150 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7151 | origin_server()->GetURL("path/to/file.html"), |
| 7152 | origin_server()->GetURL("path/to/file.html")); |
| 7153 | } |
| 7154 | |
| 7155 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
| 7156 | InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTPS); |
| 7157 | |
| 7158 | VerifyReferrerAfterRedirect( |
| 7159 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7160 | origin_server()->GetURL("path/to/file.html"), |
| 7161 | origin_server()->GetURL("path/to/file.html")); |
| 7162 | |
| 7163 | VerifyReferrerAfterRedirect( |
| 7164 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7165 | origin_server()->GetURL("path/to/file.html"), |
| 7166 | origin_server()->GetURL("path/to/file.html")); |
| 7167 | |
| 7168 | VerifyReferrerAfterRedirect( |
| 7169 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7170 | origin_server()->GetURL("path/to/file.html"), |
| 7171 | origin_server()->GetURL("path/to/file.html")); |
| 7172 | |
| 7173 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7174 | origin_server()->GetURL("path/to/file.html"), |
| 7175 | origin_server()->GetURL("path/to/file.html")); |
| 7176 | } |
| 7177 | |
| 7178 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 7179 | InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS, |
| 7180 | SpawnedTestServer::TYPE_HTTPS); |
| 7181 | |
| 7182 | VerifyReferrerAfterRedirect( |
| 7183 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7184 | origin_server()->GetURL("path/to/file.html"), |
| 7185 | origin_server()->GetURL("path/to/file.html")); |
| 7186 | |
| 7187 | VerifyReferrerAfterRedirect( |
| 7188 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7189 | origin_server()->GetURL("path/to/file.html"), |
| 7190 | origin_server()->GetURL(std::string())); |
| 7191 | |
| 7192 | VerifyReferrerAfterRedirect( |
| 7193 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7194 | origin_server()->GetURL("path/to/file.html"), |
| 7195 | origin_server()->GetURL(std::string())); |
| 7196 | |
| 7197 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7198 | origin_server()->GetURL("path/to/file.html"), |
| 7199 | origin_server()->GetURL("path/to/file.html")); |
| 7200 | } |
| 7201 | |
| 7202 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 7203 | InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP, |
| 7204 | SpawnedTestServer::TYPE_HTTPS); |
| 7205 | |
| 7206 | VerifyReferrerAfterRedirect( |
| 7207 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7208 | origin_server()->GetURL("path/to/file.html"), |
| 7209 | origin_server()->GetURL("path/to/file.html")); |
| 7210 | |
| 7211 | VerifyReferrerAfterRedirect( |
| 7212 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7213 | origin_server()->GetURL("path/to/file.html"), |
| 7214 | origin_server()->GetURL(std::string())); |
| 7215 | |
| 7216 | VerifyReferrerAfterRedirect( |
| 7217 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7218 | origin_server()->GetURL("path/to/file.html"), |
| 7219 | origin_server()->GetURL(std::string())); |
| 7220 | |
| 7221 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7222 | origin_server()->GetURL("path/to/file.html"), |
| 7223 | origin_server()->GetURL("path/to/file.html")); |
| 7224 | } |
| 7225 | |
| 7226 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 7227 | InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS, |
| 7228 | SpawnedTestServer::TYPE_HTTP); |
| 7229 | |
| 7230 | VerifyReferrerAfterRedirect( |
| 7231 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7232 | origin_server()->GetURL("path/to/file.html"), GURL()); |
| 7233 | |
| 7234 | VerifyReferrerAfterRedirect( |
| 7235 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7236 | origin_server()->GetURL("path/to/file.html"), GURL()); |
| 7237 | |
| 7238 | VerifyReferrerAfterRedirect( |
| 7239 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7240 | origin_server()->GetURL("path/to/file.html"), |
| 7241 | origin_server()->GetURL(std::string())); |
| 7242 | |
| 7243 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7244 | origin_server()->GetURL("path/to/file.html"), |
| 7245 | origin_server()->GetURL("path/to/file.html")); |
| 7246 | } |
| 7247 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 7248 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7249 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7250 | HTTPSRequestTest() : default_context_(true) { |
| 7251 | default_context_.set_network_delegate(&default_network_delegate_); |
| 7252 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7253 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 7254 | ~HTTPSRequestTest() override {} |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7255 | |
| 7256 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7257 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7258 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7259 | }; |
| 7260 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 7261 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7262 | SpawnedTestServer test_server( |
| 7263 | SpawnedTestServer::TYPE_HTTPS, |
| 7264 | SpawnedTestServer::kLocalhost, |
| 7265 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7266 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7267 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7268 | TestDelegate d; |
| 7269 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7270 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7271 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7272 | r->Start(); |
| 7273 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7274 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7275 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7276 | |
| 7277 | EXPECT_EQ(1, d.response_started_count()); |
| 7278 | EXPECT_FALSE(d.received_data_before_response()); |
| 7279 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7280 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7281 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7282 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7283 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7284 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7285 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7286 | } |
| 7287 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 7288 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7289 | SpawnedTestServer::SSLOptions ssl_options( |
| 7290 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 7291 | SpawnedTestServer test_server( |
| 7292 | SpawnedTestServer::TYPE_HTTPS, |
| 7293 | ssl_options, |
| 7294 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7295 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7296 | |
| 7297 | bool err_allowed = true; |
| 7298 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 7299 | TestDelegate d; |
| 7300 | { |
| 7301 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7302 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7303 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7304 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7305 | r->Start(); |
| 7306 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7307 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7308 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7309 | |
| 7310 | EXPECT_EQ(1, d.response_started_count()); |
| 7311 | EXPECT_FALSE(d.received_data_before_response()); |
| 7312 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7313 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7314 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7315 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7316 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7317 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7318 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7319 | } |
| 7320 | } |
| 7321 | } |
| 7322 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 7323 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7324 | SpawnedTestServer::SSLOptions ssl_options( |
| 7325 | SpawnedTestServer::SSLOptions::CERT_EXPIRED); |
| 7326 | SpawnedTestServer test_server( |
| 7327 | SpawnedTestServer::TYPE_HTTPS, |
| 7328 | ssl_options, |
| 7329 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7330 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7331 | |
| 7332 | // Iterate from false to true, just so that we do the opposite of the |
| 7333 | // previous test in order to increase test coverage. |
| 7334 | bool err_allowed = false; |
| 7335 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 7336 | TestDelegate d; |
| 7337 | { |
| 7338 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7339 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7340 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7341 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7342 | r->Start(); |
| 7343 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7344 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7345 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7346 | |
| 7347 | EXPECT_EQ(1, d.response_started_count()); |
| 7348 | EXPECT_FALSE(d.received_data_before_response()); |
| 7349 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7350 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7351 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7352 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7353 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7354 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7355 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7356 | } |
| 7357 | } |
| 7358 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 7359 | |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 7360 | // Tests that servers which require a deprecated cipher suite still work. |
| 7361 | TEST_F(HTTPSRequestTest, CipherFallbackTest) { |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 7362 | TestNetLog net_log; |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 7363 | default_context_.set_net_log(&net_log); |
| 7364 | |
| 7365 | SpawnedTestServer::SSLOptions ssl_options; |
| 7366 | ssl_options.bulk_ciphers = SpawnedTestServer::SSLOptions::BULK_CIPHER_RC4; |
| 7367 | SpawnedTestServer test_server( |
| 7368 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 7369 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7370 | ASSERT_TRUE(test_server.Start()); |
| 7371 | |
| 7372 | TestDelegate d; |
| 7373 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7374 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
| 7375 | r->Start(); |
| 7376 | EXPECT_TRUE(r->is_pending()); |
| 7377 | |
| 7378 | base::RunLoop().Run(); |
| 7379 | |
| 7380 | EXPECT_EQ(1, d.response_started_count()); |
| 7381 | EXPECT_FALSE(d.received_data_before_response()); |
| 7382 | EXPECT_NE(0, d.bytes_received()); |
| 7383 | CheckSSLInfo(r->ssl_info()); |
| 7384 | EXPECT_EQ(test_server.host_port_pair().host(), r->GetSocketAddress().host()); |
| 7385 | EXPECT_EQ(test_server.host_port_pair().port(), r->GetSocketAddress().port()); |
| 7386 | |
| 7387 | // No version downgrade should have been necessary. |
| 7388 | EXPECT_FALSE(r->ssl_info().connection_status & |
| 7389 | SSL_CONNECTION_VERSION_FALLBACK); |
| 7390 | int expected_version = SSL_CONNECTION_VERSION_TLS1_2; |
| 7391 | if (SSLClientSocket::GetMaxSupportedSSLVersion() < |
| 7392 | SSL_PROTOCOL_VERSION_TLS1_2) { |
| 7393 | expected_version = SSL_CONNECTION_VERSION_TLS1_1; |
| 7394 | } |
| 7395 | EXPECT_EQ(expected_version, |
| 7396 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 7397 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 7398 | TestNetLogEntry::List entries; |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 7399 | net_log.GetEntries(&entries); |
| 7400 | ExpectLogContainsSomewhere(entries, 0, NetLog::TYPE_SSL_CIPHER_FALLBACK, |
| 7401 | NetLog::PHASE_NONE); |
| 7402 | } |
| 7403 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7404 | // This tests that a load of www.google.com with a certificate error sets |
| 7405 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 7406 | // the interstitial to be fatal. |
| 7407 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7408 | SpawnedTestServer::SSLOptions ssl_options( |
| 7409 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 7410 | SpawnedTestServer test_server( |
| 7411 | SpawnedTestServer::TYPE_HTTPS, |
| 7412 | ssl_options, |
| 7413 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7414 | ASSERT_TRUE(test_server.Start()); |
| 7415 | |
| 7416 | // We require that the URL be www.google.com in order to pick up the |
| 7417 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 7418 | // have to use a MockHostResolver in order to direct www.google.com to the |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7419 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7420 | |
| 7421 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7422 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7423 | TestURLRequestContext context(true); |
| 7424 | context.set_network_delegate(&network_delegate); |
| 7425 | context.set_host_resolver(&host_resolver); |
| 7426 | TransportSecurityState transport_security_state; |
| 7427 | context.set_transport_security_state(&transport_security_state); |
| 7428 | context.Init(); |
| 7429 | |
| 7430 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7431 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 7432 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7433 | test_server.host_port_pair().port())), |
| 7434 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7435 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7436 | r->Start(); |
| 7437 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7438 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7439 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7440 | |
| 7441 | EXPECT_EQ(1, d.response_started_count()); |
| 7442 | EXPECT_FALSE(d.received_data_before_response()); |
| 7443 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7444 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 7445 | } |
| 7446 | |
| 7447 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 7448 | // TransportSecurityState. |
| 7449 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 7450 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 7451 | // matter. It just has to be any error. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7452 | SpawnedTestServer::SSLOptions ssl_options( |
| 7453 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 7454 | SpawnedTestServer test_server( |
| 7455 | SpawnedTestServer::TYPE_HTTPS, |
| 7456 | ssl_options, |
| 7457 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7458 | ASSERT_TRUE(test_server.Start()); |
| 7459 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7460 | // We require that the URL be www.google.com in order to pick up the static |
| 7461 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 7462 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 7463 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7464 | |
| 7465 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7466 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7467 | TestURLRequestContext context(true); |
| 7468 | context.set_network_delegate(&network_delegate); |
| 7469 | context.set_host_resolver(&host_resolver); |
| 7470 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7471 | |
| 7472 | TransportSecurityState::DomainState static_domain_state; |
| 7473 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Adam Langley | 5cbb7d7a | 2014-09-25 23:14:12 | [diff] [blame] | 7474 | "www.google.com", &static_domain_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7475 | context.set_transport_security_state(&transport_security_state); |
| 7476 | context.Init(); |
| 7477 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7478 | TransportSecurityState::DomainState dynamic_domain_state; |
| 7479 | EXPECT_FALSE(transport_security_state.GetDynamicDomainState( |
| 7480 | "www.google.com", &dynamic_domain_state)); |
| 7481 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7482 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7483 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 7484 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7485 | test_server.host_port_pair().port())), |
| 7486 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7487 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7488 | r->Start(); |
| 7489 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7490 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7491 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7492 | |
| 7493 | EXPECT_EQ(1, d.response_started_count()); |
| 7494 | EXPECT_FALSE(d.received_data_before_response()); |
| 7495 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7496 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 7497 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7498 | // Get a fresh copy of the states, and check that they haven't changed. |
| 7499 | TransportSecurityState::DomainState new_static_domain_state; |
| 7500 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Adam Langley | 5cbb7d7a | 2014-09-25 23:14:12 | [diff] [blame] | 7501 | "www.google.com", &new_static_domain_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7502 | TransportSecurityState::DomainState new_dynamic_domain_state; |
| 7503 | EXPECT_FALSE(transport_security_state.GetDynamicDomainState( |
| 7504 | "www.google.com", &new_dynamic_domain_state)); |
| 7505 | |
| 7506 | EXPECT_EQ(new_static_domain_state.sts.upgrade_mode, |
| 7507 | static_domain_state.sts.upgrade_mode); |
| 7508 | EXPECT_EQ(new_static_domain_state.sts.include_subdomains, |
| 7509 | static_domain_state.sts.include_subdomains); |
| 7510 | EXPECT_EQ(new_static_domain_state.pkp.include_subdomains, |
| 7511 | static_domain_state.pkp.include_subdomains); |
| 7512 | EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes, |
| 7513 | static_domain_state.pkp.spki_hashes)); |
| 7514 | EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes, |
| 7515 | static_domain_state.pkp.bad_spki_hashes)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7516 | } |
| 7517 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7518 | // Make sure HSTS preserves a POST request's method and body. |
| 7519 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 7520 | static const char kData[] = "hello world"; |
| 7521 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7522 | SpawnedTestServer::SSLOptions ssl_options( |
| 7523 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7524 | SpawnedTestServer test_server( |
| 7525 | SpawnedTestServer::TYPE_HTTPS, |
| 7526 | ssl_options, |
| 7527 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7528 | ASSERT_TRUE(test_server.Start()); |
| 7529 | |
| 7530 | |
| 7531 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 7532 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7533 | // the SpawnedTestServer. By default, MockHostResolver maps all hosts |
| 7534 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7535 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7536 | |
| 7537 | // Force https for www.somewhere.com. |
| 7538 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 7539 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 7540 | bool include_subdomains = false; |
| 7541 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 7542 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7543 | |
| 7544 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 7545 | |
| 7546 | TestURLRequestContext context(true); |
| 7547 | context.set_host_resolver(&host_resolver); |
| 7548 | context.set_transport_security_state(&transport_security_state); |
| 7549 | context.set_network_delegate(&network_delegate); |
| 7550 | context.Init(); |
| 7551 | |
| 7552 | TestDelegate d; |
| 7553 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 7554 | // cause a certificate error. Ignore the error. |
| 7555 | d.set_allow_certificate_errors(true); |
| 7556 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7557 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 7558 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7559 | test_server.host_port_pair().port())), |
| 7560 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7561 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7562 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7563 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7564 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7565 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7566 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7567 | EXPECT_EQ("https", req->url().scheme()); |
| 7568 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7569 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 7570 | |
| 7571 | LoadTimingInfo load_timing_info; |
| 7572 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 7573 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 7574 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7575 | } |
| 7576 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 7577 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 7578 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 7579 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 7580 | |
| 7581 | SpawnedTestServer::SSLOptions ssl_options( |
| 7582 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7583 | SpawnedTestServer test_server( |
| 7584 | SpawnedTestServer::TYPE_HTTPS, |
| 7585 | ssl_options, |
| 7586 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7587 | ASSERT_TRUE(test_server.Start()); |
| 7588 | |
| 7589 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 7590 | // address, so a MockHostResolver is needed to redirect example.net to the |
| 7591 | // SpawnedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by default. |
| 7592 | MockHostResolver host_resolver; |
| 7593 | |
| 7594 | TransportSecurityState transport_security_state; |
| 7595 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 7596 | bool include_subdomains = false; |
| 7597 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 7598 | |
| 7599 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 7600 | |
| 7601 | MockCertVerifier cert_verifier; |
| 7602 | cert_verifier.set_default_result(OK); |
| 7603 | |
| 7604 | TestURLRequestContext context(true); |
| 7605 | context.set_host_resolver(&host_resolver); |
| 7606 | context.set_transport_security_state(&transport_security_state); |
| 7607 | context.set_network_delegate(&network_delegate); |
| 7608 | context.set_cert_verifier(&cert_verifier); |
| 7609 | context.Init(); |
| 7610 | |
| 7611 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 7612 | test_server.host_port_pair().port())); |
| 7613 | url::Replacements<char> replacements; |
| 7614 | const char kNewScheme[] = "https"; |
| 7615 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 7616 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 7617 | |
| 7618 | TestDelegate d; |
| 7619 | // Quit on redirect to allow response header inspection upon redirect. |
| 7620 | d.set_quit_on_redirect(true); |
| 7621 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7622 | scoped_ptr<URLRequest> req( |
| 7623 | context.CreateRequest(hsts_http_url, DEFAULT_PRIORITY, &d)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 7624 | // Set Origin header to simulate a cross-origin request. |
| 7625 | HttpRequestHeaders request_headers; |
| 7626 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 7627 | req->SetExtraRequestHeaders(request_headers); |
| 7628 | |
| 7629 | req->Start(); |
| 7630 | base::RunLoop().Run(); |
| 7631 | |
| 7632 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7633 | |
| 7634 | const HttpResponseHeaders* headers = req->response_headers(); |
| 7635 | std::string redirect_location; |
| 7636 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 7637 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 7638 | |
| 7639 | std::string received_cors_header; |
| 7640 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 7641 | &received_cors_header)); |
| 7642 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 7643 | } |
| 7644 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 7645 | // This just tests the behaviour of GetHSTSRedirect(). End-to-end tests of HSTS |
| 7646 | // are performed in net/websockets/websocket_end_to_end_test.cc. |
| 7647 | TEST(WebSocketURLRequestTest, HSTSApplied) { |
| 7648 | TestNetworkDelegate network_delegate; |
| 7649 | TransportSecurityState transport_security_state; |
| 7650 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 7651 | bool include_subdomains = false; |
| 7652 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 7653 | TestURLRequestContext context(true); |
| 7654 | context.set_transport_security_state(&transport_security_state); |
| 7655 | context.set_network_delegate(&network_delegate); |
| 7656 | context.Init(); |
| 7657 | GURL ws_url("ws://example.net/echo"); |
| 7658 | TestDelegate delegate; |
| 7659 | scoped_ptr<URLRequest> request( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7660 | context.CreateRequest(ws_url, DEFAULT_PRIORITY, &delegate)); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 7661 | EXPECT_TRUE(request->GetHSTSRedirect(&ws_url)); |
| 7662 | EXPECT_TRUE(ws_url.SchemeIs("wss")); |
| 7663 | } |
| 7664 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7665 | namespace { |
| 7666 | |
| 7667 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 7668 | public: |
| 7669 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 7670 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 7671 | void OnCertificateRequested(URLRequest* request, |
| 7672 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7673 | on_certificate_requested_count_++; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 7674 | base::MessageLoop::current()->Quit(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7675 | } |
| 7676 | int on_certificate_requested_count() { |
| 7677 | return on_certificate_requested_count_; |
| 7678 | } |
| 7679 | private: |
| 7680 | int on_certificate_requested_count_; |
| 7681 | }; |
| 7682 | |
| 7683 | } // namespace |
| 7684 | |
| 7685 | // TODO(davidben): Test the rest of the code. Specifically, |
| 7686 | // - Filtering which certificates to select. |
| 7687 | // - Sending a certificate back. |
| 7688 | // - Getting a certificate request in an SSL renegotiation sending the |
| 7689 | // HTTP request. |
| 7690 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7691 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7692 | ssl_options.request_client_certificate = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7693 | SpawnedTestServer test_server( |
| 7694 | SpawnedTestServer::TYPE_HTTPS, |
| 7695 | ssl_options, |
| 7696 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7697 | ASSERT_TRUE(test_server.Start()); |
| 7698 | |
| 7699 | SSLClientAuthTestDelegate d; |
| 7700 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7701 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7702 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7703 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7704 | r->Start(); |
| 7705 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7706 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7707 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7708 | |
| 7709 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 7710 | EXPECT_FALSE(d.received_data_before_response()); |
| 7711 | EXPECT_EQ(0, d.bytes_received()); |
| 7712 | |
| 7713 | // Send no certificate. |
| 7714 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 7715 | // all platforms so we can test sending a cert as well. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7716 | r->ContinueWithCertificate(NULL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7717 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7718 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7719 | |
| 7720 | EXPECT_EQ(1, d.response_started_count()); |
| 7721 | EXPECT_FALSE(d.received_data_before_response()); |
| 7722 | EXPECT_NE(0, d.bytes_received()); |
| 7723 | } |
| 7724 | } |
| 7725 | |
| 7726 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 7727 | // Test that we attempt a session resume when making two connections to the |
| 7728 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7729 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7730 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7731 | SpawnedTestServer test_server( |
| 7732 | SpawnedTestServer::TYPE_HTTPS, |
| 7733 | ssl_options, |
| 7734 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7735 | ASSERT_TRUE(test_server.Start()); |
| 7736 | |
| 7737 | SSLClientSocket::ClearSessionCache(); |
| 7738 | |
| 7739 | { |
| 7740 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7741 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7742 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7743 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7744 | r->Start(); |
| 7745 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7746 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7747 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7748 | |
| 7749 | EXPECT_EQ(1, d.response_started_count()); |
| 7750 | } |
| 7751 | |
| 7752 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 7753 | CloseAllConnections(); |
| 7754 | |
| 7755 | { |
| 7756 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7757 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7758 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7759 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7760 | r->Start(); |
| 7761 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7762 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7763 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7764 | |
| 7765 | // The response will look like; |
| 7766 | // insert abc |
| 7767 | // lookup abc |
| 7768 | // insert xyz |
| 7769 | // |
| 7770 | // With a newline at the end which makes the split think that there are |
| 7771 | // four lines. |
| 7772 | |
| 7773 | EXPECT_EQ(1, d.response_started_count()); |
| 7774 | std::vector<std::string> lines; |
| 7775 | base::SplitString(d.data_received(), '\n', &lines); |
| 7776 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 7777 | |
| 7778 | std::string session_id; |
| 7779 | |
| 7780 | for (size_t i = 0; i < 2; i++) { |
| 7781 | std::vector<std::string> parts; |
| 7782 | base::SplitString(lines[i], '\t', &parts); |
| 7783 | ASSERT_EQ(2u, parts.size()); |
| 7784 | if (i == 0) { |
| 7785 | EXPECT_EQ("insert", parts[0]); |
| 7786 | session_id = parts[1]; |
| 7787 | } else { |
| 7788 | EXPECT_EQ("lookup", parts[0]); |
| 7789 | EXPECT_EQ(session_id, parts[1]); |
| 7790 | } |
| 7791 | } |
| 7792 | } |
| 7793 | } |
| 7794 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7795 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 7796 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 7797 | // that exactly two different sessions were inserted, with no lookups etc. |
| 7798 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
| 7799 | std::vector<std::string> lines; |
| 7800 | base::SplitString(session_info, '\n', &lines); |
| 7801 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 7802 | |
| 7803 | std::string session_id; |
| 7804 | for (size_t i = 0; i < 2; i++) { |
| 7805 | std::vector<std::string> parts; |
| 7806 | base::SplitString(lines[i], '\t', &parts); |
| 7807 | ASSERT_EQ(2u, parts.size()); |
| 7808 | EXPECT_EQ("insert", parts[0]); |
| 7809 | if (i == 0) { |
| 7810 | session_id = parts[1]; |
| 7811 | } else { |
| 7812 | EXPECT_NE(session_id, parts[1]); |
| 7813 | } |
| 7814 | } |
| 7815 | } |
| 7816 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7817 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 7818 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 7819 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7820 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7821 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7822 | SpawnedTestServer test_server( |
| 7823 | SpawnedTestServer::TYPE_HTTPS, |
| 7824 | ssl_options, |
| 7825 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7826 | ASSERT_TRUE(test_server.Start()); |
| 7827 | |
| 7828 | SSLClientSocket::ClearSessionCache(); |
| 7829 | |
| 7830 | { |
| 7831 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7832 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7833 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7834 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7835 | r->Start(); |
| 7836 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7837 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7838 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7839 | |
| 7840 | EXPECT_EQ(1, d.response_started_count()); |
| 7841 | } |
| 7842 | |
| 7843 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 7844 | HttpNetworkSession::Params params; |
| 7845 | params.host_resolver = default_context_.host_resolver(); |
| 7846 | params.cert_verifier = default_context_.cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 7847 | params.transport_security_state = default_context_.transport_security_state(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7848 | params.proxy_service = default_context_.proxy_service(); |
| 7849 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 7850 | params.http_auth_handler_factory = |
| 7851 | default_context_.http_auth_handler_factory(); |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 7852 | params.network_delegate = &default_network_delegate_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7853 | params.http_server_properties = default_context_.http_server_properties(); |
| 7854 | params.ssl_session_cache_shard = "alternate"; |
| 7855 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7856 | scoped_ptr<HttpCache> cache(new HttpCache( |
| 7857 | new HttpNetworkSession(params), |
| 7858 | HttpCache::DefaultBackend::InMemory(0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7859 | |
| 7860 | default_context_.set_http_transaction_factory(cache.get()); |
| 7861 | |
| 7862 | { |
| 7863 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7864 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7865 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7866 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7867 | r->Start(); |
| 7868 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7869 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7870 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7871 | |
| 7872 | // The response will look like; |
| 7873 | // insert abc |
| 7874 | // insert xyz |
| 7875 | // |
| 7876 | // With a newline at the end which makes the split think that there are |
| 7877 | // three lines. |
| 7878 | |
| 7879 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7880 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7881 | } |
| 7882 | } |
| 7883 | |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 7884 | #if defined(OS_WIN) |
| 7885 | |
| 7886 | namespace { |
| 7887 | |
| 7888 | bool IsECDSACipherSuite(uint16_t cipher_suite) { |
| 7889 | const char* key_exchange; |
| 7890 | const char* cipher; |
| 7891 | const char* mac; |
| 7892 | bool is_aead; |
| 7893 | SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
| 7894 | return std::string(key_exchange).find("ECDSA") != std::string::npos; |
| 7895 | } |
| 7896 | |
| 7897 | } // namespace |
| 7898 | |
| 7899 | // Test that ECDSA is disabled on Windows XP, where ECDSA certificates cannot be |
| 7900 | // verified. |
davidben | 9399c95 | 2014-10-07 04:09:11 | [diff] [blame] | 7901 | TEST_F(HTTPSRequestTest, DisableECDSAOnXP) { |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 7902 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
| 7903 | LOG(INFO) << "Skipping test on this version."; |
| 7904 | return; |
| 7905 | } |
| 7906 | |
| 7907 | SpawnedTestServer test_server( |
| 7908 | SpawnedTestServer::TYPE_HTTPS, |
| 7909 | SpawnedTestServer::kLocalhost, |
| 7910 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7911 | ASSERT_TRUE(test_server.Start()); |
| 7912 | |
| 7913 | TestDelegate d; |
| 7914 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7915 | test_server.GetURL("client-cipher-list"), DEFAULT_PRIORITY, &d)); |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 7916 | r->Start(); |
| 7917 | EXPECT_TRUE(r->is_pending()); |
| 7918 | |
| 7919 | base::RunLoop().Run(); |
| 7920 | |
| 7921 | EXPECT_EQ(1, d.response_started_count()); |
| 7922 | std::vector<std::string> lines; |
| 7923 | base::SplitString(d.data_received(), '\n', &lines); |
| 7924 | |
| 7925 | for (size_t i = 0; i < lines.size(); i++) { |
| 7926 | int cipher_suite; |
| 7927 | ASSERT_TRUE(base::StringToInt(lines[i], &cipher_suite)); |
| 7928 | EXPECT_FALSE(IsECDSACipherSuite(cipher_suite)) |
| 7929 | << "ClientHello advertised " << cipher_suite; |
| 7930 | } |
| 7931 | } |
| 7932 | |
| 7933 | #endif // OS_WIN |
| 7934 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7935 | class TestSSLConfigService : public SSLConfigService { |
| 7936 | public: |
| 7937 | TestSSLConfigService(bool ev_enabled, |
| 7938 | bool online_rev_checking, |
| 7939 | bool rev_checking_required_local_anchors) |
| 7940 | : ev_enabled_(ev_enabled), |
| 7941 | online_rev_checking_(online_rev_checking), |
| 7942 | rev_checking_required_local_anchors_( |
| 7943 | rev_checking_required_local_anchors), |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 7944 | min_version_(kDefaultSSLVersionMin), |
| 7945 | fallback_min_version_(kDefaultSSLVersionFallbackMin) {} |
| 7946 | |
| 7947 | void set_min_version(uint16 version) { |
| 7948 | min_version_ = version; |
| 7949 | } |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7950 | |
| 7951 | void set_fallback_min_version(uint16 version) { |
| 7952 | fallback_min_version_ = version; |
| 7953 | } |
| 7954 | |
| 7955 | // SSLConfigService: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 7956 | void GetSSLConfig(SSLConfig* config) override { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7957 | *config = SSLConfig(); |
| 7958 | config->rev_checking_enabled = online_rev_checking_; |
| 7959 | config->verify_ev_cert = ev_enabled_; |
| 7960 | config->rev_checking_required_local_anchors = |
| 7961 | rev_checking_required_local_anchors_; |
| 7962 | if (fallback_min_version_) { |
| 7963 | config->version_fallback_min = fallback_min_version_; |
| 7964 | } |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 7965 | if (min_version_) { |
| 7966 | config->version_min = min_version_; |
| 7967 | } |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7968 | } |
| 7969 | |
| 7970 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 7971 | ~TestSSLConfigService() override {} |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7972 | |
| 7973 | private: |
| 7974 | const bool ev_enabled_; |
| 7975 | const bool online_rev_checking_; |
| 7976 | const bool rev_checking_required_local_anchors_; |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 7977 | uint16 min_version_; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7978 | uint16 fallback_min_version_; |
| 7979 | }; |
| 7980 | |
| 7981 | class FallbackTestURLRequestContext : public TestURLRequestContext { |
| 7982 | public: |
| 7983 | explicit FallbackTestURLRequestContext(bool delay_initialization) |
| 7984 | : TestURLRequestContext(delay_initialization) {} |
| 7985 | |
| 7986 | void set_fallback_min_version(uint16 version) { |
| 7987 | TestSSLConfigService *ssl_config_service = |
| 7988 | new TestSSLConfigService(true /* check for EV */, |
| 7989 | false /* online revocation checking */, |
| 7990 | false /* require rev. checking for local |
| 7991 | anchors */); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7992 | ssl_config_service->set_fallback_min_version(version); |
| 7993 | set_ssl_config_service(ssl_config_service); |
| 7994 | } |
| 7995 | }; |
| 7996 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7997 | class HTTPSFallbackTest : public testing::Test { |
| 7998 | public: |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7999 | HTTPSFallbackTest() : context_(true) {} |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8000 | ~HTTPSFallbackTest() override {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8001 | |
| 8002 | protected: |
| 8003 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 8004 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8005 | context_.Init(); |
| 8006 | delegate_.set_allow_certificate_errors(true); |
| 8007 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8008 | SpawnedTestServer test_server( |
| 8009 | SpawnedTestServer::TYPE_HTTPS, |
| 8010 | ssl_options, |
| 8011 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8012 | ASSERT_TRUE(test_server.Start()); |
| 8013 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8014 | request_ = context_.CreateRequest(test_server.GetURL(std::string()), |
| 8015 | DEFAULT_PRIORITY, &delegate_); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8016 | request_->Start(); |
| 8017 | |
| 8018 | base::RunLoop().Run(); |
| 8019 | } |
| 8020 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8021 | void set_fallback_min_version(uint16 version) { |
| 8022 | context_.set_fallback_min_version(version); |
| 8023 | } |
| 8024 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8025 | void ExpectConnection(int version) { |
| 8026 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 8027 | EXPECT_NE(0, delegate_.bytes_received()); |
| 8028 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 8029 | request_->ssl_info().connection_status)); |
| 8030 | EXPECT_TRUE(request_->ssl_info().connection_status & |
| 8031 | SSL_CONNECTION_VERSION_FALLBACK); |
| 8032 | } |
| 8033 | |
| 8034 | void ExpectFailure(int error) { |
| 8035 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 8036 | EXPECT_FALSE(request_->status().is_success()); |
| 8037 | EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status()); |
| 8038 | EXPECT_EQ(error, request_->status().error()); |
| 8039 | } |
| 8040 | |
| 8041 | private: |
| 8042 | TestDelegate delegate_; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8043 | FallbackTestURLRequestContext context_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8044 | scoped_ptr<URLRequest> request_; |
| 8045 | }; |
| 8046 | |
| 8047 | // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more |
| 8048 | // than necessary. |
| 8049 | TEST_F(HTTPSFallbackTest, TLSv1Fallback) { |
| 8050 | SpawnedTestServer::SSLOptions ssl_options( |
| 8051 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8052 | ssl_options.tls_intolerant = |
| 8053 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 8054 | |
| 8055 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8056 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
| 8057 | } |
| 8058 | |
| 8059 | // This test is disabled on Android because the remote test server doesn't cause |
| 8060 | // a TCP reset. |
| 8061 | #if !defined(OS_ANDROID) |
| 8062 | // Tests fallback to TLS 1.0 on connection reset. |
| 8063 | TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) { |
| 8064 | SpawnedTestServer::SSLOptions ssl_options( |
| 8065 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8066 | ssl_options.tls_intolerant = |
| 8067 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 8068 | ssl_options.tls_intolerance_type = |
| 8069 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 8070 | |
| 8071 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8072 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
| 8073 | } |
| 8074 | #endif // !OS_ANDROID |
| 8075 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8076 | // Tests that we don't fallback on handshake failure with servers that implement |
| 8077 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8078 | TEST_F(HTTPSFallbackTest, FallbackSCSV) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8079 | SpawnedTestServer::SSLOptions ssl_options( |
| 8080 | SpawnedTestServer::SSLOptions::CERT_OK); |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8081 | // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8082 | // a version fallback. |
| 8083 | ssl_options.tls_intolerant = |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8084 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8085 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 8086 | // connections are rejected. |
| 8087 | ssl_options.fallback_scsv_enabled = true; |
| 8088 | |
| 8089 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8090 | |
| 8091 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version |
| 8092 | // intolerance. If the fallback SCSV is processed when the original error |
| 8093 | // that caused the fallback should be returned, which should be |
| 8094 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH. |
| 8095 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 8096 | } |
| 8097 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8098 | // Tests that we don't fallback on connection closed with servers that implement |
| 8099 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8100 | TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) { |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8101 | SpawnedTestServer::SSLOptions ssl_options( |
| 8102 | SpawnedTestServer::SSLOptions::CERT_OK); |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8103 | // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8104 | // a version fallback. |
| 8105 | ssl_options.tls_intolerant = |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8106 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8107 | ssl_options.tls_intolerance_type = |
| 8108 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 8109 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 8110 | // connections are rejected. |
| 8111 | ssl_options.fallback_scsv_enabled = true; |
| 8112 | |
| 8113 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8114 | |
| 8115 | // The original error should be replayed on rejected fallback. |
| 8116 | ExpectFailure(ERR_CONNECTION_CLOSED); |
| 8117 | } |
| 8118 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8119 | // Tests that the SSLv3 fallback doesn't happen. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8120 | TEST_F(HTTPSFallbackTest, SSLv3Fallback) { |
| 8121 | SpawnedTestServer::SSLOptions ssl_options( |
| 8122 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8123 | ssl_options.tls_intolerant = |
| 8124 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 8125 | |
| 8126 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 8127 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8128 | } |
| 8129 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8130 | // Tests that the TLSv1 fallback triggers on closed connections. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8131 | TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) { |
| 8132 | SpawnedTestServer::SSLOptions ssl_options( |
| 8133 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8134 | ssl_options.tls_intolerant = |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8135 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8136 | ssl_options.tls_intolerance_type = |
| 8137 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8138 | |
| 8139 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8140 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8141 | } |
| 8142 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8143 | // Test that fallback probe connections don't cause sessions to be cached. |
| 8144 | TEST_F(HTTPSRequestTest, FallbackProbeNoCache) { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8145 | SpawnedTestServer::SSLOptions ssl_options( |
| 8146 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8147 | ssl_options.tls_intolerant = |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8148 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8149 | ssl_options.tls_intolerance_type = |
| 8150 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 8151 | ssl_options.record_resume = true; |
| 8152 | |
| 8153 | SpawnedTestServer test_server( |
| 8154 | SpawnedTestServer::TYPE_HTTPS, |
| 8155 | ssl_options, |
| 8156 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8157 | ASSERT_TRUE(test_server.Start()); |
| 8158 | |
| 8159 | SSLClientSocket::ClearSessionCache(); |
| 8160 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8161 | // Make a connection that does a probe fallback to TLSv1 but fails because |
| 8162 | // TLSv1 fallback is disabled. We don't wish a session for this connection to |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8163 | // be inserted locally. |
| 8164 | { |
| 8165 | TestDelegate delegate; |
| 8166 | FallbackTestURLRequestContext context(true); |
| 8167 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8168 | context.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1_2); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8169 | context.Init(); |
| 8170 | scoped_ptr<URLRequest> request(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8171 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8172 | request->Start(); |
| 8173 | |
| 8174 | base::RunLoop().Run(); |
| 8175 | |
| 8176 | EXPECT_EQ(1, delegate.response_started_count()); |
| 8177 | EXPECT_FALSE(request->status().is_success()); |
| 8178 | EXPECT_EQ(URLRequestStatus::FAILED, request->status().status()); |
| 8179 | EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION, |
| 8180 | request->status().error()); |
| 8181 | } |
| 8182 | |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8183 | // Now allow TLSv1 fallback connections and request the session cache log. |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8184 | { |
| 8185 | TestDelegate delegate; |
| 8186 | FallbackTestURLRequestContext context(true); |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8187 | context.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8188 | |
| 8189 | context.Init(); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8190 | scoped_ptr<URLRequest> request(context.CreateRequest( |
| 8191 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &delegate)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8192 | request->Start(); |
| 8193 | |
| 8194 | base::RunLoop().Run(); |
| 8195 | |
| 8196 | EXPECT_EQ(1, delegate.response_started_count()); |
| 8197 | EXPECT_NE(0, delegate.bytes_received()); |
davidben | b937d6c | 2015-05-14 04:53:42 | [diff] [blame] | 8198 | EXPECT_EQ( |
| 8199 | SSL_CONNECTION_VERSION_TLS1, |
| 8200 | SSLConnectionStatusToVersion(request->ssl_info().connection_status)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8201 | EXPECT_TRUE(request->ssl_info().connection_status & |
| 8202 | SSL_CONNECTION_VERSION_FALLBACK); |
| 8203 | |
| 8204 | std::vector<std::string> lines; |
| 8205 | // If no sessions were cached then the server should have seen two sessions |
| 8206 | // inserted with no lookups. |
| 8207 | AssertTwoDistinctSessionsInserted(delegate.data_received()); |
| 8208 | } |
| 8209 | } |
| 8210 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8211 | class HTTPSSessionTest : public testing::Test { |
| 8212 | public: |
| 8213 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8214 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8215 | |
| 8216 | default_context_.set_network_delegate(&default_network_delegate_); |
| 8217 | default_context_.set_cert_verifier(&cert_verifier_); |
| 8218 | default_context_.Init(); |
| 8219 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8220 | ~HTTPSSessionTest() override {} |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8221 | |
| 8222 | protected: |
| 8223 | MockCertVerifier cert_verifier_; |
| 8224 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 8225 | TestURLRequestContext default_context_; |
| 8226 | }; |
| 8227 | |
| 8228 | // Tests that session resumption is not attempted if an invalid certificate |
| 8229 | // is presented. |
| 8230 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 8231 | SpawnedTestServer::SSLOptions ssl_options; |
| 8232 | ssl_options.record_resume = true; |
| 8233 | SpawnedTestServer test_server( |
| 8234 | SpawnedTestServer::TYPE_HTTPS, |
| 8235 | ssl_options, |
| 8236 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8237 | ASSERT_TRUE(test_server.Start()); |
| 8238 | |
| 8239 | SSLClientSocket::ClearSessionCache(); |
| 8240 | |
| 8241 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8242 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8243 | { |
| 8244 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8245 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8246 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8247 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8248 | r->Start(); |
| 8249 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8250 | |
| 8251 | base::RunLoop().Run(); |
| 8252 | |
| 8253 | EXPECT_EQ(1, d.response_started_count()); |
| 8254 | } |
| 8255 | |
| 8256 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 8257 | CloseAllConnections(); |
| 8258 | |
| 8259 | // Now change the certificate to be acceptable (so that the response is |
| 8260 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8261 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8262 | { |
| 8263 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8264 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8265 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8266 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8267 | r->Start(); |
| 8268 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8269 | |
| 8270 | base::RunLoop().Run(); |
| 8271 | |
| 8272 | // The response will look like; |
| 8273 | // insert abc |
| 8274 | // insert xyz |
| 8275 | // |
| 8276 | // With a newline at the end which makes the split think that there are |
| 8277 | // three lines. |
| 8278 | // |
| 8279 | // If a session was presented (eg: a bug), then the response would look |
| 8280 | // like; |
| 8281 | // insert abc |
| 8282 | // lookup abc |
| 8283 | // insert xyz |
| 8284 | |
| 8285 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8286 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8287 | } |
| 8288 | } |
| 8289 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8290 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 8291 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 8292 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8293 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 8294 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 8295 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8296 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 8297 | // testserver. |
| 8298 | static const SHA256HashValue kOCSPTestCertSPKI = { { |
| 8299 | 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, |
| 8300 | 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, |
| 8301 | 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, |
| 8302 | 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, |
| 8303 | } }; |
| 8304 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8305 | // This is the policy OID contained in the certificates that testserver |
| 8306 | // generates. |
| 8307 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 8308 | |
| 8309 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 8310 | public: |
| 8311 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8312 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8313 | ev_test_policy_( |
| 8314 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 8315 | kOCSPTestCertFingerprint, |
| 8316 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8317 | } |
| 8318 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8319 | void SetUp() override { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8320 | SetupContext(&context_); |
| 8321 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8322 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8323 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8324 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 8325 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8326 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8327 | |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 8328 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8329 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8330 | EnsureNSSHttpIOInit(); |
| 8331 | #endif |
| 8332 | } |
| 8333 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8334 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8335 | CertStatus* out_cert_status) { |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8336 | // We always overwrite out_cert_status. |
| 8337 | *out_cert_status = 0; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8338 | SpawnedTestServer test_server( |
| 8339 | SpawnedTestServer::TYPE_HTTPS, |
| 8340 | ssl_options, |
| 8341 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8342 | ASSERT_TRUE(test_server.Start()); |
| 8343 | |
| 8344 | TestDelegate d; |
| 8345 | d.set_allow_certificate_errors(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8346 | scoped_ptr<URLRequest> r(context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8347 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8348 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8349 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8350 | base::RunLoop().Run(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8351 | |
| 8352 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8353 | *out_cert_status = r->ssl_info().cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8354 | } |
| 8355 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8356 | ~HTTPSOCSPTest() override { |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 8357 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8358 | ShutdownNSSHttpIO(); |
| 8359 | #endif |
| 8360 | } |
| 8361 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8362 | protected: |
| 8363 | // SetupContext configures the URLRequestContext that will be used for making |
| 8364 | // connetions to testserver. This can be overridden in test subclasses for |
| 8365 | // different behaviour. |
| 8366 | virtual void SetupContext(URLRequestContext* context) { |
| 8367 | context->set_ssl_config_service( |
| 8368 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8369 | true /* online revocation checking */, |
| 8370 | false /* require rev. checking for local |
| 8371 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8372 | } |
| 8373 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8374 | scoped_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8375 | TestURLRequestContext context_; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8376 | scoped_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8377 | }; |
| 8378 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8379 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 8380 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8381 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 8382 | // have that ability on other platforms. |
| 8383 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 8384 | #else |
| 8385 | return 0; |
| 8386 | #endif |
| 8387 | } |
| 8388 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8389 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 8390 | // operating system supports revocation checking and can distinguish between |
| 8391 | // situations where a given certificate lacks any revocation information (eg: |
| 8392 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 8393 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 8394 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 8395 | // skipped. |
| 8396 | static bool SystemSupportsHardFailRevocationChecking() { |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 8397 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8398 | return true; |
| 8399 | #else |
| 8400 | return false; |
| 8401 | #endif |
| 8402 | } |
| 8403 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8404 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 8405 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 8406 | // several tests are effected because our testing EV certificate won't be |
| 8407 | // recognised as EV. |
| 8408 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 8409 | #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 8410 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 8411 | return false; |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 8412 | #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 8413 | // On OS X and Android, we use the system to tell us whether a certificate is |
| 8414 | // EV or not and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 8415 | return false; |
| 8416 | #else |
| 8417 | return true; |
| 8418 | #endif |
| 8419 | } |
| 8420 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8421 | static bool SystemSupportsOCSP() { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8422 | #if defined(USE_OPENSSL_CERTS) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 8423 | // http://crbug.com/117478 - OpenSSL does not support OCSP. |
| 8424 | return false; |
| 8425 | #elif defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8426 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 8427 | #elif defined(OS_ANDROID) |
| 8428 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 8429 | return false; |
| 8430 | #else |
| 8431 | return true; |
| 8432 | #endif |
| 8433 | } |
| 8434 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8435 | static bool SystemSupportsOCSPStapling() { |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 8436 | #if defined(USE_NSS_CERTS) |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8437 | return true; |
| 8438 | #elif defined(OS_WIN) |
| 8439 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 8440 | #else |
| 8441 | return false; |
| 8442 | #endif |
| 8443 | } |
| 8444 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8445 | TEST_F(HTTPSOCSPTest, Valid) { |
| 8446 | if (!SystemSupportsOCSP()) { |
| 8447 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8448 | return; |
| 8449 | } |
| 8450 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8451 | SpawnedTestServer::SSLOptions ssl_options( |
| 8452 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8453 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8454 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8455 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8456 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8457 | |
| 8458 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8459 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8460 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8461 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8462 | |
| 8463 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8464 | } |
| 8465 | |
| 8466 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 8467 | if (!SystemSupportsOCSP()) { |
| 8468 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8469 | return; |
| 8470 | } |
| 8471 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8472 | SpawnedTestServer::SSLOptions ssl_options( |
| 8473 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8474 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8475 | |
| 8476 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8477 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8478 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 8479 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8480 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 8481 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8482 | #endif |
| 8483 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8484 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8485 | } |
| 8486 | |
| 8487 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 8488 | if (!SystemSupportsOCSP()) { |
| 8489 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8490 | return; |
| 8491 | } |
| 8492 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8493 | SpawnedTestServer::SSLOptions ssl_options( |
| 8494 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8495 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8496 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8497 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8498 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8499 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8500 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8501 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8502 | |
| 8503 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 8504 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8505 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8506 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8507 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8508 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
| 8509 | if (!SystemSupportsOCSPStapling()) { |
| 8510 | LOG(WARNING) |
| 8511 | << "Skipping test because system doesn't support OCSP stapling"; |
| 8512 | return; |
| 8513 | } |
| 8514 | |
| 8515 | SpawnedTestServer::SSLOptions ssl_options( |
| 8516 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8517 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 8518 | ssl_options.staple_ocsp_response = true; |
| 8519 | ssl_options.ocsp_server_unavailable = true; |
| 8520 | |
| 8521 | CertStatus cert_status; |
| 8522 | DoConnection(ssl_options, &cert_status); |
| 8523 | |
| 8524 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8525 | |
| 8526 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8527 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 8528 | |
| 8529 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8530 | } |
| 8531 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 8532 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 8533 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 8534 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 8535 | #else |
| 8536 | #define MAYBE_RevokedStapled RevokedStapled |
| 8537 | #endif |
| 8538 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8539 | if (!SystemSupportsOCSPStapling()) { |
| 8540 | LOG(WARNING) |
| 8541 | << "Skipping test because system doesn't support OCSP stapling"; |
| 8542 | return; |
| 8543 | } |
| 8544 | |
| 8545 | SpawnedTestServer::SSLOptions ssl_options( |
| 8546 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8547 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 8548 | ssl_options.staple_ocsp_response = true; |
| 8549 | ssl_options.ocsp_server_unavailable = true; |
| 8550 | |
| 8551 | CertStatus cert_status; |
| 8552 | DoConnection(ssl_options, &cert_status); |
| 8553 | |
| 8554 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8555 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8556 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8557 | } |
| 8558 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8559 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 8560 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8561 | void SetupContext(URLRequestContext* context) override { |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8562 | context->set_ssl_config_service( |
| 8563 | new TestSSLConfigService(false /* check for EV */, |
| 8564 | false /* online revocation checking */, |
| 8565 | true /* require rev. checking for local |
| 8566 | anchors */)); |
| 8567 | } |
| 8568 | }; |
| 8569 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8570 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 8571 | if (!SystemSupportsOCSP()) { |
| 8572 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8573 | return; |
| 8574 | } |
| 8575 | |
| 8576 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 8577 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 8578 | << "revocation checking"; |
| 8579 | return; |
| 8580 | } |
| 8581 | |
| 8582 | SpawnedTestServer::SSLOptions ssl_options( |
| 8583 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8584 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 8585 | |
| 8586 | CertStatus cert_status; |
| 8587 | DoConnection(ssl_options, &cert_status); |
| 8588 | |
| 8589 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 8590 | cert_status & CERT_STATUS_REVOKED); |
| 8591 | |
| 8592 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 8593 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8594 | } |
| 8595 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8596 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 8597 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8598 | void SetupContext(URLRequestContext* context) override { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8599 | context->set_ssl_config_service( |
| 8600 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8601 | false /* online revocation checking */, |
| 8602 | false /* require rev. checking for local |
| 8603 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8604 | } |
| 8605 | }; |
| 8606 | |
| 8607 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 8608 | if (!SystemSupportsOCSP()) { |
| 8609 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8610 | return; |
| 8611 | } |
| 8612 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8613 | SpawnedTestServer::SSLOptions ssl_options( |
| 8614 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8615 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8616 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 8617 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8618 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8619 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8620 | |
| 8621 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 8622 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 8623 | |
| 8624 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8625 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8626 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8627 | } |
| 8628 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 8629 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 8630 | if (!SystemSupportsOCSP()) { |
| 8631 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8632 | return; |
| 8633 | } |
| 8634 | |
| 8635 | SpawnedTestServer::SSLOptions ssl_options( |
| 8636 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8637 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 8638 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 8639 | |
| 8640 | CertStatus cert_status; |
| 8641 | DoConnection(ssl_options, &cert_status); |
| 8642 | |
| 8643 | // Currently only works for Windows. When using NSS or OS X, it's not |
| 8644 | // possible to determine whether the check failed because of actual |
| 8645 | // revocation or because there was an OCSP failure. |
| 8646 | #if defined(OS_WIN) |
| 8647 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8648 | #else |
| 8649 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8650 | #endif |
| 8651 | |
| 8652 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8653 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8654 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 8655 | } |
| 8656 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8657 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 8658 | if (!SystemSupportsOCSP()) { |
| 8659 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8660 | return; |
| 8661 | } |
| 8662 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8663 | SpawnedTestServer::SSLOptions ssl_options( |
| 8664 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8665 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8666 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 8667 | |
| 8668 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8669 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8670 | |
| 8671 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8672 | |
| 8673 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8674 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8675 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8676 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8677 | } |
| 8678 | |
| 8679 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 8680 | if (!SystemSupportsOCSP()) { |
| 8681 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8682 | return; |
| 8683 | } |
| 8684 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8685 | SpawnedTestServer::SSLOptions ssl_options( |
| 8686 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8687 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8688 | SSLConfigService::SetCRLSet( |
| 8689 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 8690 | |
| 8691 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8692 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8693 | |
| 8694 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 8695 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 8696 | |
| 8697 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8698 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8699 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8700 | } |
| 8701 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8702 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 8703 | if (!SystemSupportsOCSP()) { |
| 8704 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8705 | return; |
| 8706 | } |
| 8707 | |
| 8708 | SpawnedTestServer::SSLOptions ssl_options( |
| 8709 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8710 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 8711 | SSLConfigService::SetCRLSet( |
| 8712 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 8713 | false, &kOCSPTestCertSPKI, ""))); |
| 8714 | |
| 8715 | CertStatus cert_status; |
| 8716 | DoConnection(ssl_options, &cert_status); |
| 8717 | |
| 8718 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 8719 | // revocation check for EV. |
| 8720 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8721 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8722 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 8723 | EXPECT_FALSE( |
| 8724 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 8725 | } |
| 8726 | |
| 8727 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 8728 | if (!SystemSupportsOCSP()) { |
| 8729 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8730 | return; |
| 8731 | } |
| 8732 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8733 | SpawnedTestServer::SSLOptions ssl_options( |
| 8734 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8735 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8736 | SSLConfigService::SetCRLSet( |
| 8737 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 8738 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8739 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8740 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8741 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8742 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 8743 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 8744 | // test. |
| 8745 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 8746 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8747 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8748 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8749 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8750 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8751 | } |
| 8752 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8753 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 8754 | // Test that when EV verification is requested, but online revocation |
| 8755 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 8756 | // no revocation checking actually happens. |
| 8757 | if (!SystemSupportsOCSP()) { |
| 8758 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8759 | return; |
| 8760 | } |
| 8761 | |
| 8762 | // Unmark the certificate's OID as EV, which should disable revocation |
| 8763 | // checking (as per the user preference) |
| 8764 | ev_test_policy_.reset(); |
| 8765 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8766 | SpawnedTestServer::SSLOptions ssl_options( |
| 8767 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8768 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8769 | SSLConfigService::SetCRLSet( |
| 8770 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 8771 | |
| 8772 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8773 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8774 | |
| 8775 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8776 | |
| 8777 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8778 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8779 | } |
| 8780 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8781 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 8782 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8783 | void SetupContext(URLRequestContext* context) override { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8784 | context->set_ssl_config_service( |
| 8785 | new TestSSLConfigService(false /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8786 | false /* online revocation checking */, |
| 8787 | false /* require rev. checking for local |
| 8788 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8789 | } |
| 8790 | }; |
| 8791 | |
| 8792 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8793 | SpawnedTestServer::SSLOptions ssl_options( |
| 8794 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8795 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8796 | SSLConfigService::SetCRLSet( |
| 8797 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 8798 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8799 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8800 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8801 | |
| 8802 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 8803 | // we don't fall back to online revocation checks. |
| 8804 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8805 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8806 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8807 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8808 | |
| 8809 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 8810 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8811 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 8812 | return; |
| 8813 | #endif |
| 8814 | |
| 8815 | SpawnedTestServer::SSLOptions ssl_options( |
| 8816 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8817 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 8818 | ssl_options.cert_serial = 10; |
| 8819 | SSLConfigService::SetCRLSet( |
| 8820 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 8821 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 8822 | |
| 8823 | CertStatus cert_status = 0; |
| 8824 | DoConnection(ssl_options, &cert_status); |
| 8825 | |
| 8826 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 8827 | // reflected without online revocation checking. |
| 8828 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8829 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8830 | EXPECT_FALSE( |
| 8831 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 8832 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8833 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8834 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8835 | #if !defined(DISABLE_FTP_SUPPORT) && !defined(OS_ANDROID) |
| 8836 | // These tests aren't passing on Android. Either the RemoteTestServer isn't |
| 8837 | // starting up successfully, or it can't access the test files. |
| 8838 | // TODO(mmenke): Fix this. See http://crbug.com/495220 |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 8839 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8840 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 8841 | URLRequestTestFTP() |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8842 | : ftp_transaction_factory_(&host_resolver_), |
| 8843 | test_server_(SpawnedTestServer::TYPE_FTP, |
| 8844 | SpawnedTestServer::kLocalhost, |
| 8845 | base::FilePath(kTestFilePath)) { |
| 8846 | // Can't use |default_context_|'s HostResolver to set up the |
| 8847 | // FTPTransactionFactory because it hasn't been created yet. |
| 8848 | default_context_.set_host_resolver(&host_resolver_); |
| 8849 | } |
| 8850 | |
| 8851 | // URLRequestTest interface: |
| 8852 | void SetUpFactory() override { |
| 8853 | // Add FTP support to the default URLRequestContext. |
| 8854 | job_factory_impl_->SetProtocolHandler( |
| 8855 | "ftp", new FtpProtocolHandler(&ftp_transaction_factory_)); |
| 8856 | } |
| 8857 | |
| 8858 | std::string GetTestFileContents() { |
| 8859 | base::FilePath path; |
| 8860 | EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); |
| 8861 | path = path.Append(kTestFilePath); |
| 8862 | path = path.AppendASCII(kFtpTestFile); |
| 8863 | std::string contents; |
| 8864 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
| 8865 | return contents; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8866 | } |
| 8867 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 8868 | protected: |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8869 | MockHostResolver host_resolver_; |
| 8870 | FtpNetworkLayer ftp_transaction_factory_; |
| 8871 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8872 | SpawnedTestServer test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 8873 | }; |
| 8874 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8875 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 8876 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8877 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8878 | |
| 8879 | TestDelegate d; |
| 8880 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8881 | scoped_ptr<URLRequest> r( |
| 8882 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8883 | r->Start(); |
| 8884 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8885 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8886 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8887 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8888 | EXPECT_FALSE(r->is_pending()); |
| 8889 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 8890 | EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8891 | } |
| 8892 | } |
| 8893 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8894 | TEST_F(URLRequestTestFTP, FTPDirectoryListing) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8895 | ASSERT_TRUE(test_server_.Start()); |
| 8896 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8897 | TestDelegate d; |
| 8898 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8899 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8900 | test_server_.GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8901 | r->Start(); |
| 8902 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8903 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8904 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8905 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8906 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8907 | EXPECT_EQ(1, d.response_started_count()); |
| 8908 | EXPECT_FALSE(d.received_data_before_response()); |
| 8909 | EXPECT_LT(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8910 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8911 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8912 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8913 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8914 | } |
| 8915 | } |
| 8916 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8917 | TEST_F(URLRequestTestFTP, FTPGetTestAnonymous) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8918 | ASSERT_TRUE(test_server_.Start()); |
| 8919 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8920 | TestDelegate d; |
| 8921 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8922 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8923 | test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8924 | r->Start(); |
| 8925 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8926 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8927 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8928 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8929 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8930 | EXPECT_EQ(1, d.response_started_count()); |
| 8931 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8932 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8933 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8934 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8935 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8936 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8937 | } |
| 8938 | } |
| 8939 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8940 | TEST_F(URLRequestTestFTP, FTPGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8941 | ASSERT_TRUE(test_server_.Start()); |
| 8942 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8943 | TestDelegate d; |
| 8944 | { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8945 | scoped_ptr<URLRequest> r( |
| 8946 | default_context_.CreateRequest(test_server_.GetURLWithUserAndPassword( |
| 8947 | kFtpTestFile, "chrome", "chrome"), |
| 8948 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8949 | r->Start(); |
| 8950 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8951 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8952 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8953 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8954 | EXPECT_FALSE(r->is_pending()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8955 | EXPECT_EQ(1, d.response_started_count()); |
| 8956 | EXPECT_FALSE(d.received_data_before_response()); |
| 8957 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8958 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8959 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8960 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8961 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8962 | |
| 8963 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8964 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8965 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8966 | } |
| 8967 | } |
| 8968 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8969 | TEST_F(URLRequestTestFTP, FTPCheckWrongPassword) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8970 | ASSERT_TRUE(test_server_.Start()); |
| 8971 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8972 | TestDelegate d; |
| 8973 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8974 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8975 | test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8976 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8977 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8978 | r->Start(); |
| 8979 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8980 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8981 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8982 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8983 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8984 | EXPECT_EQ(1, d.response_started_count()); |
| 8985 | EXPECT_FALSE(d.received_data_before_response()); |
| 8986 | EXPECT_EQ(d.bytes_received(), 0); |
| 8987 | } |
| 8988 | } |
| 8989 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8990 | TEST_F(URLRequestTestFTP, FTPCheckWrongPasswordRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8991 | ASSERT_TRUE(test_server_.Start()); |
| 8992 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8993 | TestDelegate d; |
| 8994 | // Set correct login credentials. The delegate will be asked for them when |
| 8995 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 8996 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8997 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8998 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 8999 | test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9000 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9001 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9002 | r->Start(); |
| 9003 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9004 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9005 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9006 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9007 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9008 | EXPECT_EQ(1, d.response_started_count()); |
| 9009 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9010 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9011 | } |
| 9012 | } |
| 9013 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9014 | TEST_F(URLRequestTestFTP, FTPCheckWrongUser) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9015 | ASSERT_TRUE(test_server_.Start()); |
| 9016 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9017 | TestDelegate d; |
| 9018 | { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9019 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9020 | test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 9021 | "chrome"), |
| 9022 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9023 | r->Start(); |
| 9024 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9025 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9026 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9027 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9028 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9029 | EXPECT_EQ(1, d.response_started_count()); |
| 9030 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9031 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 9032 | } |
| 9033 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9034 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9035 | TEST_F(URLRequestTestFTP, FTPCheckWrongUserRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9036 | ASSERT_TRUE(test_server_.Start()); |
| 9037 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9038 | TestDelegate d; |
| 9039 | // Set correct login credentials. The delegate will be asked for them when |
| 9040 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9041 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9042 | { |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9043 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9044 | test_server_.GetURLWithUserAndPassword(kFtpTestFile, "wrong_user", |
| 9045 | "chrome"), |
| 9046 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9047 | r->Start(); |
| 9048 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9049 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9050 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9051 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9052 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9053 | EXPECT_EQ(1, d.response_started_count()); |
| 9054 | EXPECT_FALSE(d.received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9055 | EXPECT_EQ(GetTestFileContents(), d.data_received()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9056 | } |
| 9057 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9058 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9059 | TEST_F(URLRequestTestFTP, FTPCacheURLCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9060 | ASSERT_TRUE(test_server_.Start()); |
| 9061 | |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9062 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 9063 | { |
| 9064 | // Pass correct login identity in the URL. |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9065 | scoped_ptr<URLRequest> r( |
| 9066 | default_context_.CreateRequest(test_server_.GetURLWithUserAndPassword( |
| 9067 | kFtpTestFile, "chrome", "chrome"), |
| 9068 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9069 | r->Start(); |
| 9070 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9071 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9072 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9073 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9074 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9075 | EXPECT_EQ(1, d->response_started_count()); |
| 9076 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9077 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9078 | } |
| 9079 | |
| 9080 | d.reset(new TestDelegate); |
| 9081 | { |
| 9082 | // This request should use cached identity from previous request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9083 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9084 | test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9085 | r->Start(); |
| 9086 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9087 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9088 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9089 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9090 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9091 | EXPECT_EQ(1, d->response_started_count()); |
| 9092 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9093 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9094 | } |
| 9095 | } |
| 9096 | |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9097 | TEST_F(URLRequestTestFTP, FTPCacheLoginBoxCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9098 | ASSERT_TRUE(test_server_.Start()); |
| 9099 | |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9100 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 9101 | // Set correct login credentials. The delegate will be asked for them when |
| 9102 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9103 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9104 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9105 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9106 | test_server_.GetURLWithUserAndPassword(kFtpTestFile, "chrome", |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9107 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9108 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9109 | r->Start(); |
| 9110 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9111 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9112 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9113 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9114 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9115 | EXPECT_EQ(1, d->response_started_count()); |
| 9116 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9117 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9118 | } |
| 9119 | |
| 9120 | // Use a new delegate without explicit credentials. The cached ones should be |
| 9121 | // used. |
| 9122 | d.reset(new TestDelegate); |
| 9123 | { |
| 9124 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 9125 | // ones. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9126 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9127 | test_server_.GetURL(kFtpTestFile), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9128 | r->Start(); |
| 9129 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9130 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9131 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9132 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9133 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9134 | EXPECT_EQ(1, d->response_started_count()); |
| 9135 | EXPECT_FALSE(d->received_data_before_response()); |
mmenke | 9f2ec60c | 2015-06-01 20:59:47 | [diff] [blame^] | 9136 | EXPECT_EQ(GetTestFileContents(), d->data_received()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9137 | } |
| 9138 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9139 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 9140 | |
ttuttle | c0c82849 | 2015-05-15 01:25:55 | [diff] [blame] | 9141 | TEST_F(URLRequestTest, NetworkAccessedClearBeforeNetworkStart) { |
| 9142 | TestDelegate d; |
| 9143 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 9144 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
| 9145 | d.set_quit_on_network_start(true); |
| 9146 | |
| 9147 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9148 | |
| 9149 | req->Start(); |
| 9150 | base::RunLoop().Run(); |
| 9151 | |
| 9152 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 9153 | EXPECT_EQ(0, d.response_started_count()); |
| 9154 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9155 | |
| 9156 | req->ResumeNetworkStart(); |
| 9157 | base::RunLoop().Run(); |
| 9158 | } |
| 9159 | |
| 9160 | TEST_F(URLRequestTest, NetworkAccessedClearOnDataRequest) { |
| 9161 | TestDelegate d; |
| 9162 | scoped_ptr<URLRequest> req( |
| 9163 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
| 9164 | |
| 9165 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9166 | |
| 9167 | req->Start(); |
| 9168 | base::RunLoop().Run(); |
| 9169 | |
| 9170 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 9171 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9172 | } |
| 9173 | |
| 9174 | TEST_F(URLRequestTest, NetworkAccessedSetOnHostResolutionFailure) { |
| 9175 | MockHostResolver host_resolver; |
| 9176 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 9177 | TestURLRequestContext context(true); |
| 9178 | context.set_network_delegate(&network_delegate); |
| 9179 | context.set_host_resolver(&host_resolver); |
| 9180 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 9181 | context.Init(); |
| 9182 | |
| 9183 | TestDelegate d; |
| 9184 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 9185 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
| 9186 | |
| 9187 | EXPECT_FALSE(req->response_info().network_accessed); |
| 9188 | |
| 9189 | req->Start(); |
| 9190 | base::RunLoop().Run(); |
| 9191 | EXPECT_TRUE(req->response_info().network_accessed); |
| 9192 | } |
| 9193 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 9194 | } // namespace net |