[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 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | #include <string> |
| 14 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 15 | #include "base/basictypes.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 16 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 18 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 19 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 20 | #include "base/format_macros.h" |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 21 | #include "base/memory/scoped_ptr.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 22 | #include "base/memory/weak_ptr.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 23 | #include "base/message_loop/message_loop.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 24 | #include "base/message_loop/message_loop_proxy.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 25 | #include "base/path_service.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 26 | #include "base/run_loop.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 27 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 28 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 29 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 30 | #include "base/strings/string_util.h" |
| 31 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 32 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 33 | #include "net/base/capturing_net_log.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 34 | #include "net/base/chunked_upload_data_stream.h" |
| 35 | #include "net/base/elements_upload_data_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 36 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 37 | #include "net/base/load_timing_info.h" |
| 38 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 39 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 40 | #include "net/base/net_log.h" |
| 41 | #include "net/base/net_log_unittest.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" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 44 | #include "net/base/request_priority.h" |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 45 | #include "net/base/test_data_directory.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 46 | #include "net/base/upload_bytes_element_reader.h" |
| 47 | #include "net/base/upload_data_stream.h" |
| 48 | #include "net/base/upload_file_element_reader.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 49 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 50 | #include "net/cert/mock_cert_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 51 | #include "net/cert/test_root_certs.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 52 | #include "net/cookies/cookie_monster.h" |
| 53 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 55 | #include "net/dns/mock_host_resolver.h" |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 56 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 57 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | #include "net/http/http_cache.h" |
| 59 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 60 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 61 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 62 | #include "net/http/http_response_headers.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 63 | #include "net/http/http_util.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 64 | #include "net/ocsp/nss_ocsp.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 65 | #include "net/proxy/proxy_service.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 66 | #include "net/socket/ssl_client_socket.h" |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 67 | #include "net/ssl/ssl_cipher_suite_names.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 68 | #include "net/ssl/ssl_connection_status_flags.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 69 | #include "net/test/cert_test_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 70 | #include "net/test/spawned_test_server/spawned_test_server.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 71 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 72 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 73 | #include "net/url_request/url_request.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 74 | #include "net/url_request/url_request_http_job.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 75 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 76 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 77 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 78 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 79 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 80 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 81 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 82 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 83 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 84 | #include "net/url_request/file_protocol_handler.h" |
| 85 | #include "net/url_request/url_request_file_dir_job.h" |
| 86 | #endif |
| 87 | |
| 88 | #if !defined(DISABLE_FTP_SUPPORT) |
| 89 | #include "net/url_request/ftp_protocol_handler.h" |
| 90 | #endif |
| 91 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 92 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 93 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 94 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 95 | #include "base/win/windows_version.h" |
| 96 | #endif |
| 97 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 98 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 99 | using base::Time; |
| 100 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 101 | namespace net { |
| 102 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 103 | namespace { |
| 104 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 105 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 106 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 107 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 108 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 109 | // Tests load timing information in the case a fresh connection was used, with |
| 110 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 111 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 112 | int connect_timing_flags) { |
| 113 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 114 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 115 | |
| 116 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 117 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 118 | |
| 119 | EXPECT_LE(load_timing_info.request_start, |
| 120 | load_timing_info.connect_timing.connect_start); |
| 121 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 122 | connect_timing_flags); |
| 123 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 124 | load_timing_info.send_start); |
| 125 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 126 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 127 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 128 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 129 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 130 | } |
| 131 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 132 | // Same as above, but with proxy times. |
| 133 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 134 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 135 | int connect_timing_flags) { |
| 136 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 137 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 138 | |
| 139 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 140 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 141 | |
| 142 | EXPECT_LE(load_timing_info.request_start, |
| 143 | load_timing_info.proxy_resolve_start); |
| 144 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 145 | load_timing_info.proxy_resolve_end); |
| 146 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 147 | load_timing_info.connect_timing.connect_start); |
| 148 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 149 | connect_timing_flags); |
| 150 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 151 | load_timing_info.send_start); |
| 152 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 153 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 154 | } |
| 155 | |
| 156 | // Same as above, but with a reused socket and proxy times. |
| 157 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 158 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 159 | EXPECT_TRUE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 160 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 161 | |
| 162 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 163 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 164 | |
| 165 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 166 | |
| 167 | EXPECT_LE(load_timing_info.request_start, |
| 168 | load_timing_info.proxy_resolve_start); |
| 169 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 170 | load_timing_info.proxy_resolve_end); |
| 171 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 172 | load_timing_info.send_start); |
| 173 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 174 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 175 | } |
| 176 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 177 | // Tests load timing information in the case of a cache hit, when no cache |
| 178 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 179 | base::StringPiece TestNetResourceProvider(int key) { |
| 180 | return "header"; |
| 181 | } |
| 182 | |
| 183 | void FillBuffer(char* buffer, size_t len) { |
| 184 | static bool called = false; |
| 185 | if (!called) { |
| 186 | called = true; |
| 187 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 188 | srand(seed); |
| 189 | } |
| 190 | |
| 191 | for (size_t i = 0; i < len; i++) { |
| 192 | buffer[i] = static_cast<char>(rand()); |
| 193 | if (!buffer[i]) |
| 194 | buffer[i] = 'g'; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | #if !defined(OS_IOS) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 199 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 200 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 201 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 202 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 203 | |
| 204 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 205 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 206 | |
| 207 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 208 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 209 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 210 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 211 | |
| 212 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 213 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 214 | } |
| 215 | |
| 216 | // Tests load timing in the case that there is no HTTP response. This can be |
| 217 | // used to test in the case of errors or non-HTTP requests. |
| 218 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 219 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23: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] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 222 | |
| 223 | // Only the request times should be non-null. |
| 224 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 225 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 226 | |
| 227 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 228 | |
| 229 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 230 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 231 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 232 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 233 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 234 | } |
| 235 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 236 | // Do a case-insensitive search through |haystack| for |needle|. |
| 237 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 238 | std::string::const_iterator it = |
| 239 | std::search(haystack.begin(), |
| 240 | haystack.end(), |
| 241 | needle, |
| 242 | needle + strlen(needle), |
[email protected] | 07f1cee | 2010-11-03 03:53:35 | [diff] [blame] | 243 | base::CaseInsensitiveCompare<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 244 | return it != haystack.end(); |
| 245 | } |
| 246 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 247 | scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 248 | scoped_ptr<UploadElementReader> reader( |
| 249 | new UploadBytesElementReader(data, strlen(data))); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 250 | return ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 251 | } |
| 252 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 253 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 254 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 255 | // -1 means unknown. 0 means no encryption. |
| 256 | EXPECT_GT(ssl_info.security_bits, 0); |
| 257 | |
| 258 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 259 | int cipher_suite = SSLConnectionStatusToCipherSuite( |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 260 | ssl_info.connection_status); |
| 261 | EXPECT_NE(0, cipher_suite); |
| 262 | } |
| 263 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 264 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 265 | const GURL& host_url) { |
| 266 | std::string sent_value; |
| 267 | |
| 268 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 269 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 270 | |
| 271 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 272 | EXPECT_EQ("keep-alive", sent_value); |
| 273 | } |
| 274 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 275 | bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) { |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 276 | size_t size = a.size(); |
| 277 | |
| 278 | if (size != b.size()) |
| 279 | return false; |
| 280 | |
| 281 | for (size_t i = 0; i < size; ++i) { |
| 282 | if (!a[i].Equals(b[i])) |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | return true; |
| 287 | } |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 288 | #endif // !defined(OS_IOS) |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 289 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 290 | // A network delegate that allows the user to choose a subset of request stages |
| 291 | // to block in. When blocking, the delegate can do one of the following: |
| 292 | // * synchronously return a pre-specified error code, or |
| 293 | // * asynchronously return that value via an automatically called callback, |
| 294 | // or |
| 295 | // * block and wait for the user to do a callback. |
| 296 | // Additionally, the user may also specify a redirect URL -- then each request |
| 297 | // with the current URL different from the redirect target will be redirected |
| 298 | // to that target, in the on-before-URL-request stage, independent of whether |
| 299 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 300 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 301 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 302 | // Stages in which the delegate can block. |
| 303 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 304 | NOT_BLOCKED = 0, |
| 305 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 306 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 307 | ON_HEADERS_RECEIVED = 1 << 2, |
| 308 | ON_AUTH_REQUIRED = 1 << 3 |
| 309 | }; |
| 310 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 311 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 312 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 313 | enum BlockMode { |
| 314 | SYNCHRONOUS, // No callback, returns specified return values. |
| 315 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 316 | // specified return codes. |
| 317 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 318 | // |auth_retval_| are ignored. In every blocking stage the |
| 319 | // message loop is quit. |
| 320 | }; |
| 321 | |
| 322 | // Creates a delegate which does not block at all. |
| 323 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 324 | |
| 325 | // For users to trigger a callback returning |response|. |
| 326 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 327 | // Only call if |block_mode_| == USER_CALLBACK. |
| 328 | void DoCallback(int response); |
| 329 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 330 | |
| 331 | // Setters. |
| 332 | void set_retval(int retval) { |
| 333 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 334 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 335 | ASSERT_NE(OK, retval); |
| 336 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 337 | } |
| 338 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 339 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 340 | // |auth_credentials_| will be passed with the response. |
| 341 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 342 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 343 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 344 | auth_retval_ = auth_retval; |
| 345 | } |
| 346 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 347 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 350 | void set_redirect_url(const GURL& url) { |
| 351 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 354 | void set_block_on(int block_on) { |
| 355 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 358 | // Allows the user to check in which state did we block. |
| 359 | Stage stage_blocked_for_callback() const { |
| 360 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 361 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 365 | void RunCallback(int response, const CompletionCallback& callback); |
| 366 | void RunAuthCallback(AuthRequiredResponse response, |
| 367 | const AuthCallback& callback); |
| 368 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 369 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 370 | int OnBeforeURLRequest(URLRequest* request, |
| 371 | const CompletionCallback& callback, |
| 372 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 373 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 374 | int OnBeforeSendHeaders(URLRequest* request, |
| 375 | const CompletionCallback& callback, |
| 376 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 377 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 378 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 379 | URLRequest* request, |
| 380 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 381 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 382 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 383 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 384 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 385 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 386 | URLRequest* request, |
| 387 | const AuthChallengeInfo& auth_info, |
| 388 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 389 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 390 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 391 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 392 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 393 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 394 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 395 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 396 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 397 | |
| 398 | // Configuration parameters, can be adjusted by public methods: |
| 399 | const BlockMode block_mode_; |
| 400 | |
| 401 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 402 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 403 | int retval_; // To be returned in non-auth stages. |
| 404 | AuthRequiredResponse auth_retval_; |
| 405 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 406 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 407 | int block_on_; // Bit mask: in which stages to block. |
| 408 | |
| 409 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 410 | // callback. |
| 411 | AuthCredentials auth_credentials_; |
| 412 | AuthCredentials* target_auth_credentials_; |
| 413 | |
| 414 | // Internal variables, not set by not the user: |
| 415 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 416 | // USER_CALLBACK). |
| 417 | Stage stage_blocked_for_callback_; |
| 418 | |
| 419 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 420 | CompletionCallback callback_; |
| 421 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 422 | |
| 423 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 424 | |
| 425 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 426 | }; |
| 427 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 428 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 429 | : block_mode_(block_mode), |
| 430 | retval_(OK), |
| 431 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 432 | block_on_(0), |
| 433 | target_auth_credentials_(NULL), |
| 434 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 435 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 439 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 440 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 441 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 442 | CompletionCallback callback = callback_; |
| 443 | Reset(); |
| 444 | RunCallback(response, callback); |
| 445 | } |
| 446 | |
| 447 | void BlockingNetworkDelegate::DoAuthCallback( |
| 448 | NetworkDelegate::AuthRequiredResponse response) { |
| 449 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 450 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 451 | AuthCallback auth_callback = auth_callback_; |
| 452 | Reset(); |
| 453 | RunAuthCallback(response, auth_callback); |
| 454 | } |
| 455 | |
| 456 | void BlockingNetworkDelegate::RunCallback(int response, |
| 457 | const CompletionCallback& callback) { |
| 458 | callback.Run(response); |
| 459 | } |
| 460 | |
| 461 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 462 | const AuthCallback& callback) { |
| 463 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 464 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 465 | *target_auth_credentials_ = auth_credentials_; |
| 466 | } |
| 467 | callback.Run(response); |
| 468 | } |
| 469 | |
| 470 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 471 | URLRequest* request, |
| 472 | const CompletionCallback& callback, |
| 473 | GURL* new_url) { |
| 474 | if (redirect_url_ == request->url()) |
| 475 | return OK; // We've already seen this request and redirected elsewhere. |
| 476 | |
| 477 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 478 | |
| 479 | if (!redirect_url_.is_empty()) |
| 480 | *new_url = redirect_url_; |
| 481 | |
| 482 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 483 | } |
| 484 | |
| 485 | int BlockingNetworkDelegate::OnBeforeSendHeaders( |
| 486 | URLRequest* request, |
| 487 | const CompletionCallback& callback, |
| 488 | HttpRequestHeaders* headers) { |
| 489 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 490 | |
| 491 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 492 | } |
| 493 | |
| 494 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 495 | URLRequest* request, |
| 496 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 497 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 498 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 499 | GURL* allowed_unsafe_redirect_url) { |
| 500 | TestNetworkDelegate::OnHeadersReceived(request, |
| 501 | callback, |
| 502 | original_response_headers, |
| 503 | override_response_headers, |
| 504 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 505 | |
| 506 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 507 | } |
| 508 | |
| 509 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 510 | URLRequest* request, |
| 511 | const AuthChallengeInfo& auth_info, |
| 512 | const AuthCallback& callback, |
| 513 | AuthCredentials* credentials) { |
| 514 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 515 | credentials); |
| 516 | // Check that the user has provided callback for the previous blocked stage. |
| 517 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 518 | |
| 519 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 520 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 521 | } |
| 522 | |
| 523 | target_auth_credentials_ = credentials; |
| 524 | |
| 525 | switch (block_mode_) { |
| 526 | case SYNCHRONOUS: |
| 527 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 528 | *target_auth_credentials_ = auth_credentials_; |
| 529 | return auth_retval_; |
| 530 | |
| 531 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 532 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 533 | FROM_HERE, |
| 534 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 535 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 536 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 537 | |
| 538 | case USER_CALLBACK: |
| 539 | auth_callback_ = callback; |
| 540 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 541 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 542 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 543 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 544 | } |
| 545 | NOTREACHED(); |
| 546 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 547 | } |
| 548 | |
| 549 | void BlockingNetworkDelegate::Reset() { |
| 550 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 551 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 552 | callback_.Reset(); |
| 553 | auth_callback_.Reset(); |
| 554 | } |
| 555 | |
| 556 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 557 | BlockingNetworkDelegate::Stage stage, |
| 558 | const CompletionCallback& callback) { |
| 559 | // Check that the user has provided callback for the previous blocked stage. |
| 560 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 561 | |
| 562 | if ((block_on_ & stage) == 0) { |
| 563 | return OK; |
| 564 | } |
| 565 | |
| 566 | switch (block_mode_) { |
| 567 | case SYNCHRONOUS: |
| 568 | EXPECT_NE(OK, retval_); |
| 569 | return retval_; |
| 570 | |
| 571 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 572 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 573 | FROM_HERE, |
| 574 | base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 575 | weak_factory_.GetWeakPtr(), retval_, callback)); |
| 576 | return ERR_IO_PENDING; |
| 577 | |
| 578 | case USER_CALLBACK: |
| 579 | callback_ = callback; |
| 580 | stage_blocked_for_callback_ = stage; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 581 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 582 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 583 | return ERR_IO_PENDING; |
| 584 | } |
| 585 | NOTREACHED(); |
| 586 | return 0; |
| 587 | } |
| 588 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 589 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 590 | public: |
| 591 | // Does not own |delegate|. |
| 592 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 593 | NetworkDelegate* delegate) |
| 594 | : TestURLRequestContext(true) { |
| 595 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 596 | set_network_delegate(delegate); |
| 597 | Init(); |
| 598 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 599 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 600 | }; |
| 601 | |
| 602 | } // namespace |
| 603 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 604 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 605 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 606 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 607 | URLRequestTest() : default_context_(true) { |
| 608 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 609 | default_context_.set_net_log(&net_log_); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 610 | job_factory_.SetProtocolHandler("data", new DataProtocolHandler); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 611 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 612 | job_factory_.SetProtocolHandler( |
| 613 | "file", new FileProtocolHandler(base::MessageLoopProxy::current())); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 614 | #endif |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 615 | default_context_.set_job_factory(&job_factory_); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 616 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 617 | } |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 618 | virtual ~URLRequestTest() { |
| 619 | // URLRequestJobs may post clean-up tasks on destruction. |
| 620 | base::RunLoop().RunUntilIdle(); |
| 621 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 622 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 623 | // Adds the TestJobInterceptor to the default context. |
| 624 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 625 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 626 | job_factory_.SetProtocolHandler("http", NULL); |
| 627 | job_factory_.SetProtocolHandler("http", protocol_handler_); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 628 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 629 | } |
| 630 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 631 | protected: |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 632 | CapturingNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 633 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 634 | URLRequestJobFactoryImpl job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 635 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 636 | }; |
| 637 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 638 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 639 | TestDelegate d; |
| 640 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 641 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 642 | GURL("about:blank"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 643 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 644 | r->Start(); |
| 645 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 646 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 647 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 648 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 649 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 650 | EXPECT_FALSE(d.received_data_before_response()); |
| 651 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 652 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 653 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 654 | |
| 655 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 656 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 661 | TestDelegate d; |
| 662 | { |
| 663 | // Use our nice little Chrome logo. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 664 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 665 | GURL( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 666 | "data:image/png;base64," |
| 667 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3" |
| 668 | "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD" |
| 669 | "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t" |
| 670 | "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9" |
| 671 | "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1" |
| 672 | "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z" |
| 673 | "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW" |
| 674 | "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW" |
| 675 | "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb" |
| 676 | "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5" |
| 677 | "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV" |
| 678 | "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq" |
| 679 | "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F" |
| 680 | "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB" |
| 681 | "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM" |
| 682 | "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm" |
| 683 | "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En" |
| 684 | "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 685 | DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 686 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 687 | r->Start(); |
| 688 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 689 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 690 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 691 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 692 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 693 | EXPECT_FALSE(d.received_data_before_response()); |
| 694 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 695 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 696 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 697 | |
| 698 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 699 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 703 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 704 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 705 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 706 | PathService::Get(base::FILE_EXE, &app_path); |
| 707 | GURL app_url = FilePathToFileURL(app_path); |
| 708 | |
| 709 | TestDelegate d; |
| 710 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 711 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 712 | app_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 713 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 714 | r->Start(); |
| 715 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 716 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 717 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 718 | |
| 719 | int64 file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 720 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 721 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 722 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 723 | EXPECT_EQ(1, d.response_started_count()); |
| 724 | EXPECT_FALSE(d.received_data_before_response()); |
| 725 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 726 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 727 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 728 | |
| 729 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 730 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 734 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 735 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 736 | PathService::Get(base::FILE_EXE, &app_path); |
| 737 | GURL app_url = FilePathToFileURL(app_path); |
| 738 | |
| 739 | TestDelegate d; |
| 740 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 741 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 742 | app_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 743 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 744 | r->Start(); |
| 745 | EXPECT_TRUE(r->is_pending()); |
| 746 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 747 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 748 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 749 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 750 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 751 | } |
| 752 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 753 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 754 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 755 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 756 | FillBuffer(buffer.get(), buffer_size); |
| 757 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 758 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 759 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 760 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 761 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 762 | |
| 763 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 764 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 765 | |
| 766 | const size_t first_byte_position = 500; |
| 767 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 768 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 769 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 770 | buffer.get() + last_byte_position + 1); |
| 771 | |
| 772 | TestDelegate d; |
| 773 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 774 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 775 | temp_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 776 | |
| 777 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 778 | headers.SetHeader( |
| 779 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 780 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 781 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 782 | r->SetExtraRequestHeaders(headers); |
| 783 | r->Start(); |
| 784 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 785 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 786 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 787 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 788 | EXPECT_EQ(1, d.response_started_count()); |
| 789 | EXPECT_FALSE(d.received_data_before_response()); |
| 790 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 791 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 792 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 793 | } |
| 794 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 795 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 799 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 800 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 801 | FillBuffer(buffer.get(), buffer_size); |
| 802 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 803 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 804 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 805 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 806 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 807 | |
| 808 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 809 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 810 | |
| 811 | const size_t first_byte_position = 500; |
| 812 | const size_t last_byte_position = buffer_size - 1; |
| 813 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 814 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 815 | buffer.get() + last_byte_position + 1); |
| 816 | |
| 817 | TestDelegate d; |
| 818 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 819 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 820 | temp_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 821 | |
| 822 | HttpRequestHeaders headers; |
| 823 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 824 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 825 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 826 | r->SetExtraRequestHeaders(headers); |
| 827 | r->Start(); |
| 828 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 829 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 830 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 831 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 832 | EXPECT_EQ(1, d.response_started_count()); |
| 833 | EXPECT_FALSE(d.received_data_before_response()); |
| 834 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 835 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 836 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 837 | } |
| 838 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 839 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 843 | const size_t buffer_size = 400000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 844 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 845 | FillBuffer(buffer.get(), buffer_size); |
| 846 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 847 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 848 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 849 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 850 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 851 | |
| 852 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 853 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 854 | |
| 855 | TestDelegate d; |
| 856 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 857 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 858 | temp_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 859 | |
| 860 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 861 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 862 | r->SetExtraRequestHeaders(headers); |
| 863 | r->Start(); |
| 864 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 865 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 866 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 867 | EXPECT_TRUE(d.request_failed()); |
| 868 | } |
| 869 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 870 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 871 | } |
| 872 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 873 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 874 | base::ScopedTempDir temp_dir; |
| 875 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 876 | base::FilePath test_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 877 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file)); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 878 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 879 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 880 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 881 | |
| 882 | { |
| 883 | TestDelegate d; |
| 884 | TestNetworkDelegate network_delegate; |
| 885 | network_delegate.set_can_access_files(true); |
| 886 | default_context_.set_network_delegate(&network_delegate); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 887 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 888 | test_file_url, DEFAULT_PRIORITY, &d, NULL)); |
| 889 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 890 | base::RunLoop().Run(); |
| 891 | EXPECT_FALSE(d.request_failed()); |
| 892 | EXPECT_EQ(test_data, d.data_received()); |
| 893 | } |
| 894 | |
| 895 | { |
| 896 | TestDelegate d; |
| 897 | TestNetworkDelegate network_delegate; |
| 898 | network_delegate.set_can_access_files(false); |
| 899 | default_context_.set_network_delegate(&network_delegate); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 900 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 901 | test_file_url, DEFAULT_PRIORITY, &d, NULL)); |
| 902 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 903 | base::RunLoop().Run(); |
| 904 | EXPECT_TRUE(d.request_failed()); |
| 905 | EXPECT_EQ("", d.data_received()); |
| 906 | } |
| 907 | } |
| 908 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 909 | |
| 910 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 911 | // Put in mock resource provider. |
| 912 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 913 | |
| 914 | TestDelegate d; |
| 915 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 916 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 917 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 918 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 919 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 920 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 921 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 922 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, NULL)); |
| 923 | req->Start(); |
| 924 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 925 | |
| 926 | d.set_cancel_in_received_data_pending(true); |
| 927 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 928 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | // Take out mock resource provider. |
| 932 | NetModule::SetResourceProvider(NULL); |
| 933 | } |
| 934 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 935 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 936 | // Verify the general sanity of the the output of the file: |
| 937 | // directory lister by checking for the output of a known existing |
| 938 | // file. |
| 939 | const char sentinel_name[] = "filedir-sentinel"; |
| 940 | |
| 941 | base::FilePath path; |
| 942 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 943 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 944 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 945 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 946 | |
| 947 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 948 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 949 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d, NULL)); |
| 950 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 951 | base::RunLoop().Run(); |
| 952 | |
| 953 | // Generate entry for the sentinel file. |
| 954 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 955 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 956 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 957 | EXPECT_GT(info.size, 0); |
| 958 | std::string sentinel_output = GetDirectoryListingEntry( |
| 959 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 960 | std::string(sentinel_name), |
| 961 | false /* is_dir */, |
| 962 | info.size, |
| 963 | info.last_modified); |
| 964 | |
| 965 | ASSERT_LT(0, d.bytes_received()); |
| 966 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 967 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 968 | // Check for the entry generated for the "sentinel" file. |
| 969 | const std::string& data = d.data_received(); |
| 970 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 971 | } |
| 972 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 973 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 974 | // There is an implicit redirect when loading a file path that matches a |
| 975 | // directory and does not end with a slash. Ensure that following such |
| 976 | // redirects does not crash. See http://crbug.com/18686. |
| 977 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 978 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 979 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 980 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 981 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 982 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 983 | |
| 984 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 985 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 986 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d, NULL)); |
| 987 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 988 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 989 | |
| 990 | ASSERT_EQ(1, d.received_redirect_count()); |
| 991 | ASSERT_LT(0, d.bytes_received()); |
| 992 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 993 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | #if defined(OS_WIN) |
| 997 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 998 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 999 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1000 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1001 | GURL("file:///"), DEFAULT_PRIORITY, &d, NULL)); |
| 1002 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1003 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1004 | |
| 1005 | ASSERT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1006 | ASSERT_FALSE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1007 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1008 | #endif // defined(OS_WIN) |
| 1009 | |
| 1010 | #endif // !defined(DISABLE_FILE_SUPPORT) |
| 1011 | |
| 1012 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1013 | TestDelegate d; |
| 1014 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1015 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1016 | GURL("invalid url"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1017 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1018 | r->Start(); |
| 1019 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1020 | |
| 1021 | base::RunLoop().Run(); |
| 1022 | EXPECT_TRUE(d.request_failed()); |
| 1023 | } |
| 1024 | } |
| 1025 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1026 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1027 | TestURLRequestContext context; |
| 1028 | TestNetworkDelegate network_delegate; |
| 1029 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1030 | context.set_network_delegate(&network_delegate); |
| 1031 | TestDelegate d; |
| 1032 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 1033 | GURL("http://localhost/"), DEFAULT_PRIORITY, &d, NULL)); |
| 1034 | req->SetReferrer("https://somewhere.com/"); |
| 1035 | |
| 1036 | req->Start(); |
| 1037 | base::RunLoop().Run(); |
| 1038 | EXPECT_TRUE(d.request_failed()); |
| 1039 | } |
| 1040 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1041 | #if defined(OS_WIN) |
| 1042 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1043 | base::FilePath app_path; |
| 1044 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1045 | app_path = app_path.AppendASCII("net"); |
| 1046 | app_path = app_path.AppendASCII("data"); |
| 1047 | app_path = app_path.AppendASCII("url_request_unittest"); |
| 1048 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1049 | |
| 1050 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1051 | |
| 1052 | base::win::ScopedCOMInitializer com_initializer; |
| 1053 | |
| 1054 | // Temporarily create a shortcut for test |
| 1055 | { |
| 1056 | base::win::ScopedComPtr<IShellLink> shell; |
| 1057 | ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 1058 | CLSCTX_INPROC_SERVER))); |
| 1059 | base::win::ScopedComPtr<IPersistFile> persist; |
| 1060 | ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 1061 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1062 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1063 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1064 | } |
| 1065 | |
| 1066 | TestDelegate d; |
| 1067 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1068 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1069 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1070 | NULL)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1071 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1072 | r->Start(); |
| 1073 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1074 | |
| 1075 | base::RunLoop().Run(); |
| 1076 | |
| 1077 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1078 | GetFileAttributesEx(app_path.value().c_str(), |
| 1079 | GetFileExInfoStandard, &data); |
| 1080 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1081 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1082 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1083 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 1084 | scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
| 1085 | DWORD read_size; |
| 1086 | BOOL result; |
| 1087 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1088 | &read_size, NULL); |
| 1089 | std::string content(buffer.get(), read_size); |
| 1090 | CloseHandle(file); |
| 1091 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1092 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1093 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1094 | EXPECT_EQ(content, d.data_received()); |
| 1095 | } |
| 1096 | |
| 1097 | // Clean the shortcut |
| 1098 | DeleteFile(lnk_path.c_str()); |
| 1099 | } |
| 1100 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1101 | |
| 1102 | // Custom URLRequestJobs for use with interceptor tests |
| 1103 | class RestartTestJob : public URLRequestTestJob { |
| 1104 | public: |
| 1105 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1106 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1107 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1108 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1109 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1110 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1111 | }; |
| 1112 | |
| 1113 | class CancelTestJob : public URLRequestTestJob { |
| 1114 | public: |
| 1115 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1116 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1117 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1118 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1119 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1120 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1121 | }; |
| 1122 | |
| 1123 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1124 | public: |
| 1125 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1126 | NetworkDelegate* network_delegate) |
| 1127 | : URLRequestTestJob(request, network_delegate, true) { |
| 1128 | } |
| 1129 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1130 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1131 | request_->Cancel(); |
| 1132 | this->NotifyRestartRequired(); |
| 1133 | } |
| 1134 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1135 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1136 | }; |
| 1137 | |
| 1138 | // An Interceptor for use with interceptor tests |
| 1139 | class TestInterceptor : URLRequest::Interceptor { |
| 1140 | public: |
| 1141 | TestInterceptor() |
| 1142 | : intercept_main_request_(false), restart_main_request_(false), |
| 1143 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1144 | simulate_main_network_error_(false), |
| 1145 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1146 | intercept_final_response_(false), cancel_final_request_(false), |
| 1147 | did_intercept_main_(false), did_restart_main_(false), |
| 1148 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1149 | did_simulate_error_main_(false), |
| 1150 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1151 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1152 | URLRequest::Deprecated::RegisterRequestInterceptor(this); |
| 1153 | } |
| 1154 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1155 | ~TestInterceptor() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1156 | URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
| 1157 | } |
| 1158 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1159 | URLRequestJob* MaybeIntercept(URLRequest* request, |
| 1160 | NetworkDelegate* network_delegate) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1161 | if (restart_main_request_) { |
| 1162 | restart_main_request_ = false; |
| 1163 | did_restart_main_ = true; |
| 1164 | return new RestartTestJob(request, network_delegate); |
| 1165 | } |
| 1166 | if (cancel_main_request_) { |
| 1167 | cancel_main_request_ = false; |
| 1168 | did_cancel_main_ = true; |
| 1169 | return new CancelTestJob(request, network_delegate); |
| 1170 | } |
| 1171 | if (cancel_then_restart_main_request_) { |
| 1172 | cancel_then_restart_main_request_ = false; |
| 1173 | did_cancel_then_restart_main_ = true; |
| 1174 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1175 | } |
| 1176 | if (simulate_main_network_error_) { |
| 1177 | simulate_main_network_error_ = false; |
| 1178 | did_simulate_error_main_ = true; |
| 1179 | // will error since the requeted url is not one of its canned urls |
| 1180 | return new URLRequestTestJob(request, network_delegate, true); |
| 1181 | } |
| 1182 | if (!intercept_main_request_) |
| 1183 | return NULL; |
| 1184 | intercept_main_request_ = false; |
| 1185 | did_intercept_main_ = true; |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1186 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1187 | network_delegate, |
| 1188 | main_headers_, |
| 1189 | main_data_, |
| 1190 | true); |
| 1191 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1192 | return job; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1193 | } |
| 1194 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1195 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1196 | NetworkDelegate* network_delegate, |
| 1197 | const GURL& location) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1198 | if (cancel_redirect_request_) { |
| 1199 | cancel_redirect_request_ = false; |
| 1200 | did_cancel_redirect_ = true; |
| 1201 | return new CancelTestJob(request, network_delegate); |
| 1202 | } |
| 1203 | if (!intercept_redirect_) |
| 1204 | return NULL; |
| 1205 | intercept_redirect_ = false; |
| 1206 | did_intercept_redirect_ = true; |
| 1207 | return new URLRequestTestJob(request, |
| 1208 | network_delegate, |
| 1209 | redirect_headers_, |
| 1210 | redirect_data_, |
| 1211 | true); |
| 1212 | } |
| 1213 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 1214 | URLRequestJob* MaybeInterceptResponse( |
| 1215 | URLRequest* request, |
| 1216 | NetworkDelegate* network_delegate) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1217 | if (cancel_final_request_) { |
| 1218 | cancel_final_request_ = false; |
| 1219 | did_cancel_final_ = true; |
| 1220 | return new CancelTestJob(request, network_delegate); |
| 1221 | } |
| 1222 | if (!intercept_final_response_) |
| 1223 | return NULL; |
| 1224 | intercept_final_response_ = false; |
| 1225 | did_intercept_final_ = true; |
| 1226 | return new URLRequestTestJob(request, |
| 1227 | network_delegate, |
| 1228 | final_headers_, |
| 1229 | final_data_, |
| 1230 | true); |
| 1231 | } |
| 1232 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1233 | // Whether to intercept the main request, and if so the response to return and |
| 1234 | // the LoadTimingInfo to use. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1235 | bool intercept_main_request_; |
| 1236 | std::string main_headers_; |
| 1237 | std::string main_data_; |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1238 | LoadTimingInfo main_request_load_timing_info_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1239 | |
| 1240 | // Other actions we take at MaybeIntercept time |
| 1241 | bool restart_main_request_; |
| 1242 | bool cancel_main_request_; |
| 1243 | bool cancel_then_restart_main_request_; |
| 1244 | bool simulate_main_network_error_; |
| 1245 | |
| 1246 | // Whether to intercept redirects, and if so the response to return. |
| 1247 | bool intercept_redirect_; |
| 1248 | std::string redirect_headers_; |
| 1249 | std::string redirect_data_; |
| 1250 | |
| 1251 | // Other actions we can take at MaybeInterceptRedirect time |
| 1252 | bool cancel_redirect_request_; |
| 1253 | |
| 1254 | // Whether to intercept final response, and if so the response to return. |
| 1255 | bool intercept_final_response_; |
| 1256 | std::string final_headers_; |
| 1257 | std::string final_data_; |
| 1258 | |
| 1259 | // Other actions we can take at MaybeInterceptResponse time |
| 1260 | bool cancel_final_request_; |
| 1261 | |
| 1262 | // If we did something or not |
| 1263 | bool did_intercept_main_; |
| 1264 | bool did_restart_main_; |
| 1265 | bool did_cancel_main_; |
| 1266 | bool did_cancel_then_restart_main_; |
| 1267 | bool did_simulate_error_main_; |
| 1268 | bool did_intercept_redirect_; |
| 1269 | bool did_cancel_redirect_; |
| 1270 | bool did_intercept_final_; |
| 1271 | bool did_cancel_final_; |
| 1272 | |
| 1273 | // Static getters for canned response header and data strings |
| 1274 | |
| 1275 | static std::string ok_data() { |
| 1276 | return URLRequestTestJob::test_data_1(); |
| 1277 | } |
| 1278 | |
| 1279 | static std::string ok_headers() { |
| 1280 | return URLRequestTestJob::test_headers(); |
| 1281 | } |
| 1282 | |
| 1283 | static std::string redirect_data() { |
| 1284 | return std::string(); |
| 1285 | } |
| 1286 | |
| 1287 | static std::string redirect_headers() { |
| 1288 | return URLRequestTestJob::test_redirect_headers(); |
| 1289 | } |
| 1290 | |
| 1291 | static std::string error_data() { |
| 1292 | return std::string("ohhh nooooo mr. bill!"); |
| 1293 | } |
| 1294 | |
| 1295 | static std::string error_headers() { |
| 1296 | return URLRequestTestJob::test_error_headers(); |
| 1297 | } |
| 1298 | }; |
| 1299 | |
| 1300 | TEST_F(URLRequestTest, Intercept) { |
| 1301 | TestInterceptor interceptor; |
| 1302 | |
| 1303 | // intercept the main request and respond with a simple response |
| 1304 | interceptor.intercept_main_request_ = true; |
| 1305 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1306 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1307 | |
| 1308 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1309 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1310 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1311 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1312 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1313 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1314 | req->SetUserData(NULL, user_data0); |
| 1315 | req->SetUserData(&user_data1, user_data1); |
| 1316 | req->SetUserData(&user_data2, user_data2); |
| 1317 | req->set_method("GET"); |
| 1318 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1319 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1320 | |
| 1321 | // Make sure we can retrieve our specific user data |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1322 | EXPECT_EQ(user_data0, req->GetUserData(NULL)); |
| 1323 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1324 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1325 | |
| 1326 | // Check the interceptor got called as expected |
| 1327 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1328 | |
| 1329 | // Check we got one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1330 | EXPECT_TRUE(req->status().is_success()); |
| 1331 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1332 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1333 | EXPECT_EQ(1, d.response_started_count()); |
| 1334 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1335 | } |
| 1336 | |
| 1337 | TEST_F(URLRequestTest, InterceptRedirect) { |
| 1338 | TestInterceptor interceptor; |
| 1339 | |
| 1340 | // intercept the main request and respond with a redirect |
| 1341 | interceptor.intercept_main_request_ = true; |
| 1342 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1343 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1344 | |
| 1345 | // intercept that redirect and respond a final OK response |
| 1346 | interceptor.intercept_redirect_ = true; |
| 1347 | interceptor.redirect_headers_ = TestInterceptor::ok_headers(); |
| 1348 | interceptor.redirect_data_ = TestInterceptor::ok_data(); |
| 1349 | |
| 1350 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1351 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1352 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1353 | req->set_method("GET"); |
| 1354 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1355 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1356 | |
| 1357 | // Check the interceptor got called as expected |
| 1358 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1359 | EXPECT_TRUE(interceptor.did_intercept_redirect_); |
| 1360 | |
| 1361 | // Check we got one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1362 | EXPECT_TRUE(req->status().is_success()); |
| 1363 | if (req->status().is_success()) { |
| 1364 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1365 | } |
| 1366 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1367 | EXPECT_EQ(1, d.response_started_count()); |
| 1368 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1369 | } |
| 1370 | |
| 1371 | TEST_F(URLRequestTest, InterceptServerError) { |
| 1372 | TestInterceptor interceptor; |
| 1373 | |
| 1374 | // intercept the main request to generate a server error response |
| 1375 | interceptor.intercept_main_request_ = true; |
| 1376 | interceptor.main_headers_ = TestInterceptor::error_headers(); |
| 1377 | interceptor.main_data_ = TestInterceptor::error_data(); |
| 1378 | |
| 1379 | // intercept that error and respond with an OK response |
| 1380 | interceptor.intercept_final_response_ = true; |
| 1381 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1382 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1383 | |
| 1384 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1385 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1386 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1387 | req->set_method("GET"); |
| 1388 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1389 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1390 | |
| 1391 | // Check the interceptor got called as expected |
| 1392 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1393 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1394 | |
| 1395 | // Check we got one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1396 | EXPECT_TRUE(req->status().is_success()); |
| 1397 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1398 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1399 | EXPECT_EQ(1, d.response_started_count()); |
| 1400 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1401 | } |
| 1402 | |
| 1403 | TEST_F(URLRequestTest, InterceptNetworkError) { |
| 1404 | TestInterceptor interceptor; |
| 1405 | |
| 1406 | // intercept the main request to simulate a network error |
| 1407 | interceptor.simulate_main_network_error_ = true; |
| 1408 | |
| 1409 | // intercept that error and respond with an OK response |
| 1410 | interceptor.intercept_final_response_ = true; |
| 1411 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1412 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1413 | |
| 1414 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1415 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1416 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1417 | req->set_method("GET"); |
| 1418 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1419 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1420 | |
| 1421 | // Check the interceptor got called as expected |
| 1422 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1423 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1424 | |
| 1425 | // Check we received one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1426 | EXPECT_TRUE(req->status().is_success()); |
| 1427 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1428 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1429 | EXPECT_EQ(1, d.response_started_count()); |
| 1430 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1431 | } |
| 1432 | |
| 1433 | TEST_F(URLRequestTest, InterceptRestartRequired) { |
| 1434 | TestInterceptor interceptor; |
| 1435 | |
| 1436 | // restart the main request |
| 1437 | interceptor.restart_main_request_ = true; |
| 1438 | |
| 1439 | // then intercept the new main request and respond with an OK response |
| 1440 | interceptor.intercept_main_request_ = true; |
| 1441 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1442 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1443 | |
| 1444 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1445 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1446 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1447 | req->set_method("GET"); |
| 1448 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1449 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1450 | |
| 1451 | // Check the interceptor got called as expected |
| 1452 | EXPECT_TRUE(interceptor.did_restart_main_); |
| 1453 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1454 | |
| 1455 | // Check we received one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1456 | EXPECT_TRUE(req->status().is_success()); |
| 1457 | if (req->status().is_success()) { |
| 1458 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1459 | } |
| 1460 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1461 | EXPECT_EQ(1, d.response_started_count()); |
| 1462 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1463 | } |
| 1464 | |
| 1465 | TEST_F(URLRequestTest, InterceptRespectsCancelMain) { |
| 1466 | TestInterceptor interceptor; |
| 1467 | |
| 1468 | // intercept the main request and cancel from within the restarted job |
| 1469 | interceptor.cancel_main_request_ = true; |
| 1470 | |
| 1471 | // setup to intercept final response and override it with an OK response |
| 1472 | interceptor.intercept_final_response_ = true; |
| 1473 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1474 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1475 | |
| 1476 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1477 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1478 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1479 | req->set_method("GET"); |
| 1480 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1481 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1482 | |
| 1483 | // Check the interceptor got called as expected |
| 1484 | EXPECT_TRUE(interceptor.did_cancel_main_); |
| 1485 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1486 | |
| 1487 | // Check we see a canceled request |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1488 | EXPECT_FALSE(req->status().is_success()); |
| 1489 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) { |
| 1493 | TestInterceptor interceptor; |
| 1494 | |
| 1495 | // intercept the main request and respond with a redirect |
| 1496 | interceptor.intercept_main_request_ = true; |
| 1497 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1498 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1499 | |
| 1500 | // intercept the redirect and cancel from within that job |
| 1501 | interceptor.cancel_redirect_request_ = true; |
| 1502 | |
| 1503 | // setup to intercept final response and override it with an OK response |
| 1504 | interceptor.intercept_final_response_ = true; |
| 1505 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1506 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1507 | |
| 1508 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1509 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1510 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1511 | req->set_method("GET"); |
| 1512 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1513 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1514 | |
| 1515 | // Check the interceptor got called as expected |
| 1516 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1517 | EXPECT_TRUE(interceptor.did_cancel_redirect_); |
| 1518 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1519 | |
| 1520 | // Check we see a canceled request |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1521 | EXPECT_FALSE(req->status().is_success()); |
| 1522 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | TEST_F(URLRequestTest, InterceptRespectsCancelFinal) { |
| 1526 | TestInterceptor interceptor; |
| 1527 | |
| 1528 | // intercept the main request to simulate a network error |
| 1529 | interceptor.simulate_main_network_error_ = true; |
| 1530 | |
| 1531 | // setup to intercept final response and cancel from within that job |
| 1532 | interceptor.cancel_final_request_ = true; |
| 1533 | |
| 1534 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1535 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1536 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1537 | req->set_method("GET"); |
| 1538 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1539 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1540 | |
| 1541 | // Check the interceptor got called as expected |
| 1542 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1543 | EXPECT_TRUE(interceptor.did_cancel_final_); |
| 1544 | |
| 1545 | // Check we see a canceled request |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1546 | EXPECT_FALSE(req->status().is_success()); |
| 1547 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { |
| 1551 | TestInterceptor interceptor; |
| 1552 | |
| 1553 | // intercept the main request and cancel then restart from within that job |
| 1554 | interceptor.cancel_then_restart_main_request_ = true; |
| 1555 | |
| 1556 | // setup to intercept final response and override it with an OK response |
| 1557 | interceptor.intercept_final_response_ = true; |
| 1558 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1559 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1560 | |
| 1561 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1562 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1563 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1564 | req->set_method("GET"); |
| 1565 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1566 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1567 | |
| 1568 | // Check the interceptor got called as expected |
| 1569 | EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); |
| 1570 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1571 | |
| 1572 | // Check we see a canceled request |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1573 | EXPECT_FALSE(req->status().is_success()); |
| 1574 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1575 | } |
| 1576 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1577 | LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing, |
| 1578 | URLRequestContext* context) { |
| 1579 | TestInterceptor interceptor; |
| 1580 | interceptor.intercept_main_request_ = true; |
| 1581 | interceptor.main_request_load_timing_info_ = job_load_timing; |
| 1582 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1583 | scoped_ptr<URLRequest> req(context->CreateRequest( |
| 1584 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1585 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1586 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1587 | |
| 1588 | LoadTimingInfo resulting_load_timing; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1589 | req->GetLoadTimingInfo(&resulting_load_timing); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1590 | |
| 1591 | // None of these should be modified by the URLRequest. |
| 1592 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1593 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1594 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1595 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1596 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1597 | resulting_load_timing.receive_headers_end); |
| 1598 | |
| 1599 | return resulting_load_timing; |
| 1600 | } |
| 1601 | |
| 1602 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 1603 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 1604 | // or SSL times, and |used_proxy| is used for proxy times. |
| 1605 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 1606 | int connect_time_flags, |
| 1607 | bool used_proxy) { |
| 1608 | LoadTimingInfo load_timing; |
| 1609 | load_timing.socket_log_id = 1; |
| 1610 | |
| 1611 | if (used_proxy) { |
| 1612 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1613 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1614 | } |
| 1615 | |
| 1616 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 1617 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 1618 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 1619 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 1620 | } |
| 1621 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 1622 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 1623 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 1624 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 1625 | } |
| 1626 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 1627 | |
| 1628 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1629 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1630 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1631 | return load_timing; |
| 1632 | } |
| 1633 | |
| 1634 | // Same as above, but in the case of a reused socket. |
| 1635 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 1636 | bool used_proxy) { |
| 1637 | LoadTimingInfo load_timing; |
| 1638 | load_timing.socket_log_id = 1; |
| 1639 | load_timing.socket_reused = true; |
| 1640 | |
| 1641 | if (used_proxy) { |
| 1642 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1643 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1644 | } |
| 1645 | |
| 1646 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1647 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1648 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1649 | return load_timing; |
| 1650 | } |
| 1651 | |
| 1652 | // Basic test that the intercept + load timing tests work. |
| 1653 | TEST_F(URLRequestTest, InterceptLoadTiming) { |
| 1654 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1655 | LoadTimingInfo job_load_timing = |
| 1656 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1657 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1658 | LoadTimingInfo load_timing_result = |
| 1659 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1660 | |
| 1661 | // Nothing should have been changed by the URLRequest. |
| 1662 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1663 | load_timing_result.proxy_resolve_start); |
| 1664 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1665 | load_timing_result.proxy_resolve_end); |
| 1666 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1667 | load_timing_result.connect_timing.dns_start); |
| 1668 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1669 | load_timing_result.connect_timing.dns_end); |
| 1670 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1671 | load_timing_result.connect_timing.connect_start); |
| 1672 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1673 | load_timing_result.connect_timing.connect_end); |
| 1674 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1675 | load_timing_result.connect_timing.ssl_start); |
| 1676 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1677 | load_timing_result.connect_timing.ssl_end); |
| 1678 | |
| 1679 | // Redundant sanity check. |
| 1680 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1681 | } |
| 1682 | |
| 1683 | // Another basic test, with proxy and SSL times, but no DNS times. |
| 1684 | TEST_F(URLRequestTest, InterceptLoadTimingProxy) { |
| 1685 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1686 | LoadTimingInfo job_load_timing = |
| 1687 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1688 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1689 | LoadTimingInfo load_timing_result = |
| 1690 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1691 | |
| 1692 | // Nothing should have been changed by the URLRequest. |
| 1693 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1694 | load_timing_result.proxy_resolve_start); |
| 1695 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1696 | load_timing_result.proxy_resolve_end); |
| 1697 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1698 | load_timing_result.connect_timing.dns_start); |
| 1699 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1700 | load_timing_result.connect_timing.dns_end); |
| 1701 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1702 | load_timing_result.connect_timing.connect_start); |
| 1703 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1704 | load_timing_result.connect_timing.connect_end); |
| 1705 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1706 | load_timing_result.connect_timing.ssl_start); |
| 1707 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1708 | load_timing_result.connect_timing.ssl_end); |
| 1709 | |
| 1710 | // Redundant sanity check. |
| 1711 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1712 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 1713 | } |
| 1714 | |
| 1715 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1716 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 1717 | // the case of reusing a SPDY session. The connected socket is not considered |
| 1718 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1719 | // |
| 1720 | // To mix things up from the test above, assumes DNS times but no SSL times. |
| 1721 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) { |
| 1722 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1723 | LoadTimingInfo job_load_timing = |
| 1724 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 1725 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 1726 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 1727 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 1728 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 1729 | job_load_timing.connect_timing.connect_start = |
| 1730 | now - base::TimeDelta::FromDays(2); |
| 1731 | job_load_timing.connect_timing.connect_end = |
| 1732 | now - base::TimeDelta::FromDays(1); |
| 1733 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1734 | LoadTimingInfo load_timing_result = |
| 1735 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1736 | |
| 1737 | // Proxy times, connect times, and DNS times should all be replaced with |
| 1738 | // request_start. |
| 1739 | EXPECT_EQ(load_timing_result.request_start, |
| 1740 | load_timing_result.proxy_resolve_start); |
| 1741 | EXPECT_EQ(load_timing_result.request_start, |
| 1742 | load_timing_result.proxy_resolve_end); |
| 1743 | EXPECT_EQ(load_timing_result.request_start, |
| 1744 | load_timing_result.connect_timing.dns_start); |
| 1745 | EXPECT_EQ(load_timing_result.request_start, |
| 1746 | load_timing_result.connect_timing.dns_end); |
| 1747 | EXPECT_EQ(load_timing_result.request_start, |
| 1748 | load_timing_result.connect_timing.connect_start); |
| 1749 | EXPECT_EQ(load_timing_result.request_start, |
| 1750 | load_timing_result.connect_timing.connect_end); |
| 1751 | |
| 1752 | // Other times should have been left null. |
| 1753 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1754 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 1755 | } |
| 1756 | |
| 1757 | // Same as above, but in the reused case. |
| 1758 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) { |
| 1759 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1760 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 1761 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 1762 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 1763 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1764 | LoadTimingInfo load_timing_result = |
| 1765 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1766 | |
| 1767 | // Proxy times and connect times should all be replaced with request_start. |
| 1768 | EXPECT_EQ(load_timing_result.request_start, |
| 1769 | load_timing_result.proxy_resolve_start); |
| 1770 | EXPECT_EQ(load_timing_result.request_start, |
| 1771 | load_timing_result.proxy_resolve_end); |
| 1772 | |
| 1773 | // Other times should have been left null. |
| 1774 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 1775 | } |
| 1776 | |
| 1777 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1778 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 1779 | // not considered reused in this test (May be a preconnect). |
| 1780 | // |
| 1781 | // To mix things up, the request has SSL times, but no DNS times. |
| 1782 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) { |
| 1783 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1784 | LoadTimingInfo job_load_timing = |
| 1785 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 1786 | job_load_timing.connect_timing.connect_start = |
| 1787 | now - base::TimeDelta::FromDays(1); |
| 1788 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 1789 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 1790 | job_load_timing.connect_timing.connect_end = |
| 1791 | now - base::TimeDelta::FromDays(4); |
| 1792 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1793 | LoadTimingInfo load_timing_result = |
| 1794 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1795 | |
| 1796 | // Connect times, and SSL times should be replaced with request_start. |
| 1797 | EXPECT_EQ(load_timing_result.request_start, |
| 1798 | load_timing_result.connect_timing.connect_start); |
| 1799 | EXPECT_EQ(load_timing_result.request_start, |
| 1800 | load_timing_result.connect_timing.ssl_start); |
| 1801 | EXPECT_EQ(load_timing_result.request_start, |
| 1802 | load_timing_result.connect_timing.ssl_end); |
| 1803 | EXPECT_EQ(load_timing_result.request_start, |
| 1804 | load_timing_result.connect_timing.connect_end); |
| 1805 | |
| 1806 | // Other times should have been left null. |
| 1807 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 1808 | } |
| 1809 | |
| 1810 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1811 | // |request_start|, due to reusing a connected socket in the case that there |
| 1812 | // are also proxy times. The connected socket is not considered reused in this |
| 1813 | // test (May be a preconnect). |
| 1814 | // |
| 1815 | // In this test, there are no SSL or DNS times. |
| 1816 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) { |
| 1817 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1818 | LoadTimingInfo job_load_timing = |
| 1819 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 1820 | job_load_timing.connect_timing.connect_start = |
| 1821 | now - base::TimeDelta::FromDays(1); |
| 1822 | job_load_timing.connect_timing.connect_end = |
| 1823 | now - base::TimeDelta::FromDays(2); |
| 1824 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1825 | LoadTimingInfo load_timing_result = |
| 1826 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1827 | |
| 1828 | // Connect times should be replaced with proxy_resolve_end. |
| 1829 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1830 | load_timing_result.connect_timing.connect_start); |
| 1831 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1832 | load_timing_result.connect_timing.connect_end); |
| 1833 | |
| 1834 | // Other times should have been left null. |
| 1835 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1836 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 1837 | } |
| 1838 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1839 | // Check that two different URL requests have different identifiers. |
| 1840 | TEST_F(URLRequestTest, Identifiers) { |
| 1841 | TestDelegate d; |
| 1842 | TestURLRequestContext context; |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 1843 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 1844 | GURL("http://example.com"), DEFAULT_PRIORITY, &d, NULL)); |
| 1845 | scoped_ptr<URLRequest> other_req(context.CreateRequest( |
| 1846 | GURL("http://example.com"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1847 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 1848 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | // Check that a failure to connect to the proxy is reported to the network |
| 1852 | // delegate. |
| 1853 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
| 1854 | MockHostResolver host_resolver; |
| 1855 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 1856 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 1857 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1858 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 1859 | |
| 1860 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1861 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 1862 | GURL("http://example.com"), DEFAULT_PRIORITY, &d, NULL)); |
| 1863 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1864 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1865 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1866 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1867 | |
| 1868 | // Check we see a failed request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1869 | EXPECT_FALSE(req->status().is_success()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 1870 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1871 | EXPECT_TRUE(req->proxy_server().IsEmpty()); |
| 1872 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 1873 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1874 | |
| 1875 | EXPECT_EQ(1, network_delegate.error_count()); |
| 1876 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
| 1877 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 1878 | } |
| 1879 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1880 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1881 | // content is empty. |
| 1882 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 1883 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1884 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1885 | GURL("data:,"), DEFAULT_PRIORITY, &d, NULL)); |
| 1886 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1887 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1888 | EXPECT_EQ("", d.data_received()); |
| 1889 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 1890 | } |
| 1891 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1892 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 1893 | // correctly, both before and after start. |
| 1894 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 1895 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1896 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1897 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1898 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1899 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1900 | req->SetPriority(LOW); |
| 1901 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1902 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1903 | req->Start(); |
| 1904 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1905 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1906 | req->SetPriority(MEDIUM); |
| 1907 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 1911 | // Start on it. |
| 1912 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 1913 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1914 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1915 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1916 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1917 | |
| 1918 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1919 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1920 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1921 | EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 1922 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1923 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1924 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1925 | req->Start(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1926 | EXPECT_EQ(LOW, job->priority()); |
| 1927 | } |
| 1928 | |
| 1929 | // Make sure that URLRequest passes on its priority updates to its |
| 1930 | // job. |
| 1931 | TEST_F(URLRequestTest, SetJobPriority) { |
| 1932 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1933 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1934 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1935 | |
| 1936 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1937 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1938 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1939 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1940 | req->SetPriority(LOW); |
| 1941 | req->Start(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1942 | EXPECT_EQ(LOW, job->priority()); |
| 1943 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1944 | req->SetPriority(MEDIUM); |
| 1945 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1946 | EXPECT_EQ(MEDIUM, job->priority()); |
| 1947 | } |
| 1948 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1949 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 1950 | // is MAXIMUM_PRIORITY. |
| 1951 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 1952 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1953 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1954 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, NULL)); |
| 1955 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1956 | |
| 1957 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1958 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1959 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
| 1960 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1961 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 1962 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1963 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1964 | req->SetPriority(MAXIMUM_PRIORITY); |
| 1965 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1966 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1967 | req->Start(); |
| 1968 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1969 | EXPECT_EQ(MAXIMUM_PRIORITY, job->priority()); |
| 1970 | } |
| 1971 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1972 | // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666). |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1973 | #if !defined(OS_IOS) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1974 | // A subclass of SpawnedTestServer that uses a statically-configured hostname. |
| 1975 | // This is to work around mysterious failures in chrome_frame_net_tests. See: |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1976 | // http://crbug.com/114369 |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 1977 | // TODO(erikwright): remove or update as needed; see http://crbug.com/334634. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1978 | class LocalHttpTestServer : public SpawnedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1979 | public: |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1980 | explicit LocalHttpTestServer(const base::FilePath& document_root) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1981 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 1982 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 1983 | document_root) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1984 | LocalHttpTestServer() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1985 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 1986 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 1987 | base::FilePath()) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1988 | }; |
| 1989 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 1990 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1991 | LocalHttpTestServer test_server; |
| 1992 | ASSERT_TRUE(test_server.Start()); |
| 1993 | |
| 1994 | TestURLRequestContext context; |
| 1995 | scoped_refptr<DelayedCookieMonster> delayed_cm = |
| 1996 | new DelayedCookieMonster(); |
| 1997 | scoped_refptr<CookieStore> cookie_store = delayed_cm; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1998 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1999 | |
| 2000 | // Set up a cookie. |
| 2001 | { |
| 2002 | TestNetworkDelegate network_delegate; |
| 2003 | context.set_network_delegate(&network_delegate); |
| 2004 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2005 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2006 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
| 2007 | &d, NULL)); |
| 2008 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2009 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2010 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2011 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2012 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2013 | } |
| 2014 | |
| 2015 | // Verify that the cookie is set. |
| 2016 | { |
| 2017 | TestNetworkDelegate network_delegate; |
| 2018 | context.set_network_delegate(&network_delegate); |
| 2019 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2020 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2021 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2022 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2023 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2024 | |
| 2025 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2026 | != std::string::npos); |
| 2027 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2028 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2029 | } |
| 2030 | } |
| 2031 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2032 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2033 | LocalHttpTestServer test_server; |
| 2034 | ASSERT_TRUE(test_server.Start()); |
| 2035 | |
| 2036 | // Set up a cookie. |
| 2037 | { |
| 2038 | TestNetworkDelegate network_delegate; |
| 2039 | default_context_.set_network_delegate(&network_delegate); |
| 2040 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2041 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2042 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
| 2043 | &d, NULL)); |
| 2044 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2045 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2046 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2047 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2048 | } |
| 2049 | |
| 2050 | // Verify that the cookie is set. |
| 2051 | { |
| 2052 | TestNetworkDelegate network_delegate; |
| 2053 | default_context_.set_network_delegate(&network_delegate); |
| 2054 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2055 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2056 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 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 | |
| 2060 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2061 | != std::string::npos); |
| 2062 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2063 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2064 | } |
| 2065 | |
| 2066 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2067 | { |
| 2068 | TestNetworkDelegate network_delegate; |
| 2069 | default_context_.set_network_delegate(&network_delegate); |
| 2070 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2071 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2072 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2073 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2074 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2075 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2076 | |
| 2077 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2078 | == std::string::npos); |
| 2079 | |
| 2080 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2081 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2082 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2083 | } |
| 2084 | } |
| 2085 | |
| 2086 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2087 | LocalHttpTestServer test_server; |
| 2088 | ASSERT_TRUE(test_server.Start()); |
| 2089 | |
| 2090 | // Set up a cookie. |
| 2091 | { |
| 2092 | TestNetworkDelegate network_delegate; |
| 2093 | default_context_.set_network_delegate(&network_delegate); |
| 2094 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2095 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2096 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
| 2097 | &d, NULL)); |
| 2098 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2099 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2100 | |
| 2101 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2102 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2103 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2104 | } |
| 2105 | |
| 2106 | // Try to set-up another cookie and update the previous cookie. |
| 2107 | { |
| 2108 | TestNetworkDelegate network_delegate; |
| 2109 | default_context_.set_network_delegate(&network_delegate); |
| 2110 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2111 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2112 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2113 | DEFAULT_PRIORITY, &d, NULL)); |
| 2114 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2115 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2116 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2117 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2118 | |
| 2119 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2120 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2121 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2122 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2123 | } |
| 2124 | |
| 2125 | // Verify the cookies weren't saved or updated. |
| 2126 | { |
| 2127 | TestNetworkDelegate network_delegate; |
| 2128 | default_context_.set_network_delegate(&network_delegate); |
| 2129 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2130 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2131 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2132 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2133 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2134 | |
| 2135 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2136 | == std::string::npos); |
| 2137 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2138 | != std::string::npos); |
| 2139 | |
| 2140 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2141 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2142 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2143 | } |
| 2144 | } |
| 2145 | |
| 2146 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2147 | LocalHttpTestServer test_server; |
| 2148 | ASSERT_TRUE(test_server.Start()); |
| 2149 | |
| 2150 | // Set up a cookie. |
| 2151 | { |
| 2152 | TestNetworkDelegate network_delegate; |
| 2153 | default_context_.set_network_delegate(&network_delegate); |
| 2154 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2155 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2156 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
| 2157 | &d, NULL)); |
| 2158 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2159 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2160 | |
| 2161 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2162 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2163 | } |
| 2164 | |
| 2165 | // Verify that the cookie is set. |
| 2166 | { |
| 2167 | TestNetworkDelegate network_delegate; |
| 2168 | default_context_.set_network_delegate(&network_delegate); |
| 2169 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2170 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2171 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2172 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2173 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2174 | |
| 2175 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2176 | != std::string::npos); |
| 2177 | |
| 2178 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2179 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2180 | } |
| 2181 | |
| 2182 | // Verify that the cookie isn't sent. |
| 2183 | { |
| 2184 | TestNetworkDelegate network_delegate; |
| 2185 | default_context_.set_network_delegate(&network_delegate); |
| 2186 | TestDelegate d; |
| 2187 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2188 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2189 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2190 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2191 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2192 | |
| 2193 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2194 | == std::string::npos); |
| 2195 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2196 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2197 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2202 | LocalHttpTestServer test_server; |
| 2203 | ASSERT_TRUE(test_server.Start()); |
| 2204 | |
| 2205 | // Set up a cookie. |
| 2206 | { |
| 2207 | TestNetworkDelegate network_delegate; |
| 2208 | default_context_.set_network_delegate(&network_delegate); |
| 2209 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2210 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2211 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
| 2212 | &d, NULL)); |
| 2213 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2214 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2215 | |
| 2216 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2217 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2218 | } |
| 2219 | |
| 2220 | // Try to set-up another cookie and update the previous cookie. |
| 2221 | { |
| 2222 | TestNetworkDelegate network_delegate; |
| 2223 | default_context_.set_network_delegate(&network_delegate); |
| 2224 | TestDelegate d; |
| 2225 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2226 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2227 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2228 | DEFAULT_PRIORITY, &d, NULL)); |
| 2229 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2230 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2231 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2232 | |
| 2233 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2234 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2235 | } |
| 2236 | |
| 2237 | // Verify the cookies weren't saved or updated. |
| 2238 | { |
| 2239 | TestNetworkDelegate network_delegate; |
| 2240 | default_context_.set_network_delegate(&network_delegate); |
| 2241 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2242 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2243 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2244 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2245 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2246 | |
| 2247 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2248 | == std::string::npos); |
| 2249 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2250 | != std::string::npos); |
| 2251 | |
| 2252 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2253 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2258 | LocalHttpTestServer test_server; |
| 2259 | ASSERT_TRUE(test_server.Start()); |
| 2260 | |
| 2261 | // Set up an empty cookie. |
| 2262 | { |
| 2263 | TestNetworkDelegate network_delegate; |
| 2264 | default_context_.set_network_delegate(&network_delegate); |
| 2265 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2266 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2267 | test_server.GetURL("set-cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2268 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2269 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2270 | |
| 2271 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2272 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2273 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2278 | LocalHttpTestServer test_server; |
| 2279 | ASSERT_TRUE(test_server.Start()); |
| 2280 | |
| 2281 | // Set up a cookie. |
| 2282 | { |
| 2283 | TestNetworkDelegate network_delegate; |
| 2284 | default_context_.set_network_delegate(&network_delegate); |
| 2285 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2286 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2287 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
| 2288 | &d, NULL)); |
| 2289 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2290 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2291 | |
| 2292 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2293 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2294 | } |
| 2295 | |
| 2296 | // Verify that the cookie is set. |
| 2297 | { |
| 2298 | TestNetworkDelegate network_delegate; |
| 2299 | default_context_.set_network_delegate(&network_delegate); |
| 2300 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2301 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2302 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2303 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2304 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2305 | |
| 2306 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2307 | != std::string::npos); |
| 2308 | |
| 2309 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2310 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2311 | } |
| 2312 | |
| 2313 | // Verify that the cookie isn't sent. |
| 2314 | { |
| 2315 | TestNetworkDelegate network_delegate; |
| 2316 | default_context_.set_network_delegate(&network_delegate); |
| 2317 | TestDelegate d; |
| 2318 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2319 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2320 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2321 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2322 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2323 | |
| 2324 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2325 | == std::string::npos); |
| 2326 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2327 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2328 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2333 | LocalHttpTestServer test_server; |
| 2334 | ASSERT_TRUE(test_server.Start()); |
| 2335 | |
| 2336 | // Set up a cookie. |
| 2337 | { |
| 2338 | TestNetworkDelegate network_delegate; |
| 2339 | default_context_.set_network_delegate(&network_delegate); |
| 2340 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2341 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2342 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
| 2343 | &d, NULL)); |
| 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_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2348 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2349 | } |
| 2350 | |
| 2351 | // Try to set-up another cookie and update the previous cookie. |
| 2352 | { |
| 2353 | TestNetworkDelegate network_delegate; |
| 2354 | default_context_.set_network_delegate(&network_delegate); |
| 2355 | TestDelegate d; |
| 2356 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2357 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2358 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2359 | DEFAULT_PRIORITY, &d, NULL)); |
| 2360 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2361 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2362 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2363 | |
| 2364 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2365 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2366 | } |
| 2367 | |
| 2368 | // Verify the cookies weren't saved or updated. |
| 2369 | { |
| 2370 | TestNetworkDelegate network_delegate; |
| 2371 | default_context_.set_network_delegate(&network_delegate); |
| 2372 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2373 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2374 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2375 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2376 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2377 | |
| 2378 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2379 | == std::string::npos); |
| 2380 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2381 | != std::string::npos); |
| 2382 | |
| 2383 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2384 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2385 | } |
| 2386 | } |
| 2387 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2388 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 2389 | // value for the |fixed_date| argument given to the constructor. |
| 2390 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 2391 | public: |
| 2392 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 2393 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 2394 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2395 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2396 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 2397 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2398 | URLRequest* request, |
| 2399 | const CompletionCallback& callback, |
| 2400 | const HttpResponseHeaders* original_response_headers, |
| 2401 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 2402 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2403 | |
| 2404 | private: |
| 2405 | std::string fixed_date_; |
| 2406 | |
| 2407 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 2408 | }; |
| 2409 | |
| 2410 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2411 | URLRequest* request, |
| 2412 | const CompletionCallback& callback, |
| 2413 | const HttpResponseHeaders* original_response_headers, |
| 2414 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2415 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2416 | HttpResponseHeaders* new_response_headers = |
| 2417 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2418 | |
| 2419 | new_response_headers->RemoveHeader("Date"); |
| 2420 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 2421 | |
| 2422 | *override_response_headers = new_response_headers; |
| 2423 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 2424 | callback, |
| 2425 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2426 | override_response_headers, |
| 2427 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | // Test that cookie expiration times are adjusted for server/client clock |
| 2431 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 2432 | // headers by defaulting to GMT. (crbug.com/135131) |
| 2433 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 2434 | LocalHttpTestServer test_server; |
| 2435 | ASSERT_TRUE(test_server.Start()); |
| 2436 | |
| 2437 | // Set up an expired cookie. |
| 2438 | { |
| 2439 | TestNetworkDelegate network_delegate; |
| 2440 | default_context_.set_network_delegate(&network_delegate); |
| 2441 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2442 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2443 | test_server.GetURL( |
| 2444 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2445 | DEFAULT_PRIORITY, &d, NULL)); |
| 2446 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2447 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2448 | } |
| 2449 | // Verify that the cookie is not set. |
| 2450 | { |
| 2451 | TestNetworkDelegate network_delegate; |
| 2452 | default_context_.set_network_delegate(&network_delegate); |
| 2453 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2454 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2455 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2456 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2457 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2458 | |
| 2459 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 2460 | } |
| 2461 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 2462 | { |
| 2463 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 2464 | default_context_.set_network_delegate(&network_delegate); |
| 2465 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2466 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2467 | test_server.GetURL( |
| 2468 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2469 | DEFAULT_PRIORITY, &d, NULL)); |
| 2470 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2471 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2472 | } |
| 2473 | // Verify that the cookie is set. |
| 2474 | { |
| 2475 | TestNetworkDelegate network_delegate; |
| 2476 | default_context_.set_network_delegate(&network_delegate); |
| 2477 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2478 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2479 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2480 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2481 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2482 | |
| 2483 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2488 | // Check that it is impossible to change the referrer in the extra headers of |
| 2489 | // an URLRequest. |
| 2490 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 2491 | LocalHttpTestServer test_server; |
| 2492 | ASSERT_TRUE(test_server.Start()); |
| 2493 | |
| 2494 | // If extra headers contain referer and the request contains a referer, |
| 2495 | // only the latter shall be respected. |
| 2496 | { |
| 2497 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2498 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2499 | test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
| 2500 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2501 | |
| 2502 | HttpRequestHeaders headers; |
| 2503 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2504 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2505 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2506 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2507 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2508 | |
| 2509 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 2510 | } |
| 2511 | |
| 2512 | // If extra headers contain a referer but the request does not, no referer |
| 2513 | // shall be sent in the header. |
| 2514 | { |
| 2515 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2516 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2517 | test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2518 | |
| 2519 | HttpRequestHeaders headers; |
| 2520 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2521 | req->SetExtraRequestHeaders(headers); |
| 2522 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2523 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2524 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2525 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2526 | |
| 2527 | EXPECT_EQ("None", d.data_received()); |
| 2528 | } |
| 2529 | } |
| 2530 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2531 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2532 | public: |
| 2533 | URLRequestTestHTTP() |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 2534 | : test_server_(base::FilePath(FILE_PATH_LITERAL( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2535 | "net/data/url_request_unittest"))) { |
| 2536 | } |
| 2537 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2538 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2539 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 2540 | // |request_method| is the method to use for the initial request. |
| 2541 | // |redirect_method| is the method that is expected to be used for the second |
| 2542 | // request, after redirection. |
| 2543 | // If |include_data| is true, data is uploaded with the request. The |
| 2544 | // response body is expected to match it exactly, if and only if |
| 2545 | // |request_method| == |redirect_method|. |
| 2546 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 2547 | const std::string& request_method, |
| 2548 | const std::string& redirect_method, |
| 2549 | bool include_data) { |
| 2550 | static const char kData[] = "hello world"; |
| 2551 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2552 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2553 | redirect_url, DEFAULT_PRIORITY, &d, NULL)); |
| 2554 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2555 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 2556 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2557 | HttpRequestHeaders headers; |
| 2558 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 2559 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2560 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2561 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2562 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2563 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2564 | EXPECT_EQ(redirect_method, req->method()); |
| 2565 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 2566 | EXPECT_EQ(OK, req->status().error()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2567 | if (include_data) { |
| 2568 | if (request_method == redirect_method) { |
| 2569 | EXPECT_EQ(kData, d.data_received()); |
| 2570 | } else { |
| 2571 | EXPECT_NE(kData, d.data_received()); |
| 2572 | } |
| 2573 | } |
| 2574 | if (HasFailure()) |
| 2575 | LOG(WARNING) << "Request method was: " << request_method; |
| 2576 | } |
| 2577 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2578 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2579 | const int kMsgSize = 20000; // multiple of 10 |
| 2580 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 2581 | char* uploadBytes = new char[kMsgSize+1]; |
| 2582 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2583 | char marker = 'a'; |
| 2584 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 2585 | memcpy(ptr, "----------", 10); |
| 2586 | ptr += 10; |
| 2587 | if (idx % 100 == 0) { |
| 2588 | ptr--; |
| 2589 | *ptr++ = marker; |
| 2590 | if (++marker > 'z') |
| 2591 | marker = 'a'; |
| 2592 | } |
| 2593 | } |
| 2594 | uploadBytes[kMsgSize] = '\0'; |
| 2595 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2596 | for (int i = 0; i < kIterations; ++i) { |
| 2597 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2598 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 2599 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 2600 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2601 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 2602 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2603 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2604 | r->Start(); |
| 2605 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2606 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2607 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2608 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2609 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2610 | << "request failed: " << r->status().status() |
| 2611 | << ", os error: " << r->status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2612 | |
| 2613 | EXPECT_FALSE(d.received_data_before_response()); |
| 2614 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2615 | } |
| 2616 | delete[] uploadBytes; |
| 2617 | } |
| 2618 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2619 | void AddChunksToUpload(URLRequest* r) { |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 2620 | r->AppendChunkToUpload("a", 1, false); |
| 2621 | r->AppendChunkToUpload("bcd", 3, false); |
| 2622 | r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); |
| 2623 | r->AppendChunkToUpload("\r\n\r\n", 4, false); |
| 2624 | r->AppendChunkToUpload("0", 1, false); |
| 2625 | r->AppendChunkToUpload("2323", 4, true); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2626 | } |
| 2627 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2628 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2629 | // This should match the chunks sent by AddChunksToUpload(). |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2630 | const std::string expected_data = |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2631 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 2632 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2633 | ASSERT_EQ(1, d->response_started_count()) |
| 2634 | << "request failed: " << r->status().status() |
| 2635 | << ", os error: " << r->status().error(); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2636 | |
| 2637 | EXPECT_FALSE(d->received_data_before_response()); |
| 2638 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2639 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 2640 | EXPECT_EQ(expected_data, d->data_received()); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2641 | } |
| 2642 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2643 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2644 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2645 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 2646 | test_server_.GetURL("set-many-cookies?" + |
| 2647 | base::IntToString(num_cookies)), |
| 2648 | DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2649 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2650 | r->Start(); |
| 2651 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2652 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2653 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2654 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2655 | bool is_success = r->status().is_success(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2656 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2657 | if (!is_success) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2658 | EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2659 | // The test server appears to be unable to handle subsequent requests |
| 2660 | // after this error is triggered. Force it to restart. |
| 2661 | EXPECT_TRUE(test_server_.Stop()); |
| 2662 | EXPECT_TRUE(test_server_.Start()); |
| 2663 | } |
| 2664 | |
| 2665 | return is_success; |
| 2666 | } |
| 2667 | |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2668 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2669 | }; |
| 2670 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2671 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 2672 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 2673 | // The HTTPTestServer will return a 302 response, which we should not |
| 2674 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2675 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2676 | ASSERT_TRUE(test_server_.Start()); |
| 2677 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2678 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2679 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2680 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2681 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2682 | TestDelegate d; |
| 2683 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2684 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2685 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, NULL)); |
| 2686 | r->Start(); |
| 2687 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2688 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2689 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2690 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2691 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2692 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2693 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 2694 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 2695 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2696 | // We should not have followed the redirect. |
| 2697 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2698 | } |
| 2699 | } |
| 2700 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2701 | // This is the same as the previous test, but checks that the network delegate |
| 2702 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 2703 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2704 | ASSERT_TRUE(test_server_.Start()); |
| 2705 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2706 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2707 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2708 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2709 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2710 | TestDelegate d; |
| 2711 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2712 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2713 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, NULL)); |
| 2714 | r->Start(); |
| 2715 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2716 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2717 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2718 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2719 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2720 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2721 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 2722 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2723 | EXPECT_EQ(1, d.response_started_count()); |
| 2724 | // We should not have followed the redirect. |
| 2725 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2726 | |
| 2727 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2728 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2729 | } |
| 2730 | } |
| 2731 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2732 | // Tests that we can block and asynchronously return OK in various stages. |
| 2733 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 2734 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 2735 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 2736 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 2737 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 2738 | }; |
| 2739 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 2740 | |
| 2741 | ASSERT_TRUE(test_server_.Start()); |
| 2742 | |
| 2743 | TestDelegate d; |
| 2744 | BlockingNetworkDelegate network_delegate( |
| 2745 | BlockingNetworkDelegate::USER_CALLBACK); |
| 2746 | network_delegate.set_block_on( |
| 2747 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 2748 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 2749 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 2750 | |
| 2751 | TestURLRequestContext context(true); |
| 2752 | context.set_network_delegate(&network_delegate); |
| 2753 | context.Init(); |
| 2754 | |
| 2755 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2756 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2757 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2758 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2759 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2760 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2761 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2762 | EXPECT_EQ(blocking_stages[i], |
| 2763 | network_delegate.stage_blocked_for_callback()); |
| 2764 | network_delegate.DoCallback(OK); |
| 2765 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2766 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2767 | EXPECT_EQ(200, r->GetResponseCode()); |
| 2768 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2769 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2770 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2771 | } |
| 2772 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2773 | } |
| 2774 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2775 | // Tests that the network delegate can block and cancel a request. |
| 2776 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
| 2777 | ASSERT_TRUE(test_server_.Start()); |
| 2778 | |
| 2779 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2780 | BlockingNetworkDelegate network_delegate( |
| 2781 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2782 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 2783 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2784 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2785 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2786 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2787 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2788 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2789 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2790 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2791 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2792 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2793 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2794 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2795 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2796 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2797 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 2798 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2799 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2800 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2801 | } |
| 2802 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2803 | } |
| 2804 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2805 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 2806 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 2807 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 2808 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 2809 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 2810 | BlockingNetworkDelegate::Stage stage, |
| 2811 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2812 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2813 | BlockingNetworkDelegate network_delegate(block_mode); |
| 2814 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 2815 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2816 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2817 | TestURLRequestContext context(true); |
| 2818 | context.set_network_delegate(&network_delegate); |
| 2819 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2820 | |
| 2821 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2822 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2823 | url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2824 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2825 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2826 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2827 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2828 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2829 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2830 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 2831 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2832 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2833 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2834 | } |
| 2835 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2836 | } |
| 2837 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2838 | // The following 3 tests check that the network delegate can cancel a request |
| 2839 | // synchronously in various stages of the request. |
| 2840 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
| 2841 | ASSERT_TRUE(test_server_.Start()); |
| 2842 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2843 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2844 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
| 2848 | ASSERT_TRUE(test_server_.Start()); |
| 2849 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2850 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2851 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
| 2855 | ASSERT_TRUE(test_server_.Start()); |
| 2856 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2857 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2858 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | // The following 3 tests check that the network delegate can cancel a request |
| 2862 | // asynchronously in various stages of the request. |
| 2863 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
| 2864 | ASSERT_TRUE(test_server_.Start()); |
| 2865 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2866 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2867 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2868 | } |
| 2869 | |
| 2870 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
| 2871 | ASSERT_TRUE(test_server_.Start()); |
| 2872 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2873 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2874 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2875 | } |
| 2876 | |
| 2877 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
| 2878 | ASSERT_TRUE(test_server_.Start()); |
| 2879 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2880 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2881 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2882 | } |
| 2883 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2884 | // Tests that the network delegate can block and redirect a request to a new |
| 2885 | // URL. |
| 2886 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
| 2887 | ASSERT_TRUE(test_server_.Start()); |
| 2888 | |
| 2889 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2890 | BlockingNetworkDelegate network_delegate( |
| 2891 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2892 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2893 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2894 | network_delegate.set_redirect_url(redirect_url); |
| 2895 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2896 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2897 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2898 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2899 | { |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 2900 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2901 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2902 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2903 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2904 | // Quit after hitting the redirect, so can check the headers. |
| 2905 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2906 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2907 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2908 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2909 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2910 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 2911 | EXPECT_EQ(307, r->GetResponseCode()); |
| 2912 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2913 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2914 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 2915 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2916 | EXPECT_EQ(redirect_url, GURL(location)); |
| 2917 | |
| 2918 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2919 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2920 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2921 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 2922 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 2923 | EXPECT_EQ( |
| 2924 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 2925 | EXPECT_TRUE( |
| 2926 | network_delegate.last_observed_proxy().Equals( |
| 2927 | test_server_.host_port_pair())); |
| 2928 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2929 | EXPECT_EQ(0, r->status().error()); |
| 2930 | EXPECT_EQ(redirect_url, r->url()); |
| 2931 | EXPECT_EQ(original_url, r->original_url()); |
| 2932 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2933 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2934 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2935 | } |
| 2936 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2937 | } |
| 2938 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2939 | // Tests that the network delegate can block and redirect a request to a new |
| 2940 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 2941 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
| 2942 | ASSERT_TRUE(test_server_.Start()); |
| 2943 | |
| 2944 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2945 | BlockingNetworkDelegate network_delegate( |
| 2946 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2947 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2948 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2949 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2950 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2951 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2952 | |
| 2953 | { |
| 2954 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2955 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2956 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2957 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2958 | // Quit after hitting the redirect, so can check the headers. |
| 2959 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2960 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2961 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2962 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2963 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2964 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 2965 | EXPECT_EQ(307, r->GetResponseCode()); |
| 2966 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2967 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2968 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 2969 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2970 | EXPECT_EQ(redirect_url, GURL(location)); |
| 2971 | |
| 2972 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2973 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2974 | base::RunLoop().Run(); |
| 2975 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2976 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 2977 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 2978 | EXPECT_EQ( |
| 2979 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 2980 | EXPECT_TRUE( |
| 2981 | network_delegate.last_observed_proxy().Equals( |
| 2982 | test_server_.host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2983 | EXPECT_EQ(0, r->status().error()); |
| 2984 | EXPECT_EQ(redirect_url, r->url()); |
| 2985 | EXPECT_EQ(original_url, r->original_url()); |
| 2986 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2987 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2988 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2989 | } |
| 2990 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2991 | } |
| 2992 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2993 | // Tests that redirects caused by the network delegate preserve POST data. |
| 2994 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 2995 | ASSERT_TRUE(test_server_.Start()); |
| 2996 | |
| 2997 | const char kData[] = "hello world"; |
| 2998 | |
| 2999 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3000 | BlockingNetworkDelegate network_delegate( |
| 3001 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3002 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3003 | GURL redirect_url(test_server_.GetURL("echo")); |
| 3004 | network_delegate.set_redirect_url(redirect_url); |
| 3005 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3006 | TestURLRequestContext context(true); |
| 3007 | context.set_network_delegate(&network_delegate); |
| 3008 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3009 | |
| 3010 | { |
| 3011 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3012 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3013 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 3014 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3015 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3016 | HttpRequestHeaders headers; |
| 3017 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3018 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3019 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3020 | |
| 3021 | // Quit after hitting the redirect, so can check the headers. |
| 3022 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3023 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3024 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3025 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3026 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3027 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3028 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3029 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3030 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3031 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3032 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3033 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3034 | |
| 3035 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3036 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3037 | base::RunLoop().Run(); |
| 3038 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3039 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3040 | EXPECT_EQ(0, r->status().error()); |
| 3041 | EXPECT_EQ(redirect_url, r->url()); |
| 3042 | EXPECT_EQ(original_url, r->original_url()); |
| 3043 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3044 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3045 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3046 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3047 | EXPECT_EQ(kData, d.data_received()); |
| 3048 | } |
| 3049 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3050 | } |
| 3051 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3052 | // Tests that the network delegate can block and redirect a request to a new |
| 3053 | // URL during OnHeadersReceived. |
| 3054 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
| 3055 | ASSERT_TRUE(test_server_.Start()); |
| 3056 | |
| 3057 | TestDelegate d; |
| 3058 | BlockingNetworkDelegate network_delegate( |
| 3059 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3060 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3061 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 3062 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 3063 | |
| 3064 | TestURLRequestContextWithProxy context( |
| 3065 | test_server_.host_port_pair().ToString(), &network_delegate); |
| 3066 | |
| 3067 | { |
| 3068 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3069 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3070 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3071 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3072 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3073 | base::RunLoop().Run(); |
| 3074 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3075 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3076 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3077 | EXPECT_EQ( |
| 3078 | 2, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 3079 | EXPECT_TRUE( |
| 3080 | network_delegate.last_observed_proxy().Equals( |
| 3081 | test_server_.host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3082 | |
| 3083 | EXPECT_EQ(OK, r->status().error()); |
| 3084 | EXPECT_EQ(redirect_url, r->url()); |
| 3085 | EXPECT_EQ(original_url, r->original_url()); |
| 3086 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3087 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 3088 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3089 | } |
| 3090 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3091 | } |
| 3092 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3093 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3094 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 3095 | // handle the challenge, and is passing the buck along to the |
| 3096 | // URLRequest::Delegate. |
| 3097 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 3098 | ASSERT_TRUE(test_server_.Start()); |
| 3099 | |
| 3100 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3101 | BlockingNetworkDelegate network_delegate( |
| 3102 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3103 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3104 | TestURLRequestContext context(true); |
| 3105 | context.set_network_delegate(&network_delegate); |
| 3106 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3107 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3108 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3109 | |
| 3110 | { |
| 3111 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3112 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3113 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3114 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3115 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3116 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3117 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3118 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3119 | EXPECT_EQ(0, r->status().error()); |
| 3120 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3121 | EXPECT_TRUE(d.auth_required_called()); |
| 3122 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3123 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3124 | } |
| 3125 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3126 | } |
| 3127 | |
| 3128 | TEST_F(URLRequestTestHTTP, |
| 3129 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
| 3130 | ASSERT_TRUE(test_server_.Start()); |
| 3131 | |
| 3132 | TestDelegate d; |
| 3133 | BlockingNetworkDelegate network_delegate( |
| 3134 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3135 | |
| 3136 | TestURLRequestContext context(true); |
| 3137 | context.set_network_delegate(&network_delegate); |
| 3138 | context.Init(); |
| 3139 | |
| 3140 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3141 | |
| 3142 | { |
| 3143 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3144 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3145 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3146 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3147 | |
| 3148 | { |
| 3149 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3150 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3151 | EXPECT_FALSE(headers.HasHeader("Authorization")); |
| 3152 | } |
| 3153 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3154 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3155 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3156 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3157 | EXPECT_EQ(0, r->status().error()); |
| 3158 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3159 | EXPECT_TRUE(d.auth_required_called()); |
| 3160 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3161 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3162 | } |
| 3163 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3164 | } |
| 3165 | |
| 3166 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 3167 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3168 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 3169 | ASSERT_TRUE(test_server_.Start()); |
| 3170 | |
| 3171 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3172 | BlockingNetworkDelegate network_delegate( |
| 3173 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3174 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3175 | network_delegate.set_auth_retval( |
| 3176 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3177 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3178 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3179 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3180 | TestURLRequestContext context(true); |
| 3181 | context.set_network_delegate(&network_delegate); |
| 3182 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3183 | |
| 3184 | { |
| 3185 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3186 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3187 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3188 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3189 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3190 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3191 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3192 | EXPECT_EQ(0, r->status().error()); |
| 3193 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3194 | EXPECT_FALSE(d.auth_required_called()); |
| 3195 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3196 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3197 | } |
| 3198 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3199 | } |
| 3200 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3201 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 3202 | // headers (for the first or second request) when called at the proper times. |
| 3203 | TEST_F(URLRequestTestHTTP, |
| 3204 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
| 3205 | ASSERT_TRUE(test_server_.Start()); |
| 3206 | |
| 3207 | TestDelegate d; |
| 3208 | BlockingNetworkDelegate network_delegate( |
| 3209 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3210 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 3211 | network_delegate.set_auth_retval( |
| 3212 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3213 | |
| 3214 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 3215 | |
| 3216 | TestURLRequestContext context(true); |
| 3217 | context.set_network_delegate(&network_delegate); |
| 3218 | context.Init(); |
| 3219 | |
| 3220 | { |
| 3221 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3222 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3223 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3224 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3225 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3226 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3227 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3228 | EXPECT_EQ(0, r->status().error()); |
| 3229 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3230 | EXPECT_FALSE(d.auth_required_called()); |
| 3231 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3232 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3233 | |
| 3234 | { |
| 3235 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3236 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3237 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 3238 | } |
| 3239 | } |
| 3240 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3241 | } |
| 3242 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3243 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3244 | // by cancelling authentication. |
| 3245 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 3246 | ASSERT_TRUE(test_server_.Start()); |
| 3247 | |
| 3248 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3249 | BlockingNetworkDelegate network_delegate( |
| 3250 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3251 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3252 | network_delegate.set_auth_retval( |
| 3253 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3254 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3255 | TestURLRequestContext context(true); |
| 3256 | context.set_network_delegate(&network_delegate); |
| 3257 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3258 | |
| 3259 | { |
| 3260 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3261 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3262 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3263 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3264 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3265 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3266 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3267 | EXPECT_EQ(OK, r->status().error()); |
| 3268 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3269 | EXPECT_FALSE(d.auth_required_called()); |
| 3270 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3271 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3272 | } |
| 3273 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3274 | } |
| 3275 | |
| 3276 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3277 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 3278 | // to handle the challenge, and is passing the buck along to the |
| 3279 | // URLRequest::Delegate. |
| 3280 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 3281 | ASSERT_TRUE(test_server_.Start()); |
| 3282 | |
| 3283 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3284 | BlockingNetworkDelegate network_delegate( |
| 3285 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3286 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3287 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3288 | TestURLRequestContext context(true); |
| 3289 | context.set_network_delegate(&network_delegate); |
| 3290 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3291 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3292 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3293 | |
| 3294 | { |
| 3295 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3296 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3297 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3298 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3299 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3300 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3301 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3302 | EXPECT_EQ(0, r->status().error()); |
| 3303 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3304 | EXPECT_TRUE(d.auth_required_called()); |
| 3305 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3306 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3307 | } |
| 3308 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3309 | } |
| 3310 | |
| 3311 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3312 | // by setting credentials. |
| 3313 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 3314 | ASSERT_TRUE(test_server_.Start()); |
| 3315 | |
| 3316 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3317 | BlockingNetworkDelegate network_delegate( |
| 3318 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3319 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3320 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3321 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3322 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3323 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3324 | network_delegate.set_auth_credentials(auth_credentials); |
| 3325 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3326 | TestURLRequestContext context(true); |
| 3327 | context.set_network_delegate(&network_delegate); |
| 3328 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3329 | |
| 3330 | { |
| 3331 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3332 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3333 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3334 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3335 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3336 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3337 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3338 | EXPECT_EQ(0, r->status().error()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3339 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3340 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3341 | EXPECT_FALSE(d.auth_required_called()); |
| 3342 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3343 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3344 | } |
| 3345 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3346 | } |
| 3347 | |
| 3348 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3349 | // by cancelling authentication. |
| 3350 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 3351 | ASSERT_TRUE(test_server_.Start()); |
| 3352 | |
| 3353 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3354 | BlockingNetworkDelegate network_delegate( |
| 3355 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3356 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3357 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3358 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3359 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3360 | TestURLRequestContext context(true); |
| 3361 | context.set_network_delegate(&network_delegate); |
| 3362 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3363 | |
| 3364 | { |
| 3365 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3366 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3367 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3368 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3369 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3370 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3371 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3372 | EXPECT_EQ(OK, r->status().error()); |
| 3373 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3374 | EXPECT_FALSE(d.auth_required_called()); |
| 3375 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3376 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3377 | } |
| 3378 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3379 | } |
| 3380 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3381 | // Tests that we can handle when a network request was canceled while we were |
| 3382 | // waiting for the network delegate. |
| 3383 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 3384 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 3385 | ASSERT_TRUE(test_server_.Start()); |
| 3386 | |
| 3387 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3388 | BlockingNetworkDelegate network_delegate( |
| 3389 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3390 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3391 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3392 | TestURLRequestContext context(true); |
| 3393 | context.set_network_delegate(&network_delegate); |
| 3394 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3395 | |
| 3396 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3397 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3398 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3399 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3400 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3401 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3402 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3403 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3404 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3405 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3406 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3407 | // Ensure that network delegate is notified. |
| 3408 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3409 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3410 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3411 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3412 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3413 | } |
| 3414 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3415 | } |
| 3416 | |
| 3417 | // Tests that we can handle when a network request was canceled while we were |
| 3418 | // waiting for the network delegate. |
| 3419 | // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 3420 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
| 3421 | ASSERT_TRUE(test_server_.Start()); |
| 3422 | |
| 3423 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3424 | BlockingNetworkDelegate network_delegate( |
| 3425 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3426 | network_delegate.set_block_on( |
| 3427 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3428 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3429 | TestURLRequestContext context(true); |
| 3430 | context.set_network_delegate(&network_delegate); |
| 3431 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3432 | |
| 3433 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3434 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3435 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3436 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3437 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3438 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3439 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3440 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3441 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3442 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3443 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3444 | // Ensure that network delegate is notified. |
| 3445 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3446 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3447 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3448 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3449 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3450 | } |
| 3451 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3452 | } |
| 3453 | |
| 3454 | // Tests that we can handle when a network request was canceled while we were |
| 3455 | // waiting for the network delegate. |
| 3456 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 3457 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 3458 | ASSERT_TRUE(test_server_.Start()); |
| 3459 | |
| 3460 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3461 | BlockingNetworkDelegate network_delegate( |
| 3462 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3463 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3464 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3465 | TestURLRequestContext context(true); |
| 3466 | context.set_network_delegate(&network_delegate); |
| 3467 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3468 | |
| 3469 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3470 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3471 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3472 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3473 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3474 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3475 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 3476 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3477 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3478 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3479 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3480 | // Ensure that network delegate is notified. |
| 3481 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3482 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3483 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3484 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3485 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3486 | } |
| 3487 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3488 | } |
| 3489 | |
| 3490 | // Tests that we can handle when a network request was canceled while we were |
| 3491 | // waiting for the network delegate. |
| 3492 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 3493 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3494 | ASSERT_TRUE(test_server_.Start()); |
| 3495 | |
| 3496 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3497 | BlockingNetworkDelegate network_delegate( |
| 3498 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3499 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3500 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3501 | TestURLRequestContext context(true); |
| 3502 | context.set_network_delegate(&network_delegate); |
| 3503 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3504 | |
| 3505 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3506 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3507 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3508 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3509 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3510 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3511 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 3512 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3513 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3514 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3515 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3516 | // Ensure that network delegate is notified. |
| 3517 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3518 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3519 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3520 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3521 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3522 | } |
| 3523 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3524 | } |
| 3525 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3526 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 3527 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 3528 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3529 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3530 | ASSERT_TRUE(test_server_.Start()); |
| 3531 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3532 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3533 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3534 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3535 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3536 | TestDelegate d; |
| 3537 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3538 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3539 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3540 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3541 | r->Start(); |
| 3542 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3543 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3544 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3545 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3546 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3547 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3548 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3549 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3550 | } |
| 3551 | } |
| 3552 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3553 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3554 | ASSERT_TRUE(test_server_.Start()); |
| 3555 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3556 | TestDelegate d; |
| 3557 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3558 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3559 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3560 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3561 | r->Start(); |
| 3562 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3563 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3564 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3565 | |
| 3566 | EXPECT_EQ(1, d.response_started_count()); |
| 3567 | EXPECT_FALSE(d.received_data_before_response()); |
| 3568 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3569 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3570 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3571 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3572 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 3573 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 3574 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3575 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3576 | } |
| 3577 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3578 | // This test has the server send a large number of cookies to the client. |
| 3579 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 3580 | // search is used to estimate that maximum number of cookies that are accepted |
| 3581 | // by the browser. Beyond the maximum number, the request will fail with |
| 3582 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 3583 | #if defined(OS_WIN) |
| 3584 | // http://crbug.com/177916 |
| 3585 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 3586 | #else |
| 3587 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 3588 | #endif // defined(OS_WIN) |
| 3589 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3590 | ASSERT_TRUE(test_server_.Start()); |
| 3591 | |
| 3592 | int lower_bound = 0; |
| 3593 | int upper_bound = 1; |
| 3594 | |
| 3595 | // Double the number of cookies until the response header limits are |
| 3596 | // exceeded. |
| 3597 | while (DoManyCookiesRequest(upper_bound)) { |
| 3598 | lower_bound = upper_bound; |
| 3599 | upper_bound *= 2; |
| 3600 | ASSERT_LT(upper_bound, 1000000); |
| 3601 | } |
| 3602 | |
| 3603 | int tolerance = upper_bound * 0.005; |
| 3604 | if (tolerance < 2) |
| 3605 | tolerance = 2; |
| 3606 | |
| 3607 | // Perform a binary search to find the highest possible number of cookies, |
| 3608 | // within the desired tolerance. |
| 3609 | while (upper_bound - lower_bound >= tolerance) { |
| 3610 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 3611 | |
| 3612 | if (DoManyCookiesRequest(num_cookies)) |
| 3613 | lower_bound = num_cookies; |
| 3614 | else |
| 3615 | upper_bound = num_cookies; |
| 3616 | } |
| 3617 | // Success: the test did not crash. |
| 3618 | } |
| 3619 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3620 | TEST_F(URLRequestTestHTTP, GetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3621 | ASSERT_TRUE(test_server_.Start()); |
| 3622 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3623 | TestDelegate d; |
| 3624 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3625 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3626 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3627 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3628 | r->Start(); |
| 3629 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3630 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3631 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3632 | |
| 3633 | EXPECT_EQ(1, d.response_started_count()); |
| 3634 | EXPECT_FALSE(d.received_data_before_response()); |
| 3635 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3636 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3637 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3638 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3639 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3640 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 3641 | } |
| 3642 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3643 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
| 3644 | ASSERT_TRUE(test_server_.Start()); |
| 3645 | |
| 3646 | TestDelegate d; |
| 3647 | { |
| 3648 | GURL test_url(test_server_.GetURL(std::string())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3649 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3650 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3651 | |
| 3652 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3653 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3654 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3655 | r->Start(); |
| 3656 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3657 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3658 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3659 | |
| 3660 | EXPECT_EQ(1, d.response_started_count()); |
| 3661 | EXPECT_FALSE(d.received_data_before_response()); |
| 3662 | EXPECT_NE(0, d.bytes_received()); |
| 3663 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3664 | r->GetSocketAddress().host()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3665 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3666 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3667 | |
| 3668 | EXPECT_TRUE(d.have_full_request_headers()); |
| 3669 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 3670 | } |
| 3671 | } |
| 3672 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3673 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
| 3674 | ASSERT_TRUE(test_server_.Start()); |
| 3675 | |
| 3676 | TestDelegate d; |
| 3677 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3678 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3679 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3680 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3681 | r->Start(); |
| 3682 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3683 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3684 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3685 | |
| 3686 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3687 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3688 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 3689 | |
| 3690 | EXPECT_EQ(1, d.response_started_count()); |
| 3691 | EXPECT_FALSE(d.received_data_before_response()); |
| 3692 | EXPECT_NE(0, d.bytes_received()); |
| 3693 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3694 | r->GetSocketAddress().host()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3695 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3696 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3697 | } |
| 3698 | } |
| 3699 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3700 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
| 3701 | ASSERT_TRUE(test_server_.Start()); |
| 3702 | |
| 3703 | // Parameter that specifies the Content-Length field in the response: |
| 3704 | // C - Compressed length. |
| 3705 | // U - Uncompressed length. |
| 3706 | // L - Large length (larger than both C & U). |
| 3707 | // M - Medium length (between C & U). |
| 3708 | // S - Small length (smaller than both C & U). |
| 3709 | const char test_parameters[] = "CULMS"; |
| 3710 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 3711 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 3712 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3713 | // S has too little data, but we seem to accept it. |
| 3714 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 3715 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3716 | |
| 3717 | for (int i = 0; i < num_tests ; i++) { |
| 3718 | TestDelegate d; |
| 3719 | { |
| 3720 | std::string test_file = |
| 3721 | base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c", |
| 3722 | test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3723 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3724 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3725 | TestURLRequestContext context(true); |
| 3726 | context.set_network_delegate(&network_delegate); |
| 3727 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3728 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3729 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3730 | test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, NULL)); |
| 3731 | r->Start(); |
| 3732 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3733 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3734 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3735 | |
| 3736 | EXPECT_EQ(1, d.response_started_count()); |
| 3737 | EXPECT_FALSE(d.received_data_before_response()); |
| 3738 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3739 | << " status = " << r->status().status() |
| 3740 | << " error = " << r->status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3741 | if (test_expect_success[i]) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3742 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3743 | << " Parameter = \"" << test_file << "\""; |
| 3744 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3745 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 3746 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3747 | << " Parameter = \"" << test_file << "\""; |
| 3748 | } |
| 3749 | } |
| 3750 | } |
| 3751 | } |
| 3752 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3753 | TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3754 | ASSERT_TRUE(test_server_.Start()); |
| 3755 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 3756 | SpawnedTestServer https_test_server( |
| 3757 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3758 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3759 | ASSERT_TRUE(https_test_server.Start()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3760 | |
| 3761 | // An https server is sent a request with an https referer, |
| 3762 | // and responds with a redirect to an http url. The http |
| 3763 | // server should not be sent the referer. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3764 | GURL http_destination = test_server_.GetURL(std::string()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3765 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3766 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3767 | https_test_server.GetURL("server-redirect?" + http_destination.spec()), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3768 | DEFAULT_PRIORITY, &d, NULL)); |
| 3769 | req->SetReferrer("https://www.referrer.com/"); |
| 3770 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3771 | base::RunLoop().Run(); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3772 | |
| 3773 | EXPECT_EQ(1, d.response_started_count()); |
| 3774 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3775 | EXPECT_EQ(http_destination, req->url()); |
| 3776 | EXPECT_EQ(std::string(), req->referrer()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3777 | } |
| 3778 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3779 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
| 3780 | ASSERT_TRUE(test_server_.Start()); |
| 3781 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3782 | GURL destination_url = test_server_.GetURL(std::string()); |
| 3783 | GURL original_url = |
| 3784 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3785 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3786 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3787 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 3788 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3789 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3790 | |
| 3791 | EXPECT_EQ(1, d.response_started_count()); |
| 3792 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3793 | EXPECT_EQ(destination_url, req->url()); |
| 3794 | EXPECT_EQ(original_url, req->original_url()); |
| 3795 | ASSERT_EQ(2U, req->url_chain().size()); |
| 3796 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 3797 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3798 | |
| 3799 | LoadTimingInfo load_timing_info_before_redirect; |
| 3800 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 3801 | &load_timing_info_before_redirect)); |
| 3802 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 3803 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 3804 | |
| 3805 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3806 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3807 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 3808 | |
| 3809 | // Check that a new socket was used on redirect, since the server does not |
| 3810 | // supposed keep-alive sockets, and that the times before the redirect are |
| 3811 | // before the ones recorded for the second request. |
| 3812 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 3813 | load_timing_info.socket_log_id); |
| 3814 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 3815 | load_timing_info.connect_timing.connect_start); |
| 3816 | } |
| 3817 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3818 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
| 3819 | ASSERT_TRUE(test_server_.Start()); |
| 3820 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3821 | GURL destination_url = test_server_.GetURL(std::string()); |
| 3822 | GURL middle_redirect_url = |
| 3823 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3824 | GURL original_url = test_server_.GetURL( |
| 3825 | "server-redirect?" + middle_redirect_url.spec()); |
| 3826 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3827 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3828 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 3829 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3830 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3831 | |
| 3832 | EXPECT_EQ(1, d.response_started_count()); |
| 3833 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3834 | EXPECT_EQ(destination_url, req->url()); |
| 3835 | EXPECT_EQ(original_url, req->original_url()); |
| 3836 | ASSERT_EQ(3U, req->url_chain().size()); |
| 3837 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 3838 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 3839 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3840 | } |
| 3841 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3842 | // First and second pieces of information logged by delegates to URLRequests. |
| 3843 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 3844 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 3845 | |
| 3846 | // Logs delegate information to a URLRequest. The first string is logged |
| 3847 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 3848 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 3849 | // another asynchronous call is used to clear the delegate information |
| 3850 | // before calling a callback. The object then deletes itself. |
| 3851 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 3852 | public: |
| 3853 | typedef base::Callback<void()> Callback; |
| 3854 | |
| 3855 | // Each time delegate information is added to the URLRequest, the resulting |
| 3856 | // load state is checked. The expected load state after each request is |
| 3857 | // passed in as an argument. |
| 3858 | static void Run(URLRequest* url_request, |
| 3859 | LoadState expected_first_load_state, |
| 3860 | LoadState expected_second_load_state, |
| 3861 | LoadState expected_third_load_state, |
| 3862 | const Callback& callback) { |
| 3863 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 3864 | url_request, |
| 3865 | expected_first_load_state, |
| 3866 | expected_second_load_state, |
| 3867 | expected_third_load_state, |
| 3868 | callback); |
| 3869 | logger->Start(); |
| 3870 | } |
| 3871 | |
| 3872 | // Checks that the log entries, starting with log_position, contain the |
| 3873 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 3874 | // recorded. Returns the index of entry after the expected number of |
| 3875 | // events this logged, or entries.size() if there aren't enough entries. |
| 3876 | static size_t CheckDelegateInfo( |
| 3877 | const CapturingNetLog::CapturedEntryList& entries, size_t log_position) { |
| 3878 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 3879 | if (log_position + 3 >= entries.size()) { |
| 3880 | ADD_FAILURE() << "Not enough log entries"; |
| 3881 | return entries.size(); |
| 3882 | } |
| 3883 | std::string delegate_info; |
| 3884 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3885 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 3886 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 3887 | &delegate_info)); |
| 3888 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 3889 | |
| 3890 | ++log_position; |
| 3891 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3892 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 3893 | |
| 3894 | ++log_position; |
| 3895 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3896 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 3897 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 3898 | &delegate_info)); |
| 3899 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 3900 | |
| 3901 | ++log_position; |
| 3902 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3903 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 3904 | |
| 3905 | return log_position + 1; |
| 3906 | } |
| 3907 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 3908 | // Find delegate request begin and end messages for OnBeforeNetworkStart. |
| 3909 | // Returns the position of the end message. |
| 3910 | static size_t ExpectBeforeNetworkEvents( |
| 3911 | const CapturingNetLog::CapturedEntryList& entries, |
| 3912 | size_t log_position) { |
| 3913 | log_position = |
| 3914 | ExpectLogContainsSomewhereAfter(entries, |
| 3915 | log_position, |
| 3916 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 3917 | NetLog::PHASE_BEGIN); |
| 3918 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 3919 | entries[log_position + 1].type); |
| 3920 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase); |
| 3921 | return log_position + 1; |
| 3922 | } |
| 3923 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3924 | private: |
| 3925 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 3926 | |
| 3927 | AsyncDelegateLogger(URLRequest* url_request, |
| 3928 | LoadState expected_first_load_state, |
| 3929 | LoadState expected_second_load_state, |
| 3930 | LoadState expected_third_load_state, |
| 3931 | const Callback& callback) |
| 3932 | : url_request_(url_request), |
| 3933 | expected_first_load_state_(expected_first_load_state), |
| 3934 | expected_second_load_state_(expected_second_load_state), |
| 3935 | expected_third_load_state_(expected_third_load_state), |
| 3936 | callback_(callback) { |
| 3937 | } |
| 3938 | |
| 3939 | ~AsyncDelegateLogger() {} |
| 3940 | |
| 3941 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3942 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3943 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3944 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 3945 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
| 3946 | base::MessageLoop::current()->PostTask( |
| 3947 | FROM_HERE, |
| 3948 | base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
| 3949 | } |
| 3950 | |
| 3951 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3952 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3953 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3954 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 3955 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 3956 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 3957 | } else { |
| 3958 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 3959 | } |
| 3960 | base::MessageLoop::current()->PostTask( |
| 3961 | FROM_HERE, |
| 3962 | base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
| 3963 | } |
| 3964 | |
| 3965 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3966 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3967 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3968 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 3969 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 3970 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3971 | callback_.Run(); |
| 3972 | } |
| 3973 | |
| 3974 | URLRequest* url_request_; |
| 3975 | const int expected_first_load_state_; |
| 3976 | const int expected_second_load_state_; |
| 3977 | const int expected_third_load_state_; |
| 3978 | const Callback callback_; |
| 3979 | |
| 3980 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 3981 | }; |
| 3982 | |
| 3983 | // NetworkDelegate that logs delegate information before a request is started, |
| 3984 | // before headers are sent, when headers are read, and when auth information |
| 3985 | // is requested. Uses AsyncDelegateLogger. |
| 3986 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 3987 | public: |
| 3988 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 3989 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3990 | |
| 3991 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 3992 | int OnBeforeURLRequest(URLRequest* request, |
| 3993 | const CompletionCallback& callback, |
| 3994 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3995 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 3996 | return RunCallbackAsynchronously(request, callback); |
| 3997 | } |
| 3998 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 3999 | int OnBeforeSendHeaders(URLRequest* request, |
| 4000 | const CompletionCallback& callback, |
| 4001 | HttpRequestHeaders* headers) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4002 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 4003 | return RunCallbackAsynchronously(request, callback); |
| 4004 | } |
| 4005 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 4006 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4007 | URLRequest* request, |
| 4008 | const CompletionCallback& callback, |
| 4009 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4010 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4011 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4012 | TestNetworkDelegate::OnHeadersReceived(request, |
| 4013 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4014 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4015 | override_response_headers, |
| 4016 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4017 | return RunCallbackAsynchronously(request, callback); |
| 4018 | } |
| 4019 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 4020 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4021 | URLRequest* request, |
| 4022 | const AuthChallengeInfo& auth_info, |
| 4023 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4024 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4025 | AsyncDelegateLogger::Run( |
| 4026 | request, |
| 4027 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4028 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4029 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4030 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 4031 | callback, credentials)); |
| 4032 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 4033 | } |
| 4034 | |
| 4035 | private: |
| 4036 | static int RunCallbackAsynchronously( |
| 4037 | URLRequest* request, |
| 4038 | const CompletionCallback& callback) { |
| 4039 | AsyncDelegateLogger::Run( |
| 4040 | request, |
| 4041 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4042 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4043 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4044 | base::Bind(callback, OK)); |
| 4045 | return ERR_IO_PENDING; |
| 4046 | } |
| 4047 | |
| 4048 | static void SetAuthAndResume(const AuthCallback& callback, |
| 4049 | AuthCredentials* credentials) { |
| 4050 | *credentials = AuthCredentials(kUser, kSecret); |
| 4051 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4052 | } |
| 4053 | |
| 4054 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 4055 | }; |
| 4056 | |
| 4057 | // URLRequest::Delegate that logs delegate information when the headers |
| 4058 | // are received, when each read completes, and during redirects. Uses |
| 4059 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 4060 | // |
| 4061 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 4062 | // advancing to the next step in most cases, as well as cancellation. |
| 4063 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 4064 | public: |
| 4065 | enum CancelStage { |
| 4066 | NO_CANCEL = 0, |
| 4067 | CANCEL_ON_RECEIVED_REDIRECT, |
| 4068 | CANCEL_ON_RESPONSE_STARTED, |
| 4069 | CANCEL_ON_READ_COMPLETED |
| 4070 | }; |
| 4071 | |
| 4072 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 4073 | : cancel_stage_(cancel_stage) { |
| 4074 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 4075 | set_cancel_in_received_redirect(true); |
| 4076 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 4077 | set_cancel_in_response_started(true); |
| 4078 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 4079 | set_cancel_in_received_data(true); |
| 4080 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 4081 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4082 | |
| 4083 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 4084 | void OnReceivedRedirect(URLRequest* request, |
| 4085 | const RedirectInfo& redirect_info, |
| 4086 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4087 | *defer_redirect = true; |
| 4088 | AsyncDelegateLogger::Run( |
| 4089 | request, |
| 4090 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4091 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4092 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4093 | base::Bind( |
| 4094 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4095 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4096 | } |
| 4097 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 4098 | void OnResponseStarted(URLRequest* request) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4099 | AsyncDelegateLogger::Run( |
| 4100 | request, |
| 4101 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4102 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4103 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4104 | base::Bind( |
| 4105 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 4106 | base::Unretained(this), request)); |
| 4107 | } |
| 4108 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 4109 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4110 | AsyncDelegateLogger::Run( |
| 4111 | request, |
| 4112 | LOAD_STATE_IDLE, |
| 4113 | LOAD_STATE_IDLE, |
| 4114 | LOAD_STATE_IDLE, |
| 4115 | base::Bind( |
| 4116 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 4117 | base::Unretained(this), request, bytes_read)); |
| 4118 | } |
| 4119 | |
| 4120 | private: |
| 4121 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4122 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4123 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4124 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4125 | // FollowDeferredRedirect should not be called after cancellation. |
| 4126 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 4127 | return; |
| 4128 | if (!defer_redirect) |
| 4129 | request->FollowDeferredRedirect(); |
| 4130 | } |
| 4131 | |
| 4132 | void OnResponseStartedLoggingComplete(URLRequest* request) { |
| 4133 | // The parent class continues the request. |
| 4134 | TestDelegate::OnResponseStarted(request); |
| 4135 | } |
| 4136 | |
| 4137 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 4138 | // The parent class continues the request. |
| 4139 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 4140 | } |
| 4141 | |
| 4142 | const CancelStage cancel_stage_; |
| 4143 | |
| 4144 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 4145 | }; |
| 4146 | |
| 4147 | // Tests handling of delegate info before a request starts. |
| 4148 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
| 4149 | ASSERT_TRUE(test_server_.Start()); |
| 4150 | |
| 4151 | TestDelegate request_delegate; |
| 4152 | TestURLRequestContext context(true); |
| 4153 | context.set_network_delegate(NULL); |
| 4154 | context.set_net_log(&net_log_); |
| 4155 | context.Init(); |
| 4156 | |
| 4157 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4158 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4159 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &request_delegate, |
| 4160 | NULL)); |
| 4161 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4162 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4163 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4164 | |
| 4165 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4166 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4167 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4168 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4169 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4170 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4171 | |
| 4172 | base::RunLoop().Run(); |
| 4173 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4174 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4175 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | CapturingNetLog::CapturedEntryList entries; |
| 4179 | net_log_.GetEntries(&entries); |
| 4180 | size_t log_position = ExpectLogContainsSomewhereAfter( |
| 4181 | entries, |
| 4182 | 0, |
| 4183 | NetLog::TYPE_DELEGATE_INFO, |
| 4184 | NetLog::PHASE_BEGIN); |
| 4185 | |
| 4186 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 4187 | |
| 4188 | // Nothing else should add any delegate info to the request. |
| 4189 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4190 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4191 | } |
| 4192 | |
| 4193 | // Tests handling of delegate info from a network delegate. |
| 4194 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
| 4195 | ASSERT_TRUE(test_server_.Start()); |
| 4196 | |
| 4197 | TestDelegate request_delegate; |
| 4198 | AsyncLoggingNetworkDelegate network_delegate; |
| 4199 | TestURLRequestContext context(true); |
| 4200 | context.set_network_delegate(&network_delegate); |
| 4201 | context.set_net_log(&net_log_); |
| 4202 | context.Init(); |
| 4203 | |
| 4204 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4205 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4206 | test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &request_delegate, |
| 4207 | NULL)); |
| 4208 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4209 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4210 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4211 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4212 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4213 | base::RunLoop().Run(); |
| 4214 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4215 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4216 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4217 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4218 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4219 | } |
| 4220 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4221 | |
| 4222 | size_t log_position = 0; |
| 4223 | CapturingNetLog::CapturedEntryList entries; |
| 4224 | net_log_.GetEntries(&entries); |
| 4225 | for (size_t i = 0; i < 3; ++i) { |
| 4226 | log_position = ExpectLogContainsSomewhereAfter( |
| 4227 | entries, |
| 4228 | log_position + 1, |
| 4229 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4230 | NetLog::PHASE_BEGIN); |
| 4231 | |
| 4232 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4233 | log_position + 1); |
| 4234 | |
| 4235 | ASSERT_LT(log_position, entries.size()); |
| 4236 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4237 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4238 | |
| 4239 | if (i == 1) { |
| 4240 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4241 | entries, log_position + 1); |
| 4242 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4246 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4247 | } |
| 4248 | |
| 4249 | // Tests handling of delegate info from a network delegate in the case of an |
| 4250 | // HTTP redirect. |
| 4251 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
| 4252 | ASSERT_TRUE(test_server_.Start()); |
| 4253 | |
| 4254 | TestDelegate request_delegate; |
| 4255 | AsyncLoggingNetworkDelegate network_delegate; |
| 4256 | TestURLRequestContext context(true); |
| 4257 | context.set_network_delegate(&network_delegate); |
| 4258 | context.set_net_log(&net_log_); |
| 4259 | context.Init(); |
| 4260 | |
| 4261 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4262 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4263 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
| 4264 | &request_delegate, NULL)); |
| 4265 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4266 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4267 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4268 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4269 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4270 | base::RunLoop().Run(); |
| 4271 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4272 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4273 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4274 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4275 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4276 | } |
| 4277 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4278 | |
| 4279 | size_t log_position = 0; |
| 4280 | CapturingNetLog::CapturedEntryList entries; |
| 4281 | net_log_.GetEntries(&entries); |
| 4282 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
| 4283 | // OnBeforeSendHeaders, and OnHeadersReceived. |
| 4284 | for (size_t i = 0; i < 3; ++i) { |
| 4285 | log_position = ExpectLogContainsSomewhereAfter( |
| 4286 | entries, |
| 4287 | log_position + 1, |
| 4288 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4289 | NetLog::PHASE_BEGIN); |
| 4290 | |
| 4291 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4292 | log_position + 1); |
| 4293 | |
| 4294 | ASSERT_LT(log_position, entries.size()); |
| 4295 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4296 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4297 | |
| 4298 | if (i == 1) { |
| 4299 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4300 | entries, log_position + 1); |
| 4301 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | // The URLRequest::Delegate then gets informed about the redirect. |
| 4305 | log_position = ExpectLogContainsSomewhereAfter( |
| 4306 | entries, |
| 4307 | log_position + 1, |
| 4308 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4309 | NetLog::PHASE_BEGIN); |
| 4310 | |
| 4311 | // The NetworkDelegate logged information in the same three events as before. |
| 4312 | for (size_t i = 0; i < 3; ++i) { |
| 4313 | log_position = ExpectLogContainsSomewhereAfter( |
| 4314 | entries, |
| 4315 | log_position + 1, |
| 4316 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4317 | NetLog::PHASE_BEGIN); |
| 4318 | |
| 4319 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4320 | log_position + 1); |
| 4321 | |
| 4322 | ASSERT_LT(log_position, entries.size()); |
| 4323 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4324 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4325 | } |
| 4326 | |
| 4327 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4328 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4329 | } |
| 4330 | |
| 4331 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 4332 | // AUTH. |
| 4333 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
| 4334 | ASSERT_TRUE(test_server_.Start()); |
| 4335 | |
| 4336 | TestDelegate request_delegate; |
| 4337 | AsyncLoggingNetworkDelegate network_delegate; |
| 4338 | TestURLRequestContext context(true); |
| 4339 | context.set_network_delegate(&network_delegate); |
| 4340 | context.set_net_log(&net_log_); |
| 4341 | context.Init(); |
| 4342 | |
| 4343 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4344 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4345 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &request_delegate, |
| 4346 | NULL)); |
| 4347 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4348 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4349 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4350 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4351 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4352 | base::RunLoop().Run(); |
| 4353 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4354 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4355 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4356 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4357 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4358 | } |
| 4359 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4360 | |
| 4361 | size_t log_position = 0; |
| 4362 | CapturingNetLog::CapturedEntryList entries; |
| 4363 | net_log_.GetEntries(&entries); |
| 4364 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
| 4365 | // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in |
| 4366 | // OnBeforeURLRequest and OnBeforeSendHeaders. |
| 4367 | for (size_t i = 0; i < 6; ++i) { |
| 4368 | log_position = ExpectLogContainsSomewhereAfter( |
| 4369 | entries, |
| 4370 | log_position + 1, |
| 4371 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4372 | NetLog::PHASE_BEGIN); |
| 4373 | |
| 4374 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4375 | log_position + 1); |
| 4376 | |
| 4377 | ASSERT_LT(log_position, entries.size()); |
| 4378 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4379 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4380 | |
| 4381 | if (i == 1) { |
| 4382 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4383 | entries, log_position + 1); |
| 4384 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4388 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4389 | } |
| 4390 | |
| 4391 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 4392 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
| 4393 | ASSERT_TRUE(test_server_.Start()); |
| 4394 | |
| 4395 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4396 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4397 | TestURLRequestContext context(true); |
| 4398 | context.set_network_delegate(NULL); |
| 4399 | context.set_net_log(&net_log_); |
| 4400 | context.Init(); |
| 4401 | |
| 4402 | { |
| 4403 | // A chunked response with delays between chunks is used to make sure that |
| 4404 | // attempts by the URLRequest delegate to log information while reading the |
| 4405 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4406 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4407 | // that it occurs. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4408 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4409 | test_server_.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
| 4410 | &request_delegate, NULL)); |
| 4411 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4412 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4413 | base::RunLoop().Run(); |
| 4414 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4415 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4416 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | CapturingNetLog::CapturedEntryList entries; |
| 4420 | net_log_.GetEntries(&entries); |
| 4421 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4422 | size_t log_position = 0; |
| 4423 | |
| 4424 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4425 | entries, log_position); |
| 4426 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4427 | // The delegate info should only have been logged on header complete. Other |
| 4428 | // times it should silently be ignored. |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4429 | log_position = |
| 4430 | ExpectLogContainsSomewhereAfter(entries, |
| 4431 | log_position + 1, |
| 4432 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4433 | NetLog::PHASE_BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4434 | |
| 4435 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4436 | log_position + 1); |
| 4437 | |
| 4438 | ASSERT_LT(log_position, entries.size()); |
| 4439 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4440 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4441 | |
| 4442 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4443 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4444 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4445 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4446 | } |
| 4447 | |
| 4448 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4449 | // an HTTP redirect. |
| 4450 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
| 4451 | ASSERT_TRUE(test_server_.Start()); |
| 4452 | |
| 4453 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4454 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4455 | TestURLRequestContext context(true); |
| 4456 | context.set_network_delegate(NULL); |
| 4457 | context.set_net_log(&net_log_); |
| 4458 | context.Init(); |
| 4459 | |
| 4460 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4461 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4462 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
| 4463 | &request_delegate, NULL)); |
| 4464 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4465 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4466 | base::RunLoop().Run(); |
| 4467 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4468 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4469 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | CapturingNetLog::CapturedEntryList entries; |
| 4473 | net_log_.GetEntries(&entries); |
| 4474 | |
| 4475 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 4476 | // OnResponseStarted. |
| 4477 | size_t log_position = 0; |
| 4478 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4479 | if (i == 0) { |
| 4480 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4481 | entries, log_position) + 1; |
| 4482 | } |
| 4483 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4484 | log_position = ExpectLogContainsSomewhereAfter( |
| 4485 | entries, |
| 4486 | log_position, |
| 4487 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4488 | NetLog::PHASE_BEGIN); |
| 4489 | |
| 4490 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4491 | log_position + 1); |
| 4492 | |
| 4493 | ASSERT_LT(log_position, entries.size()); |
| 4494 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4495 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4496 | } |
| 4497 | |
| 4498 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4499 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4500 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4501 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4502 | } |
| 4503 | |
| 4504 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4505 | // an HTTP redirect, with cancellation at various points. |
| 4506 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
| 4507 | ASSERT_TRUE(test_server_.Start()); |
| 4508 | |
| 4509 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 4510 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 4511 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 4512 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 4513 | }; |
| 4514 | |
| 4515 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 4516 | ++test_case) { |
| 4517 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
| 4518 | TestURLRequestContext context(true); |
| 4519 | CapturingNetLog net_log; |
| 4520 | context.set_network_delegate(NULL); |
| 4521 | context.set_net_log(&net_log); |
| 4522 | context.Init(); |
| 4523 | |
| 4524 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4525 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4526 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
| 4527 | &request_delegate, NULL)); |
| 4528 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4529 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4530 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4531 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | CapturingNetLog::CapturedEntryList entries; |
| 4535 | net_log.GetEntries(&entries); |
| 4536 | |
| 4537 | // Delegate info is always logged in both OnReceivedRedirect and |
| 4538 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 4539 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 4540 | // still currently supported in that call. |
| 4541 | size_t log_position = 0; |
| 4542 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4543 | if (i == 0) { |
| 4544 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4545 | entries, log_position) + 1; |
| 4546 | } |
| 4547 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4548 | log_position = ExpectLogContainsSomewhereAfter( |
| 4549 | entries, |
| 4550 | log_position, |
| 4551 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4552 | NetLog::PHASE_BEGIN); |
| 4553 | |
| 4554 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4555 | log_position + 1); |
| 4556 | |
| 4557 | ASSERT_LT(log_position, entries.size()); |
| 4558 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4559 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4560 | } |
| 4561 | |
| 4562 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4563 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4564 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4565 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4566 | } |
| 4567 | } |
| 4568 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4569 | namespace { |
| 4570 | |
| 4571 | const char kExtraHeader[] = "Allow-Snafu"; |
| 4572 | const char kExtraValue[] = "fubar"; |
| 4573 | |
| 4574 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 4575 | void OnReceivedRedirect(URLRequest* request, |
| 4576 | const RedirectInfo& redirect_info, |
| 4577 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4578 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4579 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 4580 | } |
| 4581 | }; |
| 4582 | |
| 4583 | } // namespace |
| 4584 | |
| 4585 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
| 4586 | ASSERT_TRUE(test_server_.Start()); |
| 4587 | |
| 4588 | GURL destination_url = test_server_.GetURL( |
| 4589 | "echoheader?" + std::string(kExtraHeader)); |
| 4590 | GURL original_url = test_server_.GetURL( |
| 4591 | "server-redirect?" + destination_url.spec()); |
| 4592 | RedirectWithAdditionalHeadersDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4593 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 4594 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 4595 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4596 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4597 | |
| 4598 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4599 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4600 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 4601 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4602 | EXPECT_FALSE(req->is_pending()); |
| 4603 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4604 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 4605 | } |
| 4606 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4607 | namespace { |
| 4608 | |
| 4609 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 4610 | |
| 4611 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 4612 | void OnReceivedRedirect(URLRequest* request, |
| 4613 | const RedirectInfo& redirect_info, |
| 4614 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4615 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4616 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 4617 | } |
| 4618 | }; |
| 4619 | |
| 4620 | } // namespace |
| 4621 | |
| 4622 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
| 4623 | ASSERT_TRUE(test_server_.Start()); |
| 4624 | |
| 4625 | GURL destination_url = test_server_.GetURL( |
| 4626 | "echoheader?" + std::string(kExtraHeaderToRemove)); |
| 4627 | GURL original_url = test_server_.GetURL( |
| 4628 | "server-redirect?" + destination_url.spec()); |
| 4629 | RedirectWithHeaderRemovalDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4630 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 4631 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 4632 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 4633 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4634 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4635 | |
| 4636 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4637 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4638 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4639 | EXPECT_FALSE(req->is_pending()); |
| 4640 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4641 | EXPECT_EQ("None", d.data_received()); |
| 4642 | } |
| 4643 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4644 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 4645 | TestDelegate d; |
| 4646 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4647 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4648 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4649 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4650 | r->Start(); |
| 4651 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4652 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4653 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4654 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4655 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4656 | |
| 4657 | // We expect to receive OnResponseStarted even though the request has been |
| 4658 | // cancelled. |
| 4659 | EXPECT_EQ(1, d.response_started_count()); |
| 4660 | EXPECT_EQ(0, d.bytes_received()); |
| 4661 | EXPECT_FALSE(d.received_data_before_response()); |
| 4662 | } |
| 4663 | } |
| 4664 | |
| 4665 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
| 4666 | ASSERT_TRUE(test_server_.Start()); |
| 4667 | |
| 4668 | TestDelegate d; |
| 4669 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4670 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4671 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4672 | |
| 4673 | d.set_cancel_in_response_started(true); |
| 4674 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4675 | r->Start(); |
| 4676 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4677 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4678 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4679 | |
| 4680 | EXPECT_EQ(1, d.response_started_count()); |
| 4681 | EXPECT_EQ(0, d.bytes_received()); |
| 4682 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4683 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4684 | } |
| 4685 | } |
| 4686 | |
| 4687 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
| 4688 | ASSERT_TRUE(test_server_.Start()); |
| 4689 | |
| 4690 | TestDelegate d; |
| 4691 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4692 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4693 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4694 | |
| 4695 | d.set_cancel_in_received_data(true); |
| 4696 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4697 | r->Start(); |
| 4698 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4699 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4700 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4701 | |
| 4702 | EXPECT_EQ(1, d.response_started_count()); |
| 4703 | // There is no guarantee about how much data was received |
| 4704 | // before the cancel was issued. It could have been 0 bytes, |
| 4705 | // or it could have been all the bytes. |
| 4706 | // EXPECT_EQ(0, d.bytes_received()); |
| 4707 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4708 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4709 | } |
| 4710 | } |
| 4711 | |
| 4712 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
| 4713 | ASSERT_TRUE(test_server_.Start()); |
| 4714 | |
| 4715 | TestDelegate d; |
| 4716 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4717 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4718 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4719 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4720 | r->Start(); |
| 4721 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4722 | |
| 4723 | // The request will be implicitly canceled when it is destroyed. The |
| 4724 | // test delegate must not post a quit message when this happens because |
| 4725 | // this test doesn't actually have a message loop. The quit message would |
| 4726 | // get put on this thread's message queue and the next test would exit |
| 4727 | // early, causing problems. |
| 4728 | d.set_quit_on_complete(false); |
| 4729 | } |
| 4730 | // expect things to just cleanup properly. |
| 4731 | |
| 4732 | // we won't actually get a received reponse here because we've never run the |
| 4733 | // message loop |
| 4734 | EXPECT_FALSE(d.received_data_before_response()); |
| 4735 | EXPECT_EQ(0, d.bytes_received()); |
| 4736 | } |
| 4737 | |
| 4738 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
| 4739 | ASSERT_TRUE(test_server_.Start()); |
| 4740 | |
| 4741 | // populate cache |
| 4742 | { |
| 4743 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4744 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4745 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d, NULL)); |
| 4746 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4747 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4748 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4749 | } |
| 4750 | |
| 4751 | // cancel read from cache (see bug 990242) |
| 4752 | { |
| 4753 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4754 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4755 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d, NULL)); |
| 4756 | r->Start(); |
| 4757 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4758 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4759 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4760 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4761 | EXPECT_EQ(1, d.response_started_count()); |
| 4762 | EXPECT_EQ(0, d.bytes_received()); |
| 4763 | EXPECT_FALSE(d.received_data_before_response()); |
| 4764 | } |
| 4765 | } |
| 4766 | |
| 4767 | TEST_F(URLRequestTestHTTP, PostTest) { |
| 4768 | ASSERT_TRUE(test_server_.Start()); |
| 4769 | HTTPUploadDataOperationTest("POST"); |
| 4770 | } |
| 4771 | |
| 4772 | TEST_F(URLRequestTestHTTP, PutTest) { |
| 4773 | ASSERT_TRUE(test_server_.Start()); |
| 4774 | HTTPUploadDataOperationTest("PUT"); |
| 4775 | } |
| 4776 | |
| 4777 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
| 4778 | ASSERT_TRUE(test_server_.Start()); |
| 4779 | |
| 4780 | TestDelegate d; |
| 4781 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4782 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4783 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4784 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4785 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4786 | r->Start(); |
| 4787 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4788 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4789 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4790 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4791 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4792 | << "request failed: " << r->status().status() |
| 4793 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4794 | |
| 4795 | EXPECT_FALSE(d.received_data_before_response()); |
| 4796 | EXPECT_TRUE(d.data_received().empty()); |
| 4797 | } |
| 4798 | } |
| 4799 | |
| 4800 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
| 4801 | ASSERT_TRUE(test_server_.Start()); |
| 4802 | |
| 4803 | TestDelegate d; |
| 4804 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4805 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4806 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4807 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4808 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4809 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4810 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 4811 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4812 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 4813 | ScopedVector<UploadElementReader> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4814 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4815 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4816 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 4817 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 4818 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 4819 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 4820 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 4821 | element_readers.push_back( |
| 4822 | new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 4823 | path, |
| 4824 | 0, |
| 4825 | kuint64max, |
| 4826 | base::Time())); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4827 | r->set_upload(make_scoped_ptr<UploadDataStream>( |
| 4828 | new ElementsUploadDataStream(element_readers.Pass(), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4829 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4830 | r->Start(); |
| 4831 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4832 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4833 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4834 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4835 | int64 size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 4836 | ASSERT_EQ(true, base::GetFileSize(path, &size)); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 4837 | scoped_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4838 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 4839 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4840 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4841 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4842 | << "request failed: " << r->status().status() |
| 4843 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4844 | |
| 4845 | EXPECT_FALSE(d.received_data_before_response()); |
| 4846 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4847 | EXPECT_EQ(size, d.bytes_received()); |
| 4848 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4849 | } |
| 4850 | } |
| 4851 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4852 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
| 4853 | ASSERT_TRUE(test_server_.Start()); |
| 4854 | |
| 4855 | TestDelegate d; |
| 4856 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4857 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4858 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4859 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4860 | |
| 4861 | ScopedVector<UploadElementReader> element_readers; |
| 4862 | |
| 4863 | element_readers.push_back(new UploadFileElementReader( |
| 4864 | base::MessageLoopProxy::current().get(), |
| 4865 | base::FilePath(FILE_PATH_LITERAL( |
| 4866 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
| 4867 | 0, |
| 4868 | kuint64max, |
| 4869 | base::Time())); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 4870 | r->set_upload(make_scoped_ptr<UploadDataStream>( |
| 4871 | new ElementsUploadDataStream(element_readers.Pass(), 0))); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4872 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4873 | r->Start(); |
| 4874 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4875 | |
| 4876 | base::RunLoop().Run(); |
| 4877 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4878 | EXPECT_TRUE(d.request_failed()); |
| 4879 | EXPECT_FALSE(d.received_data_before_response()); |
| 4880 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4881 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 4882 | EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4883 | } |
| 4884 | } |
| 4885 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4886 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
| 4887 | ASSERT_TRUE(test_server_.Start()); |
| 4888 | |
| 4889 | TestDelegate d; |
| 4890 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4891 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4892 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4893 | r->EnableChunkedUpload(); |
| 4894 | r->set_method("POST"); |
| 4895 | AddChunksToUpload(r.get()); |
| 4896 | r->Start(); |
| 4897 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4898 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4899 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4900 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4901 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4902 | } |
| 4903 | } |
| 4904 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4905 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
| 4906 | ASSERT_TRUE(test_server_.Start()); |
| 4907 | |
| 4908 | TestDelegate d; |
| 4909 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4910 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4911 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4912 | r->EnableChunkedUpload(); |
| 4913 | r->set_method("POST"); |
| 4914 | r->Start(); |
| 4915 | EXPECT_TRUE(r->is_pending()); |
| 4916 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4917 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4918 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4919 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4920 | } |
| 4921 | } |
| 4922 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4923 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
| 4924 | ASSERT_TRUE(test_server_.Start()); |
| 4925 | |
| 4926 | TestDelegate d; |
| 4927 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4928 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4929 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4930 | r->EnableChunkedUpload(); |
| 4931 | r->set_method("POST"); |
| 4932 | r->Start(); |
| 4933 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4934 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4935 | base::RunLoop().RunUntilIdle(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4936 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4937 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4938 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4939 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
| 4944 | ASSERT_TRUE(test_server_.Start()); |
| 4945 | |
| 4946 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4947 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 4948 | test_server_.GetURL("files/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 4949 | NULL)); |
| 4950 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4951 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4952 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4953 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4954 | |
| 4955 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4956 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4957 | |
| 4958 | std::string header; |
| 4959 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 4960 | EXPECT_EQ("private", header); |
| 4961 | |
| 4962 | header.clear(); |
| 4963 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 4964 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 4965 | |
| 4966 | // The response has two "X-Multiple-Entries" headers. |
| 4967 | // This verfies our output has them concatenated together. |
| 4968 | header.clear(); |
| 4969 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 4970 | EXPECT_EQ("a, b", header); |
| 4971 | } |
| 4972 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4973 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 4974 | SpawnedTestServer::SSLOptions ssl_options; |
| 4975 | SpawnedTestServer https_test_server( |
| 4976 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4977 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4978 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4979 | ASSERT_TRUE(https_test_server.Start()); |
| 4980 | |
| 4981 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4982 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 4983 | https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 4984 | NULL)); |
| 4985 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4986 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4987 | |
| 4988 | TransportSecurityState* security_state = |
| 4989 | default_context_.transport_security_state(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4990 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 4991 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 4992 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4993 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 4994 | domain_state.sts.upgrade_mode); |
| 4995 | EXPECT_TRUE(domain_state.sts.include_subdomains); |
| 4996 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 4997 | #if defined(OS_ANDROID) |
| 4998 | // Android's CertVerifyProc does not (yet) handle pins. |
| 4999 | #else |
| 5000 | EXPECT_FALSE(domain_state.HasPublicKeyPins()); |
| 5001 | #endif |
| 5002 | } |
| 5003 | |
| 5004 | // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will |
| 5005 | // reject HPKP headers, and a test setting only HPKP headers will fail (no |
| 5006 | // DomainState present because header rejected). |
| 5007 | #if defined(OS_ANDROID) |
| 5008 | #define MAYBE_ProcessPKP DISABLED_ProcessPKP |
| 5009 | #else |
| 5010 | #define MAYBE_ProcessPKP ProcessPKP |
| 5011 | #endif |
| 5012 | |
| 5013 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 5014 | // validity/expiration. |
| 5015 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) { |
| 5016 | SpawnedTestServer::SSLOptions ssl_options; |
| 5017 | SpawnedTestServer https_test_server( |
| 5018 | SpawnedTestServer::TYPE_HTTPS, |
| 5019 | ssl_options, |
| 5020 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5021 | ASSERT_TRUE(https_test_server.Start()); |
| 5022 | |
| 5023 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5024 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 5025 | https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 5026 | NULL)); |
| 5027 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5028 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5029 | |
| 5030 | TransportSecurityState* security_state = |
| 5031 | default_context_.transport_security_state(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5032 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5033 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 5034 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5035 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5036 | domain_state.sts.upgrade_mode); |
| 5037 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
| 5038 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5039 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5040 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 5044 | SpawnedTestServer::SSLOptions ssl_options; |
| 5045 | SpawnedTestServer https_test_server( |
| 5046 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5047 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5048 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5049 | ASSERT_TRUE(https_test_server.Start()); |
| 5050 | |
| 5051 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5052 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5053 | https_test_server.GetURL("files/hsts-multiple-headers.html"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5054 | DEFAULT_PRIORITY, &d, NULL)); |
| 5055 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5056 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5057 | |
| 5058 | // We should have set parameters from the first header, not the second. |
| 5059 | TransportSecurityState* security_state = |
| 5060 | default_context_.transport_security_state(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5061 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5062 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 5063 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5064 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5065 | domain_state.sts.upgrade_mode); |
| 5066 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
| 5067 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5068 | } |
| 5069 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5070 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 5071 | SpawnedTestServer::SSLOptions ssl_options; |
| 5072 | SpawnedTestServer https_test_server( |
| 5073 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5074 | ssl_options, |
| 5075 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5076 | ASSERT_TRUE(https_test_server.Start()); |
| 5077 | |
| 5078 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5079 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5080 | https_test_server.GetURL("files/hsts-and-hpkp-headers.html"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5081 | DEFAULT_PRIORITY, &d, NULL)); |
| 5082 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5083 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5084 | |
| 5085 | // We should have set parameters from the first header, not the second. |
| 5086 | TransportSecurityState* security_state = |
| 5087 | default_context_.transport_security_state(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5088 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5089 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 5090 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5091 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5092 | domain_state.sts.upgrade_mode); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5093 | #if defined(OS_ANDROID) |
| 5094 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5095 | #else |
| 5096 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5097 | #endif |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5098 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5099 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5100 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 5101 | // the *second* such header, and we MUST process only the first. |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5102 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5103 | // includeSubdomains does not occur in the test HPKP header. |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5104 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5105 | } |
| 5106 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5107 | // Tests that when multiple HPKP headers are present, asserting different |
| 5108 | // policies, that only the first such policy is processed. |
| 5109 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
| 5110 | SpawnedTestServer::SSLOptions ssl_options; |
| 5111 | SpawnedTestServer https_test_server( |
| 5112 | SpawnedTestServer::TYPE_HTTPS, |
| 5113 | ssl_options, |
| 5114 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5115 | ASSERT_TRUE(https_test_server.Start()); |
| 5116 | |
| 5117 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5118 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5119 | https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5120 | DEFAULT_PRIORITY, &d, NULL)); |
| 5121 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5122 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5123 | |
| 5124 | TransportSecurityState* security_state = |
| 5125 | default_context_.transport_security_state(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5126 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5127 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 5128 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5129 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5130 | domain_state.sts.upgrade_mode); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5131 | #if defined(OS_ANDROID) |
| 5132 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5133 | #else |
| 5134 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5135 | #endif |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5136 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5137 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5138 | EXPECT_TRUE(domain_state.sts.include_subdomains); |
| 5139 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5140 | } |
| 5141 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5142 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
| 5143 | ASSERT_TRUE(test_server_.Start()); |
| 5144 | |
| 5145 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5146 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5147 | test_server_.GetURL("files/content-type-normalization.html"), |
| 5148 | DEFAULT_PRIORITY, &d, NULL)); |
| 5149 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5150 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5151 | |
| 5152 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5153 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5154 | EXPECT_EQ("text/html", mime_type); |
| 5155 | |
| 5156 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5157 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5158 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5159 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5160 | } |
| 5161 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5162 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5163 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5164 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5165 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5166 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5167 | |
| 5168 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5169 | EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5170 | } |
| 5171 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5172 | #if !defined(DISABLE_FILE_SUPPORT) |
| 5173 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 5174 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 5175 | GURL file_url("file:///foo.txt"); |
| 5176 | FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current()); |
| 5177 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 5178 | |
| 5179 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
| 5180 | EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url)); |
| 5181 | } |
| 5182 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5183 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5184 | ASSERT_TRUE(test_server_.Start()); |
| 5185 | |
| 5186 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5187 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5188 | test_server_.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY, &d, |
| 5189 | NULL)); |
| 5190 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5191 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5192 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5193 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5194 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5195 | } |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5196 | #endif // !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5197 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5198 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
| 5199 | ASSERT_TRUE(test_server_.Start()); |
| 5200 | |
| 5201 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5202 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5203 | test_server_.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY, &d, |
| 5204 | NULL)); |
| 5205 | req->Start(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5206 | base::MessageLoop::current()->Run(); |
| 5207 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5208 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5209 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5210 | } |
| 5211 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5212 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
| 5213 | ASSERT_TRUE(test_server_.Start()); |
| 5214 | |
| 5215 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5216 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5217 | test_server_.GetURL("files/redirect-to-invalid-url.html"), |
| 5218 | DEFAULT_PRIORITY, &d, NULL)); |
| 5219 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5220 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5221 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5222 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5223 | EXPECT_EQ(ERR_INVALID_URL, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5224 | } |
| 5225 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5226 | // Make sure redirects are cached, despite not reading their bodies. |
| 5227 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
| 5228 | ASSERT_TRUE(test_server_.Start()); |
| 5229 | GURL redirect_url = |
| 5230 | test_server_.GetURL("files/redirect302-to-echo-cacheable"); |
| 5231 | |
| 5232 | { |
| 5233 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5234 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5235 | redirect_url, DEFAULT_PRIORITY, &d, NULL)); |
| 5236 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5237 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5238 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5239 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5240 | EXPECT_EQ(test_server_.GetURL("echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5241 | } |
| 5242 | |
| 5243 | { |
| 5244 | TestDelegate d; |
| 5245 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5246 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5247 | redirect_url, DEFAULT_PRIORITY, &d, NULL)); |
| 5248 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5249 | base::RunLoop().Run(); |
| 5250 | |
| 5251 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5252 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5253 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5254 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5255 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5256 | base::RunLoop().Run(); |
| 5257 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5258 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5259 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 5260 | EXPECT_EQ(test_server_.GetURL("echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5261 | } |
| 5262 | } |
| 5263 | |
| 5264 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 5265 | // when the headers are read, since the body won't have been read. |
| 5266 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
| 5267 | ASSERT_TRUE(test_server_.Start()); |
| 5268 | // URL that is normally cached. |
| 5269 | GURL initial_url = test_server_.GetURL("cachetime"); |
| 5270 | |
| 5271 | { |
| 5272 | // Set up the TestNetworkDelegate tp force a redirect. |
| 5273 | GURL redirect_to_url = test_server_.GetURL("echo"); |
| 5274 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 5275 | redirect_to_url); |
| 5276 | |
| 5277 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5278 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5279 | initial_url, DEFAULT_PRIORITY, &d, NULL)); |
| 5280 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5281 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5282 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5283 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5284 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | { |
| 5288 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5289 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5290 | initial_url, DEFAULT_PRIORITY, &d, NULL)); |
| 5291 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5292 | base::RunLoop().Run(); |
| 5293 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5294 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 5295 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5296 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5297 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5298 | } |
| 5299 | } |
| 5300 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5301 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 5302 | // safe. |
| 5303 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
| 5304 | ASSERT_TRUE(test_server_.Start()); |
| 5305 | |
| 5306 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 5307 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5308 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5309 | |
| 5310 | TestDelegate d; |
| 5311 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5312 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5313 | test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5314 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5315 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5316 | base::RunLoop().Run(); |
| 5317 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5318 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5319 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5320 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5321 | EXPECT_EQ(OK, r->status().error()); |
| 5322 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5323 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 5324 | } |
| 5325 | } |
| 5326 | |
| 5327 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 5328 | // some other URL to the whitelist. |
| 5329 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
| 5330 | ASSERT_TRUE(test_server_.Start()); |
| 5331 | |
| 5332 | GURL unsafe_url("data:text/html,something"); |
| 5333 | GURL different_unsafe_url("data:text/html,something-else"); |
| 5334 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5335 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 5336 | different_unsafe_url); |
| 5337 | |
| 5338 | TestDelegate d; |
| 5339 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5340 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5341 | test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5342 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5343 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5344 | base::RunLoop().Run(); |
| 5345 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5346 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 5347 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5348 | } |
| 5349 | } |
| 5350 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5351 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 5352 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 5353 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
| 5354 | ASSERT_TRUE(test_server_.Start()); |
| 5355 | |
| 5356 | GURL original_url(test_server_.GetURL("original#fragment1")); |
| 5357 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 5358 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 5359 | |
| 5360 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5361 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5362 | |
| 5363 | TestDelegate d; |
| 5364 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5365 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5366 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5367 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5368 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5369 | base::RunLoop().Run(); |
| 5370 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5371 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5372 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5373 | EXPECT_EQ(OK, r->status().error()); |
| 5374 | EXPECT_EQ(original_url, r->original_url()); |
| 5375 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5376 | } |
| 5377 | } |
| 5378 | |
| 5379 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 5380 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5381 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5382 | ASSERT_TRUE(test_server_.Start()); |
| 5383 | |
| 5384 | GURL original_url(test_server_.GetURL("original#expected-fragment")); |
| 5385 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
| 5386 | GURL redirect_url(test_server_.GetURL("target")); |
| 5387 | GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment")); |
| 5388 | |
| 5389 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 5390 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5391 | |
| 5392 | TestDelegate d; |
| 5393 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5394 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5395 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5396 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5397 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5398 | base::RunLoop().Run(); |
| 5399 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5400 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5401 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5402 | EXPECT_EQ(OK, r->status().error()); |
| 5403 | EXPECT_EQ(original_url, r->original_url()); |
| 5404 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5405 | } |
| 5406 | } |
| 5407 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5408 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 5409 | // URL should not be changed. In particular, the reference fragment should not |
| 5410 | // be modified. |
| 5411 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
| 5412 | ASSERT_TRUE(test_server_.Start()); |
| 5413 | |
| 5414 | GURL original_url(test_server_.GetURL("original#should-not-be-appended")); |
| 5415 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 5416 | |
| 5417 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 5418 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 5419 | |
| 5420 | TestDelegate d; |
| 5421 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5422 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5423 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5424 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5425 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5426 | base::RunLoop().Run(); |
| 5427 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5428 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5429 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5430 | EXPECT_EQ(OK, r->status().error()); |
| 5431 | EXPECT_EQ(original_url, r->original_url()); |
| 5432 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5433 | } |
| 5434 | } |
| 5435 | |
| 5436 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 5437 | // the reference fragment of the target URL must not be modified. |
| 5438 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
| 5439 | ASSERT_TRUE(test_server_.Start()); |
| 5440 | |
| 5441 | GURL original_url(test_server_.GetURL("original#should-not-be-appended")); |
| 5442 | GURL redirect_url(test_server_.GetURL("echo")); |
| 5443 | |
| 5444 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5445 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5446 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5447 | |
| 5448 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5449 | r.get(), &default_network_delegate_, redirect_url, |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5450 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
| 5451 | AddTestInterceptor()->set_main_intercept_job(job); |
| 5452 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5453 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5454 | base::RunLoop().Run(); |
| 5455 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5456 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5457 | EXPECT_EQ(OK, r->status().error()); |
| 5458 | EXPECT_EQ(original_url, r->original_url()); |
| 5459 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5460 | } |
| 5461 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5462 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
| 5463 | ASSERT_TRUE(test_server_.Start()); |
| 5464 | |
| 5465 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5466 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5467 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
| 5468 | req->SetReferrer("http://user:[email protected]/"); |
| 5469 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5470 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5471 | |
| 5472 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 5473 | } |
| 5474 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5475 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
| 5476 | ASSERT_TRUE(test_server_.Start()); |
| 5477 | |
| 5478 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5479 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5480 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
| 5481 | req->SetReferrer("http://foo.com/test#fragment"); |
| 5482 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5483 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5484 | |
| 5485 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 5486 | } |
| 5487 | |
| 5488 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
| 5489 | ASSERT_TRUE(test_server_.Start()); |
| 5490 | |
| 5491 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5492 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5493 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
| 5494 | req->SetReferrer("http://foo.com/test#fragment"); |
| 5495 | req->SetReferrer(""); |
| 5496 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5497 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5498 | |
| 5499 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 5500 | } |
| 5501 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5502 | // Defer network start and then resume, checking that the request was a success |
| 5503 | // and bytes were received. |
| 5504 | TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) { |
| 5505 | ASSERT_TRUE(test_server_.Start()); |
| 5506 | |
| 5507 | TestDelegate d; |
| 5508 | { |
| 5509 | d.set_quit_on_network_start(true); |
| 5510 | GURL test_url(test_server_.GetURL("echo")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5511 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5512 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5513 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5514 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5515 | base::RunLoop().Run(); |
| 5516 | |
| 5517 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5518 | EXPECT_EQ(0, d.response_started_count()); |
| 5519 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5520 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5521 | base::RunLoop().Run(); |
| 5522 | |
| 5523 | EXPECT_EQ(1, d.response_started_count()); |
| 5524 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5525 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5526 | } |
| 5527 | } |
| 5528 | |
| 5529 | // Check that OnBeforeNetworkStart is only called once even if there is a |
| 5530 | // redirect. |
| 5531 | TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) { |
| 5532 | ASSERT_TRUE(test_server_.Start()); |
| 5533 | |
| 5534 | TestDelegate d; |
| 5535 | { |
| 5536 | d.set_quit_on_redirect(true); |
| 5537 | d.set_quit_on_network_start(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5538 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5539 | test_server_.GetURL("server-redirect?echo"), DEFAULT_PRIORITY, &d, |
| 5540 | NULL)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5541 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5542 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5543 | base::RunLoop().Run(); |
| 5544 | |
| 5545 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5546 | EXPECT_EQ(0, d.response_started_count()); |
| 5547 | EXPECT_EQ(0, d.received_redirect_count()); |
| 5548 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5549 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5550 | base::RunLoop().Run(); |
| 5551 | |
| 5552 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5553 | req->FollowDeferredRedirect(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5554 | base::RunLoop().Run(); |
| 5555 | |
| 5556 | // Check that the redirect's new network transaction does not get propagated |
| 5557 | // to a second OnBeforeNetworkStart() notification. |
| 5558 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5559 | |
| 5560 | EXPECT_EQ(1, d.response_started_count()); |
| 5561 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5562 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5563 | } |
| 5564 | } |
| 5565 | |
| 5566 | // Cancel the request after learning that the request would use the network. |
| 5567 | TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) { |
| 5568 | ASSERT_TRUE(test_server_.Start()); |
| 5569 | |
| 5570 | TestDelegate d; |
| 5571 | { |
| 5572 | d.set_quit_on_network_start(true); |
| 5573 | GURL test_url(test_server_.GetURL("echo")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5574 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5575 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5576 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5577 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5578 | base::RunLoop().Run(); |
| 5579 | |
| 5580 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5581 | EXPECT_EQ(0, d.response_started_count()); |
| 5582 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5583 | req->Cancel(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5584 | base::RunLoop().Run(); |
| 5585 | |
| 5586 | EXPECT_EQ(1, d.response_started_count()); |
| 5587 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5588 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5589 | } |
| 5590 | } |
| 5591 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5592 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
| 5593 | ASSERT_TRUE(test_server_.Start()); |
| 5594 | |
| 5595 | TestDelegate d; |
| 5596 | { |
| 5597 | d.set_cancel_in_received_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5598 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5599 | test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 5600 | NULL)); |
| 5601 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5602 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5603 | |
| 5604 | EXPECT_EQ(1, d.response_started_count()); |
| 5605 | EXPECT_EQ(0, d.bytes_received()); |
| 5606 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5607 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
| 5612 | ASSERT_TRUE(test_server_.Start()); |
| 5613 | |
| 5614 | TestDelegate d; |
| 5615 | { |
| 5616 | d.set_quit_on_redirect(true); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5617 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5618 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5619 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5620 | |
[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 | |
| 5624 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5625 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5626 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5627 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5628 | |
| 5629 | EXPECT_EQ(1, d.response_started_count()); |
| 5630 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5631 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5632 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5633 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5634 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 5635 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 5636 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 5637 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 5638 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 5639 | |
| 5640 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 5641 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5642 | EXPECT_EQ(contents, d.data_received()); |
| 5643 | } |
| 5644 | } |
| 5645 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5646 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
| 5647 | ASSERT_TRUE(test_server_.Start()); |
| 5648 | |
| 5649 | TestDelegate d; |
| 5650 | { |
| 5651 | d.set_quit_on_redirect(true); |
| 5652 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5653 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5654 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5655 | |
| 5656 | EXPECT_FALSE(d.have_full_request_headers()); |
| 5657 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5658 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5659 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5660 | |
| 5661 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5662 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5663 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5664 | d.ClearFullRequestHeaders(); |
| 5665 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5666 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5667 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5668 | |
| 5669 | GURL target_url(test_server_.GetURL("files/with-headers.html")); |
| 5670 | EXPECT_EQ(1, d.response_started_count()); |
| 5671 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5672 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 5673 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5674 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5675 | |
| 5676 | base::FilePath path; |
| 5677 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 5678 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 5679 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 5680 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 5681 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 5682 | |
| 5683 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 5684 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5685 | EXPECT_EQ(contents, d.data_received()); |
| 5686 | } |
| 5687 | } |
| 5688 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5689 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
| 5690 | ASSERT_TRUE(test_server_.Start()); |
| 5691 | |
| 5692 | TestDelegate d; |
| 5693 | { |
| 5694 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5695 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5696 | test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 5697 | NULL)); |
| 5698 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5699 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5700 | |
| 5701 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5702 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5703 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5704 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5705 | |
| 5706 | EXPECT_EQ(1, d.response_started_count()); |
| 5707 | EXPECT_EQ(0, d.bytes_received()); |
| 5708 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5709 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5710 | } |
| 5711 | } |
| 5712 | |
| 5713 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
| 5714 | ASSERT_TRUE(test_server_.Start()); |
| 5715 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5716 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5717 | { |
| 5718 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5719 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5720 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d, |
| 5721 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5722 | HttpRequestHeaders headers; |
| 5723 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5724 | req->SetExtraRequestHeaders(headers); |
| 5725 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5726 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5727 | |
| 5728 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5729 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5730 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5731 | } |
| 5732 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5733 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5734 | { |
| 5735 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5736 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5737 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d, |
| 5738 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5739 | HttpRequestHeaders headers; |
| 5740 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5741 | req->SetExtraRequestHeaders(headers); |
| 5742 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5743 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5744 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5745 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5746 | |
| 5747 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5748 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5749 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5750 | } |
| 5751 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5752 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5753 | { |
| 5754 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5755 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5756 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d, |
| 5757 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5758 | HttpRequestHeaders headers; |
| 5759 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5760 | req->SetExtraRequestHeaders(headers); |
| 5761 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5762 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5763 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5764 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5765 | |
| 5766 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5767 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5768 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5769 | } |
| 5770 | } |
| 5771 | |
| 5772 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
| 5773 | ASSERT_TRUE(test_server_.Start()); |
| 5774 | |
| 5775 | // populate the cache |
| 5776 | { |
| 5777 | TestDelegate d; |
| 5778 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5779 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5780 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5781 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
| 5782 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5783 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5784 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5785 | |
| 5786 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5787 | } |
| 5788 | |
| 5789 | // repeat request with end-to-end validation. since auth-basic results in a |
| 5790 | // cachable page, we expect this test to result in a 304. in which case, the |
| 5791 | // response should be fetched from the cache. |
| 5792 | { |
| 5793 | TestDelegate d; |
| 5794 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5795 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5796 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5797 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
| 5798 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 5799 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5800 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5801 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5802 | |
| 5803 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5804 | |
| 5805 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5806 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5807 | } |
| 5808 | } |
| 5809 | |
| 5810 | // Check that Set-Cookie headers in 401 responses are respected. |
| 5811 | // http://crbug.com/6450 |
| 5812 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
| 5813 | ASSERT_TRUE(test_server_.Start()); |
| 5814 | |
| 5815 | GURL url_requiring_auth = |
| 5816 | test_server_.GetURL("auth-basic?set-cookie-if-challenged"); |
| 5817 | |
| 5818 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 5819 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 5820 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5821 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5822 | TestURLRequestContext context(true); |
| 5823 | context.set_network_delegate(&network_delegate); |
| 5824 | context.Init(); |
| 5825 | |
| 5826 | TestDelegate d; |
| 5827 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5828 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5829 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 5830 | url_requiring_auth, DEFAULT_PRIORITY, &d, NULL)); |
| 5831 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5832 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5833 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5834 | |
| 5835 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5836 | |
| 5837 | // Make sure we sent the cookie in the restarted transaction. |
| 5838 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 5839 | != std::string::npos); |
| 5840 | } |
| 5841 | |
| 5842 | // Same test as above, except this time the restart is initiated earlier |
| 5843 | // (without user intervention since identity is embedded in the URL). |
| 5844 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5845 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5846 | TestURLRequestContext context(true); |
| 5847 | context.set_network_delegate(&network_delegate); |
| 5848 | context.Init(); |
| 5849 | |
| 5850 | TestDelegate d; |
| 5851 | |
| 5852 | GURL::Replacements replacements; |
| 5853 | std::string username("user2"); |
| 5854 | std::string password("secret"); |
| 5855 | replacements.SetUsernameStr(username); |
| 5856 | replacements.SetPasswordStr(password); |
| 5857 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 5858 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5859 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 5860 | url_with_identity, DEFAULT_PRIORITY, &d, NULL)); |
| 5861 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5862 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5863 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5864 | |
| 5865 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 5866 | |
| 5867 | // Make sure we sent the cookie in the restarted transaction. |
| 5868 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 5869 | != std::string::npos); |
| 5870 | } |
| 5871 | } |
| 5872 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5873 | // Tests that load timing works as expected with auth and the cache. |
| 5874 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
| 5875 | ASSERT_TRUE(test_server_.Start()); |
| 5876 | |
| 5877 | // populate the cache |
| 5878 | { |
| 5879 | TestDelegate d; |
| 5880 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5881 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5882 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5883 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
| 5884 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5885 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5886 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5887 | |
| 5888 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5889 | |
| 5890 | LoadTimingInfo load_timing_info_before_auth; |
| 5891 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 5892 | &load_timing_info_before_auth)); |
| 5893 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 5894 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5895 | |
| 5896 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5897 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5898 | // The test server does not support keep alive sockets, so the second |
| 5899 | // request with auth should use a new socket. |
| 5900 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5901 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 5902 | load_timing_info.socket_log_id); |
| 5903 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 5904 | load_timing_info.connect_timing.connect_start); |
| 5905 | } |
| 5906 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5907 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 5908 | // 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] | 5909 | // response should be fetched from the cache. |
| 5910 | { |
| 5911 | TestDelegate d; |
| 5912 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5913 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5914 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5915 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
| 5916 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 5917 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5918 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5919 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5920 | |
| 5921 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5922 | |
| 5923 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5924 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5925 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5926 | // Since there was a request that went over the wire, the load timing |
| 5927 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5928 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5929 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5930 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5931 | } |
| 5932 | } |
| 5933 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5934 | // In this test, we do a POST which the server will 302 redirect. |
| 5935 | // The subsequent transaction should use GET, and should not send the |
| 5936 | // Content-Type header. |
| 5937 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 5938 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
| 5939 | ASSERT_TRUE(test_server_.Start()); |
| 5940 | |
| 5941 | const char kData[] = "hello world"; |
| 5942 | |
| 5943 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5944 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5945 | test_server_.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 5946 | NULL)); |
| 5947 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 5948 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5949 | |
| 5950 | // Set headers (some of which are specific to the POST). |
| 5951 | HttpRequestHeaders headers; |
| 5952 | headers.AddHeadersFromString( |
| 5953 | "Content-Type: multipart/form-data; " |
| 5954 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 5955 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 5956 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 5957 | "Accept-Language: en-US,en\r\n" |
| 5958 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 5959 | "Content-Length: 11\r\n" |
| 5960 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5961 | req->SetExtraRequestHeaders(headers); |
| 5962 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5963 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5964 | |
| 5965 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5966 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5967 | EXPECT_EQ("text/html", mime_type); |
| 5968 | |
| 5969 | const std::string& data = d.data_received(); |
| 5970 | |
| 5971 | // Check that the post-specific headers were stripped: |
| 5972 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 5973 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 5974 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 5975 | |
| 5976 | // These extra request headers should not have been stripped. |
| 5977 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 5978 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 5979 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 5980 | } |
| 5981 | |
| 5982 | // The following tests check that we handle mutating the request method for |
| 5983 | // HTTP redirects as expected. |
| 5984 | // See http://crbug.com/56373 and http://crbug.com/102130. |
| 5985 | |
| 5986 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
| 5987 | ASSERT_TRUE(test_server_.Start()); |
| 5988 | |
| 5989 | const GURL url = test_server_.GetURL("files/redirect301-to-echo"); |
| 5990 | |
| 5991 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 5992 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 5993 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 5994 | } |
| 5995 | |
| 5996 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
| 5997 | ASSERT_TRUE(test_server_.Start()); |
| 5998 | |
| 5999 | const GURL url = test_server_.GetURL("files/redirect302-to-echo"); |
| 6000 | |
| 6001 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 6002 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6003 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 6004 | } |
| 6005 | |
| 6006 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
| 6007 | ASSERT_TRUE(test_server_.Start()); |
| 6008 | |
| 6009 | const GURL url = test_server_.GetURL("files/redirect303-to-echo"); |
| 6010 | |
| 6011 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 6012 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 6013 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 6014 | } |
| 6015 | |
| 6016 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
| 6017 | ASSERT_TRUE(test_server_.Start()); |
| 6018 | |
| 6019 | const GURL url = test_server_.GetURL("files/redirect307-to-echo"); |
| 6020 | |
| 6021 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 6022 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6023 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 6024 | } |
| 6025 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6026 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
| 6027 | ASSERT_TRUE(test_server_.Start()); |
| 6028 | |
| 6029 | const GURL url = test_server_.GetURL("files/redirect308-to-echo"); |
| 6030 | |
| 6031 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 6032 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6033 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 6034 | } |
| 6035 | |
| 6036 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 6037 | // Certain legacy apis that pre-date the response code expect this behavior |
| 6038 | // (Like Google Drive). |
| 6039 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
| 6040 | ASSERT_TRUE(test_server_.Start()); |
| 6041 | |
| 6042 | TestDelegate d; |
| 6043 | const GURL url = test_server_.GetURL("files/308-without-location-header"); |
| 6044 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6045 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 6046 | url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6047 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6048 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6049 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6050 | EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
| 6051 | EXPECT_EQ(OK, request->status().error()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6052 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6053 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6054 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 6055 | } |
| 6056 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6057 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
| 6058 | ASSERT_TRUE(test_server_.Start()); |
| 6059 | |
| 6060 | GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment")); |
| 6061 | GURL expected_url(test_server_.GetURL("echo#fragment")); |
| 6062 | |
| 6063 | TestDelegate d; |
| 6064 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6065 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6066 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6067 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6068 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6069 | base::RunLoop().Run(); |
| 6070 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6071 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6072 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6073 | EXPECT_EQ(OK, r->status().error()); |
| 6074 | EXPECT_EQ(original_url, r->original_url()); |
| 6075 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6076 | } |
| 6077 | } |
| 6078 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6079 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
| 6080 | ASSERT_TRUE(test_server_.Start()); |
| 6081 | |
| 6082 | GURL url(test_server_.GetURL("files/redirect302-to-echo")); |
| 6083 | GURL first_party_url("http://example.com"); |
| 6084 | |
| 6085 | TestDelegate d; |
| 6086 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6087 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6088 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 6089 | r->set_first_party_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6090 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6091 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6092 | base::RunLoop().Run(); |
| 6093 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6094 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6095 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6096 | EXPECT_EQ(OK, r->status().error()); |
| 6097 | EXPECT_EQ(first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6098 | } |
| 6099 | } |
| 6100 | |
| 6101 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
| 6102 | ASSERT_TRUE(test_server_.Start()); |
| 6103 | |
| 6104 | GURL url(test_server_.GetURL("files/redirect302-to-echo")); |
| 6105 | GURL original_first_party_url("http://example.com"); |
| 6106 | GURL expected_first_party_url(test_server_.GetURL("echo")); |
| 6107 | |
| 6108 | TestDelegate d; |
| 6109 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6110 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6111 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 6112 | r->set_first_party_for_cookies(original_first_party_url); |
| 6113 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6114 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 6115 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6116 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6117 | base::RunLoop().Run(); |
| 6118 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6119 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6120 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6121 | EXPECT_EQ(OK, r->status().error()); |
| 6122 | EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6123 | } |
| 6124 | } |
| 6125 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6126 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
| 6127 | ASSERT_TRUE(test_server_.Start()); |
| 6128 | |
| 6129 | const char kData[] = "hello world"; |
| 6130 | |
| 6131 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6132 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6133 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL)); |
| 6134 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6135 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6136 | HttpRequestHeaders headers; |
| 6137 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 6138 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6139 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6140 | |
| 6141 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6142 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6143 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6144 | AddTestInterceptor()->set_main_intercept_job(job); |
| 6145 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6146 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6147 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6148 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6149 | } |
| 6150 | |
| 6151 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 6152 | ASSERT_TRUE(test_server_.Start()); |
| 6153 | |
| 6154 | const char kData[] = "hello world"; |
| 6155 | |
| 6156 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6157 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6158 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL)); |
| 6159 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6160 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6161 | HttpRequestHeaders headers; |
| 6162 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 6163 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6164 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6165 | |
| 6166 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6167 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6168 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
| 6169 | "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6170 | AddTestInterceptor()->set_main_intercept_job(job); |
| 6171 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6172 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6173 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6174 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6175 | EXPECT_EQ(kData, d.data_received()); |
| 6176 | } |
| 6177 | |
| 6178 | // Check that default A-L header is sent. |
| 6179 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
| 6180 | ASSERT_TRUE(test_server_.Start()); |
| 6181 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 6182 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6183 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6184 | TestURLRequestContext context(true); |
| 6185 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6186 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6187 | context.Init(); |
| 6188 | |
| 6189 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6190 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6191 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d, |
| 6192 | NULL)); |
| 6193 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6194 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6195 | EXPECT_EQ("en", d.data_received()); |
| 6196 | } |
| 6197 | |
| 6198 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 6199 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
| 6200 | ASSERT_TRUE(test_server_.Start()); |
| 6201 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 6202 | std::string empty_string; // Avoid most vexing parse on line below. |
| 6203 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6204 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6205 | TestURLRequestContext context(true); |
| 6206 | context.set_network_delegate(&network_delegate); |
| 6207 | context.Init(); |
| 6208 | // We override the language after initialization because empty entries |
| 6209 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6210 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6211 | |
| 6212 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6213 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6214 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d, |
| 6215 | NULL)); |
| 6216 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6217 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6218 | EXPECT_EQ("None", d.data_received()); |
| 6219 | } |
| 6220 | |
| 6221 | // Check that if request overrides the A-L header, the default is not appended. |
| 6222 | // See http://crbug.com/20894 |
| 6223 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
| 6224 | ASSERT_TRUE(test_server_.Start()); |
| 6225 | |
| 6226 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6227 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6228 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d, |
| 6229 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6230 | HttpRequestHeaders headers; |
| 6231 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6232 | req->SetExtraRequestHeaders(headers); |
| 6233 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6234 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6235 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 6236 | } |
| 6237 | |
| 6238 | // Check that default A-E header is sent. |
| 6239 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
| 6240 | ASSERT_TRUE(test_server_.Start()); |
| 6241 | |
| 6242 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6243 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6244 | test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d, |
| 6245 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6246 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6247 | req->SetExtraRequestHeaders(headers); |
| 6248 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6249 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6250 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 6251 | } |
| 6252 | |
| 6253 | // Check that if request overrides the A-E header, the default is not appended. |
| 6254 | // See http://crbug.com/47381 |
| 6255 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
| 6256 | ASSERT_TRUE(test_server_.Start()); |
| 6257 | |
| 6258 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6259 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6260 | test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d, |
| 6261 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6262 | HttpRequestHeaders headers; |
| 6263 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6264 | req->SetExtraRequestHeaders(headers); |
| 6265 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6266 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6267 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 6268 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 6269 | } |
| 6270 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 6271 | // Check that setting the A-C header sends the proper header. |
| 6272 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6273 | ASSERT_TRUE(test_server_.Start()); |
| 6274 | |
| 6275 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6276 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6277 | test_server_.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY, &d, |
| 6278 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6279 | HttpRequestHeaders headers; |
| 6280 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6281 | req->SetExtraRequestHeaders(headers); |
| 6282 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6283 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6284 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 6285 | } |
| 6286 | |
| 6287 | // Check that default User-Agent header is sent. |
| 6288 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
| 6289 | ASSERT_TRUE(test_server_.Start()); |
| 6290 | |
| 6291 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6292 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6293 | test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d, |
| 6294 | NULL)); |
| 6295 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6296 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6297 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 6298 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | // Check that if request overrides the User-Agent header, |
| 6302 | // the default is not appended. |
| 6303 | TEST_F(URLRequestTestHTTP, OverrideUserAgent) { |
| 6304 | ASSERT_TRUE(test_server_.Start()); |
| 6305 | |
| 6306 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6307 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6308 | test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d, |
| 6309 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6310 | HttpRequestHeaders headers; |
| 6311 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6312 | req->SetExtraRequestHeaders(headers); |
| 6313 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6314 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 6315 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6316 | } |
| 6317 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6318 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 6319 | // User-Agent header to be sent but does not send the Accept-Language and |
| 6320 | // Accept-Charset headers. |
| 6321 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
| 6322 | ASSERT_TRUE(test_server_.Start()); |
| 6323 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6324 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6325 | TestURLRequestContext context(true); |
| 6326 | context.set_network_delegate(&network_delegate); |
| 6327 | context.Init(); |
| 6328 | // We override the HttpUserAgentSettings after initialization because empty |
| 6329 | // entries get overridden by Init(). |
| 6330 | context.set_http_user_agent_settings(NULL); |
| 6331 | |
| 6332 | struct { |
| 6333 | const char* request; |
| 6334 | const char* expected_response; |
| 6335 | } tests[] = { { "echoheader?Accept-Language", "None" }, |
| 6336 | { "echoheader?Accept-Charset", "None" }, |
| 6337 | { "echoheader?User-Agent", "" } }; |
| 6338 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 6339 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6340 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6341 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6342 | test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, NULL)); |
| 6343 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6344 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6345 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 6346 | << " Request = \"" << tests[i].request << "\""; |
| 6347 | } |
| 6348 | } |
| 6349 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6350 | // Make sure that URLRequest passes on its priority updates to |
| 6351 | // newly-created jobs after the first one. |
| 6352 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
| 6353 | ASSERT_TRUE(test_server_.Start()); |
| 6354 | |
| 6355 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6356 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6357 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL)); |
| 6358 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6359 | |
| 6360 | scoped_refptr<URLRequestRedirectJob> redirect_job = |
| 6361 | new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6362 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6363 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6364 | AddTestInterceptor()->set_main_intercept_job(redirect_job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6365 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6366 | req->SetPriority(LOW); |
| 6367 | req->Start(); |
| 6368 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6369 | |
| 6370 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6371 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6372 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6373 | |
| 6374 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6375 | base::RunLoop().Run(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6376 | EXPECT_EQ(LOW, job->priority()); |
| 6377 | } |
| 6378 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6379 | // Check that creating a network request while entering/exiting suspend mode |
| 6380 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 6381 | // does not return an HttpTransaction. |
| 6382 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 6383 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6384 | HttpNetworkSession::Params params; |
| 6385 | params.host_resolver = default_context_.host_resolver(); |
| 6386 | params.cert_verifier = default_context_.cert_verifier(); |
| 6387 | params.transport_security_state = default_context_.transport_security_state(); |
| 6388 | params.proxy_service = default_context_.proxy_service(); |
| 6389 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6390 | params.http_auth_handler_factory = |
| 6391 | default_context_.http_auth_handler_factory(); |
| 6392 | params.network_delegate = &default_network_delegate_; |
| 6393 | params.http_server_properties = default_context_.http_server_properties(); |
| 6394 | scoped_ptr<HttpNetworkLayer> network_layer( |
| 6395 | new HttpNetworkLayer(new HttpNetworkSession(params))); |
| 6396 | network_layer->OnSuspend(); |
| 6397 | |
| 6398 | HttpCache http_cache(network_layer.release(), default_context_.net_log(), |
| 6399 | HttpCache::DefaultBackend::InMemory(0)); |
| 6400 | |
| 6401 | TestURLRequestContext context(true); |
| 6402 | context.set_http_transaction_factory(&http_cache); |
| 6403 | context.Init(); |
| 6404 | |
| 6405 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6406 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6407 | GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL)); |
| 6408 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6409 | base::RunLoop().Run(); |
| 6410 | |
| 6411 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6412 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6413 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6414 | } |
| 6415 | |
| 6416 | // Check that creating a network request while entering/exiting suspend mode |
| 6417 | // fails as it should in the case there is no cache. This is the only case |
| 6418 | // where an HttpTransactionFactory does not return an HttpTransaction. |
| 6419 | TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) { |
| 6420 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6421 | HttpNetworkSession::Params params; |
| 6422 | params.host_resolver = default_context_.host_resolver(); |
| 6423 | params.cert_verifier = default_context_.cert_verifier(); |
| 6424 | params.transport_security_state = default_context_.transport_security_state(); |
| 6425 | params.proxy_service = default_context_.proxy_service(); |
| 6426 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6427 | params.http_auth_handler_factory = |
| 6428 | default_context_.http_auth_handler_factory(); |
| 6429 | params.network_delegate = &default_network_delegate_; |
| 6430 | params.http_server_properties = default_context_.http_server_properties(); |
| 6431 | HttpNetworkLayer network_layer(new HttpNetworkSession(params)); |
| 6432 | network_layer.OnSuspend(); |
| 6433 | |
| 6434 | TestURLRequestContext context(true); |
| 6435 | context.set_http_transaction_factory(&network_layer); |
| 6436 | context.Init(); |
| 6437 | |
| 6438 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6439 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6440 | GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL)); |
| 6441 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6442 | base::RunLoop().Run(); |
| 6443 | |
| 6444 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6445 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6446 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6447 | } |
| 6448 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 6449 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 6450 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6451 | HTTPSRequestTest() : default_context_(true) { |
| 6452 | default_context_.set_network_delegate(&default_network_delegate_); |
| 6453 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 6454 | } |
| 6455 | virtual ~HTTPSRequestTest() {} |
| 6456 | |
| 6457 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6458 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6459 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6460 | }; |
| 6461 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 6462 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6463 | SpawnedTestServer test_server( |
| 6464 | SpawnedTestServer::TYPE_HTTPS, |
| 6465 | SpawnedTestServer::kLocalhost, |
| 6466 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6467 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6468 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6469 | TestDelegate d; |
| 6470 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6471 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6472 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
| 6473 | r->Start(); |
| 6474 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6475 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6476 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6477 | |
| 6478 | EXPECT_EQ(1, d.response_started_count()); |
| 6479 | EXPECT_FALSE(d.received_data_before_response()); |
| 6480 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6481 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 6482 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6483 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 6484 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6485 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6486 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6487 | } |
| 6488 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 6489 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6490 | SpawnedTestServer::SSLOptions ssl_options( |
| 6491 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6492 | SpawnedTestServer test_server( |
| 6493 | SpawnedTestServer::TYPE_HTTPS, |
| 6494 | ssl_options, |
| 6495 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6496 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6497 | |
| 6498 | bool err_allowed = true; |
| 6499 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 6500 | TestDelegate d; |
| 6501 | { |
| 6502 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6503 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6504 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6505 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6506 | r->Start(); |
| 6507 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6508 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6509 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6510 | |
| 6511 | EXPECT_EQ(1, d.response_started_count()); |
| 6512 | EXPECT_FALSE(d.received_data_before_response()); |
| 6513 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6514 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6515 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6516 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6517 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6518 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6519 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6520 | } |
| 6521 | } |
| 6522 | } |
| 6523 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 6524 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6525 | SpawnedTestServer::SSLOptions ssl_options( |
| 6526 | SpawnedTestServer::SSLOptions::CERT_EXPIRED); |
| 6527 | SpawnedTestServer test_server( |
| 6528 | SpawnedTestServer::TYPE_HTTPS, |
| 6529 | ssl_options, |
| 6530 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6531 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6532 | |
| 6533 | // Iterate from false to true, just so that we do the opposite of the |
| 6534 | // previous test in order to increase test coverage. |
| 6535 | bool err_allowed = false; |
| 6536 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 6537 | TestDelegate d; |
| 6538 | { |
| 6539 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6540 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6541 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6542 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6543 | r->Start(); |
| 6544 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6545 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6546 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6547 | |
| 6548 | EXPECT_EQ(1, d.response_started_count()); |
| 6549 | EXPECT_FALSE(d.received_data_before_response()); |
| 6550 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6551 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6552 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6553 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6554 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6555 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6556 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6557 | } |
| 6558 | } |
| 6559 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 6560 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6561 | // This tests that a load of www.google.com with a certificate error sets |
| 6562 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 6563 | // the interstitial to be fatal. |
| 6564 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6565 | SpawnedTestServer::SSLOptions ssl_options( |
| 6566 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6567 | SpawnedTestServer test_server( |
| 6568 | SpawnedTestServer::TYPE_HTTPS, |
| 6569 | ssl_options, |
| 6570 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6571 | ASSERT_TRUE(test_server.Start()); |
| 6572 | |
| 6573 | // We require that the URL be www.google.com in order to pick up the |
| 6574 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 6575 | // 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] | 6576 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6577 | |
| 6578 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6579 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6580 | TestURLRequestContext context(true); |
| 6581 | context.set_network_delegate(&network_delegate); |
| 6582 | context.set_host_resolver(&host_resolver); |
| 6583 | TransportSecurityState transport_security_state; |
| 6584 | context.set_transport_security_state(&transport_security_state); |
| 6585 | context.Init(); |
| 6586 | |
| 6587 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6588 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 6589 | GURL(base::StringPrintf("https://www.google.com:%d", |
| 6590 | test_server.host_port_pair().port())), |
| 6591 | DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6592 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6593 | r->Start(); |
| 6594 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6595 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6596 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6597 | |
| 6598 | EXPECT_EQ(1, d.response_started_count()); |
| 6599 | EXPECT_FALSE(d.received_data_before_response()); |
| 6600 | EXPECT_TRUE(d.have_certificate_errors()); |
| 6601 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 6602 | } |
| 6603 | |
| 6604 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 6605 | // TransportSecurityState. |
| 6606 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 6607 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 6608 | // matter. It just has to be any error. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6609 | SpawnedTestServer::SSLOptions ssl_options( |
| 6610 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6611 | SpawnedTestServer test_server( |
| 6612 | SpawnedTestServer::TYPE_HTTPS, |
| 6613 | ssl_options, |
| 6614 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6615 | ASSERT_TRUE(test_server.Start()); |
| 6616 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6617 | // We require that the URL be www.google.com in order to pick up the static |
| 6618 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 6619 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 6620 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6621 | |
| 6622 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6623 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6624 | TestURLRequestContext context(true); |
| 6625 | context.set_network_delegate(&network_delegate); |
| 6626 | context.set_host_resolver(&host_resolver); |
| 6627 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6628 | |
| 6629 | TransportSecurityState::DomainState static_domain_state; |
| 6630 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Adam Langley | 5cbb7d7a | 2014-09-25 23:14:12 | [diff] [blame] | 6631 | "www.google.com", &static_domain_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6632 | context.set_transport_security_state(&transport_security_state); |
| 6633 | context.Init(); |
| 6634 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6635 | TransportSecurityState::DomainState dynamic_domain_state; |
| 6636 | EXPECT_FALSE(transport_security_state.GetDynamicDomainState( |
| 6637 | "www.google.com", &dynamic_domain_state)); |
| 6638 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6639 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6640 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 6641 | GURL(base::StringPrintf("https://www.google.com:%d", |
| 6642 | test_server.host_port_pair().port())), |
| 6643 | DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6644 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6645 | r->Start(); |
| 6646 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6647 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6648 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6649 | |
| 6650 | EXPECT_EQ(1, d.response_started_count()); |
| 6651 | EXPECT_FALSE(d.received_data_before_response()); |
| 6652 | EXPECT_TRUE(d.have_certificate_errors()); |
| 6653 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 6654 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6655 | // Get a fresh copy of the states, and check that they haven't changed. |
| 6656 | TransportSecurityState::DomainState new_static_domain_state; |
| 6657 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Adam Langley | 5cbb7d7a | 2014-09-25 23:14:12 | [diff] [blame] | 6658 | "www.google.com", &new_static_domain_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6659 | TransportSecurityState::DomainState new_dynamic_domain_state; |
| 6660 | EXPECT_FALSE(transport_security_state.GetDynamicDomainState( |
| 6661 | "www.google.com", &new_dynamic_domain_state)); |
| 6662 | |
| 6663 | EXPECT_EQ(new_static_domain_state.sts.upgrade_mode, |
| 6664 | static_domain_state.sts.upgrade_mode); |
| 6665 | EXPECT_EQ(new_static_domain_state.sts.include_subdomains, |
| 6666 | static_domain_state.sts.include_subdomains); |
| 6667 | EXPECT_EQ(new_static_domain_state.pkp.include_subdomains, |
| 6668 | static_domain_state.pkp.include_subdomains); |
| 6669 | EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes, |
| 6670 | static_domain_state.pkp.spki_hashes)); |
| 6671 | EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes, |
| 6672 | static_domain_state.pkp.bad_spki_hashes)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6673 | } |
| 6674 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6675 | // Make sure HSTS preserves a POST request's method and body. |
| 6676 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 6677 | static const char kData[] = "hello world"; |
| 6678 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6679 | SpawnedTestServer::SSLOptions ssl_options( |
| 6680 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 6681 | SpawnedTestServer test_server( |
| 6682 | SpawnedTestServer::TYPE_HTTPS, |
| 6683 | ssl_options, |
| 6684 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6685 | ASSERT_TRUE(test_server.Start()); |
| 6686 | |
| 6687 | |
| 6688 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 6689 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6690 | // the SpawnedTestServer. By default, MockHostResolver maps all hosts |
| 6691 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6692 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6693 | |
| 6694 | // Force https for www.somewhere.com. |
| 6695 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 6696 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 6697 | bool include_subdomains = false; |
| 6698 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 6699 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6700 | |
| 6701 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 6702 | |
| 6703 | TestURLRequestContext context(true); |
| 6704 | context.set_host_resolver(&host_resolver); |
| 6705 | context.set_transport_security_state(&transport_security_state); |
| 6706 | context.set_network_delegate(&network_delegate); |
| 6707 | context.Init(); |
| 6708 | |
| 6709 | TestDelegate d; |
| 6710 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 6711 | // cause a certificate error. Ignore the error. |
| 6712 | d.set_allow_certificate_errors(true); |
| 6713 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6714 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6715 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
| 6716 | test_server.host_port_pair().port())), |
| 6717 | DEFAULT_PRIORITY, &d, NULL)); |
| 6718 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6719 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6720 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6721 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6722 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6723 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6724 | EXPECT_EQ("https", req->url().scheme()); |
| 6725 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6726 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 6727 | |
| 6728 | LoadTimingInfo load_timing_info; |
| 6729 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 6730 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 6731 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6732 | } |
| 6733 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 6734 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 6735 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 6736 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 6737 | |
| 6738 | SpawnedTestServer::SSLOptions ssl_options( |
| 6739 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 6740 | SpawnedTestServer test_server( |
| 6741 | SpawnedTestServer::TYPE_HTTPS, |
| 6742 | ssl_options, |
| 6743 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 6744 | ASSERT_TRUE(test_server.Start()); |
| 6745 | |
| 6746 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 6747 | // address, so a MockHostResolver is needed to redirect example.net to the |
| 6748 | // SpawnedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by default. |
| 6749 | MockHostResolver host_resolver; |
| 6750 | |
| 6751 | TransportSecurityState transport_security_state; |
| 6752 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 6753 | bool include_subdomains = false; |
| 6754 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 6755 | |
| 6756 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 6757 | |
| 6758 | MockCertVerifier cert_verifier; |
| 6759 | cert_verifier.set_default_result(OK); |
| 6760 | |
| 6761 | TestURLRequestContext context(true); |
| 6762 | context.set_host_resolver(&host_resolver); |
| 6763 | context.set_transport_security_state(&transport_security_state); |
| 6764 | context.set_network_delegate(&network_delegate); |
| 6765 | context.set_cert_verifier(&cert_verifier); |
| 6766 | context.Init(); |
| 6767 | |
| 6768 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 6769 | test_server.host_port_pair().port())); |
| 6770 | url::Replacements<char> replacements; |
| 6771 | const char kNewScheme[] = "https"; |
| 6772 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 6773 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 6774 | |
| 6775 | TestDelegate d; |
| 6776 | // Quit on redirect to allow response header inspection upon redirect. |
| 6777 | d.set_quit_on_redirect(true); |
| 6778 | |
| 6779 | scoped_ptr<URLRequest> req(context.CreateRequest(hsts_http_url, |
| 6780 | DEFAULT_PRIORITY, &d, NULL)); |
| 6781 | // Set Origin header to simulate a cross-origin request. |
| 6782 | HttpRequestHeaders request_headers; |
| 6783 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 6784 | req->SetExtraRequestHeaders(request_headers); |
| 6785 | |
| 6786 | req->Start(); |
| 6787 | base::RunLoop().Run(); |
| 6788 | |
| 6789 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6790 | |
| 6791 | const HttpResponseHeaders* headers = req->response_headers(); |
| 6792 | std::string redirect_location; |
| 6793 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 6794 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 6795 | |
| 6796 | std::string received_cors_header; |
| 6797 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 6798 | &received_cors_header)); |
| 6799 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 6800 | } |
| 6801 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6802 | namespace { |
| 6803 | |
| 6804 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 6805 | public: |
| 6806 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 6807 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 6808 | void OnCertificateRequested(URLRequest* request, |
| 6809 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6810 | on_certificate_requested_count_++; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6811 | base::MessageLoop::current()->Quit(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6812 | } |
| 6813 | int on_certificate_requested_count() { |
| 6814 | return on_certificate_requested_count_; |
| 6815 | } |
| 6816 | private: |
| 6817 | int on_certificate_requested_count_; |
| 6818 | }; |
| 6819 | |
| 6820 | } // namespace |
| 6821 | |
| 6822 | // TODO(davidben): Test the rest of the code. Specifically, |
| 6823 | // - Filtering which certificates to select. |
| 6824 | // - Sending a certificate back. |
| 6825 | // - Getting a certificate request in an SSL renegotiation sending the |
| 6826 | // HTTP request. |
| 6827 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6828 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6829 | ssl_options.request_client_certificate = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6830 | SpawnedTestServer test_server( |
| 6831 | SpawnedTestServer::TYPE_HTTPS, |
| 6832 | ssl_options, |
| 6833 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6834 | ASSERT_TRUE(test_server.Start()); |
| 6835 | |
| 6836 | SSLClientAuthTestDelegate d; |
| 6837 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6838 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6839 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6840 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6841 | r->Start(); |
| 6842 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6843 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6844 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6845 | |
| 6846 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 6847 | EXPECT_FALSE(d.received_data_before_response()); |
| 6848 | EXPECT_EQ(0, d.bytes_received()); |
| 6849 | |
| 6850 | // Send no certificate. |
| 6851 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 6852 | // all platforms so we can test sending a cert as well. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6853 | r->ContinueWithCertificate(NULL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6854 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6855 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6856 | |
| 6857 | EXPECT_EQ(1, d.response_started_count()); |
| 6858 | EXPECT_FALSE(d.received_data_before_response()); |
| 6859 | EXPECT_NE(0, d.bytes_received()); |
| 6860 | } |
| 6861 | } |
| 6862 | |
| 6863 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 6864 | // Test that we attempt a session resume when making two connections to the |
| 6865 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6866 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6867 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6868 | SpawnedTestServer test_server( |
| 6869 | SpawnedTestServer::TYPE_HTTPS, |
| 6870 | ssl_options, |
| 6871 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6872 | ASSERT_TRUE(test_server.Start()); |
| 6873 | |
| 6874 | SSLClientSocket::ClearSessionCache(); |
| 6875 | |
| 6876 | { |
| 6877 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6878 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6879 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6880 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6881 | r->Start(); |
| 6882 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6883 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6884 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6885 | |
| 6886 | EXPECT_EQ(1, d.response_started_count()); |
| 6887 | } |
| 6888 | |
| 6889 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 6890 | CloseAllConnections(); |
| 6891 | |
| 6892 | { |
| 6893 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6894 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6895 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6896 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6897 | r->Start(); |
| 6898 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6899 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6900 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6901 | |
| 6902 | // The response will look like; |
| 6903 | // insert abc |
| 6904 | // lookup abc |
| 6905 | // insert xyz |
| 6906 | // |
| 6907 | // With a newline at the end which makes the split think that there are |
| 6908 | // four lines. |
| 6909 | |
| 6910 | EXPECT_EQ(1, d.response_started_count()); |
| 6911 | std::vector<std::string> lines; |
| 6912 | base::SplitString(d.data_received(), '\n', &lines); |
| 6913 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 6914 | |
| 6915 | std::string session_id; |
| 6916 | |
| 6917 | for (size_t i = 0; i < 2; i++) { |
| 6918 | std::vector<std::string> parts; |
| 6919 | base::SplitString(lines[i], '\t', &parts); |
| 6920 | ASSERT_EQ(2u, parts.size()); |
| 6921 | if (i == 0) { |
| 6922 | EXPECT_EQ("insert", parts[0]); |
| 6923 | session_id = parts[1]; |
| 6924 | } else { |
| 6925 | EXPECT_EQ("lookup", parts[0]); |
| 6926 | EXPECT_EQ(session_id, parts[1]); |
| 6927 | } |
| 6928 | } |
| 6929 | } |
| 6930 | } |
| 6931 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 6932 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 6933 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 6934 | // that exactly two different sessions were inserted, with no lookups etc. |
| 6935 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
| 6936 | std::vector<std::string> lines; |
| 6937 | base::SplitString(session_info, '\n', &lines); |
| 6938 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 6939 | |
| 6940 | std::string session_id; |
| 6941 | for (size_t i = 0; i < 2; i++) { |
| 6942 | std::vector<std::string> parts; |
| 6943 | base::SplitString(lines[i], '\t', &parts); |
| 6944 | ASSERT_EQ(2u, parts.size()); |
| 6945 | EXPECT_EQ("insert", parts[0]); |
| 6946 | if (i == 0) { |
| 6947 | session_id = parts[1]; |
| 6948 | } else { |
| 6949 | EXPECT_NE(session_id, parts[1]); |
| 6950 | } |
| 6951 | } |
| 6952 | } |
| 6953 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6954 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 6955 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 6956 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6957 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6958 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6959 | SpawnedTestServer test_server( |
| 6960 | SpawnedTestServer::TYPE_HTTPS, |
| 6961 | ssl_options, |
| 6962 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6963 | ASSERT_TRUE(test_server.Start()); |
| 6964 | |
| 6965 | SSLClientSocket::ClearSessionCache(); |
| 6966 | |
| 6967 | { |
| 6968 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6969 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6970 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6971 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6972 | r->Start(); |
| 6973 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6974 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6975 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6976 | |
| 6977 | EXPECT_EQ(1, d.response_started_count()); |
| 6978 | } |
| 6979 | |
| 6980 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 6981 | HttpNetworkSession::Params params; |
| 6982 | params.host_resolver = default_context_.host_resolver(); |
| 6983 | params.cert_verifier = default_context_.cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 6984 | params.transport_security_state = default_context_.transport_security_state(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6985 | params.proxy_service = default_context_.proxy_service(); |
| 6986 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6987 | params.http_auth_handler_factory = |
| 6988 | default_context_.http_auth_handler_factory(); |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 6989 | params.network_delegate = &default_network_delegate_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6990 | params.http_server_properties = default_context_.http_server_properties(); |
| 6991 | params.ssl_session_cache_shard = "alternate"; |
| 6992 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6993 | scoped_ptr<HttpCache> cache(new HttpCache( |
| 6994 | new HttpNetworkSession(params), |
| 6995 | HttpCache::DefaultBackend::InMemory(0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6996 | |
| 6997 | default_context_.set_http_transaction_factory(cache.get()); |
| 6998 | |
| 6999 | { |
| 7000 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7001 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7002 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7003 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7004 | r->Start(); |
| 7005 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7006 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7007 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7008 | |
| 7009 | // The response will look like; |
| 7010 | // insert abc |
| 7011 | // insert xyz |
| 7012 | // |
| 7013 | // With a newline at the end which makes the split think that there are |
| 7014 | // three lines. |
| 7015 | |
| 7016 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7017 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7018 | } |
| 7019 | } |
| 7020 | |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 7021 | #if defined(OS_WIN) |
| 7022 | |
| 7023 | namespace { |
| 7024 | |
| 7025 | bool IsECDSACipherSuite(uint16_t cipher_suite) { |
| 7026 | const char* key_exchange; |
| 7027 | const char* cipher; |
| 7028 | const char* mac; |
| 7029 | bool is_aead; |
| 7030 | SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
| 7031 | return std::string(key_exchange).find("ECDSA") != std::string::npos; |
| 7032 | } |
| 7033 | |
| 7034 | } // namespace |
| 7035 | |
| 7036 | // Test that ECDSA is disabled on Windows XP, where ECDSA certificates cannot be |
| 7037 | // verified. |
davidben | 9399c95 | 2014-10-07 04:09:11 | [diff] [blame] | 7038 | TEST_F(HTTPSRequestTest, DisableECDSAOnXP) { |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 7039 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
| 7040 | LOG(INFO) << "Skipping test on this version."; |
| 7041 | return; |
| 7042 | } |
| 7043 | |
| 7044 | SpawnedTestServer test_server( |
| 7045 | SpawnedTestServer::TYPE_HTTPS, |
| 7046 | SpawnedTestServer::kLocalhost, |
| 7047 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7048 | ASSERT_TRUE(test_server.Start()); |
| 7049 | |
| 7050 | TestDelegate d; |
| 7051 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7052 | test_server.GetURL("client-cipher-list"), DEFAULT_PRIORITY, &d, NULL)); |
| 7053 | r->Start(); |
| 7054 | EXPECT_TRUE(r->is_pending()); |
| 7055 | |
| 7056 | base::RunLoop().Run(); |
| 7057 | |
| 7058 | EXPECT_EQ(1, d.response_started_count()); |
| 7059 | std::vector<std::string> lines; |
| 7060 | base::SplitString(d.data_received(), '\n', &lines); |
| 7061 | |
| 7062 | for (size_t i = 0; i < lines.size(); i++) { |
| 7063 | int cipher_suite; |
| 7064 | ASSERT_TRUE(base::StringToInt(lines[i], &cipher_suite)); |
| 7065 | EXPECT_FALSE(IsECDSACipherSuite(cipher_suite)) |
| 7066 | << "ClientHello advertised " << cipher_suite; |
| 7067 | } |
| 7068 | } |
| 7069 | |
| 7070 | #endif // OS_WIN |
| 7071 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7072 | class TestSSLConfigService : public SSLConfigService { |
| 7073 | public: |
| 7074 | TestSSLConfigService(bool ev_enabled, |
| 7075 | bool online_rev_checking, |
| 7076 | bool rev_checking_required_local_anchors) |
| 7077 | : ev_enabled_(ev_enabled), |
| 7078 | online_rev_checking_(online_rev_checking), |
| 7079 | rev_checking_required_local_anchors_( |
| 7080 | rev_checking_required_local_anchors), |
| 7081 | fallback_min_version_(0) {} |
| 7082 | |
| 7083 | void set_fallback_min_version(uint16 version) { |
| 7084 | fallback_min_version_ = version; |
| 7085 | } |
| 7086 | |
| 7087 | // SSLConfigService: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 7088 | void GetSSLConfig(SSLConfig* config) override { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7089 | *config = SSLConfig(); |
| 7090 | config->rev_checking_enabled = online_rev_checking_; |
| 7091 | config->verify_ev_cert = ev_enabled_; |
| 7092 | config->rev_checking_required_local_anchors = |
| 7093 | rev_checking_required_local_anchors_; |
| 7094 | if (fallback_min_version_) { |
| 7095 | config->version_fallback_min = fallback_min_version_; |
| 7096 | } |
| 7097 | } |
| 7098 | |
| 7099 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 7100 | ~TestSSLConfigService() override {} |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7101 | |
| 7102 | private: |
| 7103 | const bool ev_enabled_; |
| 7104 | const bool online_rev_checking_; |
| 7105 | const bool rev_checking_required_local_anchors_; |
| 7106 | uint16 fallback_min_version_; |
| 7107 | }; |
| 7108 | |
| 7109 | class FallbackTestURLRequestContext : public TestURLRequestContext { |
| 7110 | public: |
| 7111 | explicit FallbackTestURLRequestContext(bool delay_initialization) |
| 7112 | : TestURLRequestContext(delay_initialization) {} |
| 7113 | |
| 7114 | void set_fallback_min_version(uint16 version) { |
| 7115 | TestSSLConfigService *ssl_config_service = |
| 7116 | new TestSSLConfigService(true /* check for EV */, |
| 7117 | false /* online revocation checking */, |
| 7118 | false /* require rev. checking for local |
| 7119 | anchors */); |
| 7120 | ssl_config_service->set_fallback_min_version(version); |
| 7121 | set_ssl_config_service(ssl_config_service); |
| 7122 | } |
| 7123 | }; |
| 7124 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7125 | class HTTPSFallbackTest : public testing::Test { |
| 7126 | public: |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7127 | HTTPSFallbackTest() : context_(true) {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7128 | virtual ~HTTPSFallbackTest() {} |
| 7129 | |
| 7130 | protected: |
| 7131 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 7132 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7133 | context_.Init(); |
| 7134 | delegate_.set_allow_certificate_errors(true); |
| 7135 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7136 | SpawnedTestServer test_server( |
| 7137 | SpawnedTestServer::TYPE_HTTPS, |
| 7138 | ssl_options, |
| 7139 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7140 | ASSERT_TRUE(test_server.Start()); |
| 7141 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7142 | request_ = context_.CreateRequest( |
| 7143 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate_, NULL); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7144 | request_->Start(); |
| 7145 | |
| 7146 | base::RunLoop().Run(); |
| 7147 | } |
| 7148 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7149 | void set_fallback_min_version(uint16 version) { |
| 7150 | context_.set_fallback_min_version(version); |
| 7151 | } |
| 7152 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7153 | void ExpectConnection(int version) { |
| 7154 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 7155 | EXPECT_NE(0, delegate_.bytes_received()); |
| 7156 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 7157 | request_->ssl_info().connection_status)); |
| 7158 | EXPECT_TRUE(request_->ssl_info().connection_status & |
| 7159 | SSL_CONNECTION_VERSION_FALLBACK); |
| 7160 | } |
| 7161 | |
| 7162 | void ExpectFailure(int error) { |
| 7163 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 7164 | EXPECT_FALSE(request_->status().is_success()); |
| 7165 | EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status()); |
| 7166 | EXPECT_EQ(error, request_->status().error()); |
| 7167 | } |
| 7168 | |
| 7169 | private: |
| 7170 | TestDelegate delegate_; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7171 | FallbackTestURLRequestContext context_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7172 | scoped_ptr<URLRequest> request_; |
| 7173 | }; |
| 7174 | |
| 7175 | // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more |
| 7176 | // than necessary. |
| 7177 | TEST_F(HTTPSFallbackTest, TLSv1Fallback) { |
| 7178 | SpawnedTestServer::SSLOptions ssl_options( |
| 7179 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7180 | ssl_options.tls_intolerant = |
| 7181 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 7182 | |
| 7183 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7184 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
| 7185 | } |
| 7186 | |
| 7187 | // This test is disabled on Android because the remote test server doesn't cause |
| 7188 | // a TCP reset. |
| 7189 | #if !defined(OS_ANDROID) |
| 7190 | // Tests fallback to TLS 1.0 on connection reset. |
| 7191 | TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) { |
| 7192 | SpawnedTestServer::SSLOptions ssl_options( |
| 7193 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7194 | ssl_options.tls_intolerant = |
| 7195 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 7196 | ssl_options.tls_intolerance_type = |
| 7197 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 7198 | |
| 7199 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7200 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
| 7201 | } |
| 7202 | #endif // !OS_ANDROID |
| 7203 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7204 | // Tests that we don't fallback on handshake failure with servers that implement |
| 7205 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7206 | TEST_F(HTTPSFallbackTest, FallbackSCSV) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7207 | SpawnedTestServer::SSLOptions ssl_options( |
| 7208 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7209 | // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger |
| 7210 | // a version fallback. |
| 7211 | ssl_options.tls_intolerant = |
| 7212 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7213 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 7214 | // connections are rejected. |
| 7215 | ssl_options.fallback_scsv_enabled = true; |
| 7216 | |
| 7217 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7218 | |
| 7219 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version |
| 7220 | // intolerance. If the fallback SCSV is processed when the original error |
| 7221 | // that caused the fallback should be returned, which should be |
| 7222 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH. |
| 7223 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 7224 | } |
| 7225 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7226 | // Tests that we don't fallback on connection closed with servers that implement |
| 7227 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7228 | TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) { |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7229 | SpawnedTestServer::SSLOptions ssl_options( |
| 7230 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7231 | // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger |
| 7232 | // a version fallback. |
| 7233 | ssl_options.tls_intolerant = |
| 7234 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7235 | ssl_options.tls_intolerance_type = |
| 7236 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 7237 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 7238 | // connections are rejected. |
| 7239 | ssl_options.fallback_scsv_enabled = true; |
| 7240 | |
| 7241 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7242 | |
| 7243 | // The original error should be replayed on rejected fallback. |
| 7244 | ExpectFailure(ERR_CONNECTION_CLOSED); |
| 7245 | } |
| 7246 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7247 | // Tests that the SSLv3 fallback doesn't happen by default. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7248 | TEST_F(HTTPSFallbackTest, SSLv3Fallback) { |
| 7249 | SpawnedTestServer::SSLOptions ssl_options( |
| 7250 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7251 | ssl_options.tls_intolerant = |
| 7252 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7253 | |
| 7254 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7255 | ExpectFailure(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION); |
| 7256 | } |
| 7257 | |
| 7258 | // Tests that the SSLv3 fallback works when explicitly enabled. |
| 7259 | TEST_F(HTTPSFallbackTest, SSLv3FallbackEnabled) { |
| 7260 | SpawnedTestServer::SSLOptions ssl_options( |
| 7261 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7262 | ssl_options.tls_intolerant = |
| 7263 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7264 | set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3); |
| 7265 | |
| 7266 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7267 | ExpectConnection(SSL_CONNECTION_VERSION_SSL3); |
| 7268 | } |
| 7269 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7270 | // Tests that the SSLv3 fallback triggers on closed connections when explicitly |
| 7271 | // enabled. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7272 | TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) { |
| 7273 | SpawnedTestServer::SSLOptions ssl_options( |
| 7274 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7275 | ssl_options.tls_intolerant = |
| 7276 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7277 | ssl_options.tls_intolerance_type = |
| 7278 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7279 | set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7280 | |
| 7281 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7282 | ExpectConnection(SSL_CONNECTION_VERSION_SSL3); |
| 7283 | } |
| 7284 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7285 | // Test that SSLv3 fallback probe connections don't cause sessions to be cached. |
| 7286 | TEST_F(HTTPSRequestTest, SSLv3FallbackNoCache) { |
| 7287 | SpawnedTestServer::SSLOptions ssl_options( |
| 7288 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7289 | ssl_options.tls_intolerant = |
| 7290 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7291 | ssl_options.tls_intolerance_type = |
| 7292 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 7293 | ssl_options.record_resume = true; |
| 7294 | |
| 7295 | SpawnedTestServer test_server( |
| 7296 | SpawnedTestServer::TYPE_HTTPS, |
| 7297 | ssl_options, |
| 7298 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7299 | ASSERT_TRUE(test_server.Start()); |
| 7300 | |
| 7301 | SSLClientSocket::ClearSessionCache(); |
| 7302 | |
| 7303 | // Make a connection that does a probe fallback to SSLv3 but fails because |
| 7304 | // SSLv3 fallback is disabled. We don't wish a session for this connection to |
| 7305 | // be inserted locally. |
| 7306 | { |
| 7307 | TestDelegate delegate; |
| 7308 | FallbackTestURLRequestContext context(true); |
| 7309 | |
| 7310 | context.Init(); |
| 7311 | scoped_ptr<URLRequest> request(context.CreateRequest( |
| 7312 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate, NULL)); |
| 7313 | request->Start(); |
| 7314 | |
| 7315 | base::RunLoop().Run(); |
| 7316 | |
| 7317 | EXPECT_EQ(1, delegate.response_started_count()); |
| 7318 | EXPECT_FALSE(request->status().is_success()); |
| 7319 | EXPECT_EQ(URLRequestStatus::FAILED, request->status().status()); |
| 7320 | EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION, |
| 7321 | request->status().error()); |
| 7322 | } |
| 7323 | |
| 7324 | // Now allow SSLv3 connections and request the session cache log. |
| 7325 | { |
| 7326 | TestDelegate delegate; |
| 7327 | FallbackTestURLRequestContext context(true); |
| 7328 | context.set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3); |
| 7329 | |
| 7330 | context.Init(); |
| 7331 | scoped_ptr<URLRequest> request( |
| 7332 | context.CreateRequest(test_server.GetURL("ssl-session-cache"), |
| 7333 | DEFAULT_PRIORITY, |
| 7334 | &delegate, |
| 7335 | NULL)); |
| 7336 | request->Start(); |
| 7337 | |
| 7338 | base::RunLoop().Run(); |
| 7339 | |
| 7340 | EXPECT_EQ(1, delegate.response_started_count()); |
| 7341 | EXPECT_NE(0, delegate.bytes_received()); |
| 7342 | EXPECT_EQ(SSL_CONNECTION_VERSION_SSL3, SSLConnectionStatusToVersion( |
| 7343 | request->ssl_info().connection_status)); |
| 7344 | EXPECT_TRUE(request->ssl_info().connection_status & |
| 7345 | SSL_CONNECTION_VERSION_FALLBACK); |
| 7346 | |
| 7347 | std::vector<std::string> lines; |
| 7348 | // If no sessions were cached then the server should have seen two sessions |
| 7349 | // inserted with no lookups. |
| 7350 | AssertTwoDistinctSessionsInserted(delegate.data_received()); |
| 7351 | } |
| 7352 | } |
| 7353 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7354 | // This test is disabled on Android because the remote test server doesn't cause |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 7355 | // a TCP reset. |
| 7356 | #if !defined(OS_ANDROID) |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7357 | // Tests that a reset connection does not fallback down to SSL3. |
| 7358 | TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) { |
| 7359 | SpawnedTestServer::SSLOptions ssl_options( |
| 7360 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7361 | ssl_options.tls_intolerant = |
| 7362 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7363 | ssl_options.tls_intolerance_type = |
| 7364 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 7365 | |
| 7366 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7367 | ExpectFailure(ERR_CONNECTION_RESET); |
| 7368 | } |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 7369 | #endif // !OS_ANDROID |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7370 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7371 | class HTTPSSessionTest : public testing::Test { |
| 7372 | public: |
| 7373 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7374 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7375 | |
| 7376 | default_context_.set_network_delegate(&default_network_delegate_); |
| 7377 | default_context_.set_cert_verifier(&cert_verifier_); |
| 7378 | default_context_.Init(); |
| 7379 | } |
| 7380 | virtual ~HTTPSSessionTest() {} |
| 7381 | |
| 7382 | protected: |
| 7383 | MockCertVerifier cert_verifier_; |
| 7384 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 7385 | TestURLRequestContext default_context_; |
| 7386 | }; |
| 7387 | |
| 7388 | // Tests that session resumption is not attempted if an invalid certificate |
| 7389 | // is presented. |
| 7390 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 7391 | SpawnedTestServer::SSLOptions ssl_options; |
| 7392 | ssl_options.record_resume = true; |
| 7393 | SpawnedTestServer test_server( |
| 7394 | SpawnedTestServer::TYPE_HTTPS, |
| 7395 | ssl_options, |
| 7396 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7397 | ASSERT_TRUE(test_server.Start()); |
| 7398 | |
| 7399 | SSLClientSocket::ClearSessionCache(); |
| 7400 | |
| 7401 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7402 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7403 | { |
| 7404 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7405 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7406 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7407 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7408 | r->Start(); |
| 7409 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7410 | |
| 7411 | base::RunLoop().Run(); |
| 7412 | |
| 7413 | EXPECT_EQ(1, d.response_started_count()); |
| 7414 | } |
| 7415 | |
| 7416 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 7417 | CloseAllConnections(); |
| 7418 | |
| 7419 | // Now change the certificate to be acceptable (so that the response is |
| 7420 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7421 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7422 | { |
| 7423 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7424 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7425 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7426 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7427 | r->Start(); |
| 7428 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7429 | |
| 7430 | base::RunLoop().Run(); |
| 7431 | |
| 7432 | // The response will look like; |
| 7433 | // insert abc |
| 7434 | // insert xyz |
| 7435 | // |
| 7436 | // With a newline at the end which makes the split think that there are |
| 7437 | // three lines. |
| 7438 | // |
| 7439 | // If a session was presented (eg: a bug), then the response would look |
| 7440 | // like; |
| 7441 | // insert abc |
| 7442 | // lookup abc |
| 7443 | // insert xyz |
| 7444 | |
| 7445 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7446 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7447 | } |
| 7448 | } |
| 7449 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7450 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 7451 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 7452 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7453 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 7454 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 7455 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7456 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 7457 | // testserver. |
| 7458 | static const SHA256HashValue kOCSPTestCertSPKI = { { |
| 7459 | 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, |
| 7460 | 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, |
| 7461 | 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, |
| 7462 | 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, |
| 7463 | } }; |
| 7464 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7465 | // This is the policy OID contained in the certificates that testserver |
| 7466 | // generates. |
| 7467 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 7468 | |
| 7469 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 7470 | public: |
| 7471 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7472 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7473 | ev_test_policy_( |
| 7474 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 7475 | kOCSPTestCertFingerprint, |
| 7476 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7477 | } |
| 7478 | |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 7479 | virtual void SetUp() override { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7480 | SetupContext(&context_); |
| 7481 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7482 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7483 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7484 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 7485 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7486 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7487 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 7488 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7489 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7490 | EnsureNSSHttpIOInit(); |
| 7491 | #endif |
| 7492 | } |
| 7493 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7494 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7495 | CertStatus* out_cert_status) { |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7496 | // We always overwrite out_cert_status. |
| 7497 | *out_cert_status = 0; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7498 | SpawnedTestServer test_server( |
| 7499 | SpawnedTestServer::TYPE_HTTPS, |
| 7500 | ssl_options, |
| 7501 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7502 | ASSERT_TRUE(test_server.Start()); |
| 7503 | |
| 7504 | TestDelegate d; |
| 7505 | d.set_allow_certificate_errors(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7506 | scoped_ptr<URLRequest> r(context_.CreateRequest( |
| 7507 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
| 7508 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7509 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7510 | base::RunLoop().Run(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7511 | |
| 7512 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7513 | *out_cert_status = r->ssl_info().cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7514 | } |
| 7515 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 7516 | virtual ~HTTPSOCSPTest() { |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 7517 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7518 | ShutdownNSSHttpIO(); |
| 7519 | #endif |
| 7520 | } |
| 7521 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7522 | protected: |
| 7523 | // SetupContext configures the URLRequestContext that will be used for making |
| 7524 | // connetions to testserver. This can be overridden in test subclasses for |
| 7525 | // different behaviour. |
| 7526 | virtual void SetupContext(URLRequestContext* context) { |
| 7527 | context->set_ssl_config_service( |
| 7528 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7529 | true /* online revocation checking */, |
| 7530 | false /* require rev. checking for local |
| 7531 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7532 | } |
| 7533 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7534 | scoped_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7535 | TestURLRequestContext context_; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7536 | scoped_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7537 | }; |
| 7538 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7539 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 7540 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7541 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 7542 | // have that ability on other platforms. |
| 7543 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 7544 | #else |
| 7545 | return 0; |
| 7546 | #endif |
| 7547 | } |
| 7548 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7549 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 7550 | // operating system supports revocation checking and can distinguish between |
| 7551 | // situations where a given certificate lacks any revocation information (eg: |
| 7552 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 7553 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 7554 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 7555 | // skipped. |
| 7556 | static bool SystemSupportsHardFailRevocationChecking() { |
| 7557 | #if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS) |
| 7558 | return true; |
| 7559 | #else |
| 7560 | return false; |
| 7561 | #endif |
| 7562 | } |
| 7563 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7564 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 7565 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 7566 | // several tests are effected because our testing EV certificate won't be |
| 7567 | // recognised as EV. |
| 7568 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 7569 | #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 7570 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 7571 | return false; |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 7572 | #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 7573 | // On OS X and Android, we use the system to tell us whether a certificate is |
| 7574 | // EV or not and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 7575 | return false; |
| 7576 | #else |
| 7577 | return true; |
| 7578 | #endif |
| 7579 | } |
| 7580 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7581 | static bool SystemSupportsOCSP() { |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 7582 | #if defined(USE_OPENSSL) |
| 7583 | // http://crbug.com/117478 - OpenSSL does not support OCSP. |
| 7584 | return false; |
| 7585 | #elif defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7586 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 7587 | #elif defined(OS_ANDROID) |
| 7588 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 7589 | return false; |
| 7590 | #else |
| 7591 | return true; |
| 7592 | #endif |
| 7593 | } |
| 7594 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7595 | TEST_F(HTTPSOCSPTest, Valid) { |
| 7596 | if (!SystemSupportsOCSP()) { |
| 7597 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7598 | return; |
| 7599 | } |
| 7600 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7601 | SpawnedTestServer::SSLOptions ssl_options( |
| 7602 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7603 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7604 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7605 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7606 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7607 | |
| 7608 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7609 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7610 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7611 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7612 | |
| 7613 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7614 | } |
| 7615 | |
| 7616 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 7617 | if (!SystemSupportsOCSP()) { |
| 7618 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7619 | return; |
| 7620 | } |
| 7621 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7622 | SpawnedTestServer::SSLOptions ssl_options( |
| 7623 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7624 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7625 | |
| 7626 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7627 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7628 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 7629 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7630 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 7631 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7632 | #endif |
| 7633 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7634 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7635 | } |
| 7636 | |
| 7637 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 7638 | if (!SystemSupportsOCSP()) { |
| 7639 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7640 | return; |
| 7641 | } |
| 7642 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7643 | SpawnedTestServer::SSLOptions ssl_options( |
| 7644 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7645 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7646 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7647 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7648 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7649 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7650 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7651 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7652 | |
| 7653 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 7654 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7655 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7656 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7657 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7658 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 7659 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 7660 | void SetupContext(URLRequestContext* context) override { |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7661 | context->set_ssl_config_service( |
| 7662 | new TestSSLConfigService(false /* check for EV */, |
| 7663 | false /* online revocation checking */, |
| 7664 | true /* require rev. checking for local |
| 7665 | anchors */)); |
| 7666 | } |
| 7667 | }; |
| 7668 | |
| 7669 | |
| 7670 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 7671 | if (!SystemSupportsOCSP()) { |
| 7672 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7673 | return; |
| 7674 | } |
| 7675 | |
| 7676 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 7677 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 7678 | << "revocation checking"; |
| 7679 | return; |
| 7680 | } |
| 7681 | |
| 7682 | SpawnedTestServer::SSLOptions ssl_options( |
| 7683 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7684 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 7685 | |
| 7686 | CertStatus cert_status; |
| 7687 | DoConnection(ssl_options, &cert_status); |
| 7688 | |
| 7689 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 7690 | cert_status & CERT_STATUS_REVOKED); |
| 7691 | |
| 7692 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 7693 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7694 | } |
| 7695 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7696 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 7697 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 7698 | void SetupContext(URLRequestContext* context) override { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7699 | context->set_ssl_config_service( |
| 7700 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7701 | false /* online revocation checking */, |
| 7702 | false /* require rev. checking for local |
| 7703 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7704 | } |
| 7705 | }; |
| 7706 | |
| 7707 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 7708 | if (!SystemSupportsOCSP()) { |
| 7709 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7710 | return; |
| 7711 | } |
| 7712 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7713 | SpawnedTestServer::SSLOptions ssl_options( |
| 7714 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7715 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7716 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7717 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7718 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7719 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7720 | |
| 7721 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7722 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 7723 | |
| 7724 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7725 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7726 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7727 | } |
| 7728 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 7729 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 7730 | if (!SystemSupportsOCSP()) { |
| 7731 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7732 | return; |
| 7733 | } |
| 7734 | |
| 7735 | SpawnedTestServer::SSLOptions ssl_options( |
| 7736 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7737 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 7738 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7739 | |
| 7740 | CertStatus cert_status; |
| 7741 | DoConnection(ssl_options, &cert_status); |
| 7742 | |
| 7743 | // Currently only works for Windows. When using NSS or OS X, it's not |
| 7744 | // possible to determine whether the check failed because of actual |
| 7745 | // revocation or because there was an OCSP failure. |
| 7746 | #if defined(OS_WIN) |
| 7747 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7748 | #else |
| 7749 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7750 | #endif |
| 7751 | |
| 7752 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7753 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7754 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7755 | } |
| 7756 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7757 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 7758 | if (!SystemSupportsOCSP()) { |
| 7759 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7760 | return; |
| 7761 | } |
| 7762 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7763 | SpawnedTestServer::SSLOptions ssl_options( |
| 7764 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7765 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7766 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7767 | |
| 7768 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7769 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7770 | |
| 7771 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7772 | |
| 7773 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7774 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7775 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7776 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7777 | } |
| 7778 | |
| 7779 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 7780 | if (!SystemSupportsOCSP()) { |
| 7781 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7782 | return; |
| 7783 | } |
| 7784 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7785 | SpawnedTestServer::SSLOptions ssl_options( |
| 7786 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7787 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7788 | SSLConfigService::SetCRLSet( |
| 7789 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7790 | |
| 7791 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7792 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7793 | |
| 7794 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7795 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 7796 | |
| 7797 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7798 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7799 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7800 | } |
| 7801 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7802 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 7803 | if (!SystemSupportsOCSP()) { |
| 7804 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7805 | return; |
| 7806 | } |
| 7807 | |
| 7808 | SpawnedTestServer::SSLOptions ssl_options( |
| 7809 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7810 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 7811 | SSLConfigService::SetCRLSet( |
| 7812 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 7813 | false, &kOCSPTestCertSPKI, ""))); |
| 7814 | |
| 7815 | CertStatus cert_status; |
| 7816 | DoConnection(ssl_options, &cert_status); |
| 7817 | |
| 7818 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 7819 | // revocation check for EV. |
| 7820 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7821 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7822 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 7823 | EXPECT_FALSE( |
| 7824 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7825 | } |
| 7826 | |
| 7827 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 7828 | if (!SystemSupportsOCSP()) { |
| 7829 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7830 | return; |
| 7831 | } |
| 7832 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7833 | SpawnedTestServer::SSLOptions ssl_options( |
| 7834 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7835 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7836 | SSLConfigService::SetCRLSet( |
| 7837 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 7838 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7839 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7840 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7841 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7842 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 7843 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 7844 | // test. |
| 7845 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7846 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7847 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7848 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7849 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7850 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7851 | } |
| 7852 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7853 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 7854 | // Test that when EV verification is requested, but online revocation |
| 7855 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 7856 | // no revocation checking actually happens. |
| 7857 | if (!SystemSupportsOCSP()) { |
| 7858 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7859 | return; |
| 7860 | } |
| 7861 | |
| 7862 | // Unmark the certificate's OID as EV, which should disable revocation |
| 7863 | // checking (as per the user preference) |
| 7864 | ev_test_policy_.reset(); |
| 7865 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7866 | SpawnedTestServer::SSLOptions ssl_options( |
| 7867 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7868 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7869 | SSLConfigService::SetCRLSet( |
| 7870 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7871 | |
| 7872 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7873 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7874 | |
| 7875 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7876 | |
| 7877 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7878 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7879 | } |
| 7880 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7881 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 7882 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame^] | 7883 | void SetupContext(URLRequestContext* context) override { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7884 | context->set_ssl_config_service( |
| 7885 | new TestSSLConfigService(false /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7886 | false /* online revocation checking */, |
| 7887 | false /* require rev. checking for local |
| 7888 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7889 | } |
| 7890 | }; |
| 7891 | |
| 7892 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7893 | SpawnedTestServer::SSLOptions ssl_options( |
| 7894 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7895 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7896 | SSLConfigService::SetCRLSet( |
| 7897 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7898 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7899 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7900 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7901 | |
| 7902 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 7903 | // we don't fall back to online revocation checks. |
| 7904 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7905 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7906 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7907 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7908 | |
| 7909 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 7910 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7911 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 7912 | return; |
| 7913 | #endif |
| 7914 | |
| 7915 | SpawnedTestServer::SSLOptions ssl_options( |
| 7916 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7917 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 7918 | ssl_options.cert_serial = 10; |
| 7919 | SSLConfigService::SetCRLSet( |
| 7920 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 7921 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 7922 | |
| 7923 | CertStatus cert_status = 0; |
| 7924 | DoConnection(ssl_options, &cert_status); |
| 7925 | |
| 7926 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 7927 | // reflected without online revocation checking. |
| 7928 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7929 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7930 | EXPECT_FALSE( |
| 7931 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7932 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7933 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7934 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7935 | #if !defined(DISABLE_FTP_SUPPORT) |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7936 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7937 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 7938 | URLRequestTestFTP() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7939 | : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7940 | base::FilePath()) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7941 | } |
| 7942 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7943 | protected: |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7944 | SpawnedTestServer test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7945 | }; |
| 7946 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7947 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 7948 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7949 | ASSERT_TRUE(test_server_.Start()); |
| 7950 | |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 7951 | URLRequestJobFactoryImpl job_factory; |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7952 | FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7953 | |
| 7954 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7955 | job_factory.SetProtocolHandler( |
| 7956 | "ftp", |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7957 | new FtpProtocolHandler(&ftp_transaction_factory)); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7958 | default_context_.set_job_factory(&job_factory); |
| 7959 | |
| 7960 | TestDelegate d; |
| 7961 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7962 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7963 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 7964 | r->Start(); |
| 7965 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7966 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7967 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7968 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7969 | EXPECT_FALSE(r->is_pending()); |
| 7970 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 7971 | EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7972 | } |
| 7973 | } |
| 7974 | |
[email protected] | 5accf733 | 2009-11-24 03:41:38 | [diff] [blame] | 7975 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7976 | TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7977 | ASSERT_TRUE(test_server_.Start()); |
| 7978 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7979 | TestDelegate d; |
| 7980 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7981 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7982 | test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, NULL)); |
| 7983 | r->Start(); |
| 7984 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7985 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7986 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7987 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7988 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7989 | EXPECT_EQ(1, d.response_started_count()); |
| 7990 | EXPECT_FALSE(d.received_data_before_response()); |
| 7991 | EXPECT_LT(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7992 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7993 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7994 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7995 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7996 | } |
| 7997 | } |
| 7998 | |
[email protected] | 7df7001 | 2010-02-04 00:09:55 | [diff] [blame] | 7999 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8000 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8001 | ASSERT_TRUE(test_server_.Start()); |
| 8002 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8003 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8004 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 8005 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8006 | TestDelegate d; |
| 8007 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8008 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8009 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, &d, NULL)); |
| 8010 | r->Start(); |
| 8011 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8012 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8013 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8014 | |
| 8015 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8016 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8017 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8018 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8019 | EXPECT_EQ(1, d.response_started_count()); |
| 8020 | EXPECT_FALSE(d.received_data_before_response()); |
| 8021 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8022 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8023 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8024 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8025 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8026 | } |
| 8027 | } |
| 8028 | |
[email protected] | e9ecbd1 | 2009-12-20 18:44:40 | [diff] [blame] | 8029 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8030 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8031 | ASSERT_TRUE(test_server_.Start()); |
| 8032 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8033 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8034 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 8035 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8036 | TestDelegate d; |
| 8037 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8038 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8039 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8040 | DEFAULT_PRIORITY, &d, NULL)); |
| 8041 | r->Start(); |
| 8042 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8043 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8044 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8045 | |
| 8046 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8047 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8048 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8049 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8050 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8051 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8052 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8053 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8054 | EXPECT_EQ(1, d.response_started_count()); |
| 8055 | EXPECT_FALSE(d.received_data_before_response()); |
| 8056 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8057 | |
| 8058 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8059 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8060 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8061 | } |
| 8062 | } |
| 8063 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 8064 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8065 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8066 | ASSERT_TRUE(test_server_.Start()); |
| 8067 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8068 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8069 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 8070 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8071 | TestDelegate d; |
| 8072 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8073 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8074 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 8075 | "wrong_password"), |
| 8076 | DEFAULT_PRIORITY, &d, NULL)); |
| 8077 | r->Start(); |
| 8078 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8079 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8080 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8081 | |
| 8082 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8083 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8084 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8085 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8086 | EXPECT_EQ(1, d.response_started_count()); |
| 8087 | EXPECT_FALSE(d.received_data_before_response()); |
| 8088 | EXPECT_EQ(d.bytes_received(), 0); |
| 8089 | } |
| 8090 | } |
| 8091 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 8092 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8093 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8094 | ASSERT_TRUE(test_server_.Start()); |
| 8095 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8096 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8097 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 8098 | app_path = app_path.AppendASCII("LICENSE"); |
| 8099 | TestDelegate d; |
| 8100 | // Set correct login credentials. The delegate will be asked for them when |
| 8101 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 8102 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8103 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8104 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8105 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 8106 | "wrong_password"), |
| 8107 | DEFAULT_PRIORITY, &d, NULL)); |
| 8108 | r->Start(); |
| 8109 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8110 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8111 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8112 | |
| 8113 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8114 | base::GetFileSize(app_path, &file_size); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8115 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8116 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8117 | EXPECT_EQ(1, d.response_started_count()); |
| 8118 | EXPECT_FALSE(d.received_data_before_response()); |
| 8119 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 8120 | } |
| 8121 | } |
| 8122 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 8123 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8124 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8125 | ASSERT_TRUE(test_server_.Start()); |
| 8126 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8127 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8128 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 8129 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8130 | TestDelegate d; |
| 8131 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8132 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8133 | test_server_.GetURLWithUserAndPassword("/LICENSE", "wrong_user", |
| 8134 | "chrome"), |
| 8135 | DEFAULT_PRIORITY, &d, NULL)); |
| 8136 | r->Start(); |
| 8137 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8138 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8139 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8140 | |
| 8141 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8142 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8143 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8144 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8145 | EXPECT_EQ(1, d.response_started_count()); |
| 8146 | EXPECT_FALSE(d.received_data_before_response()); |
| 8147 | EXPECT_EQ(d.bytes_received(), 0); |
| 8148 | } |
| 8149 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8150 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 8151 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8152 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8153 | ASSERT_TRUE(test_server_.Start()); |
| 8154 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8155 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8156 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 8157 | app_path = app_path.AppendASCII("LICENSE"); |
| 8158 | TestDelegate d; |
| 8159 | // Set correct login credentials. The delegate will be asked for them when |
| 8160 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 8161 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8162 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8163 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8164 | test_server_.GetURLWithUserAndPassword("/LICENSE", "wrong_user", |
| 8165 | "chrome"), |
| 8166 | DEFAULT_PRIORITY, &d, NULL)); |
| 8167 | r->Start(); |
| 8168 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8169 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8170 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8171 | |
| 8172 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8173 | base::GetFileSize(app_path, &file_size); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8174 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8175 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8176 | EXPECT_EQ(1, d.response_started_count()); |
| 8177 | EXPECT_FALSE(d.received_data_before_response()); |
| 8178 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 8179 | } |
| 8180 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8181 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 8182 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8183 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8184 | ASSERT_TRUE(test_server_.Start()); |
| 8185 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8186 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8187 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 8188 | app_path = app_path.AppendASCII("LICENSE"); |
| 8189 | |
| 8190 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 8191 | { |
| 8192 | // Pass correct login identity in the URL. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8193 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 8194 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8195 | DEFAULT_PRIORITY, d.get(), NULL)); |
| 8196 | r->Start(); |
| 8197 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8198 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8199 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8200 | |
| 8201 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8202 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8203 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8204 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8205 | EXPECT_EQ(1, d->response_started_count()); |
| 8206 | EXPECT_FALSE(d->received_data_before_response()); |
| 8207 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 8208 | } |
| 8209 | |
| 8210 | d.reset(new TestDelegate); |
| 8211 | { |
| 8212 | // This request should use cached identity from previous request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8213 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8214 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get(), NULL)); |
| 8215 | r->Start(); |
| 8216 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8217 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8218 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8219 | |
| 8220 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8221 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8222 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8223 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8224 | EXPECT_EQ(1, d->response_started_count()); |
| 8225 | EXPECT_FALSE(d->received_data_before_response()); |
| 8226 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 8227 | } |
| 8228 | } |
| 8229 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 8230 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8231 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8232 | ASSERT_TRUE(test_server_.Start()); |
| 8233 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8234 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8235 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 8236 | app_path = app_path.AppendASCII("LICENSE"); |
| 8237 | |
| 8238 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 8239 | // Set correct login credentials. The delegate will be asked for them when |
| 8240 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 8241 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8242 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8243 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8244 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 8245 | "wrong_password"), |
| 8246 | DEFAULT_PRIORITY, d.get(), NULL)); |
| 8247 | r->Start(); |
| 8248 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8249 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8250 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8251 | |
| 8252 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8253 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8254 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8255 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8256 | EXPECT_EQ(1, d->response_started_count()); |
| 8257 | EXPECT_FALSE(d->received_data_before_response()); |
| 8258 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 8259 | } |
| 8260 | |
| 8261 | // Use a new delegate without explicit credentials. The cached ones should be |
| 8262 | // used. |
| 8263 | d.reset(new TestDelegate); |
| 8264 | { |
| 8265 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 8266 | // ones. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8267 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8268 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get(), NULL)); |
| 8269 | r->Start(); |
| 8270 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8271 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8272 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8273 | |
| 8274 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8275 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8276 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8277 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8278 | EXPECT_EQ(1, d->response_started_count()); |
| 8279 | EXPECT_FALSE(d->received_data_before_response()); |
| 8280 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 8281 | } |
| 8282 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8283 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 8284 | |
| 8285 | } // namespace net |