[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" |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 18 | #include "base/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" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 21 | #include "base/memory/weak_ptr.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 22 | #include "base/message_loop/message_loop.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 23 | #include "base/message_loop/message_loop_proxy.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 24 | #include "base/path_service.h" |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 25 | #include "base/run_loop.h" |
[email protected] | 4dc3ad4f | 2013-06-11 07:15:50 | [diff] [blame] | 26 | #include "base/strings/string_number_conversions.h" |
[email protected] | d069c11a | 2013-04-13 00:01:55 | [diff] [blame] | 27 | #include "base/strings/string_piece.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 28 | #include "base/strings/string_split.h" |
[email protected] | 7f86564d | 2013-07-18 00:41:22 | [diff] [blame] | 29 | #include "base/strings/string_util.h" |
| 30 | #include "base/strings/stringprintf.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 31 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 32 | #include "net/base/capturing_net_log.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 34 | #include "net/base/load_timing_info.h" |
| 35 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 36 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 37 | #include "net/base/net_log.h" |
| 38 | #include "net/base/net_log_unittest.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 39 | #include "net/base/net_module.h" |
| 40 | #include "net/base/net_util.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 41 | #include "net/base/request_priority.h" |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 42 | #include "net/base/test_data_directory.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 43 | #include "net/base/upload_bytes_element_reader.h" |
| 44 | #include "net/base/upload_data_stream.h" |
| 45 | #include "net/base/upload_file_element_reader.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 46 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 47 | #include "net/cert/mock_cert_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 48 | #include "net/cert/test_root_certs.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 49 | #include "net/cookies/cookie_monster.h" |
| 50 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 51 | #include "net/disk_cache/disk_cache.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 52 | #include "net/dns/mock_host_resolver.h" |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 53 | #include "net/ftp/ftp_network_layer.h" |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 54 | #include "net/http/http_byte_range.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | #include "net/http/http_cache.h" |
| 56 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 57 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 58 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 59 | #include "net/http/http_response_headers.h" |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame^] | 60 | #include "net/http/http_util.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 61 | #include "net/ocsp/nss_ocsp.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 62 | #include "net/proxy/proxy_service.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 63 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 64 | #include "net/ssl/ssl_connection_status_flags.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 65 | #include "net/test/cert_test_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 66 | #include "net/test/spawned_test_server/spawned_test_server.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 67 | #include "net/url_request/data_protocol_handler.h" |
| 68 | #include "net/url_request/file_protocol_handler.h" |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 69 | #include "net/url_request/ftp_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 70 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 71 | #include "net/url_request/url_request.h" |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 72 | #include "net/url_request/url_request_file_dir_job.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 73 | #include "net/url_request/url_request_http_job.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 74 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 75 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 76 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 77 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 78 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 79 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 81 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 82 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 83 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 84 | #include "base/win/windows_version.h" |
| 85 | #endif |
| 86 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 87 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 88 | using base::Time; |
| 89 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 90 | namespace net { |
| 91 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 92 | namespace { |
| 93 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 94 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 95 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 96 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 97 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 98 | // Tests load timing information in the case a fresh connection was used, with |
| 99 | // no proxy. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 100 | void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info, |
| 101 | int connect_timing_flags) { |
| 102 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 103 | EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 104 | |
| 105 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 106 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 107 | |
| 108 | EXPECT_LE(load_timing_info.request_start, |
| 109 | load_timing_info.connect_timing.connect_start); |
| 110 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 111 | connect_timing_flags); |
| 112 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 113 | load_timing_info.send_start); |
| 114 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 115 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 116 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 117 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 118 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 119 | } |
| 120 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 121 | // Same as above, but with proxy times. |
| 122 | void TestLoadTimingNotReusedWithProxy( |
| 123 | const net::LoadTimingInfo& load_timing_info, |
| 124 | int connect_timing_flags) { |
| 125 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 126 | EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 127 | |
| 128 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 129 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 130 | |
| 131 | EXPECT_LE(load_timing_info.request_start, |
| 132 | load_timing_info.proxy_resolve_start); |
| 133 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 134 | load_timing_info.proxy_resolve_end); |
| 135 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 136 | load_timing_info.connect_timing.connect_start); |
| 137 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 138 | connect_timing_flags); |
| 139 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 140 | load_timing_info.send_start); |
| 141 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 142 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 143 | } |
| 144 | |
| 145 | // Same as above, but with a reused socket and proxy times. |
| 146 | void TestLoadTimingReusedWithProxy( |
| 147 | const net::LoadTimingInfo& load_timing_info) { |
| 148 | EXPECT_TRUE(load_timing_info.socket_reused); |
| 149 | EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 150 | |
| 151 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 152 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 153 | |
| 154 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 155 | |
| 156 | EXPECT_LE(load_timing_info.request_start, |
| 157 | load_timing_info.proxy_resolve_start); |
| 158 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 159 | load_timing_info.proxy_resolve_end); |
| 160 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 161 | load_timing_info.send_start); |
| 162 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 163 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 164 | } |
| 165 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 166 | // Tests load timing information in the case of a cache hit, when no cache |
| 167 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 168 | base::StringPiece TestNetResourceProvider(int key) { |
| 169 | return "header"; |
| 170 | } |
| 171 | |
| 172 | void FillBuffer(char* buffer, size_t len) { |
| 173 | static bool called = false; |
| 174 | if (!called) { |
| 175 | called = true; |
| 176 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 177 | srand(seed); |
| 178 | } |
| 179 | |
| 180 | for (size_t i = 0; i < len; i++) { |
| 181 | buffer[i] = static_cast<char>(rand()); |
| 182 | if (!buffer[i]) |
| 183 | buffer[i] = 'g'; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | #if !defined(OS_IOS) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 188 | void TestLoadTimingCacheHitNoNetwork( |
| 189 | const net::LoadTimingInfo& load_timing_info) { |
| 190 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 191 | EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 192 | |
| 193 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 194 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 195 | |
| 196 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 197 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 198 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 199 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 200 | |
| 201 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 202 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 203 | } |
| 204 | |
| 205 | // Tests load timing in the case that there is no HTTP response. This can be |
| 206 | // used to test in the case of errors or non-HTTP requests. |
| 207 | void TestLoadTimingNoHttpResponse( |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 208 | const net::LoadTimingInfo& load_timing_info) { |
| 209 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 210 | EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 211 | |
| 212 | // Only the request times should be non-null. |
| 213 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 214 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 215 | |
| 216 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 217 | |
| 218 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 219 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 220 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 221 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 222 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 223 | } |
| 224 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 225 | // Do a case-insensitive search through |haystack| for |needle|. |
| 226 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 227 | std::string::const_iterator it = |
| 228 | std::search(haystack.begin(), |
| 229 | haystack.end(), |
| 230 | needle, |
| 231 | needle + strlen(needle), |
[email protected] | 07f1cee | 2010-11-03 03:53:35 | [diff] [blame] | 232 | base::CaseInsensitiveCompare<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 233 | return it != haystack.end(); |
| 234 | } |
| 235 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 236 | UploadDataStream* CreateSimpleUploadData(const char* data) { |
| 237 | scoped_ptr<UploadElementReader> reader( |
| 238 | new UploadBytesElementReader(data, strlen(data))); |
| 239 | return UploadDataStream::CreateWithReader(reader.Pass(), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 240 | } |
| 241 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 242 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 243 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 244 | // -1 means unknown. 0 means no encryption. |
| 245 | EXPECT_GT(ssl_info.security_bits, 0); |
| 246 | |
| 247 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 248 | int cipher_suite = SSLConnectionStatusToCipherSuite( |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 249 | ssl_info.connection_status); |
| 250 | EXPECT_NE(0, cipher_suite); |
| 251 | } |
| 252 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 253 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 254 | const GURL& host_url) { |
| 255 | std::string sent_value; |
| 256 | |
| 257 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 258 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 259 | |
| 260 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 261 | EXPECT_EQ("keep-alive", sent_value); |
| 262 | } |
| 263 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 264 | bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) { |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 265 | size_t size = a.size(); |
| 266 | |
| 267 | if (size != b.size()) |
| 268 | return false; |
| 269 | |
| 270 | for (size_t i = 0; i < size; ++i) { |
| 271 | if (!a[i].Equals(b[i])) |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | return true; |
| 276 | } |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 277 | #endif // !defined(OS_IOS) |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 278 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 279 | // A network delegate that allows the user to choose a subset of request stages |
| 280 | // to block in. When blocking, the delegate can do one of the following: |
| 281 | // * synchronously return a pre-specified error code, or |
| 282 | // * asynchronously return that value via an automatically called callback, |
| 283 | // or |
| 284 | // * block and wait for the user to do a callback. |
| 285 | // Additionally, the user may also specify a redirect URL -- then each request |
| 286 | // with the current URL different from the redirect target will be redirected |
| 287 | // to that target, in the on-before-URL-request stage, independent of whether |
| 288 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 289 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 290 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 291 | // Stages in which the delegate can block. |
| 292 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 293 | NOT_BLOCKED = 0, |
| 294 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 295 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 296 | ON_HEADERS_RECEIVED = 1 << 2, |
| 297 | ON_AUTH_REQUIRED = 1 << 3 |
| 298 | }; |
| 299 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 300 | // Behavior during blocked stages. During other stages, just |
| 301 | // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
| 302 | enum BlockMode { |
| 303 | SYNCHRONOUS, // No callback, returns specified return values. |
| 304 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 305 | // specified return codes. |
| 306 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 307 | // |auth_retval_| are ignored. In every blocking stage the |
| 308 | // message loop is quit. |
| 309 | }; |
| 310 | |
| 311 | // Creates a delegate which does not block at all. |
| 312 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 313 | |
| 314 | // For users to trigger a callback returning |response|. |
| 315 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 316 | // Only call if |block_mode_| == USER_CALLBACK. |
| 317 | void DoCallback(int response); |
| 318 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 319 | |
| 320 | // Setters. |
| 321 | void set_retval(int retval) { |
| 322 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 323 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 324 | ASSERT_NE(OK, retval); |
| 325 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 328 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 329 | // |auth_credentials_| will be passed with the response. |
| 330 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 331 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 332 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 333 | auth_retval_ = auth_retval; |
| 334 | } |
| 335 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 336 | auth_credentials_ = auth_credentials; |
[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 | void set_redirect_url(const GURL& url) { |
| 340 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 341 | } |
| 342 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 343 | void set_block_on(int block_on) { |
| 344 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 347 | // Allows the user to check in which state did we block. |
| 348 | Stage stage_blocked_for_callback() const { |
| 349 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 350 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 354 | void RunCallback(int response, const CompletionCallback& callback); |
| 355 | void RunAuthCallback(AuthRequiredResponse response, |
| 356 | const AuthCallback& callback); |
| 357 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 358 | // TestNetworkDelegate implementation. |
| 359 | virtual int OnBeforeURLRequest(URLRequest* request, |
| 360 | const CompletionCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 361 | GURL* new_url) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 362 | |
| 363 | virtual int OnBeforeSendHeaders(URLRequest* request, |
| 364 | const CompletionCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 365 | HttpRequestHeaders* headers) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 366 | |
| 367 | virtual int OnHeadersReceived( |
| 368 | URLRequest* request, |
| 369 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 370 | const HttpResponseHeaders* original_response_headers, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 371 | scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 372 | |
| 373 | virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 374 | URLRequest* request, |
| 375 | const AuthChallengeInfo& auth_info, |
| 376 | const AuthCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 377 | AuthCredentials* credentials) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 378 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 379 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 380 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 381 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 382 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 383 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 384 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 385 | |
| 386 | // Configuration parameters, can be adjusted by public methods: |
| 387 | const BlockMode block_mode_; |
| 388 | |
| 389 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 390 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 391 | int retval_; // To be returned in non-auth stages. |
| 392 | AuthRequiredResponse auth_retval_; |
| 393 | |
| 394 | GURL redirect_url_; // Used if non-empty. |
| 395 | int block_on_; // Bit mask: in which stages to block. |
| 396 | |
| 397 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 398 | // callback. |
| 399 | AuthCredentials auth_credentials_; |
| 400 | AuthCredentials* target_auth_credentials_; |
| 401 | |
| 402 | // Internal variables, not set by not the user: |
| 403 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 404 | // USER_CALLBACK). |
| 405 | Stage stage_blocked_for_callback_; |
| 406 | |
| 407 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 408 | CompletionCallback callback_; |
| 409 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 410 | |
| 411 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 412 | |
| 413 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 414 | }; |
| 415 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 416 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 417 | : block_mode_(block_mode), |
| 418 | retval_(OK), |
| 419 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 420 | block_on_(0), |
| 421 | target_auth_credentials_(NULL), |
| 422 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 423 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 427 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 428 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 429 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 430 | CompletionCallback callback = callback_; |
| 431 | Reset(); |
| 432 | RunCallback(response, callback); |
| 433 | } |
| 434 | |
| 435 | void BlockingNetworkDelegate::DoAuthCallback( |
| 436 | NetworkDelegate::AuthRequiredResponse response) { |
| 437 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 438 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 439 | AuthCallback auth_callback = auth_callback_; |
| 440 | Reset(); |
| 441 | RunAuthCallback(response, auth_callback); |
| 442 | } |
| 443 | |
| 444 | void BlockingNetworkDelegate::RunCallback(int response, |
| 445 | const CompletionCallback& callback) { |
| 446 | callback.Run(response); |
| 447 | } |
| 448 | |
| 449 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 450 | const AuthCallback& callback) { |
| 451 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 452 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 453 | *target_auth_credentials_ = auth_credentials_; |
| 454 | } |
| 455 | callback.Run(response); |
| 456 | } |
| 457 | |
| 458 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 459 | URLRequest* request, |
| 460 | const CompletionCallback& callback, |
| 461 | GURL* new_url) { |
| 462 | if (redirect_url_ == request->url()) |
| 463 | return OK; // We've already seen this request and redirected elsewhere. |
| 464 | |
| 465 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 466 | |
| 467 | if (!redirect_url_.is_empty()) |
| 468 | *new_url = redirect_url_; |
| 469 | |
| 470 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 471 | } |
| 472 | |
| 473 | int BlockingNetworkDelegate::OnBeforeSendHeaders( |
| 474 | URLRequest* request, |
| 475 | const CompletionCallback& callback, |
| 476 | HttpRequestHeaders* headers) { |
| 477 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 478 | |
| 479 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 480 | } |
| 481 | |
| 482 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 483 | URLRequest* request, |
| 484 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 485 | const HttpResponseHeaders* original_response_headers, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 486 | scoped_refptr<HttpResponseHeaders>* override_response_headers) { |
| 487 | TestNetworkDelegate::OnHeadersReceived( |
| 488 | request, callback, original_response_headers, |
| 489 | override_response_headers); |
| 490 | |
| 491 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 492 | } |
| 493 | |
| 494 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 495 | URLRequest* request, |
| 496 | const AuthChallengeInfo& auth_info, |
| 497 | const AuthCallback& callback, |
| 498 | AuthCredentials* credentials) { |
| 499 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 500 | credentials); |
| 501 | // Check that the user has provided callback for the previous blocked stage. |
| 502 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 503 | |
| 504 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 505 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 506 | } |
| 507 | |
| 508 | target_auth_credentials_ = credentials; |
| 509 | |
| 510 | switch (block_mode_) { |
| 511 | case SYNCHRONOUS: |
| 512 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 513 | *target_auth_credentials_ = auth_credentials_; |
| 514 | return auth_retval_; |
| 515 | |
| 516 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 517 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 518 | FROM_HERE, |
| 519 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 520 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 521 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 522 | |
| 523 | case USER_CALLBACK: |
| 524 | auth_callback_ = callback; |
| 525 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 526 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 527 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 528 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 529 | } |
| 530 | NOTREACHED(); |
| 531 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 532 | } |
| 533 | |
| 534 | void BlockingNetworkDelegate::Reset() { |
| 535 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 536 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 537 | callback_.Reset(); |
| 538 | auth_callback_.Reset(); |
| 539 | } |
| 540 | |
| 541 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 542 | BlockingNetworkDelegate::Stage stage, |
| 543 | const CompletionCallback& callback) { |
| 544 | // Check that the user has provided callback for the previous blocked stage. |
| 545 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 546 | |
| 547 | if ((block_on_ & stage) == 0) { |
| 548 | return OK; |
| 549 | } |
| 550 | |
| 551 | switch (block_mode_) { |
| 552 | case SYNCHRONOUS: |
| 553 | EXPECT_NE(OK, retval_); |
| 554 | return retval_; |
| 555 | |
| 556 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 557 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 558 | FROM_HERE, |
| 559 | base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 560 | weak_factory_.GetWeakPtr(), retval_, callback)); |
| 561 | return ERR_IO_PENDING; |
| 562 | |
| 563 | case USER_CALLBACK: |
| 564 | callback_ = callback; |
| 565 | stage_blocked_for_callback_ = stage; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 566 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 567 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 568 | return ERR_IO_PENDING; |
| 569 | } |
| 570 | NOTREACHED(); |
| 571 | return 0; |
| 572 | } |
| 573 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 574 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 575 | public: |
| 576 | // Does not own |delegate|. |
| 577 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 578 | NetworkDelegate* delegate) |
| 579 | : TestURLRequestContext(true) { |
| 580 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 581 | set_network_delegate(delegate); |
| 582 | Init(); |
| 583 | } |
| 584 | virtual ~TestURLRequestContextWithProxy() {} |
| 585 | }; |
| 586 | |
| 587 | } // namespace |
| 588 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 589 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 590 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 591 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 592 | URLRequestTest() : default_context_(true) { |
| 593 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 594 | default_context_.set_net_log(&net_log_); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 595 | job_factory_.SetProtocolHandler("data", new DataProtocolHandler); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 596 | job_factory_.SetProtocolHandler( |
| 597 | "file", new FileProtocolHandler(base::MessageLoopProxy::current())); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 598 | default_context_.set_job_factory(&job_factory_); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 599 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 600 | } |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 601 | virtual ~URLRequestTest() { |
| 602 | // URLRequestJobs may post clean-up tasks on destruction. |
| 603 | base::RunLoop().RunUntilIdle(); |
| 604 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 605 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 606 | // Adds the TestJobInterceptor to the default context. |
| 607 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 608 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 609 | job_factory_.SetProtocolHandler("http", NULL); |
| 610 | job_factory_.SetProtocolHandler("http", protocol_handler_); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 611 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 612 | } |
| 613 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 614 | protected: |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 615 | CapturingNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 616 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 617 | URLRequestJobFactoryImpl job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 618 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 619 | }; |
| 620 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 621 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 622 | TestDelegate d; |
| 623 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 624 | URLRequest r(GURL("about:blank"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 625 | |
| 626 | r.Start(); |
| 627 | EXPECT_TRUE(r.is_pending()); |
| 628 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 629 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 630 | |
| 631 | EXPECT_TRUE(!r.is_pending()); |
| 632 | EXPECT_FALSE(d.received_data_before_response()); |
| 633 | EXPECT_EQ(d.bytes_received(), 0); |
| 634 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 635 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 636 | |
| 637 | HttpRequestHeaders headers; |
| 638 | EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
| 642 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 643 | TestDelegate d; |
| 644 | { |
| 645 | // Use our nice little Chrome logo. |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 646 | URLRequest r( |
| 647 | GURL( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 648 | "data:image/png;base64," |
| 649 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3" |
| 650 | "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD" |
| 651 | "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t" |
| 652 | "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9" |
| 653 | "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1" |
| 654 | "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z" |
| 655 | "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW" |
| 656 | "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW" |
| 657 | "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb" |
| 658 | "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5" |
| 659 | "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV" |
| 660 | "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq" |
| 661 | "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F" |
| 662 | "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB" |
| 663 | "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM" |
| 664 | "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm" |
| 665 | "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En" |
| 666 | "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 667 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 668 | &d, |
| 669 | &default_context_); |
| 670 | |
| 671 | r.Start(); |
| 672 | EXPECT_TRUE(r.is_pending()); |
| 673 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 674 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 675 | |
| 676 | EXPECT_TRUE(!r.is_pending()); |
| 677 | EXPECT_FALSE(d.received_data_before_response()); |
| 678 | EXPECT_EQ(d.bytes_received(), 911); |
| 679 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 680 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 681 | |
| 682 | HttpRequestHeaders headers; |
| 683 | EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 688 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 689 | PathService::Get(base::FILE_EXE, &app_path); |
| 690 | GURL app_url = FilePathToFileURL(app_path); |
| 691 | |
| 692 | TestDelegate d; |
| 693 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 694 | URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 695 | |
| 696 | r.Start(); |
| 697 | EXPECT_TRUE(r.is_pending()); |
| 698 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 699 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 700 | |
| 701 | int64 file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 702 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 703 | |
| 704 | EXPECT_TRUE(!r.is_pending()); |
| 705 | EXPECT_EQ(1, d.response_started_count()); |
| 706 | EXPECT_FALSE(d.received_data_before_response()); |
| 707 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 708 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 709 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 710 | |
| 711 | HttpRequestHeaders headers; |
| 712 | EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 716 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 717 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 718 | PathService::Get(base::FILE_EXE, &app_path); |
| 719 | GURL app_url = FilePathToFileURL(app_path); |
| 720 | |
| 721 | TestDelegate d; |
| 722 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 723 | URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 724 | |
| 725 | r.Start(); |
| 726 | EXPECT_TRUE(r.is_pending()); |
| 727 | r.Cancel(); |
| 728 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 729 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 730 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 731 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 732 | } |
| 733 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 734 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 735 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 736 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 737 | FillBuffer(buffer.get(), buffer_size); |
| 738 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 739 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 740 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 741 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 742 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 743 | |
| 744 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 745 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 746 | |
| 747 | const size_t first_byte_position = 500; |
| 748 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 749 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 750 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 751 | buffer.get() + last_byte_position + 1); |
| 752 | |
| 753 | TestDelegate d; |
| 754 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 755 | URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 756 | |
| 757 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 758 | headers.SetHeader( |
| 759 | HttpRequestHeaders::kRange, |
| 760 | net::HttpByteRange::Bounded( |
| 761 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 762 | r.SetExtraRequestHeaders(headers); |
| 763 | r.Start(); |
| 764 | EXPECT_TRUE(r.is_pending()); |
| 765 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 766 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 767 | EXPECT_TRUE(!r.is_pending()); |
| 768 | EXPECT_EQ(1, d.response_started_count()); |
| 769 | EXPECT_FALSE(d.received_data_before_response()); |
| 770 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 771 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 772 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 773 | } |
| 774 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 775 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 779 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 780 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 781 | FillBuffer(buffer.get(), buffer_size); |
| 782 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 783 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 784 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 785 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 786 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 787 | |
| 788 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 789 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 790 | |
| 791 | const size_t first_byte_position = 500; |
| 792 | const size_t last_byte_position = buffer_size - 1; |
| 793 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 794 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 795 | buffer.get() + last_byte_position + 1); |
| 796 | |
| 797 | TestDelegate d; |
| 798 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 799 | URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 800 | |
| 801 | HttpRequestHeaders headers; |
| 802 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 803 | net::HttpByteRange::RightUnbounded( |
| 804 | first_byte_position).GetHeaderValue()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 805 | r.SetExtraRequestHeaders(headers); |
| 806 | r.Start(); |
| 807 | EXPECT_TRUE(r.is_pending()); |
| 808 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 809 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 810 | EXPECT_TRUE(!r.is_pending()); |
| 811 | EXPECT_EQ(1, d.response_started_count()); |
| 812 | EXPECT_FALSE(d.received_data_before_response()); |
| 813 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 814 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 815 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 816 | } |
| 817 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 818 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 822 | const size_t buffer_size = 400000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 823 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 824 | FillBuffer(buffer.get(), buffer_size); |
| 825 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 826 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 827 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 828 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 829 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 830 | |
| 831 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 832 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 833 | |
| 834 | TestDelegate d; |
| 835 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 836 | URLRequest r(temp_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 837 | |
| 838 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 839 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 840 | r.SetExtraRequestHeaders(headers); |
| 841 | r.Start(); |
| 842 | EXPECT_TRUE(r.is_pending()); |
| 843 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 844 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 845 | EXPECT_TRUE(d.request_failed()); |
| 846 | } |
| 847 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 848 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 849 | } |
| 850 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 851 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 852 | base::ScopedTempDir temp_dir; |
| 853 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 854 | base::FilePath test_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 855 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file)); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 856 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 857 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 858 | GURL test_file_url = net::FilePathToFileURL(test_file); |
| 859 | |
| 860 | { |
| 861 | TestDelegate d; |
| 862 | TestNetworkDelegate network_delegate; |
| 863 | network_delegate.set_can_access_files(true); |
| 864 | default_context_.set_network_delegate(&network_delegate); |
| 865 | URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_); |
| 866 | r.Start(); |
| 867 | base::RunLoop().Run(); |
| 868 | EXPECT_FALSE(d.request_failed()); |
| 869 | EXPECT_EQ(test_data, d.data_received()); |
| 870 | } |
| 871 | |
| 872 | { |
| 873 | TestDelegate d; |
| 874 | TestNetworkDelegate network_delegate; |
| 875 | network_delegate.set_can_access_files(false); |
| 876 | default_context_.set_network_delegate(&network_delegate); |
| 877 | URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_); |
| 878 | r.Start(); |
| 879 | base::RunLoop().Run(); |
| 880 | EXPECT_TRUE(d.request_failed()); |
| 881 | EXPECT_EQ("", d.data_received()); |
| 882 | } |
| 883 | } |
| 884 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 885 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 886 | TestDelegate d; |
| 887 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 888 | URLRequest r(GURL("invalid url"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 889 | |
| 890 | r.Start(); |
| 891 | EXPECT_TRUE(r.is_pending()); |
| 892 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 893 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 894 | EXPECT_TRUE(d.request_failed()); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | #if defined(OS_WIN) |
| 899 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 900 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 901 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 902 | app_path = app_path.AppendASCII("net"); |
| 903 | app_path = app_path.AppendASCII("data"); |
| 904 | app_path = app_path.AppendASCII("url_request_unittest"); |
| 905 | app_path = app_path.AppendASCII("with-headers.html"); |
| 906 | |
| 907 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 908 | |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 909 | base::win::ScopedCOMInitializer com_initializer; |
[email protected] | 00d60fa | 2012-10-01 18:20:17 | [diff] [blame] | 910 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 911 | // Temporarily create a shortcut for test |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 912 | { |
| 913 | base::win::ScopedComPtr<IShellLink> shell; |
| 914 | ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 915 | CLSCTX_INPROC_SERVER))); |
| 916 | base::win::ScopedComPtr<IPersistFile> persist; |
| 917 | ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 918 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 919 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 920 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 921 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 922 | |
| 923 | TestDelegate d; |
| 924 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 925 | URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)), |
| 926 | DEFAULT_PRIORITY, |
| 927 | &d, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 928 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 929 | |
| 930 | r.Start(); |
| 931 | EXPECT_TRUE(r.is_pending()); |
| 932 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 933 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 934 | |
| 935 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 936 | GetFileAttributesEx(app_path.value().c_str(), |
| 937 | GetFileExInfoStandard, &data); |
| 938 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 939 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 940 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 941 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
[email protected] | ec530c2 | 2013-04-11 15:54:04 | [diff] [blame] | 942 | scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 943 | DWORD read_size; |
| 944 | BOOL result; |
| 945 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 946 | &read_size, NULL); |
| 947 | std::string content(buffer.get(), read_size); |
| 948 | CloseHandle(file); |
| 949 | |
| 950 | EXPECT_TRUE(!r.is_pending()); |
| 951 | EXPECT_EQ(1, d.received_redirect_count()); |
| 952 | EXPECT_EQ(content, d.data_received()); |
| 953 | } |
| 954 | |
| 955 | // Clean the shortcut |
| 956 | DeleteFile(lnk_path.c_str()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 957 | } |
| 958 | #endif // defined(OS_WIN) |
| 959 | |
| 960 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 961 | // Put in mock resource provider. |
| 962 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 963 | |
| 964 | TestDelegate d; |
| 965 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 966 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 967 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 968 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 969 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 970 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 971 | URLRequest req( |
| 972 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 973 | req.Start(); |
| 974 | EXPECT_TRUE(req.is_pending()); |
| 975 | |
| 976 | d.set_cancel_in_received_data_pending(true); |
| 977 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 978 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | // Take out mock resource provider. |
| 982 | NetModule::SetResourceProvider(NULL); |
| 983 | } |
| 984 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 985 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 986 | // Verify the general sanity of the the output of the file: |
| 987 | // directory lister by checking for the output of a known existing |
| 988 | // file. |
| 989 | const char sentinel_name[] = "filedir-sentinel"; |
| 990 | |
| 991 | base::FilePath path; |
| 992 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 993 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 994 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 995 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 996 | |
| 997 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 998 | URLRequest req( |
| 999 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1000 | req.Start(); |
| 1001 | base::RunLoop().Run(); |
| 1002 | |
| 1003 | // Generate entry for the sentinel file. |
| 1004 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 1005 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 1006 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 1007 | EXPECT_GT(info.size, 0); |
| 1008 | std::string sentinel_output = GetDirectoryListingEntry( |
| 1009 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 1010 | std::string(sentinel_name), |
| 1011 | false /* is_dir */, |
| 1012 | info.size, |
| 1013 | info.last_modified); |
| 1014 | |
| 1015 | ASSERT_LT(0, d.bytes_received()); |
| 1016 | ASSERT_FALSE(d.request_failed()); |
| 1017 | ASSERT_TRUE(req.status().is_success()); |
| 1018 | // Check for the entry generated for the "sentinel" file. |
| 1019 | const std::string& data = d.data_received(); |
| 1020 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 1021 | } |
| 1022 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1023 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1024 | // There is an implicit redirect when loading a file path that matches a |
| 1025 | // directory and does not end with a slash. Ensure that following such |
| 1026 | // redirects does not crash. See http://crbug.com/18686. |
| 1027 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1028 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1029 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 1030 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 1031 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 1032 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 1033 | |
| 1034 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1035 | URLRequest req( |
| 1036 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1037 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1038 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1039 | |
| 1040 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1041 | ASSERT_LT(0, d.bytes_received()); |
| 1042 | ASSERT_FALSE(d.request_failed()); |
| 1043 | ASSERT_TRUE(req.status().is_success()); |
| 1044 | } |
| 1045 | |
| 1046 | #if defined(OS_WIN) |
| 1047 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1048 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1049 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1050 | URLRequest req(GURL("file:///"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1051 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1052 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1053 | |
| 1054 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1055 | ASSERT_FALSE(req.status().is_success()); |
| 1056 | } |
| 1057 | #endif |
| 1058 | |
| 1059 | // Custom URLRequestJobs for use with interceptor tests |
| 1060 | class RestartTestJob : public URLRequestTestJob { |
| 1061 | public: |
| 1062 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1063 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1064 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1065 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1066 | this->NotifyRestartRequired(); |
| 1067 | } |
| 1068 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1069 | virtual ~RestartTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1070 | }; |
| 1071 | |
| 1072 | class CancelTestJob : public URLRequestTestJob { |
| 1073 | public: |
| 1074 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1075 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1076 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1077 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1078 | request_->Cancel(); |
| 1079 | } |
| 1080 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1081 | virtual ~CancelTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1082 | }; |
| 1083 | |
| 1084 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1085 | public: |
| 1086 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1087 | NetworkDelegate* network_delegate) |
| 1088 | : URLRequestTestJob(request, network_delegate, true) { |
| 1089 | } |
| 1090 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1091 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1092 | request_->Cancel(); |
| 1093 | this->NotifyRestartRequired(); |
| 1094 | } |
| 1095 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1096 | virtual ~CancelThenRestartTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1097 | }; |
| 1098 | |
| 1099 | // An Interceptor for use with interceptor tests |
| 1100 | class TestInterceptor : URLRequest::Interceptor { |
| 1101 | public: |
| 1102 | TestInterceptor() |
| 1103 | : intercept_main_request_(false), restart_main_request_(false), |
| 1104 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1105 | simulate_main_network_error_(false), |
| 1106 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1107 | intercept_final_response_(false), cancel_final_request_(false), |
| 1108 | did_intercept_main_(false), did_restart_main_(false), |
| 1109 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1110 | did_simulate_error_main_(false), |
| 1111 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1112 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1113 | URLRequest::Deprecated::RegisterRequestInterceptor(this); |
| 1114 | } |
| 1115 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1116 | virtual ~TestInterceptor() { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1117 | URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
| 1118 | } |
| 1119 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1120 | virtual URLRequestJob* MaybeIntercept( |
| 1121 | URLRequest* request, |
| 1122 | NetworkDelegate* network_delegate) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1123 | if (restart_main_request_) { |
| 1124 | restart_main_request_ = false; |
| 1125 | did_restart_main_ = true; |
| 1126 | return new RestartTestJob(request, network_delegate); |
| 1127 | } |
| 1128 | if (cancel_main_request_) { |
| 1129 | cancel_main_request_ = false; |
| 1130 | did_cancel_main_ = true; |
| 1131 | return new CancelTestJob(request, network_delegate); |
| 1132 | } |
| 1133 | if (cancel_then_restart_main_request_) { |
| 1134 | cancel_then_restart_main_request_ = false; |
| 1135 | did_cancel_then_restart_main_ = true; |
| 1136 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1137 | } |
| 1138 | if (simulate_main_network_error_) { |
| 1139 | simulate_main_network_error_ = false; |
| 1140 | did_simulate_error_main_ = true; |
| 1141 | // will error since the requeted url is not one of its canned urls |
| 1142 | return new URLRequestTestJob(request, network_delegate, true); |
| 1143 | } |
| 1144 | if (!intercept_main_request_) |
| 1145 | return NULL; |
| 1146 | intercept_main_request_ = false; |
| 1147 | did_intercept_main_ = true; |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1148 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1149 | network_delegate, |
| 1150 | main_headers_, |
| 1151 | main_data_, |
| 1152 | true); |
| 1153 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1154 | return job; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | virtual URLRequestJob* MaybeInterceptRedirect( |
| 1158 | URLRequest* request, |
| 1159 | NetworkDelegate* network_delegate, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1160 | const GURL& location) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1161 | if (cancel_redirect_request_) { |
| 1162 | cancel_redirect_request_ = false; |
| 1163 | did_cancel_redirect_ = true; |
| 1164 | return new CancelTestJob(request, network_delegate); |
| 1165 | } |
| 1166 | if (!intercept_redirect_) |
| 1167 | return NULL; |
| 1168 | intercept_redirect_ = false; |
| 1169 | did_intercept_redirect_ = true; |
| 1170 | return new URLRequestTestJob(request, |
| 1171 | network_delegate, |
| 1172 | redirect_headers_, |
| 1173 | redirect_data_, |
| 1174 | true); |
| 1175 | } |
| 1176 | |
| 1177 | virtual URLRequestJob* MaybeInterceptResponse( |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1178 | URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1179 | if (cancel_final_request_) { |
| 1180 | cancel_final_request_ = false; |
| 1181 | did_cancel_final_ = true; |
| 1182 | return new CancelTestJob(request, network_delegate); |
| 1183 | } |
| 1184 | if (!intercept_final_response_) |
| 1185 | return NULL; |
| 1186 | intercept_final_response_ = false; |
| 1187 | did_intercept_final_ = true; |
| 1188 | return new URLRequestTestJob(request, |
| 1189 | network_delegate, |
| 1190 | final_headers_, |
| 1191 | final_data_, |
| 1192 | true); |
| 1193 | } |
| 1194 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1195 | // Whether to intercept the main request, and if so the response to return and |
| 1196 | // the LoadTimingInfo to use. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1197 | bool intercept_main_request_; |
| 1198 | std::string main_headers_; |
| 1199 | std::string main_data_; |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1200 | LoadTimingInfo main_request_load_timing_info_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1201 | |
| 1202 | // Other actions we take at MaybeIntercept time |
| 1203 | bool restart_main_request_; |
| 1204 | bool cancel_main_request_; |
| 1205 | bool cancel_then_restart_main_request_; |
| 1206 | bool simulate_main_network_error_; |
| 1207 | |
| 1208 | // Whether to intercept redirects, and if so the response to return. |
| 1209 | bool intercept_redirect_; |
| 1210 | std::string redirect_headers_; |
| 1211 | std::string redirect_data_; |
| 1212 | |
| 1213 | // Other actions we can take at MaybeInterceptRedirect time |
| 1214 | bool cancel_redirect_request_; |
| 1215 | |
| 1216 | // Whether to intercept final response, and if so the response to return. |
| 1217 | bool intercept_final_response_; |
| 1218 | std::string final_headers_; |
| 1219 | std::string final_data_; |
| 1220 | |
| 1221 | // Other actions we can take at MaybeInterceptResponse time |
| 1222 | bool cancel_final_request_; |
| 1223 | |
| 1224 | // If we did something or not |
| 1225 | bool did_intercept_main_; |
| 1226 | bool did_restart_main_; |
| 1227 | bool did_cancel_main_; |
| 1228 | bool did_cancel_then_restart_main_; |
| 1229 | bool did_simulate_error_main_; |
| 1230 | bool did_intercept_redirect_; |
| 1231 | bool did_cancel_redirect_; |
| 1232 | bool did_intercept_final_; |
| 1233 | bool did_cancel_final_; |
| 1234 | |
| 1235 | // Static getters for canned response header and data strings |
| 1236 | |
| 1237 | static std::string ok_data() { |
| 1238 | return URLRequestTestJob::test_data_1(); |
| 1239 | } |
| 1240 | |
| 1241 | static std::string ok_headers() { |
| 1242 | return URLRequestTestJob::test_headers(); |
| 1243 | } |
| 1244 | |
| 1245 | static std::string redirect_data() { |
| 1246 | return std::string(); |
| 1247 | } |
| 1248 | |
| 1249 | static std::string redirect_headers() { |
| 1250 | return URLRequestTestJob::test_redirect_headers(); |
| 1251 | } |
| 1252 | |
| 1253 | static std::string error_data() { |
| 1254 | return std::string("ohhh nooooo mr. bill!"); |
| 1255 | } |
| 1256 | |
| 1257 | static std::string error_headers() { |
| 1258 | return URLRequestTestJob::test_error_headers(); |
| 1259 | } |
| 1260 | }; |
| 1261 | |
| 1262 | TEST_F(URLRequestTest, Intercept) { |
| 1263 | TestInterceptor interceptor; |
| 1264 | |
| 1265 | // intercept the main request and respond with a simple response |
| 1266 | interceptor.intercept_main_request_ = true; |
| 1267 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1268 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1269 | |
| 1270 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1271 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1272 | DEFAULT_PRIORITY, |
| 1273 | &d, |
| 1274 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1275 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1276 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1277 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
| 1278 | req.SetUserData(NULL, user_data0); |
| 1279 | req.SetUserData(&user_data1, user_data1); |
| 1280 | req.SetUserData(&user_data2, user_data2); |
| 1281 | req.set_method("GET"); |
| 1282 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1283 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1284 | |
| 1285 | // Make sure we can retrieve our specific user data |
| 1286 | EXPECT_EQ(user_data0, req.GetUserData(NULL)); |
| 1287 | EXPECT_EQ(user_data1, req.GetUserData(&user_data1)); |
| 1288 | EXPECT_EQ(user_data2, req.GetUserData(&user_data2)); |
| 1289 | |
| 1290 | // Check the interceptor got called as expected |
| 1291 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1292 | |
| 1293 | // Check we got one good response |
| 1294 | EXPECT_TRUE(req.status().is_success()); |
| 1295 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1296 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1297 | EXPECT_EQ(1, d.response_started_count()); |
| 1298 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1299 | } |
| 1300 | |
| 1301 | TEST_F(URLRequestTest, InterceptRedirect) { |
| 1302 | TestInterceptor interceptor; |
| 1303 | |
| 1304 | // intercept the main request and respond with a redirect |
| 1305 | interceptor.intercept_main_request_ = true; |
| 1306 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1307 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1308 | |
| 1309 | // intercept that redirect and respond a final OK response |
| 1310 | interceptor.intercept_redirect_ = true; |
| 1311 | interceptor.redirect_headers_ = TestInterceptor::ok_headers(); |
| 1312 | interceptor.redirect_data_ = TestInterceptor::ok_data(); |
| 1313 | |
| 1314 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1315 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1316 | DEFAULT_PRIORITY, |
| 1317 | &d, |
| 1318 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1319 | req.set_method("GET"); |
| 1320 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1321 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1322 | |
| 1323 | // Check the interceptor got called as expected |
| 1324 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1325 | EXPECT_TRUE(interceptor.did_intercept_redirect_); |
| 1326 | |
| 1327 | // Check we got one good response |
| 1328 | EXPECT_TRUE(req.status().is_success()); |
| 1329 | if (req.status().is_success()) { |
| 1330 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1331 | } |
| 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, InterceptServerError) { |
| 1338 | TestInterceptor interceptor; |
| 1339 | |
| 1340 | // intercept the main request to generate a server error response |
| 1341 | interceptor.intercept_main_request_ = true; |
| 1342 | interceptor.main_headers_ = TestInterceptor::error_headers(); |
| 1343 | interceptor.main_data_ = TestInterceptor::error_data(); |
| 1344 | |
| 1345 | // intercept that error and respond with an OK response |
| 1346 | interceptor.intercept_final_response_ = true; |
| 1347 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1348 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1349 | |
| 1350 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1351 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1352 | DEFAULT_PRIORITY, |
| 1353 | &d, |
| 1354 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1355 | req.set_method("GET"); |
| 1356 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1357 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1358 | |
| 1359 | // Check the interceptor got called as expected |
| 1360 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1361 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1362 | |
| 1363 | // Check we got one good response |
| 1364 | EXPECT_TRUE(req.status().is_success()); |
| 1365 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1366 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1367 | EXPECT_EQ(1, d.response_started_count()); |
| 1368 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1369 | } |
| 1370 | |
| 1371 | TEST_F(URLRequestTest, InterceptNetworkError) { |
| 1372 | TestInterceptor interceptor; |
| 1373 | |
| 1374 | // intercept the main request to simulate a network error |
| 1375 | interceptor.simulate_main_network_error_ = true; |
| 1376 | |
| 1377 | // intercept that error and respond with an OK response |
| 1378 | interceptor.intercept_final_response_ = true; |
| 1379 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1380 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1381 | |
| 1382 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1383 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1384 | DEFAULT_PRIORITY, |
| 1385 | &d, |
| 1386 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 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_simulate_error_main_); |
| 1393 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1394 | |
| 1395 | // Check we received one good response |
| 1396 | EXPECT_TRUE(req.status().is_success()); |
| 1397 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1398 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1399 | EXPECT_EQ(1, d.response_started_count()); |
| 1400 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1401 | } |
| 1402 | |
| 1403 | TEST_F(URLRequestTest, InterceptRestartRequired) { |
| 1404 | TestInterceptor interceptor; |
| 1405 | |
| 1406 | // restart the main request |
| 1407 | interceptor.restart_main_request_ = true; |
| 1408 | |
| 1409 | // then intercept the new main request and respond with an OK response |
| 1410 | interceptor.intercept_main_request_ = true; |
| 1411 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1412 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1413 | |
| 1414 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1415 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1416 | DEFAULT_PRIORITY, |
| 1417 | &d, |
| 1418 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1419 | req.set_method("GET"); |
| 1420 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1421 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1422 | |
| 1423 | // Check the interceptor got called as expected |
| 1424 | EXPECT_TRUE(interceptor.did_restart_main_); |
| 1425 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1426 | |
| 1427 | // Check we received one good response |
| 1428 | EXPECT_TRUE(req.status().is_success()); |
| 1429 | if (req.status().is_success()) { |
| 1430 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1431 | } |
| 1432 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1433 | EXPECT_EQ(1, d.response_started_count()); |
| 1434 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1435 | } |
| 1436 | |
| 1437 | TEST_F(URLRequestTest, InterceptRespectsCancelMain) { |
| 1438 | TestInterceptor interceptor; |
| 1439 | |
| 1440 | // intercept the main request and cancel from within the restarted job |
| 1441 | interceptor.cancel_main_request_ = true; |
| 1442 | |
| 1443 | // setup to intercept final response and override it with an OK response |
| 1444 | interceptor.intercept_final_response_ = true; |
| 1445 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1446 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1447 | |
| 1448 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1449 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1450 | DEFAULT_PRIORITY, |
| 1451 | &d, |
| 1452 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1453 | req.set_method("GET"); |
| 1454 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1455 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1456 | |
| 1457 | // Check the interceptor got called as expected |
| 1458 | EXPECT_TRUE(interceptor.did_cancel_main_); |
| 1459 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1460 | |
| 1461 | // Check we see a canceled request |
| 1462 | EXPECT_FALSE(req.status().is_success()); |
| 1463 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1464 | } |
| 1465 | |
| 1466 | TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) { |
| 1467 | TestInterceptor interceptor; |
| 1468 | |
| 1469 | // intercept the main request and respond with a redirect |
| 1470 | interceptor.intercept_main_request_ = true; |
| 1471 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1472 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1473 | |
| 1474 | // intercept the redirect and cancel from within that job |
| 1475 | interceptor.cancel_redirect_request_ = true; |
| 1476 | |
| 1477 | // setup to intercept final response and override it with an OK response |
| 1478 | interceptor.intercept_final_response_ = true; |
| 1479 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1480 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1481 | |
| 1482 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1483 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1484 | DEFAULT_PRIORITY, |
| 1485 | &d, |
| 1486 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1487 | req.set_method("GET"); |
| 1488 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1489 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1490 | |
| 1491 | // Check the interceptor got called as expected |
| 1492 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1493 | EXPECT_TRUE(interceptor.did_cancel_redirect_); |
| 1494 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1495 | |
| 1496 | // Check we see a canceled request |
| 1497 | EXPECT_FALSE(req.status().is_success()); |
| 1498 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1499 | } |
| 1500 | |
| 1501 | TEST_F(URLRequestTest, InterceptRespectsCancelFinal) { |
| 1502 | TestInterceptor interceptor; |
| 1503 | |
| 1504 | // intercept the main request to simulate a network error |
| 1505 | interceptor.simulate_main_network_error_ = true; |
| 1506 | |
| 1507 | // setup to intercept final response and cancel from within that job |
| 1508 | interceptor.cancel_final_request_ = true; |
| 1509 | |
| 1510 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1511 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1512 | DEFAULT_PRIORITY, |
| 1513 | &d, |
| 1514 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1515 | req.set_method("GET"); |
| 1516 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1517 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1518 | |
| 1519 | // Check the interceptor got called as expected |
| 1520 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1521 | EXPECT_TRUE(interceptor.did_cancel_final_); |
| 1522 | |
| 1523 | // Check we see a canceled request |
| 1524 | EXPECT_FALSE(req.status().is_success()); |
| 1525 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1526 | } |
| 1527 | |
| 1528 | TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { |
| 1529 | TestInterceptor interceptor; |
| 1530 | |
| 1531 | // intercept the main request and cancel then restart from within that job |
| 1532 | interceptor.cancel_then_restart_main_request_ = true; |
| 1533 | |
| 1534 | // setup to intercept final response and override it with an OK response |
| 1535 | interceptor.intercept_final_response_ = true; |
| 1536 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1537 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1538 | |
| 1539 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1540 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1541 | DEFAULT_PRIORITY, |
| 1542 | &d, |
| 1543 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1544 | req.set_method("GET"); |
| 1545 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1546 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1547 | |
| 1548 | // Check the interceptor got called as expected |
| 1549 | EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); |
| 1550 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1551 | |
| 1552 | // Check we see a canceled request |
| 1553 | EXPECT_FALSE(req.status().is_success()); |
| 1554 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1555 | } |
| 1556 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1557 | LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing, |
| 1558 | URLRequestContext* context) { |
| 1559 | TestInterceptor interceptor; |
| 1560 | interceptor.intercept_main_request_ = true; |
| 1561 | interceptor.main_request_load_timing_info_ = job_load_timing; |
| 1562 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1563 | URLRequest req( |
| 1564 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, context); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1565 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1566 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1567 | |
| 1568 | LoadTimingInfo resulting_load_timing; |
| 1569 | req.GetLoadTimingInfo(&resulting_load_timing); |
| 1570 | |
| 1571 | // None of these should be modified by the URLRequest. |
| 1572 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1573 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1574 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1575 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1576 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1577 | resulting_load_timing.receive_headers_end); |
| 1578 | |
| 1579 | return resulting_load_timing; |
| 1580 | } |
| 1581 | |
| 1582 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 1583 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 1584 | // or SSL times, and |used_proxy| is used for proxy times. |
| 1585 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 1586 | int connect_time_flags, |
| 1587 | bool used_proxy) { |
| 1588 | LoadTimingInfo load_timing; |
| 1589 | load_timing.socket_log_id = 1; |
| 1590 | |
| 1591 | if (used_proxy) { |
| 1592 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1593 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1594 | } |
| 1595 | |
| 1596 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 1597 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 1598 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 1599 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 1600 | } |
| 1601 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 1602 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 1603 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 1604 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 1605 | } |
| 1606 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 1607 | |
| 1608 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1609 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1610 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1611 | return load_timing; |
| 1612 | } |
| 1613 | |
| 1614 | // Same as above, but in the case of a reused socket. |
| 1615 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 1616 | bool used_proxy) { |
| 1617 | LoadTimingInfo load_timing; |
| 1618 | load_timing.socket_log_id = 1; |
| 1619 | load_timing.socket_reused = true; |
| 1620 | |
| 1621 | if (used_proxy) { |
| 1622 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1623 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1624 | } |
| 1625 | |
| 1626 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1627 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1628 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1629 | return load_timing; |
| 1630 | } |
| 1631 | |
| 1632 | // Basic test that the intercept + load timing tests work. |
| 1633 | TEST_F(URLRequestTest, InterceptLoadTiming) { |
| 1634 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1635 | LoadTimingInfo job_load_timing = |
| 1636 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1637 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1638 | LoadTimingInfo load_timing_result = |
| 1639 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1640 | |
| 1641 | // Nothing should have been changed by the URLRequest. |
| 1642 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1643 | load_timing_result.proxy_resolve_start); |
| 1644 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1645 | load_timing_result.proxy_resolve_end); |
| 1646 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1647 | load_timing_result.connect_timing.dns_start); |
| 1648 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1649 | load_timing_result.connect_timing.dns_end); |
| 1650 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1651 | load_timing_result.connect_timing.connect_start); |
| 1652 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1653 | load_timing_result.connect_timing.connect_end); |
| 1654 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1655 | load_timing_result.connect_timing.ssl_start); |
| 1656 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1657 | load_timing_result.connect_timing.ssl_end); |
| 1658 | |
| 1659 | // Redundant sanity check. |
| 1660 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1661 | } |
| 1662 | |
| 1663 | // Another basic test, with proxy and SSL times, but no DNS times. |
| 1664 | TEST_F(URLRequestTest, InterceptLoadTimingProxy) { |
| 1665 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1666 | LoadTimingInfo job_load_timing = |
| 1667 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1668 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1669 | LoadTimingInfo load_timing_result = |
| 1670 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1671 | |
| 1672 | // Nothing should have been changed by the URLRequest. |
| 1673 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1674 | load_timing_result.proxy_resolve_start); |
| 1675 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1676 | load_timing_result.proxy_resolve_end); |
| 1677 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1678 | load_timing_result.connect_timing.dns_start); |
| 1679 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1680 | load_timing_result.connect_timing.dns_end); |
| 1681 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1682 | load_timing_result.connect_timing.connect_start); |
| 1683 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1684 | load_timing_result.connect_timing.connect_end); |
| 1685 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1686 | load_timing_result.connect_timing.ssl_start); |
| 1687 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1688 | load_timing_result.connect_timing.ssl_end); |
| 1689 | |
| 1690 | // Redundant sanity check. |
| 1691 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1692 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 1693 | } |
| 1694 | |
| 1695 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1696 | // |request_start|, due to already having a connected socket. This happens in |
| 1697 | // the case of reusing a SPDY session or HTTP pipeline. The connected socket is |
| 1698 | // not considered reused in this test (May be a preconnect). |
| 1699 | // |
| 1700 | // To mix things up from the test above, assumes DNS times but no SSL times. |
| 1701 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) { |
| 1702 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1703 | LoadTimingInfo job_load_timing = |
| 1704 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 1705 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 1706 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 1707 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 1708 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 1709 | job_load_timing.connect_timing.connect_start = |
| 1710 | now - base::TimeDelta::FromDays(2); |
| 1711 | job_load_timing.connect_timing.connect_end = |
| 1712 | now - base::TimeDelta::FromDays(1); |
| 1713 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1714 | LoadTimingInfo load_timing_result = |
| 1715 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1716 | |
| 1717 | // Proxy times, connect times, and DNS times should all be replaced with |
| 1718 | // request_start. |
| 1719 | EXPECT_EQ(load_timing_result.request_start, |
| 1720 | load_timing_result.proxy_resolve_start); |
| 1721 | EXPECT_EQ(load_timing_result.request_start, |
| 1722 | load_timing_result.proxy_resolve_end); |
| 1723 | EXPECT_EQ(load_timing_result.request_start, |
| 1724 | load_timing_result.connect_timing.dns_start); |
| 1725 | EXPECT_EQ(load_timing_result.request_start, |
| 1726 | load_timing_result.connect_timing.dns_end); |
| 1727 | EXPECT_EQ(load_timing_result.request_start, |
| 1728 | load_timing_result.connect_timing.connect_start); |
| 1729 | EXPECT_EQ(load_timing_result.request_start, |
| 1730 | load_timing_result.connect_timing.connect_end); |
| 1731 | |
| 1732 | // Other times should have been left null. |
| 1733 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1734 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 1735 | } |
| 1736 | |
| 1737 | // Same as above, but in the reused case. |
| 1738 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) { |
| 1739 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1740 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 1741 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 1742 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 1743 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1744 | LoadTimingInfo load_timing_result = |
| 1745 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1746 | |
| 1747 | // Proxy times and connect times should all be replaced with request_start. |
| 1748 | EXPECT_EQ(load_timing_result.request_start, |
| 1749 | load_timing_result.proxy_resolve_start); |
| 1750 | EXPECT_EQ(load_timing_result.request_start, |
| 1751 | load_timing_result.proxy_resolve_end); |
| 1752 | |
| 1753 | // Other times should have been left null. |
| 1754 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 1755 | } |
| 1756 | |
| 1757 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1758 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 1759 | // not considered reused in this test (May be a preconnect). |
| 1760 | // |
| 1761 | // To mix things up, the request has SSL times, but no DNS times. |
| 1762 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) { |
| 1763 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1764 | LoadTimingInfo job_load_timing = |
| 1765 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 1766 | job_load_timing.connect_timing.connect_start = |
| 1767 | now - base::TimeDelta::FromDays(1); |
| 1768 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 1769 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 1770 | job_load_timing.connect_timing.connect_end = |
| 1771 | now - base::TimeDelta::FromDays(4); |
| 1772 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1773 | LoadTimingInfo load_timing_result = |
| 1774 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1775 | |
| 1776 | // Connect times, and SSL times should be replaced with request_start. |
| 1777 | EXPECT_EQ(load_timing_result.request_start, |
| 1778 | load_timing_result.connect_timing.connect_start); |
| 1779 | EXPECT_EQ(load_timing_result.request_start, |
| 1780 | load_timing_result.connect_timing.ssl_start); |
| 1781 | EXPECT_EQ(load_timing_result.request_start, |
| 1782 | load_timing_result.connect_timing.ssl_end); |
| 1783 | EXPECT_EQ(load_timing_result.request_start, |
| 1784 | load_timing_result.connect_timing.connect_end); |
| 1785 | |
| 1786 | // Other times should have been left null. |
| 1787 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 1788 | } |
| 1789 | |
| 1790 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1791 | // |request_start|, due to reusing a connected socket in the case that there |
| 1792 | // are also proxy times. The connected socket is not considered reused in this |
| 1793 | // test (May be a preconnect). |
| 1794 | // |
| 1795 | // In this test, there are no SSL or DNS times. |
| 1796 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) { |
| 1797 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1798 | LoadTimingInfo job_load_timing = |
| 1799 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 1800 | job_load_timing.connect_timing.connect_start = |
| 1801 | now - base::TimeDelta::FromDays(1); |
| 1802 | job_load_timing.connect_timing.connect_end = |
| 1803 | now - base::TimeDelta::FromDays(2); |
| 1804 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1805 | LoadTimingInfo load_timing_result = |
| 1806 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1807 | |
| 1808 | // Connect times should be replaced with proxy_resolve_end. |
| 1809 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1810 | load_timing_result.connect_timing.connect_start); |
| 1811 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1812 | load_timing_result.connect_timing.connect_end); |
| 1813 | |
| 1814 | // Other times should have been left null. |
| 1815 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1816 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 1817 | } |
| 1818 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1819 | // Check that two different URL requests have different identifiers. |
| 1820 | TEST_F(URLRequestTest, Identifiers) { |
| 1821 | TestDelegate d; |
| 1822 | TestURLRequestContext context; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1823 | TestURLRequest req( |
| 1824 | GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context); |
| 1825 | TestURLRequest other_req( |
| 1826 | GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1827 | |
| 1828 | ASSERT_NE(req.identifier(), other_req.identifier()); |
| 1829 | } |
| 1830 | |
| 1831 | // Check that a failure to connect to the proxy is reported to the network |
| 1832 | // delegate. |
| 1833 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
| 1834 | MockHostResolver host_resolver; |
| 1835 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 1836 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 1837 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1838 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 1839 | |
| 1840 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1841 | URLRequest req(GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1842 | req.set_method("GET"); |
| 1843 | |
| 1844 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1845 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1846 | |
| 1847 | // Check we see a failed request. |
| 1848 | EXPECT_FALSE(req.status().is_success()); |
| 1849 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 1850 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error()); |
| 1851 | |
| 1852 | EXPECT_EQ(1, network_delegate.error_count()); |
| 1853 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
| 1854 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 1855 | } |
| 1856 | |
| 1857 | // Make sure that net::NetworkDelegate::NotifyCompleted is called if |
| 1858 | // content is empty. |
| 1859 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 1860 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1861 | URLRequest req(GURL("data:,"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1862 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1863 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1864 | EXPECT_EQ("", d.data_received()); |
| 1865 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 1866 | } |
| 1867 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1868 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 1869 | // correctly, both before and after start. |
| 1870 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 1871 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1872 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1873 | DEFAULT_PRIORITY, |
| 1874 | &d, |
| 1875 | &default_context_); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1876 | EXPECT_EQ(DEFAULT_PRIORITY, req.priority()); |
| 1877 | |
| 1878 | req.SetPriority(LOW); |
| 1879 | EXPECT_EQ(LOW, req.priority()); |
| 1880 | |
| 1881 | req.Start(); |
| 1882 | EXPECT_EQ(LOW, req.priority()); |
| 1883 | |
| 1884 | req.SetPriority(MEDIUM); |
| 1885 | EXPECT_EQ(MEDIUM, req.priority()); |
| 1886 | } |
| 1887 | |
| 1888 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 1889 | // Start on it. |
| 1890 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 1891 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1892 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1893 | DEFAULT_PRIORITY, |
| 1894 | &d, |
| 1895 | &default_context_); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1896 | EXPECT_EQ(DEFAULT_PRIORITY, req.priority()); |
| 1897 | |
| 1898 | scoped_refptr<URLRequestTestJob> job = |
| 1899 | new URLRequestTestJob(&req, &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1900 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1901 | EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 1902 | |
| 1903 | req.SetPriority(LOW); |
| 1904 | |
| 1905 | req.Start(); |
| 1906 | EXPECT_EQ(LOW, job->priority()); |
| 1907 | } |
| 1908 | |
| 1909 | // Make sure that URLRequest passes on its priority updates to its |
| 1910 | // job. |
| 1911 | TEST_F(URLRequestTest, SetJobPriority) { |
| 1912 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1913 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1914 | DEFAULT_PRIORITY, |
| 1915 | &d, |
| 1916 | &default_context_); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1917 | |
| 1918 | scoped_refptr<URLRequestTestJob> job = |
| 1919 | new URLRequestTestJob(&req, &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 | |
| 1922 | req.SetPriority(LOW); |
| 1923 | req.Start(); |
| 1924 | EXPECT_EQ(LOW, job->priority()); |
| 1925 | |
| 1926 | req.SetPriority(MEDIUM); |
| 1927 | EXPECT_EQ(MEDIUM, req.priority()); |
| 1928 | EXPECT_EQ(MEDIUM, job->priority()); |
| 1929 | } |
| 1930 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1931 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 1932 | // is MAXIMUM_PRIORITY. |
| 1933 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 1934 | TestDelegate d; |
| 1935 | URLRequest req(GURL("http://test_intercept/foo"), |
| 1936 | MAXIMUM_PRIORITY, |
| 1937 | &d, |
| 1938 | &default_context_); |
| 1939 | EXPECT_EQ(MAXIMUM_PRIORITY, req.priority()); |
| 1940 | |
| 1941 | scoped_refptr<URLRequestTestJob> job = |
| 1942 | new URLRequestTestJob(&req, &default_network_delegate_); |
| 1943 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
| 1944 | |
| 1945 | req.SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 1946 | EXPECT_EQ(MAXIMUM_PRIORITY, req.priority()); |
| 1947 | |
| 1948 | req.SetPriority(MAXIMUM_PRIORITY); |
| 1949 | EXPECT_EQ(MAXIMUM_PRIORITY, req.priority()); |
| 1950 | |
| 1951 | req.Start(); |
| 1952 | EXPECT_EQ(MAXIMUM_PRIORITY, req.priority()); |
| 1953 | EXPECT_EQ(MAXIMUM_PRIORITY, job->priority()); |
| 1954 | } |
| 1955 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1956 | // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666). |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1957 | #if !defined(OS_IOS) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1958 | // A subclass of SpawnedTestServer that uses a statically-configured hostname. |
| 1959 | // This is to work around mysterious failures in chrome_frame_net_tests. See: |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1960 | // http://crbug.com/114369 |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 1961 | // TODO(erikwright): remove or update as needed; see http://crbug.com/334634. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1962 | class LocalHttpTestServer : public SpawnedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1963 | public: |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1964 | explicit LocalHttpTestServer(const base::FilePath& document_root) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1965 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 1966 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 1967 | document_root) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1968 | LocalHttpTestServer() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1969 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 1970 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 1971 | base::FilePath()) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1972 | }; |
| 1973 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 1974 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1975 | LocalHttpTestServer test_server; |
| 1976 | ASSERT_TRUE(test_server.Start()); |
| 1977 | |
| 1978 | TestURLRequestContext context; |
| 1979 | scoped_refptr<DelayedCookieMonster> delayed_cm = |
| 1980 | new DelayedCookieMonster(); |
| 1981 | scoped_refptr<CookieStore> cookie_store = delayed_cm; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1982 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1983 | |
| 1984 | // Set up a cookie. |
| 1985 | { |
| 1986 | TestNetworkDelegate network_delegate; |
| 1987 | context.set_network_delegate(&network_delegate); |
| 1988 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1989 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), |
| 1990 | DEFAULT_PRIORITY, |
| 1991 | &d, |
| 1992 | &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1993 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1994 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1995 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1996 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1997 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 1998 | } |
| 1999 | |
| 2000 | // Verify that the cookie is set. |
| 2001 | { |
| 2002 | TestNetworkDelegate network_delegate; |
| 2003 | context.set_network_delegate(&network_delegate); |
| 2004 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2005 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2006 | DEFAULT_PRIORITY, |
| 2007 | &d, |
| 2008 | &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2009 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2010 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2011 | |
| 2012 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2013 | != std::string::npos); |
| 2014 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2015 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2016 | } |
| 2017 | } |
| 2018 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2019 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2020 | LocalHttpTestServer test_server; |
| 2021 | ASSERT_TRUE(test_server.Start()); |
| 2022 | |
| 2023 | // Set up a cookie. |
| 2024 | { |
| 2025 | TestNetworkDelegate network_delegate; |
| 2026 | default_context_.set_network_delegate(&network_delegate); |
| 2027 | TestDelegate d; |
| 2028 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2029 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2030 | &d, |
| 2031 | &default_context_); |
| 2032 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2033 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2034 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2035 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2036 | } |
| 2037 | |
| 2038 | // Verify that the cookie is set. |
| 2039 | { |
| 2040 | TestNetworkDelegate network_delegate; |
| 2041 | default_context_.set_network_delegate(&network_delegate); |
| 2042 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2043 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2044 | DEFAULT_PRIORITY, |
| 2045 | &d, |
| 2046 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2047 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2048 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2049 | |
| 2050 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2051 | != std::string::npos); |
| 2052 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2053 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2054 | } |
| 2055 | |
| 2056 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2057 | { |
| 2058 | TestNetworkDelegate network_delegate; |
| 2059 | default_context_.set_network_delegate(&network_delegate); |
| 2060 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2061 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2062 | DEFAULT_PRIORITY, |
| 2063 | &d, |
| 2064 | &default_context_); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2065 | req.SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2066 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2067 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2068 | |
| 2069 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2070 | == std::string::npos); |
| 2071 | |
| 2072 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2073 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2074 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2079 | LocalHttpTestServer test_server; |
| 2080 | ASSERT_TRUE(test_server.Start()); |
| 2081 | |
| 2082 | // Set up a cookie. |
| 2083 | { |
| 2084 | TestNetworkDelegate network_delegate; |
| 2085 | default_context_.set_network_delegate(&network_delegate); |
| 2086 | TestDelegate d; |
| 2087 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2088 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2089 | &d, |
| 2090 | &default_context_); |
| 2091 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2092 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2093 | |
| 2094 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2095 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2096 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2097 | } |
| 2098 | |
| 2099 | // Try to set-up another cookie and update the previous cookie. |
| 2100 | { |
| 2101 | TestNetworkDelegate network_delegate; |
| 2102 | default_context_.set_network_delegate(&network_delegate); |
| 2103 | TestDelegate d; |
| 2104 | URLRequest req( |
| 2105 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2106 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2107 | &d, |
| 2108 | &default_context_); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2109 | req.SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2110 | req.Start(); |
| 2111 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2112 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2113 | |
| 2114 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2115 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2116 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2117 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2118 | } |
| 2119 | |
| 2120 | // Verify the cookies weren't saved or updated. |
| 2121 | { |
| 2122 | TestNetworkDelegate network_delegate; |
| 2123 | default_context_.set_network_delegate(&network_delegate); |
| 2124 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2125 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2126 | DEFAULT_PRIORITY, |
| 2127 | &d, |
| 2128 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2129 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2130 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2131 | |
| 2132 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2133 | == std::string::npos); |
| 2134 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2135 | != std::string::npos); |
| 2136 | |
| 2137 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2138 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2139 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2144 | LocalHttpTestServer test_server; |
| 2145 | ASSERT_TRUE(test_server.Start()); |
| 2146 | |
| 2147 | // Set up a cookie. |
| 2148 | { |
| 2149 | TestNetworkDelegate network_delegate; |
| 2150 | default_context_.set_network_delegate(&network_delegate); |
| 2151 | TestDelegate d; |
| 2152 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2153 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2154 | &d, |
| 2155 | &default_context_); |
| 2156 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2157 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2158 | |
| 2159 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2160 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2161 | } |
| 2162 | |
| 2163 | // Verify that the cookie is set. |
| 2164 | { |
| 2165 | TestNetworkDelegate network_delegate; |
| 2166 | default_context_.set_network_delegate(&network_delegate); |
| 2167 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2168 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2169 | DEFAULT_PRIORITY, |
| 2170 | &d, |
| 2171 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 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] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2188 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2189 | DEFAULT_PRIORITY, |
| 2190 | &d, |
| 2191 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2192 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2193 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2194 | |
| 2195 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2196 | == std::string::npos); |
| 2197 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2198 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2199 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2200 | } |
| 2201 | } |
| 2202 | |
| 2203 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2204 | LocalHttpTestServer test_server; |
| 2205 | ASSERT_TRUE(test_server.Start()); |
| 2206 | |
| 2207 | // Set up a cookie. |
| 2208 | { |
| 2209 | TestNetworkDelegate network_delegate; |
| 2210 | default_context_.set_network_delegate(&network_delegate); |
| 2211 | TestDelegate d; |
| 2212 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2213 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2214 | &d, |
| 2215 | &default_context_); |
| 2216 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2217 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2218 | |
| 2219 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2220 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2221 | } |
| 2222 | |
| 2223 | // Try to set-up another cookie and update the previous cookie. |
| 2224 | { |
| 2225 | TestNetworkDelegate network_delegate; |
| 2226 | default_context_.set_network_delegate(&network_delegate); |
| 2227 | TestDelegate d; |
| 2228 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
| 2229 | URLRequest req( |
| 2230 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2231 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2232 | &d, |
| 2233 | &default_context_); |
| 2234 | req.Start(); |
| 2235 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2236 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2237 | |
| 2238 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2239 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2240 | } |
| 2241 | |
| 2242 | // Verify the cookies weren't saved or updated. |
| 2243 | { |
| 2244 | TestNetworkDelegate network_delegate; |
| 2245 | default_context_.set_network_delegate(&network_delegate); |
| 2246 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2247 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2248 | DEFAULT_PRIORITY, |
| 2249 | &d, |
| 2250 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2251 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2252 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2253 | |
| 2254 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2255 | == std::string::npos); |
| 2256 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2257 | != std::string::npos); |
| 2258 | |
| 2259 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2260 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2265 | LocalHttpTestServer test_server; |
| 2266 | ASSERT_TRUE(test_server.Start()); |
| 2267 | |
| 2268 | // Set up an empty cookie. |
| 2269 | { |
| 2270 | TestNetworkDelegate network_delegate; |
| 2271 | default_context_.set_network_delegate(&network_delegate); |
| 2272 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2273 | URLRequest req(test_server.GetURL("set-cookie"), |
| 2274 | DEFAULT_PRIORITY, |
| 2275 | &d, |
| 2276 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2277 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2278 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2279 | |
| 2280 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2281 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2282 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2287 | LocalHttpTestServer test_server; |
| 2288 | ASSERT_TRUE(test_server.Start()); |
| 2289 | |
| 2290 | // Set up a cookie. |
| 2291 | { |
| 2292 | TestNetworkDelegate network_delegate; |
| 2293 | default_context_.set_network_delegate(&network_delegate); |
| 2294 | TestDelegate d; |
| 2295 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2296 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2297 | &d, |
| 2298 | &default_context_); |
| 2299 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2300 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2301 | |
| 2302 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2303 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2304 | } |
| 2305 | |
| 2306 | // Verify that the cookie is set. |
| 2307 | { |
| 2308 | TestNetworkDelegate network_delegate; |
| 2309 | default_context_.set_network_delegate(&network_delegate); |
| 2310 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2311 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2312 | DEFAULT_PRIORITY, |
| 2313 | &d, |
| 2314 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2315 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2316 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2317 | |
| 2318 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2319 | != std::string::npos); |
| 2320 | |
| 2321 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2322 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2323 | } |
| 2324 | |
| 2325 | // Verify that the cookie isn't sent. |
| 2326 | { |
| 2327 | TestNetworkDelegate network_delegate; |
| 2328 | default_context_.set_network_delegate(&network_delegate); |
| 2329 | TestDelegate d; |
| 2330 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2331 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2332 | DEFAULT_PRIORITY, |
| 2333 | &d, |
| 2334 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2335 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2336 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2337 | |
| 2338 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2339 | == std::string::npos); |
| 2340 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2341 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2342 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2347 | LocalHttpTestServer test_server; |
| 2348 | ASSERT_TRUE(test_server.Start()); |
| 2349 | |
| 2350 | // Set up a cookie. |
| 2351 | { |
| 2352 | TestNetworkDelegate network_delegate; |
| 2353 | default_context_.set_network_delegate(&network_delegate); |
| 2354 | TestDelegate d; |
| 2355 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2356 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2357 | &d, |
| 2358 | &default_context_); |
| 2359 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2360 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2361 | |
| 2362 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2363 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2364 | } |
| 2365 | |
| 2366 | // Try to set-up another cookie and update the previous cookie. |
| 2367 | { |
| 2368 | TestNetworkDelegate network_delegate; |
| 2369 | default_context_.set_network_delegate(&network_delegate); |
| 2370 | TestDelegate d; |
| 2371 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
| 2372 | URLRequest req( |
| 2373 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2374 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2375 | &d, |
| 2376 | &default_context_); |
| 2377 | req.Start(); |
| 2378 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2379 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2380 | |
| 2381 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2382 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2383 | } |
| 2384 | |
| 2385 | // Verify the cookies weren't saved or updated. |
| 2386 | { |
| 2387 | TestNetworkDelegate network_delegate; |
| 2388 | default_context_.set_network_delegate(&network_delegate); |
| 2389 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2390 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2391 | DEFAULT_PRIORITY, |
| 2392 | &d, |
| 2393 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2394 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2395 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2396 | |
| 2397 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2398 | == std::string::npos); |
| 2399 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2400 | != std::string::npos); |
| 2401 | |
| 2402 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2403 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2404 | } |
| 2405 | } |
| 2406 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2407 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 2408 | // value for the |fixed_date| argument given to the constructor. |
| 2409 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 2410 | public: |
| 2411 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 2412 | : fixed_date_(fixed_date) {} |
| 2413 | virtual ~FixedDateNetworkDelegate() {} |
| 2414 | |
| 2415 | // net::NetworkDelegate implementation |
| 2416 | virtual int OnHeadersReceived( |
| 2417 | net::URLRequest* request, |
| 2418 | const net::CompletionCallback& callback, |
| 2419 | const net::HttpResponseHeaders* original_response_headers, |
| 2420 | scoped_refptr<net::HttpResponseHeaders>* override_response_headers) |
| 2421 | OVERRIDE; |
| 2422 | |
| 2423 | private: |
| 2424 | std::string fixed_date_; |
| 2425 | |
| 2426 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 2427 | }; |
| 2428 | |
| 2429 | int FixedDateNetworkDelegate::OnHeadersReceived( |
| 2430 | net::URLRequest* request, |
| 2431 | const net::CompletionCallback& callback, |
| 2432 | const net::HttpResponseHeaders* original_response_headers, |
| 2433 | scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { |
| 2434 | net::HttpResponseHeaders* new_response_headers = |
| 2435 | new net::HttpResponseHeaders(original_response_headers->raw_headers()); |
| 2436 | |
| 2437 | new_response_headers->RemoveHeader("Date"); |
| 2438 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 2439 | |
| 2440 | *override_response_headers = new_response_headers; |
| 2441 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 2442 | callback, |
| 2443 | original_response_headers, |
| 2444 | override_response_headers); |
| 2445 | } |
| 2446 | |
| 2447 | // Test that cookie expiration times are adjusted for server/client clock |
| 2448 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 2449 | // headers by defaulting to GMT. (crbug.com/135131) |
| 2450 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 2451 | LocalHttpTestServer test_server; |
| 2452 | ASSERT_TRUE(test_server.Start()); |
| 2453 | |
| 2454 | // Set up an expired cookie. |
| 2455 | { |
| 2456 | TestNetworkDelegate network_delegate; |
| 2457 | default_context_.set_network_delegate(&network_delegate); |
| 2458 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2459 | URLRequest req( |
| 2460 | test_server.GetURL( |
| 2461 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
| 2462 | DEFAULT_PRIORITY, |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2463 | &d, |
| 2464 | &default_context_); |
| 2465 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2466 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2467 | } |
| 2468 | // Verify that the cookie is not set. |
| 2469 | { |
| 2470 | TestNetworkDelegate network_delegate; |
| 2471 | default_context_.set_network_delegate(&network_delegate); |
| 2472 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2473 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2474 | DEFAULT_PRIORITY, |
| 2475 | &d, |
| 2476 | &default_context_); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2477 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2478 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2479 | |
| 2480 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 2481 | } |
| 2482 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 2483 | { |
| 2484 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 2485 | default_context_.set_network_delegate(&network_delegate); |
| 2486 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2487 | URLRequest req( |
| 2488 | test_server.GetURL( |
| 2489 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
| 2490 | DEFAULT_PRIORITY, |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2491 | &d, |
| 2492 | &default_context_); |
| 2493 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2494 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2495 | } |
| 2496 | // Verify that the cookie is set. |
| 2497 | { |
| 2498 | TestNetworkDelegate network_delegate; |
| 2499 | default_context_.set_network_delegate(&network_delegate); |
| 2500 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2501 | URLRequest req(test_server.GetURL("echoheader?Cookie"), |
| 2502 | DEFAULT_PRIORITY, |
| 2503 | &d, |
| 2504 | &default_context_); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2505 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2506 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2507 | |
| 2508 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2513 | // Check that it is impossible to change the referrer in the extra headers of |
| 2514 | // an URLRequest. |
| 2515 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 2516 | LocalHttpTestServer test_server; |
| 2517 | ASSERT_TRUE(test_server.Start()); |
| 2518 | |
| 2519 | // If extra headers contain referer and the request contains a referer, |
| 2520 | // only the latter shall be respected. |
| 2521 | { |
| 2522 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2523 | URLRequest req(test_server.GetURL("echoheader?Referer"), |
| 2524 | DEFAULT_PRIORITY, |
| 2525 | &d, |
| 2526 | &default_context_); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 2527 | req.SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2528 | |
| 2529 | HttpRequestHeaders headers; |
| 2530 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
| 2531 | req.SetExtraRequestHeaders(headers); |
| 2532 | |
| 2533 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2534 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2535 | |
| 2536 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 2537 | } |
| 2538 | |
| 2539 | // If extra headers contain a referer but the request does not, no referer |
| 2540 | // shall be sent in the header. |
| 2541 | { |
| 2542 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2543 | URLRequest req(test_server.GetURL("echoheader?Referer"), |
| 2544 | DEFAULT_PRIORITY, |
| 2545 | &d, |
| 2546 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2547 | |
| 2548 | HttpRequestHeaders headers; |
| 2549 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
| 2550 | req.SetExtraRequestHeaders(headers); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2551 | req.SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2552 | |
| 2553 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2554 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2555 | |
| 2556 | EXPECT_EQ("None", d.data_received()); |
| 2557 | } |
| 2558 | } |
| 2559 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2560 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2561 | public: |
| 2562 | URLRequestTestHTTP() |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 2563 | : test_server_(base::FilePath(FILE_PATH_LITERAL( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2564 | "net/data/url_request_unittest"))) { |
| 2565 | } |
| 2566 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2567 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2568 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 2569 | // |request_method| is the method to use for the initial request. |
| 2570 | // |redirect_method| is the method that is expected to be used for the second |
| 2571 | // request, after redirection. |
| 2572 | // If |include_data| is true, data is uploaded with the request. The |
| 2573 | // response body is expected to match it exactly, if and only if |
| 2574 | // |request_method| == |redirect_method|. |
| 2575 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 2576 | const std::string& request_method, |
| 2577 | const std::string& redirect_method, |
| 2578 | bool include_data) { |
| 2579 | static const char kData[] = "hello world"; |
| 2580 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2581 | URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2582 | req.set_method(request_method); |
| 2583 | if (include_data) { |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 2584 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2585 | HttpRequestHeaders headers; |
| 2586 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 2587 | base::UintToString(arraysize(kData) - 1)); |
| 2588 | req.SetExtraRequestHeaders(headers); |
| 2589 | } |
| 2590 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2591 | base::RunLoop().Run(); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2592 | EXPECT_EQ(redirect_method, req.method()); |
| 2593 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 2594 | EXPECT_EQ(OK, req.status().error()); |
| 2595 | if (include_data) { |
| 2596 | if (request_method == redirect_method) { |
| 2597 | EXPECT_EQ(kData, d.data_received()); |
| 2598 | } else { |
| 2599 | EXPECT_NE(kData, d.data_received()); |
| 2600 | } |
| 2601 | } |
| 2602 | if (HasFailure()) |
| 2603 | LOG(WARNING) << "Request method was: " << request_method; |
| 2604 | } |
| 2605 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2606 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2607 | const int kMsgSize = 20000; // multiple of 10 |
| 2608 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 2609 | char* uploadBytes = new char[kMsgSize+1]; |
| 2610 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2611 | char marker = 'a'; |
| 2612 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 2613 | memcpy(ptr, "----------", 10); |
| 2614 | ptr += 10; |
| 2615 | if (idx % 100 == 0) { |
| 2616 | ptr--; |
| 2617 | *ptr++ = marker; |
| 2618 | if (++marker > 'z') |
| 2619 | marker = 'a'; |
| 2620 | } |
| 2621 | } |
| 2622 | uploadBytes[kMsgSize] = '\0'; |
| 2623 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2624 | for (int i = 0; i < kIterations; ++i) { |
| 2625 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2626 | URLRequest r( |
| 2627 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2628 | r.set_method(method.c_str()); |
| 2629 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 2630 | r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes))); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2631 | |
| 2632 | r.Start(); |
| 2633 | EXPECT_TRUE(r.is_pending()); |
| 2634 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2635 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2636 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2637 | ASSERT_EQ(1, d.response_started_count()) |
| 2638 | << "request failed: " << r.status().status() |
| 2639 | << ", os error: " << r.status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2640 | |
| 2641 | EXPECT_FALSE(d.received_data_before_response()); |
| 2642 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2643 | } |
| 2644 | delete[] uploadBytes; |
| 2645 | } |
| 2646 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2647 | void AddChunksToUpload(URLRequest* r) { |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 2648 | r->AppendChunkToUpload("a", 1, false); |
| 2649 | r->AppendChunkToUpload("bcd", 3, false); |
| 2650 | r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); |
| 2651 | r->AppendChunkToUpload("\r\n\r\n", 4, false); |
| 2652 | r->AppendChunkToUpload("0", 1, false); |
| 2653 | r->AppendChunkToUpload("2323", 4, true); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2654 | } |
| 2655 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2656 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2657 | // This should match the chunks sent by AddChunksToUpload(). |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2658 | const std::string expected_data = |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2659 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 2660 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2661 | ASSERT_EQ(1, d->response_started_count()) |
| 2662 | << "request failed: " << r->status().status() |
| 2663 | << ", os error: " << r->status().error(); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2664 | |
| 2665 | EXPECT_FALSE(d->received_data_before_response()); |
| 2666 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2667 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 2668 | EXPECT_EQ(expected_data, d->data_received()); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2669 | } |
| 2670 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2671 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2672 | TestDelegate d; |
| 2673 | URLRequest r(test_server_.GetURL("set-many-cookies?" + |
| 2674 | base::IntToString(num_cookies)), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2675 | DEFAULT_PRIORITY, |
| 2676 | &d, |
| 2677 | &default_context_); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2678 | |
| 2679 | r.Start(); |
| 2680 | EXPECT_TRUE(r.is_pending()); |
| 2681 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2682 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2683 | |
| 2684 | bool is_success = r.status().is_success(); |
| 2685 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2686 | if (!is_success) { |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 2687 | EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2688 | // The test server appears to be unable to handle subsequent requests |
| 2689 | // after this error is triggered. Force it to restart. |
| 2690 | EXPECT_TRUE(test_server_.Stop()); |
| 2691 | EXPECT_TRUE(test_server_.Start()); |
| 2692 | } |
| 2693 | |
| 2694 | return is_success; |
| 2695 | } |
| 2696 | |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2697 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2698 | }; |
| 2699 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2700 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 2701 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 2702 | // The HTTPTestServer will return a 302 response, which we should not |
| 2703 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2704 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2705 | ASSERT_TRUE(test_server_.Start()); |
| 2706 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2707 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2708 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2709 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2710 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2711 | TestDelegate d; |
| 2712 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2713 | URLRequest r( |
| 2714 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2715 | r.Start(); |
| 2716 | EXPECT_TRUE(r.is_pending()); |
| 2717 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2718 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2719 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2720 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2721 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 2722 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2723 | // We should not have followed the redirect. |
| 2724 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2725 | } |
| 2726 | } |
| 2727 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2728 | // This is the same as the previous test, but checks that the network delegate |
| 2729 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 2730 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2731 | ASSERT_TRUE(test_server_.Start()); |
| 2732 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2733 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2734 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2735 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2736 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2737 | TestDelegate d; |
| 2738 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2739 | URLRequest r( |
| 2740 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2741 | r.Start(); |
| 2742 | EXPECT_TRUE(r.is_pending()); |
| 2743 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2744 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2745 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2746 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2747 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2748 | EXPECT_EQ(1, d.response_started_count()); |
| 2749 | // We should not have followed the redirect. |
| 2750 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2751 | |
| 2752 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2753 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2757 | // Tests that we can block and asynchronously return OK in various stages. |
| 2758 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 2759 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 2760 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 2761 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 2762 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 2763 | }; |
| 2764 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 2765 | |
| 2766 | ASSERT_TRUE(test_server_.Start()); |
| 2767 | |
| 2768 | TestDelegate d; |
| 2769 | BlockingNetworkDelegate network_delegate( |
| 2770 | BlockingNetworkDelegate::USER_CALLBACK); |
| 2771 | network_delegate.set_block_on( |
| 2772 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 2773 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 2774 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 2775 | |
| 2776 | TestURLRequestContext context(true); |
| 2777 | context.set_network_delegate(&network_delegate); |
| 2778 | context.Init(); |
| 2779 | |
| 2780 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2781 | URLRequest r( |
| 2782 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, &context); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2783 | |
| 2784 | r.Start(); |
| 2785 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2786 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2787 | EXPECT_EQ(blocking_stages[i], |
| 2788 | network_delegate.stage_blocked_for_callback()); |
| 2789 | network_delegate.DoCallback(OK); |
| 2790 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2791 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2792 | EXPECT_EQ(200, r.GetResponseCode()); |
| 2793 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2794 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2795 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2796 | } |
| 2797 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2798 | } |
| 2799 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2800 | // Tests that the network delegate can block and cancel a request. |
| 2801 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
| 2802 | ASSERT_TRUE(test_server_.Start()); |
| 2803 | |
| 2804 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2805 | BlockingNetworkDelegate network_delegate( |
| 2806 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2807 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 2808 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2809 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2810 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2811 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2812 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2813 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2814 | URLRequest r( |
| 2815 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2816 | |
| 2817 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2818 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2819 | |
| 2820 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2821 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2822 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2823 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2824 | } |
| 2825 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2826 | } |
| 2827 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2828 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 2829 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 2830 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 2831 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 2832 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 2833 | BlockingNetworkDelegate::Stage stage, |
| 2834 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2835 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2836 | BlockingNetworkDelegate network_delegate(block_mode); |
| 2837 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 2838 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2839 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2840 | TestURLRequestContext context(true); |
| 2841 | context.set_network_delegate(&network_delegate); |
| 2842 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2843 | |
| 2844 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2845 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2846 | |
| 2847 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2848 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2849 | |
| 2850 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2851 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2852 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2853 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2854 | } |
| 2855 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2856 | } |
| 2857 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2858 | // The following 3 tests check that the network delegate can cancel a request |
| 2859 | // synchronously in various stages of the request. |
| 2860 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
| 2861 | ASSERT_TRUE(test_server_.Start()); |
| 2862 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2863 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2864 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
| 2868 | ASSERT_TRUE(test_server_.Start()); |
| 2869 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2870 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2871 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
| 2875 | ASSERT_TRUE(test_server_.Start()); |
| 2876 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2877 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2878 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | // The following 3 tests check that the network delegate can cancel a request |
| 2882 | // asynchronously in various stages of the request. |
| 2883 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
| 2884 | ASSERT_TRUE(test_server_.Start()); |
| 2885 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2886 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2887 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
| 2891 | ASSERT_TRUE(test_server_.Start()); |
| 2892 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2893 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2894 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
| 2898 | ASSERT_TRUE(test_server_.Start()); |
| 2899 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2900 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2901 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2902 | } |
| 2903 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2904 | // Tests that the network delegate can block and redirect a request to a new |
| 2905 | // URL. |
| 2906 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
| 2907 | ASSERT_TRUE(test_server_.Start()); |
| 2908 | |
| 2909 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2910 | BlockingNetworkDelegate network_delegate( |
| 2911 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2912 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2913 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2914 | network_delegate.set_redirect_url(redirect_url); |
| 2915 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2916 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2917 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2918 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2919 | { |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 2920 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2921 | URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2922 | |
| 2923 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2924 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2925 | |
| 2926 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2927 | EXPECT_EQ(0, r.status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2928 | EXPECT_EQ(redirect_url, r.url()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 2929 | EXPECT_EQ(original_url, r.original_url()); |
| 2930 | EXPECT_EQ(2U, r.url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2931 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2932 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2933 | } |
| 2934 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2935 | } |
| 2936 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2937 | // Tests that the network delegate can block and redirect a request to a new |
| 2938 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 2939 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
| 2940 | ASSERT_TRUE(test_server_.Start()); |
| 2941 | |
| 2942 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2943 | BlockingNetworkDelegate network_delegate( |
| 2944 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2945 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2946 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2947 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2948 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2949 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2950 | |
| 2951 | { |
| 2952 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2953 | URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2954 | |
| 2955 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2956 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2957 | |
| 2958 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2959 | EXPECT_EQ(0, r.status().error()); |
| 2960 | EXPECT_EQ(redirect_url, r.url()); |
| 2961 | EXPECT_EQ(original_url, r.original_url()); |
| 2962 | EXPECT_EQ(2U, r.url_chain().size()); |
| 2963 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2964 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2965 | } |
| 2966 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2967 | } |
| 2968 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2969 | // Tests that redirects caused by the network delegate preserve POST data. |
| 2970 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 2971 | ASSERT_TRUE(test_server_.Start()); |
| 2972 | |
| 2973 | const char kData[] = "hello world"; |
| 2974 | |
| 2975 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2976 | BlockingNetworkDelegate network_delegate( |
| 2977 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2978 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2979 | GURL redirect_url(test_server_.GetURL("echo")); |
| 2980 | network_delegate.set_redirect_url(redirect_url); |
| 2981 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2982 | TestURLRequestContext context(true); |
| 2983 | context.set_network_delegate(&network_delegate); |
| 2984 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2985 | |
| 2986 | { |
| 2987 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2988 | URLRequest r(original_url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2989 | r.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 2990 | r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2991 | HttpRequestHeaders headers; |
| 2992 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 2993 | base::UintToString(arraysize(kData) - 1)); |
| 2994 | r.SetExtraRequestHeaders(headers); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2995 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2996 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2997 | |
| 2998 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2999 | EXPECT_EQ(0, r.status().error()); |
| 3000 | EXPECT_EQ(redirect_url, r.url()); |
| 3001 | EXPECT_EQ(original_url, r.original_url()); |
| 3002 | EXPECT_EQ(2U, r.url_chain().size()); |
| 3003 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3004 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3005 | EXPECT_EQ("POST", r.method()); |
| 3006 | EXPECT_EQ(kData, d.data_received()); |
| 3007 | } |
| 3008 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3009 | } |
| 3010 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3011 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3012 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 3013 | // handle the challenge, and is passing the buck along to the |
| 3014 | // URLRequest::Delegate. |
| 3015 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 3016 | ASSERT_TRUE(test_server_.Start()); |
| 3017 | |
| 3018 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3019 | BlockingNetworkDelegate network_delegate( |
| 3020 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3021 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3022 | TestURLRequestContext context(true); |
| 3023 | context.set_network_delegate(&network_delegate); |
| 3024 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3025 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3026 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3027 | |
| 3028 | { |
| 3029 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3030 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3031 | r.Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3032 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3033 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3034 | |
| 3035 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3036 | EXPECT_EQ(0, r.status().error()); |
| 3037 | EXPECT_EQ(200, r.GetResponseCode()); |
| 3038 | EXPECT_TRUE(d.auth_required_called()); |
| 3039 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3040 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3041 | } |
| 3042 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3043 | } |
| 3044 | |
| 3045 | TEST_F(URLRequestTestHTTP, |
| 3046 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
| 3047 | ASSERT_TRUE(test_server_.Start()); |
| 3048 | |
| 3049 | TestDelegate d; |
| 3050 | BlockingNetworkDelegate network_delegate( |
| 3051 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3052 | |
| 3053 | TestURLRequestContext context(true); |
| 3054 | context.set_network_delegate(&network_delegate); |
| 3055 | context.Init(); |
| 3056 | |
| 3057 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3058 | |
| 3059 | { |
| 3060 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3061 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3062 | r.Start(); |
| 3063 | |
| 3064 | { |
| 3065 | HttpRequestHeaders headers; |
| 3066 | EXPECT_TRUE(r.GetFullRequestHeaders(&headers)); |
| 3067 | EXPECT_FALSE(headers.HasHeader("Authorization")); |
| 3068 | } |
| 3069 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3070 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3071 | |
| 3072 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3073 | EXPECT_EQ(0, r.status().error()); |
| 3074 | EXPECT_EQ(200, r.GetResponseCode()); |
| 3075 | EXPECT_TRUE(d.auth_required_called()); |
| 3076 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3077 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3078 | } |
| 3079 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3080 | } |
| 3081 | |
| 3082 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 3083 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3084 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 3085 | ASSERT_TRUE(test_server_.Start()); |
| 3086 | |
| 3087 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3088 | BlockingNetworkDelegate network_delegate( |
| 3089 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3090 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3091 | network_delegate.set_auth_retval( |
| 3092 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3093 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3094 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3095 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3096 | TestURLRequestContext context(true); |
| 3097 | context.set_network_delegate(&network_delegate); |
| 3098 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3099 | |
| 3100 | { |
| 3101 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3102 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3103 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3104 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3105 | |
| 3106 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3107 | EXPECT_EQ(0, r.status().error()); |
| 3108 | EXPECT_EQ(200, r.GetResponseCode()); |
| 3109 | EXPECT_FALSE(d.auth_required_called()); |
| 3110 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3111 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3112 | } |
| 3113 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3114 | } |
| 3115 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3116 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 3117 | // headers (for the first or second request) when called at the proper times. |
| 3118 | TEST_F(URLRequestTestHTTP, |
| 3119 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
| 3120 | ASSERT_TRUE(test_server_.Start()); |
| 3121 | |
| 3122 | TestDelegate d; |
| 3123 | BlockingNetworkDelegate network_delegate( |
| 3124 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3125 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 3126 | network_delegate.set_auth_retval( |
| 3127 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3128 | |
| 3129 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 3130 | |
| 3131 | TestURLRequestContext context(true); |
| 3132 | context.set_network_delegate(&network_delegate); |
| 3133 | context.Init(); |
| 3134 | |
| 3135 | { |
| 3136 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3137 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3138 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3139 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3140 | |
| 3141 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3142 | EXPECT_EQ(0, r.status().error()); |
| 3143 | EXPECT_EQ(200, r.GetResponseCode()); |
| 3144 | EXPECT_FALSE(d.auth_required_called()); |
| 3145 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3146 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3147 | |
| 3148 | { |
| 3149 | HttpRequestHeaders headers; |
| 3150 | EXPECT_TRUE(r.GetFullRequestHeaders(&headers)); |
| 3151 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 3152 | } |
| 3153 | } |
| 3154 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3155 | } |
| 3156 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3157 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3158 | // by cancelling authentication. |
| 3159 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 3160 | ASSERT_TRUE(test_server_.Start()); |
| 3161 | |
| 3162 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3163 | BlockingNetworkDelegate network_delegate( |
| 3164 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3165 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3166 | network_delegate.set_auth_retval( |
| 3167 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3168 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3169 | TestURLRequestContext context(true); |
| 3170 | context.set_network_delegate(&network_delegate); |
| 3171 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3172 | |
| 3173 | { |
| 3174 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3175 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3176 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3177 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3178 | |
| 3179 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3180 | EXPECT_EQ(OK, r.status().error()); |
| 3181 | EXPECT_EQ(401, r.GetResponseCode()); |
| 3182 | EXPECT_FALSE(d.auth_required_called()); |
| 3183 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3184 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3185 | } |
| 3186 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3187 | } |
| 3188 | |
| 3189 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3190 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 3191 | // to handle the challenge, and is passing the buck along to the |
| 3192 | // URLRequest::Delegate. |
| 3193 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 3194 | ASSERT_TRUE(test_server_.Start()); |
| 3195 | |
| 3196 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3197 | BlockingNetworkDelegate network_delegate( |
| 3198 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3199 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3200 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3201 | TestURLRequestContext context(true); |
| 3202 | context.set_network_delegate(&network_delegate); |
| 3203 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3204 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3205 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3206 | |
| 3207 | { |
| 3208 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3209 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3210 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3211 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3212 | |
| 3213 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3214 | EXPECT_EQ(0, r.status().error()); |
| 3215 | EXPECT_EQ(200, r.GetResponseCode()); |
| 3216 | EXPECT_TRUE(d.auth_required_called()); |
| 3217 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3218 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3219 | } |
| 3220 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3221 | } |
| 3222 | |
| 3223 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3224 | // by setting credentials. |
| 3225 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 3226 | ASSERT_TRUE(test_server_.Start()); |
| 3227 | |
| 3228 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3229 | BlockingNetworkDelegate network_delegate( |
| 3230 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3231 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3232 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3233 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3234 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3235 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3236 | network_delegate.set_auth_credentials(auth_credentials); |
| 3237 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3238 | TestURLRequestContext context(true); |
| 3239 | context.set_network_delegate(&network_delegate); |
| 3240 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3241 | |
| 3242 | { |
| 3243 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3244 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3245 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3246 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3247 | |
| 3248 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3249 | EXPECT_EQ(0, r.status().error()); |
| 3250 | |
| 3251 | EXPECT_EQ(200, r.GetResponseCode()); |
| 3252 | EXPECT_FALSE(d.auth_required_called()); |
| 3253 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3254 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3255 | } |
| 3256 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3257 | } |
| 3258 | |
| 3259 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3260 | // by cancelling authentication. |
| 3261 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 3262 | ASSERT_TRUE(test_server_.Start()); |
| 3263 | |
| 3264 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3265 | BlockingNetworkDelegate network_delegate( |
| 3266 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3267 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3268 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3269 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3270 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3271 | TestURLRequestContext context(true); |
| 3272 | context.set_network_delegate(&network_delegate); |
| 3273 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3274 | |
| 3275 | { |
| 3276 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3277 | URLRequest r(url, DEFAULT_PRIORITY, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3278 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3279 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3280 | |
| 3281 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3282 | EXPECT_EQ(OK, r.status().error()); |
| 3283 | EXPECT_EQ(401, r.GetResponseCode()); |
| 3284 | EXPECT_FALSE(d.auth_required_called()); |
| 3285 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3286 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3287 | } |
| 3288 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3289 | } |
| 3290 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3291 | // Tests that we can handle when a network request was canceled while we were |
| 3292 | // waiting for the network delegate. |
| 3293 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 3294 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 3295 | ASSERT_TRUE(test_server_.Start()); |
| 3296 | |
| 3297 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3298 | BlockingNetworkDelegate network_delegate( |
| 3299 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3300 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3301 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3302 | TestURLRequestContext context(true); |
| 3303 | context.set_network_delegate(&network_delegate); |
| 3304 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3305 | |
| 3306 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3307 | URLRequest r( |
| 3308 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3309 | |
| 3310 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3311 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3312 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3313 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3314 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3315 | // Cancel before callback. |
| 3316 | r.Cancel(); |
| 3317 | // Ensure that network delegate is notified. |
| 3318 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 3319 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 3320 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 3321 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3322 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3323 | } |
| 3324 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3325 | } |
| 3326 | |
| 3327 | // Tests that we can handle when a network request was canceled while we were |
| 3328 | // waiting for the network delegate. |
| 3329 | // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 3330 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
| 3331 | ASSERT_TRUE(test_server_.Start()); |
| 3332 | |
| 3333 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3334 | BlockingNetworkDelegate network_delegate( |
| 3335 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3336 | network_delegate.set_block_on( |
| 3337 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3338 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3339 | TestURLRequestContext context(true); |
| 3340 | context.set_network_delegate(&network_delegate); |
| 3341 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3342 | |
| 3343 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3344 | URLRequest r( |
| 3345 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3346 | |
| 3347 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3348 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3349 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3350 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3351 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3352 | // Cancel before callback. |
| 3353 | r.Cancel(); |
| 3354 | // Ensure that network delegate is notified. |
| 3355 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 3356 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 3357 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 3358 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3359 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3360 | } |
| 3361 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3362 | } |
| 3363 | |
| 3364 | // Tests that we can handle when a network request was canceled while we were |
| 3365 | // waiting for the network delegate. |
| 3366 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 3367 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 3368 | ASSERT_TRUE(test_server_.Start()); |
| 3369 | |
| 3370 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3371 | BlockingNetworkDelegate network_delegate( |
| 3372 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3373 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3374 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3375 | TestURLRequestContext context(true); |
| 3376 | context.set_network_delegate(&network_delegate); |
| 3377 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3378 | |
| 3379 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3380 | URLRequest r( |
| 3381 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3382 | |
| 3383 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3384 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3385 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 3386 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3387 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3388 | // Cancel before callback. |
| 3389 | r.Cancel(); |
| 3390 | // Ensure that network delegate is notified. |
| 3391 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 3392 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 3393 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 3394 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3395 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3396 | } |
| 3397 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3398 | } |
| 3399 | |
| 3400 | // Tests that we can handle when a network request was canceled while we were |
| 3401 | // waiting for the network delegate. |
| 3402 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 3403 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3404 | ASSERT_TRUE(test_server_.Start()); |
| 3405 | |
| 3406 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3407 | BlockingNetworkDelegate network_delegate( |
| 3408 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3409 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3410 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3411 | TestURLRequestContext context(true); |
| 3412 | context.set_network_delegate(&network_delegate); |
| 3413 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3414 | |
| 3415 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3416 | URLRequest r( |
| 3417 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3418 | |
| 3419 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3420 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3421 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 3422 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3423 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3424 | // Cancel before callback. |
| 3425 | r.Cancel(); |
| 3426 | // Ensure that network delegate is notified. |
| 3427 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 3428 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 3429 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 3430 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3431 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3432 | } |
| 3433 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3434 | } |
| 3435 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3436 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 3437 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 3438 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3439 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3440 | ASSERT_TRUE(test_server_.Start()); |
| 3441 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3442 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3443 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3444 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3445 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3446 | TestDelegate d; |
| 3447 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3448 | URLRequest r( |
| 3449 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, &context); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3450 | |
| 3451 | r.Start(); |
| 3452 | EXPECT_TRUE(r.is_pending()); |
| 3453 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3454 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3455 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3456 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 3457 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3458 | } |
| 3459 | } |
| 3460 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3461 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3462 | ASSERT_TRUE(test_server_.Start()); |
| 3463 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3464 | TestDelegate d; |
| 3465 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3466 | URLRequest r(test_server_.GetURL(std::string()), |
| 3467 | DEFAULT_PRIORITY, |
| 3468 | &d, |
| 3469 | &default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3470 | |
| 3471 | r.Start(); |
| 3472 | EXPECT_TRUE(r.is_pending()); |
| 3473 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3474 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3475 | |
| 3476 | EXPECT_EQ(1, d.response_started_count()); |
| 3477 | EXPECT_FALSE(d.received_data_before_response()); |
| 3478 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3479 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 3480 | r.GetSocketAddress().host()); |
| 3481 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 3482 | r.GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 3483 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 3484 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3485 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3486 | } |
| 3487 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3488 | // This test has the server send a large number of cookies to the client. |
| 3489 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 3490 | // search is used to estimate that maximum number of cookies that are accepted |
| 3491 | // by the browser. Beyond the maximum number, the request will fail with |
| 3492 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 3493 | #if defined(OS_WIN) |
| 3494 | // http://crbug.com/177916 |
| 3495 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 3496 | #else |
| 3497 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 3498 | #endif // defined(OS_WIN) |
| 3499 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3500 | ASSERT_TRUE(test_server_.Start()); |
| 3501 | |
| 3502 | int lower_bound = 0; |
| 3503 | int upper_bound = 1; |
| 3504 | |
| 3505 | // Double the number of cookies until the response header limits are |
| 3506 | // exceeded. |
| 3507 | while (DoManyCookiesRequest(upper_bound)) { |
| 3508 | lower_bound = upper_bound; |
| 3509 | upper_bound *= 2; |
| 3510 | ASSERT_LT(upper_bound, 1000000); |
| 3511 | } |
| 3512 | |
| 3513 | int tolerance = upper_bound * 0.005; |
| 3514 | if (tolerance < 2) |
| 3515 | tolerance = 2; |
| 3516 | |
| 3517 | // Perform a binary search to find the highest possible number of cookies, |
| 3518 | // within the desired tolerance. |
| 3519 | while (upper_bound - lower_bound >= tolerance) { |
| 3520 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 3521 | |
| 3522 | if (DoManyCookiesRequest(num_cookies)) |
| 3523 | lower_bound = num_cookies; |
| 3524 | else |
| 3525 | upper_bound = num_cookies; |
| 3526 | } |
| 3527 | // Success: the test did not crash. |
| 3528 | } |
| 3529 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3530 | TEST_F(URLRequestTestHTTP, GetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3531 | ASSERT_TRUE(test_server_.Start()); |
| 3532 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3533 | TestDelegate d; |
| 3534 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3535 | URLRequest r(test_server_.GetURL(std::string()), |
| 3536 | DEFAULT_PRIORITY, |
| 3537 | &d, |
| 3538 | &default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3539 | |
| 3540 | r.Start(); |
| 3541 | EXPECT_TRUE(r.is_pending()); |
| 3542 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3543 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3544 | |
| 3545 | EXPECT_EQ(1, d.response_started_count()); |
| 3546 | EXPECT_FALSE(d.received_data_before_response()); |
| 3547 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3548 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 3549 | r.GetSocketAddress().host()); |
| 3550 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 3551 | r.GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3552 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 3553 | } |
| 3554 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3555 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
| 3556 | ASSERT_TRUE(test_server_.Start()); |
| 3557 | |
| 3558 | TestDelegate d; |
| 3559 | { |
| 3560 | GURL test_url(test_server_.GetURL(std::string())); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3561 | URLRequest r(test_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3562 | |
| 3563 | HttpRequestHeaders headers; |
| 3564 | EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); |
| 3565 | |
| 3566 | r.Start(); |
| 3567 | EXPECT_TRUE(r.is_pending()); |
| 3568 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3569 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3570 | |
| 3571 | EXPECT_EQ(1, d.response_started_count()); |
| 3572 | EXPECT_FALSE(d.received_data_before_response()); |
| 3573 | EXPECT_NE(0, d.bytes_received()); |
| 3574 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 3575 | r.GetSocketAddress().host()); |
| 3576 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 3577 | r.GetSocketAddress().port()); |
| 3578 | |
| 3579 | EXPECT_TRUE(d.have_full_request_headers()); |
| 3580 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 3581 | } |
| 3582 | } |
| 3583 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3584 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
| 3585 | ASSERT_TRUE(test_server_.Start()); |
| 3586 | |
| 3587 | TestDelegate d; |
| 3588 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3589 | URLRequest r(test_server_.GetURL(std::string()), |
| 3590 | DEFAULT_PRIORITY, |
| 3591 | &d, |
| 3592 | &default_context_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3593 | |
| 3594 | r.Start(); |
| 3595 | EXPECT_TRUE(r.is_pending()); |
| 3596 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3597 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3598 | |
| 3599 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 3600 | r.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3601 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 3602 | |
| 3603 | EXPECT_EQ(1, d.response_started_count()); |
| 3604 | EXPECT_FALSE(d.received_data_before_response()); |
| 3605 | EXPECT_NE(0, d.bytes_received()); |
| 3606 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 3607 | r.GetSocketAddress().host()); |
| 3608 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 3609 | r.GetSocketAddress().port()); |
| 3610 | } |
| 3611 | } |
| 3612 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3613 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
| 3614 | ASSERT_TRUE(test_server_.Start()); |
| 3615 | |
| 3616 | // Parameter that specifies the Content-Length field in the response: |
| 3617 | // C - Compressed length. |
| 3618 | // U - Uncompressed length. |
| 3619 | // L - Large length (larger than both C & U). |
| 3620 | // M - Medium length (between C & U). |
| 3621 | // S - Small length (smaller than both C & U). |
| 3622 | const char test_parameters[] = "CULMS"; |
| 3623 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 3624 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 3625 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3626 | // S has too little data, but we seem to accept it. |
| 3627 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 3628 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3629 | |
| 3630 | for (int i = 0; i < num_tests ; i++) { |
| 3631 | TestDelegate d; |
| 3632 | { |
| 3633 | std::string test_file = |
| 3634 | base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c", |
| 3635 | test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3636 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3637 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3638 | TestURLRequestContext context(true); |
| 3639 | context.set_network_delegate(&network_delegate); |
| 3640 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3641 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3642 | URLRequest r( |
| 3643 | test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, &context); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3644 | r.Start(); |
| 3645 | EXPECT_TRUE(r.is_pending()); |
| 3646 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3647 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3648 | |
| 3649 | EXPECT_EQ(1, d.response_started_count()); |
| 3650 | EXPECT_FALSE(d.received_data_before_response()); |
| 3651 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
| 3652 | << " status = " << r.status().status() |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 3653 | << " error = " << r.status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3654 | if (test_expect_success[i]) { |
| 3655 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()) |
| 3656 | << " Parameter = \"" << test_file << "\""; |
| 3657 | } else { |
| 3658 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | 5543cbb | 2012-04-20 16:35:23 | [diff] [blame] | 3659 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3660 | << " Parameter = \"" << test_file << "\""; |
| 3661 | } |
| 3662 | } |
| 3663 | } |
| 3664 | } |
| 3665 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3666 | TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3667 | ASSERT_TRUE(test_server_.Start()); |
| 3668 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 3669 | SpawnedTestServer https_test_server( |
| 3670 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3671 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3672 | ASSERT_TRUE(https_test_server.Start()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3673 | |
| 3674 | // An https server is sent a request with an https referer, |
| 3675 | // and responds with a redirect to an http url. The http |
| 3676 | // server should not be sent the referer. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3677 | GURL http_destination = test_server_.GetURL(std::string()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3678 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3679 | URLRequest req( |
| 3680 | https_test_server.GetURL("server-redirect?" + http_destination.spec()), |
| 3681 | DEFAULT_PRIORITY, |
| 3682 | &d, |
| 3683 | &default_context_); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 3684 | req.SetReferrer("https://www.referrer.com/"); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3685 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3686 | base::RunLoop().Run(); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3687 | |
| 3688 | EXPECT_EQ(1, d.response_started_count()); |
| 3689 | EXPECT_EQ(1, d.received_redirect_count()); |
| 3690 | EXPECT_EQ(http_destination, req.url()); |
| 3691 | EXPECT_EQ(std::string(), req.referrer()); |
| 3692 | } |
| 3693 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3694 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
| 3695 | ASSERT_TRUE(test_server_.Start()); |
| 3696 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3697 | GURL destination_url = test_server_.GetURL(std::string()); |
| 3698 | GURL original_url = |
| 3699 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3700 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3701 | URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3702 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3703 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3704 | |
| 3705 | EXPECT_EQ(1, d.response_started_count()); |
| 3706 | EXPECT_EQ(1, d.received_redirect_count()); |
| 3707 | EXPECT_EQ(destination_url, req.url()); |
| 3708 | EXPECT_EQ(original_url, req.original_url()); |
| 3709 | ASSERT_EQ(2U, req.url_chain().size()); |
| 3710 | EXPECT_EQ(original_url, req.url_chain()[0]); |
| 3711 | EXPECT_EQ(destination_url, req.url_chain()[1]); |
| 3712 | |
| 3713 | LoadTimingInfo load_timing_info_before_redirect; |
| 3714 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 3715 | &load_timing_info_before_redirect)); |
| 3716 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 3717 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 3718 | |
| 3719 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 3720 | req.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3721 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 3722 | |
| 3723 | // Check that a new socket was used on redirect, since the server does not |
| 3724 | // supposed keep-alive sockets, and that the times before the redirect are |
| 3725 | // before the ones recorded for the second request. |
| 3726 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 3727 | load_timing_info.socket_log_id); |
| 3728 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 3729 | load_timing_info.connect_timing.connect_start); |
| 3730 | } |
| 3731 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3732 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
| 3733 | ASSERT_TRUE(test_server_.Start()); |
| 3734 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3735 | GURL destination_url = test_server_.GetURL(std::string()); |
| 3736 | GURL middle_redirect_url = |
| 3737 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3738 | GURL original_url = test_server_.GetURL( |
| 3739 | "server-redirect?" + middle_redirect_url.spec()); |
| 3740 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3741 | URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3742 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3743 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3744 | |
| 3745 | EXPECT_EQ(1, d.response_started_count()); |
| 3746 | EXPECT_EQ(2, d.received_redirect_count()); |
| 3747 | EXPECT_EQ(destination_url, req.url()); |
| 3748 | EXPECT_EQ(original_url, req.original_url()); |
| 3749 | ASSERT_EQ(3U, req.url_chain().size()); |
| 3750 | EXPECT_EQ(original_url, req.url_chain()[0]); |
| 3751 | EXPECT_EQ(middle_redirect_url, req.url_chain()[1]); |
| 3752 | EXPECT_EQ(destination_url, req.url_chain()[2]); |
| 3753 | } |
| 3754 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3755 | // First and second pieces of information logged by delegates to URLRequests. |
| 3756 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 3757 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 3758 | |
| 3759 | // Logs delegate information to a URLRequest. The first string is logged |
| 3760 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 3761 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 3762 | // another asynchronous call is used to clear the delegate information |
| 3763 | // before calling a callback. The object then deletes itself. |
| 3764 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 3765 | public: |
| 3766 | typedef base::Callback<void()> Callback; |
| 3767 | |
| 3768 | // Each time delegate information is added to the URLRequest, the resulting |
| 3769 | // load state is checked. The expected load state after each request is |
| 3770 | // passed in as an argument. |
| 3771 | static void Run(URLRequest* url_request, |
| 3772 | LoadState expected_first_load_state, |
| 3773 | LoadState expected_second_load_state, |
| 3774 | LoadState expected_third_load_state, |
| 3775 | const Callback& callback) { |
| 3776 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 3777 | url_request, |
| 3778 | expected_first_load_state, |
| 3779 | expected_second_load_state, |
| 3780 | expected_third_load_state, |
| 3781 | callback); |
| 3782 | logger->Start(); |
| 3783 | } |
| 3784 | |
| 3785 | // Checks that the log entries, starting with log_position, contain the |
| 3786 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 3787 | // recorded. Returns the index of entry after the expected number of |
| 3788 | // events this logged, or entries.size() if there aren't enough entries. |
| 3789 | static size_t CheckDelegateInfo( |
| 3790 | const CapturingNetLog::CapturedEntryList& entries, size_t log_position) { |
| 3791 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 3792 | if (log_position + 3 >= entries.size()) { |
| 3793 | ADD_FAILURE() << "Not enough log entries"; |
| 3794 | return entries.size(); |
| 3795 | } |
| 3796 | std::string delegate_info; |
| 3797 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3798 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 3799 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 3800 | &delegate_info)); |
| 3801 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 3802 | |
| 3803 | ++log_position; |
| 3804 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3805 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 3806 | |
| 3807 | ++log_position; |
| 3808 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3809 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 3810 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 3811 | &delegate_info)); |
| 3812 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 3813 | |
| 3814 | ++log_position; |
| 3815 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3816 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 3817 | |
| 3818 | return log_position + 1; |
| 3819 | } |
| 3820 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 3821 | // Find delegate request begin and end messages for OnBeforeNetworkStart. |
| 3822 | // Returns the position of the end message. |
| 3823 | static size_t ExpectBeforeNetworkEvents( |
| 3824 | const CapturingNetLog::CapturedEntryList& entries, |
| 3825 | size_t log_position) { |
| 3826 | log_position = |
| 3827 | ExpectLogContainsSomewhereAfter(entries, |
| 3828 | log_position, |
| 3829 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 3830 | NetLog::PHASE_BEGIN); |
| 3831 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 3832 | entries[log_position + 1].type); |
| 3833 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase); |
| 3834 | return log_position + 1; |
| 3835 | } |
| 3836 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3837 | private: |
| 3838 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 3839 | |
| 3840 | AsyncDelegateLogger(URLRequest* url_request, |
| 3841 | LoadState expected_first_load_state, |
| 3842 | LoadState expected_second_load_state, |
| 3843 | LoadState expected_third_load_state, |
| 3844 | const Callback& callback) |
| 3845 | : url_request_(url_request), |
| 3846 | expected_first_load_state_(expected_first_load_state), |
| 3847 | expected_second_load_state_(expected_second_load_state), |
| 3848 | expected_third_load_state_(expected_third_load_state), |
| 3849 | callback_(callback) { |
| 3850 | } |
| 3851 | |
| 3852 | ~AsyncDelegateLogger() {} |
| 3853 | |
| 3854 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3855 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3856 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3857 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 3858 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
| 3859 | base::MessageLoop::current()->PostTask( |
| 3860 | FROM_HERE, |
| 3861 | base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
| 3862 | } |
| 3863 | |
| 3864 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3865 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3866 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3867 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 3868 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 3869 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 3870 | } else { |
| 3871 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 3872 | } |
| 3873 | base::MessageLoop::current()->PostTask( |
| 3874 | FROM_HERE, |
| 3875 | base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
| 3876 | } |
| 3877 | |
| 3878 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3879 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3880 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3881 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 3882 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 3883 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3884 | callback_.Run(); |
| 3885 | } |
| 3886 | |
| 3887 | URLRequest* url_request_; |
| 3888 | const int expected_first_load_state_; |
| 3889 | const int expected_second_load_state_; |
| 3890 | const int expected_third_load_state_; |
| 3891 | const Callback callback_; |
| 3892 | |
| 3893 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 3894 | }; |
| 3895 | |
| 3896 | // NetworkDelegate that logs delegate information before a request is started, |
| 3897 | // before headers are sent, when headers are read, and when auth information |
| 3898 | // is requested. Uses AsyncDelegateLogger. |
| 3899 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 3900 | public: |
| 3901 | AsyncLoggingNetworkDelegate() {} |
| 3902 | virtual ~AsyncLoggingNetworkDelegate() {} |
| 3903 | |
| 3904 | // NetworkDelegate implementation. |
| 3905 | virtual int OnBeforeURLRequest(URLRequest* request, |
| 3906 | const CompletionCallback& callback, |
| 3907 | GURL* new_url) OVERRIDE { |
| 3908 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 3909 | return RunCallbackAsynchronously(request, callback); |
| 3910 | } |
| 3911 | |
| 3912 | virtual int OnBeforeSendHeaders(URLRequest* request, |
| 3913 | const CompletionCallback& callback, |
| 3914 | HttpRequestHeaders* headers) OVERRIDE { |
| 3915 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 3916 | return RunCallbackAsynchronously(request, callback); |
| 3917 | } |
| 3918 | |
| 3919 | virtual int OnHeadersReceived( |
| 3920 | URLRequest* request, |
| 3921 | const CompletionCallback& callback, |
| 3922 | const HttpResponseHeaders* original_response_headers, |
| 3923 | scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE { |
| 3924 | TestNetworkDelegate::OnHeadersReceived(request, callback, |
| 3925 | original_response_headers, |
| 3926 | override_response_headers); |
| 3927 | return RunCallbackAsynchronously(request, callback); |
| 3928 | } |
| 3929 | |
| 3930 | virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 3931 | URLRequest* request, |
| 3932 | const AuthChallengeInfo& auth_info, |
| 3933 | const AuthCallback& callback, |
| 3934 | AuthCredentials* credentials) OVERRIDE { |
| 3935 | AsyncDelegateLogger::Run( |
| 3936 | request, |
| 3937 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 3938 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 3939 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 3940 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 3941 | callback, credentials)); |
| 3942 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 3943 | } |
| 3944 | |
| 3945 | private: |
| 3946 | static int RunCallbackAsynchronously( |
| 3947 | URLRequest* request, |
| 3948 | const CompletionCallback& callback) { |
| 3949 | AsyncDelegateLogger::Run( |
| 3950 | request, |
| 3951 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 3952 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 3953 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 3954 | base::Bind(callback, OK)); |
| 3955 | return ERR_IO_PENDING; |
| 3956 | } |
| 3957 | |
| 3958 | static void SetAuthAndResume(const AuthCallback& callback, |
| 3959 | AuthCredentials* credentials) { |
| 3960 | *credentials = AuthCredentials(kUser, kSecret); |
| 3961 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3962 | } |
| 3963 | |
| 3964 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 3965 | }; |
| 3966 | |
| 3967 | // URLRequest::Delegate that logs delegate information when the headers |
| 3968 | // are received, when each read completes, and during redirects. Uses |
| 3969 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 3970 | // |
| 3971 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 3972 | // advancing to the next step in most cases, as well as cancellation. |
| 3973 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 3974 | public: |
| 3975 | enum CancelStage { |
| 3976 | NO_CANCEL = 0, |
| 3977 | CANCEL_ON_RECEIVED_REDIRECT, |
| 3978 | CANCEL_ON_RESPONSE_STARTED, |
| 3979 | CANCEL_ON_READ_COMPLETED |
| 3980 | }; |
| 3981 | |
| 3982 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 3983 | : cancel_stage_(cancel_stage) { |
| 3984 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 3985 | set_cancel_in_received_redirect(true); |
| 3986 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 3987 | set_cancel_in_response_started(true); |
| 3988 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 3989 | set_cancel_in_received_data(true); |
| 3990 | } |
| 3991 | virtual ~AsyncLoggingUrlRequestDelegate() {} |
| 3992 | |
| 3993 | // URLRequest::Delegate implementation: |
| 3994 | void virtual OnReceivedRedirect(URLRequest* request, |
| 3995 | const GURL& new_url, |
| 3996 | bool* defer_redirect) OVERRIDE { |
| 3997 | *defer_redirect = true; |
| 3998 | AsyncDelegateLogger::Run( |
| 3999 | request, |
| 4000 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4001 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4002 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4003 | base::Bind( |
| 4004 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
| 4005 | base::Unretained(this), request, new_url)); |
| 4006 | } |
| 4007 | |
| 4008 | virtual void OnResponseStarted(URLRequest* request) OVERRIDE { |
| 4009 | AsyncDelegateLogger::Run( |
| 4010 | request, |
| 4011 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4012 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4013 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4014 | base::Bind( |
| 4015 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 4016 | base::Unretained(this), request)); |
| 4017 | } |
| 4018 | |
| 4019 | virtual void OnReadCompleted(URLRequest* request, |
| 4020 | int bytes_read) OVERRIDE { |
| 4021 | AsyncDelegateLogger::Run( |
| 4022 | request, |
| 4023 | LOAD_STATE_IDLE, |
| 4024 | LOAD_STATE_IDLE, |
| 4025 | LOAD_STATE_IDLE, |
| 4026 | base::Bind( |
| 4027 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 4028 | base::Unretained(this), request, bytes_read)); |
| 4029 | } |
| 4030 | |
| 4031 | private: |
| 4032 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
| 4033 | const GURL& new_url) { |
| 4034 | bool defer_redirect = false; |
| 4035 | TestDelegate::OnReceivedRedirect(request, new_url, &defer_redirect); |
| 4036 | // FollowDeferredRedirect should not be called after cancellation. |
| 4037 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 4038 | return; |
| 4039 | if (!defer_redirect) |
| 4040 | request->FollowDeferredRedirect(); |
| 4041 | } |
| 4042 | |
| 4043 | void OnResponseStartedLoggingComplete(URLRequest* request) { |
| 4044 | // The parent class continues the request. |
| 4045 | TestDelegate::OnResponseStarted(request); |
| 4046 | } |
| 4047 | |
| 4048 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 4049 | // The parent class continues the request. |
| 4050 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 4051 | } |
| 4052 | |
| 4053 | const CancelStage cancel_stage_; |
| 4054 | |
| 4055 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 4056 | }; |
| 4057 | |
| 4058 | // Tests handling of delegate info before a request starts. |
| 4059 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
| 4060 | ASSERT_TRUE(test_server_.Start()); |
| 4061 | |
| 4062 | TestDelegate request_delegate; |
| 4063 | TestURLRequestContext context(true); |
| 4064 | context.set_network_delegate(NULL); |
| 4065 | context.set_net_log(&net_log_); |
| 4066 | context.Init(); |
| 4067 | |
| 4068 | { |
| 4069 | URLRequest r(test_server_.GetURL("empty.html"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4070 | DEFAULT_PRIORITY, |
| 4071 | &request_delegate, |
| 4072 | &context); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4073 | LoadStateWithParam load_state = r.GetLoadState(); |
| 4074 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4075 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4076 | |
| 4077 | AsyncDelegateLogger::Run( |
| 4078 | &r, |
| 4079 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4080 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4081 | LOAD_STATE_IDLE, |
| 4082 | base::Bind(&URLRequest::Start, base::Unretained(&r))); |
| 4083 | |
| 4084 | base::RunLoop().Run(); |
| 4085 | |
| 4086 | EXPECT_EQ(200, r.GetResponseCode()); |
| 4087 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 4088 | } |
| 4089 | |
| 4090 | CapturingNetLog::CapturedEntryList entries; |
| 4091 | net_log_.GetEntries(&entries); |
| 4092 | size_t log_position = ExpectLogContainsSomewhereAfter( |
| 4093 | entries, |
| 4094 | 0, |
| 4095 | NetLog::TYPE_DELEGATE_INFO, |
| 4096 | NetLog::PHASE_BEGIN); |
| 4097 | |
| 4098 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 4099 | |
| 4100 | // Nothing else should add any delegate info to the request. |
| 4101 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4102 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4103 | } |
| 4104 | |
| 4105 | // Tests handling of delegate info from a network delegate. |
| 4106 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
| 4107 | ASSERT_TRUE(test_server_.Start()); |
| 4108 | |
| 4109 | TestDelegate request_delegate; |
| 4110 | AsyncLoggingNetworkDelegate network_delegate; |
| 4111 | TestURLRequestContext context(true); |
| 4112 | context.set_network_delegate(&network_delegate); |
| 4113 | context.set_net_log(&net_log_); |
| 4114 | context.Init(); |
| 4115 | |
| 4116 | { |
| 4117 | URLRequest r(test_server_.GetURL("simple.html"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4118 | DEFAULT_PRIORITY, |
| 4119 | &request_delegate, |
| 4120 | &context); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4121 | LoadStateWithParam load_state = r.GetLoadState(); |
| 4122 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4123 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4124 | |
| 4125 | r.Start(); |
| 4126 | base::RunLoop().Run(); |
| 4127 | |
| 4128 | EXPECT_EQ(200, r.GetResponseCode()); |
| 4129 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 4130 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4131 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4132 | } |
| 4133 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4134 | |
| 4135 | size_t log_position = 0; |
| 4136 | CapturingNetLog::CapturedEntryList entries; |
| 4137 | net_log_.GetEntries(&entries); |
| 4138 | for (size_t i = 0; i < 3; ++i) { |
| 4139 | log_position = ExpectLogContainsSomewhereAfter( |
| 4140 | entries, |
| 4141 | log_position + 1, |
| 4142 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4143 | NetLog::PHASE_BEGIN); |
| 4144 | |
| 4145 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4146 | log_position + 1); |
| 4147 | |
| 4148 | ASSERT_LT(log_position, entries.size()); |
| 4149 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4150 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4151 | |
| 4152 | if (i == 1) { |
| 4153 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4154 | entries, log_position + 1); |
| 4155 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4159 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4160 | } |
| 4161 | |
| 4162 | // Tests handling of delegate info from a network delegate in the case of an |
| 4163 | // HTTP redirect. |
| 4164 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
| 4165 | ASSERT_TRUE(test_server_.Start()); |
| 4166 | |
| 4167 | TestDelegate request_delegate; |
| 4168 | AsyncLoggingNetworkDelegate network_delegate; |
| 4169 | TestURLRequestContext context(true); |
| 4170 | context.set_network_delegate(&network_delegate); |
| 4171 | context.set_net_log(&net_log_); |
| 4172 | context.Init(); |
| 4173 | |
| 4174 | { |
| 4175 | URLRequest r(test_server_.GetURL("server-redirect?simple.html"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4176 | DEFAULT_PRIORITY, |
| 4177 | &request_delegate, |
| 4178 | &context); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4179 | LoadStateWithParam load_state = r.GetLoadState(); |
| 4180 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4181 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4182 | |
| 4183 | r.Start(); |
| 4184 | base::RunLoop().Run(); |
| 4185 | |
| 4186 | EXPECT_EQ(200, r.GetResponseCode()); |
| 4187 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 4188 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4189 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4190 | } |
| 4191 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4192 | |
| 4193 | size_t log_position = 0; |
| 4194 | CapturingNetLog::CapturedEntryList entries; |
| 4195 | net_log_.GetEntries(&entries); |
| 4196 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
| 4197 | // OnBeforeSendHeaders, and OnHeadersReceived. |
| 4198 | for (size_t i = 0; i < 3; ++i) { |
| 4199 | log_position = ExpectLogContainsSomewhereAfter( |
| 4200 | entries, |
| 4201 | log_position + 1, |
| 4202 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4203 | NetLog::PHASE_BEGIN); |
| 4204 | |
| 4205 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4206 | log_position + 1); |
| 4207 | |
| 4208 | ASSERT_LT(log_position, entries.size()); |
| 4209 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4210 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4211 | |
| 4212 | if (i == 1) { |
| 4213 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4214 | entries, log_position + 1); |
| 4215 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | // The URLRequest::Delegate then gets informed about the redirect. |
| 4219 | log_position = ExpectLogContainsSomewhereAfter( |
| 4220 | entries, |
| 4221 | log_position + 1, |
| 4222 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4223 | NetLog::PHASE_BEGIN); |
| 4224 | |
| 4225 | // The NetworkDelegate logged information in the same three events as before. |
| 4226 | for (size_t i = 0; i < 3; ++i) { |
| 4227 | log_position = ExpectLogContainsSomewhereAfter( |
| 4228 | entries, |
| 4229 | log_position + 1, |
| 4230 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4231 | NetLog::PHASE_BEGIN); |
| 4232 | |
| 4233 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4234 | log_position + 1); |
| 4235 | |
| 4236 | ASSERT_LT(log_position, entries.size()); |
| 4237 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4238 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4239 | } |
| 4240 | |
| 4241 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4242 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4243 | } |
| 4244 | |
| 4245 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 4246 | // AUTH. |
| 4247 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
| 4248 | ASSERT_TRUE(test_server_.Start()); |
| 4249 | |
| 4250 | TestDelegate request_delegate; |
| 4251 | AsyncLoggingNetworkDelegate network_delegate; |
| 4252 | TestURLRequestContext context(true); |
| 4253 | context.set_network_delegate(&network_delegate); |
| 4254 | context.set_net_log(&net_log_); |
| 4255 | context.Init(); |
| 4256 | |
| 4257 | { |
| 4258 | URLRequest r(test_server_.GetURL("auth-basic"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4259 | DEFAULT_PRIORITY, |
| 4260 | &request_delegate, |
| 4261 | &context); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4262 | LoadStateWithParam load_state = r.GetLoadState(); |
| 4263 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4264 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4265 | |
| 4266 | r.Start(); |
| 4267 | base::RunLoop().Run(); |
| 4268 | |
| 4269 | EXPECT_EQ(200, r.GetResponseCode()); |
| 4270 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 4271 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4272 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4273 | } |
| 4274 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4275 | |
| 4276 | size_t log_position = 0; |
| 4277 | CapturingNetLog::CapturedEntryList entries; |
| 4278 | net_log_.GetEntries(&entries); |
| 4279 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
| 4280 | // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in |
| 4281 | // OnBeforeURLRequest and OnBeforeSendHeaders. |
| 4282 | for (size_t i = 0; i < 6; ++i) { |
| 4283 | log_position = ExpectLogContainsSomewhereAfter( |
| 4284 | entries, |
| 4285 | log_position + 1, |
| 4286 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4287 | NetLog::PHASE_BEGIN); |
| 4288 | |
| 4289 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4290 | log_position + 1); |
| 4291 | |
| 4292 | ASSERT_LT(log_position, entries.size()); |
| 4293 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4294 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4295 | |
| 4296 | if (i == 1) { |
| 4297 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4298 | entries, log_position + 1); |
| 4299 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4300 | } |
| 4301 | |
| 4302 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4303 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4304 | } |
| 4305 | |
| 4306 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 4307 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
| 4308 | ASSERT_TRUE(test_server_.Start()); |
| 4309 | |
| 4310 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4311 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4312 | TestURLRequestContext context(true); |
| 4313 | context.set_network_delegate(NULL); |
| 4314 | context.set_net_log(&net_log_); |
| 4315 | context.Init(); |
| 4316 | |
| 4317 | { |
| 4318 | // A chunked response with delays between chunks is used to make sure that |
| 4319 | // attempts by the URLRequest delegate to log information while reading the |
| 4320 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4321 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4322 | // that it occurs. |
| 4323 | URLRequest r(test_server_.GetURL("chunked?waitBetweenChunks=20"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4324 | DEFAULT_PRIORITY, |
| 4325 | &request_delegate, |
| 4326 | &context); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4327 | LoadStateWithParam load_state = r.GetLoadState(); |
| 4328 | r.Start(); |
| 4329 | base::RunLoop().Run(); |
| 4330 | |
| 4331 | EXPECT_EQ(200, r.GetResponseCode()); |
| 4332 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 4333 | } |
| 4334 | |
| 4335 | CapturingNetLog::CapturedEntryList entries; |
| 4336 | net_log_.GetEntries(&entries); |
| 4337 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4338 | size_t log_position = 0; |
| 4339 | |
| 4340 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4341 | entries, log_position); |
| 4342 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4343 | // The delegate info should only have been logged on header complete. Other |
| 4344 | // times it should silently be ignored. |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4345 | log_position = |
| 4346 | ExpectLogContainsSomewhereAfter(entries, |
| 4347 | log_position + 1, |
| 4348 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4349 | NetLog::PHASE_BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4350 | |
| 4351 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4352 | log_position + 1); |
| 4353 | |
| 4354 | ASSERT_LT(log_position, entries.size()); |
| 4355 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4356 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4357 | |
| 4358 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4359 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4360 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4361 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4362 | } |
| 4363 | |
| 4364 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4365 | // an HTTP redirect. |
| 4366 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
| 4367 | ASSERT_TRUE(test_server_.Start()); |
| 4368 | |
| 4369 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4370 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4371 | TestURLRequestContext context(true); |
| 4372 | context.set_network_delegate(NULL); |
| 4373 | context.set_net_log(&net_log_); |
| 4374 | context.Init(); |
| 4375 | |
| 4376 | { |
| 4377 | URLRequest r(test_server_.GetURL("server-redirect?simple.html"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4378 | DEFAULT_PRIORITY, |
| 4379 | &request_delegate, |
| 4380 | &context); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4381 | LoadStateWithParam load_state = r.GetLoadState(); |
| 4382 | r.Start(); |
| 4383 | base::RunLoop().Run(); |
| 4384 | |
| 4385 | EXPECT_EQ(200, r.GetResponseCode()); |
| 4386 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 4387 | } |
| 4388 | |
| 4389 | CapturingNetLog::CapturedEntryList entries; |
| 4390 | net_log_.GetEntries(&entries); |
| 4391 | |
| 4392 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 4393 | // OnResponseStarted. |
| 4394 | size_t log_position = 0; |
| 4395 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4396 | if (i == 0) { |
| 4397 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4398 | entries, log_position) + 1; |
| 4399 | } |
| 4400 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4401 | log_position = ExpectLogContainsSomewhereAfter( |
| 4402 | entries, |
| 4403 | log_position, |
| 4404 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4405 | NetLog::PHASE_BEGIN); |
| 4406 | |
| 4407 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4408 | log_position + 1); |
| 4409 | |
| 4410 | ASSERT_LT(log_position, entries.size()); |
| 4411 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4412 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4413 | } |
| 4414 | |
| 4415 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4416 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4417 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4418 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4419 | } |
| 4420 | |
| 4421 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4422 | // an HTTP redirect, with cancellation at various points. |
| 4423 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
| 4424 | ASSERT_TRUE(test_server_.Start()); |
| 4425 | |
| 4426 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 4427 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 4428 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 4429 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 4430 | }; |
| 4431 | |
| 4432 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 4433 | ++test_case) { |
| 4434 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
| 4435 | TestURLRequestContext context(true); |
| 4436 | CapturingNetLog net_log; |
| 4437 | context.set_network_delegate(NULL); |
| 4438 | context.set_net_log(&net_log); |
| 4439 | context.Init(); |
| 4440 | |
| 4441 | { |
| 4442 | URLRequest r(test_server_.GetURL("server-redirect?simple.html"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4443 | DEFAULT_PRIORITY, |
| 4444 | &request_delegate, |
| 4445 | &context); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4446 | LoadStateWithParam load_state = r.GetLoadState(); |
| 4447 | r.Start(); |
| 4448 | base::RunLoop().Run(); |
| 4449 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 4450 | } |
| 4451 | |
| 4452 | CapturingNetLog::CapturedEntryList entries; |
| 4453 | net_log.GetEntries(&entries); |
| 4454 | |
| 4455 | // Delegate info is always logged in both OnReceivedRedirect and |
| 4456 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 4457 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 4458 | // still currently supported in that call. |
| 4459 | size_t log_position = 0; |
| 4460 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4461 | if (i == 0) { |
| 4462 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4463 | entries, log_position) + 1; |
| 4464 | } |
| 4465 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4466 | log_position = ExpectLogContainsSomewhereAfter( |
| 4467 | entries, |
| 4468 | log_position, |
| 4469 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4470 | NetLog::PHASE_BEGIN); |
| 4471 | |
| 4472 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4473 | log_position + 1); |
| 4474 | |
| 4475 | ASSERT_LT(log_position, entries.size()); |
| 4476 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4477 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4478 | } |
| 4479 | |
| 4480 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4481 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4482 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4483 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4484 | } |
| 4485 | } |
| 4486 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4487 | namespace { |
| 4488 | |
| 4489 | const char kExtraHeader[] = "Allow-Snafu"; |
| 4490 | const char kExtraValue[] = "fubar"; |
| 4491 | |
| 4492 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 4493 | virtual void OnReceivedRedirect(net::URLRequest* request, |
| 4494 | const GURL& new_url, |
| 4495 | bool* defer_redirect) OVERRIDE { |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4496 | TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect); |
| 4497 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 4498 | } |
| 4499 | }; |
| 4500 | |
| 4501 | } // namespace |
| 4502 | |
| 4503 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
| 4504 | ASSERT_TRUE(test_server_.Start()); |
| 4505 | |
| 4506 | GURL destination_url = test_server_.GetURL( |
| 4507 | "echoheader?" + std::string(kExtraHeader)); |
| 4508 | GURL original_url = test_server_.GetURL( |
| 4509 | "server-redirect?" + destination_url.spec()); |
| 4510 | RedirectWithAdditionalHeadersDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4511 | URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4512 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4513 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4514 | |
| 4515 | std::string value; |
| 4516 | const HttpRequestHeaders& headers = req.extra_request_headers(); |
| 4517 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 4518 | EXPECT_EQ(kExtraValue, value); |
| 4519 | EXPECT_FALSE(req.is_pending()); |
| 4520 | EXPECT_FALSE(req.is_redirecting()); |
| 4521 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 4522 | } |
| 4523 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4524 | namespace { |
| 4525 | |
| 4526 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 4527 | |
| 4528 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 4529 | virtual void OnReceivedRedirect(net::URLRequest* request, |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4530 | const GURL& new_url, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 4531 | bool* defer_redirect) OVERRIDE { |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4532 | TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect); |
| 4533 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 4534 | } |
| 4535 | }; |
| 4536 | |
| 4537 | } // namespace |
| 4538 | |
| 4539 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
| 4540 | ASSERT_TRUE(test_server_.Start()); |
| 4541 | |
| 4542 | GURL destination_url = test_server_.GetURL( |
| 4543 | "echoheader?" + std::string(kExtraHeaderToRemove)); |
| 4544 | GURL original_url = test_server_.GetURL( |
| 4545 | "server-redirect?" + destination_url.spec()); |
| 4546 | RedirectWithHeaderRemovalDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4547 | URLRequest req(original_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4548 | req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 4549 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4550 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4551 | |
| 4552 | std::string value; |
| 4553 | const HttpRequestHeaders& headers = req.extra_request_headers(); |
| 4554 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
| 4555 | EXPECT_FALSE(req.is_pending()); |
| 4556 | EXPECT_FALSE(req.is_redirecting()); |
| 4557 | EXPECT_EQ("None", d.data_received()); |
| 4558 | } |
| 4559 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4560 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 4561 | TestDelegate d; |
| 4562 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4563 | URLRequest r(GURL("http://www.google.com/"), |
| 4564 | DEFAULT_PRIORITY, |
| 4565 | &d, |
| 4566 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4567 | |
| 4568 | r.Start(); |
| 4569 | EXPECT_TRUE(r.is_pending()); |
| 4570 | |
| 4571 | r.Cancel(); |
| 4572 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4573 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4574 | |
| 4575 | // We expect to receive OnResponseStarted even though the request has been |
| 4576 | // cancelled. |
| 4577 | EXPECT_EQ(1, d.response_started_count()); |
| 4578 | EXPECT_EQ(0, d.bytes_received()); |
| 4579 | EXPECT_FALSE(d.received_data_before_response()); |
| 4580 | } |
| 4581 | } |
| 4582 | |
| 4583 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
| 4584 | ASSERT_TRUE(test_server_.Start()); |
| 4585 | |
| 4586 | TestDelegate d; |
| 4587 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4588 | URLRequest r(test_server_.GetURL(std::string()), |
| 4589 | DEFAULT_PRIORITY, |
| 4590 | &d, |
| 4591 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4592 | |
| 4593 | d.set_cancel_in_response_started(true); |
| 4594 | |
| 4595 | r.Start(); |
| 4596 | EXPECT_TRUE(r.is_pending()); |
| 4597 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4598 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4599 | |
| 4600 | EXPECT_EQ(1, d.response_started_count()); |
| 4601 | EXPECT_EQ(0, d.bytes_received()); |
| 4602 | EXPECT_FALSE(d.received_data_before_response()); |
| 4603 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 4604 | } |
| 4605 | } |
| 4606 | |
| 4607 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
| 4608 | ASSERT_TRUE(test_server_.Start()); |
| 4609 | |
| 4610 | TestDelegate d; |
| 4611 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4612 | URLRequest r(test_server_.GetURL(std::string()), |
| 4613 | DEFAULT_PRIORITY, |
| 4614 | &d, |
| 4615 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4616 | |
| 4617 | d.set_cancel_in_received_data(true); |
| 4618 | |
| 4619 | r.Start(); |
| 4620 | EXPECT_TRUE(r.is_pending()); |
| 4621 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4622 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4623 | |
| 4624 | EXPECT_EQ(1, d.response_started_count()); |
| 4625 | // There is no guarantee about how much data was received |
| 4626 | // before the cancel was issued. It could have been 0 bytes, |
| 4627 | // or it could have been all the bytes. |
| 4628 | // EXPECT_EQ(0, d.bytes_received()); |
| 4629 | EXPECT_FALSE(d.received_data_before_response()); |
| 4630 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 4631 | } |
| 4632 | } |
| 4633 | |
| 4634 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
| 4635 | ASSERT_TRUE(test_server_.Start()); |
| 4636 | |
| 4637 | TestDelegate d; |
| 4638 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4639 | URLRequest r(test_server_.GetURL(std::string()), |
| 4640 | DEFAULT_PRIORITY, |
| 4641 | &d, |
| 4642 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4643 | |
| 4644 | r.Start(); |
| 4645 | EXPECT_TRUE(r.is_pending()); |
| 4646 | |
| 4647 | // The request will be implicitly canceled when it is destroyed. The |
| 4648 | // test delegate must not post a quit message when this happens because |
| 4649 | // this test doesn't actually have a message loop. The quit message would |
| 4650 | // get put on this thread's message queue and the next test would exit |
| 4651 | // early, causing problems. |
| 4652 | d.set_quit_on_complete(false); |
| 4653 | } |
| 4654 | // expect things to just cleanup properly. |
| 4655 | |
| 4656 | // we won't actually get a received reponse here because we've never run the |
| 4657 | // message loop |
| 4658 | EXPECT_FALSE(d.received_data_before_response()); |
| 4659 | EXPECT_EQ(0, d.bytes_received()); |
| 4660 | } |
| 4661 | |
| 4662 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
| 4663 | ASSERT_TRUE(test_server_.Start()); |
| 4664 | |
| 4665 | // populate cache |
| 4666 | { |
| 4667 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4668 | URLRequest r(test_server_.GetURL("cachetime"), |
| 4669 | DEFAULT_PRIORITY, |
| 4670 | &d, |
| 4671 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4672 | r.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4673 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4674 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 4675 | } |
| 4676 | |
| 4677 | // cancel read from cache (see bug 990242) |
| 4678 | { |
| 4679 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4680 | URLRequest r(test_server_.GetURL("cachetime"), |
| 4681 | DEFAULT_PRIORITY, |
| 4682 | &d, |
| 4683 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4684 | r.Start(); |
| 4685 | r.Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4686 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4687 | |
| 4688 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 4689 | EXPECT_EQ(1, d.response_started_count()); |
| 4690 | EXPECT_EQ(0, d.bytes_received()); |
| 4691 | EXPECT_FALSE(d.received_data_before_response()); |
| 4692 | } |
| 4693 | } |
| 4694 | |
| 4695 | TEST_F(URLRequestTestHTTP, PostTest) { |
| 4696 | ASSERT_TRUE(test_server_.Start()); |
| 4697 | HTTPUploadDataOperationTest("POST"); |
| 4698 | } |
| 4699 | |
| 4700 | TEST_F(URLRequestTestHTTP, PutTest) { |
| 4701 | ASSERT_TRUE(test_server_.Start()); |
| 4702 | HTTPUploadDataOperationTest("PUT"); |
| 4703 | } |
| 4704 | |
| 4705 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
| 4706 | ASSERT_TRUE(test_server_.Start()); |
| 4707 | |
| 4708 | TestDelegate d; |
| 4709 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4710 | URLRequest r( |
| 4711 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4712 | r.set_method("POST"); |
| 4713 | |
| 4714 | r.Start(); |
| 4715 | EXPECT_TRUE(r.is_pending()); |
| 4716 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4717 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4718 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4719 | ASSERT_EQ(1, d.response_started_count()) |
| 4720 | << "request failed: " << r.status().status() |
| 4721 | << ", error: " << r.status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4722 | |
| 4723 | EXPECT_FALSE(d.received_data_before_response()); |
| 4724 | EXPECT_TRUE(d.data_received().empty()); |
| 4725 | } |
| 4726 | } |
| 4727 | |
| 4728 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
| 4729 | ASSERT_TRUE(test_server_.Start()); |
| 4730 | |
| 4731 | TestDelegate d; |
| 4732 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4733 | URLRequest r( |
| 4734 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4735 | r.set_method("POST"); |
| 4736 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4737 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4738 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 4739 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4740 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 4741 | ScopedVector<UploadElementReader> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4742 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4743 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4744 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 4745 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 4746 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 4747 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 4748 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 4749 | element_readers.push_back( |
| 4750 | new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 4751 | path, |
| 4752 | 0, |
| 4753 | kuint64max, |
| 4754 | base::Time())); |
[email protected] | 96c77a7 | 2013-09-24 09:49:20 | [diff] [blame] | 4755 | r.set_upload(make_scoped_ptr( |
| 4756 | new UploadDataStream(element_readers.Pass(), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4757 | |
| 4758 | r.Start(); |
| 4759 | EXPECT_TRUE(r.is_pending()); |
| 4760 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4761 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4762 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4763 | int64 size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 4764 | ASSERT_EQ(true, base::GetFileSize(path, &size)); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 4765 | scoped_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4766 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 4767 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4768 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4769 | ASSERT_EQ(1, d.response_started_count()) |
| 4770 | << "request failed: " << r.status().status() |
| 4771 | << ", error: " << r.status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4772 | |
| 4773 | EXPECT_FALSE(d.received_data_before_response()); |
| 4774 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4775 | EXPECT_EQ(size, d.bytes_received()); |
| 4776 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4777 | } |
| 4778 | } |
| 4779 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4780 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
| 4781 | ASSERT_TRUE(test_server_.Start()); |
| 4782 | |
| 4783 | TestDelegate d; |
| 4784 | { |
| 4785 | URLRequest r(test_server_.GetURL("echo"), DEFAULT_PRIORITY, |
| 4786 | &d, &default_context_); |
| 4787 | r.set_method("POST"); |
| 4788 | |
| 4789 | ScopedVector<UploadElementReader> element_readers; |
| 4790 | |
| 4791 | element_readers.push_back(new UploadFileElementReader( |
| 4792 | base::MessageLoopProxy::current().get(), |
| 4793 | base::FilePath(FILE_PATH_LITERAL( |
| 4794 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
| 4795 | 0, |
| 4796 | kuint64max, |
| 4797 | base::Time())); |
| 4798 | r.set_upload(make_scoped_ptr( |
| 4799 | new UploadDataStream(element_readers.Pass(), 0))); |
| 4800 | |
| 4801 | r.Start(); |
| 4802 | EXPECT_TRUE(r.is_pending()); |
| 4803 | |
| 4804 | base::RunLoop().Run(); |
| 4805 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4806 | EXPECT_TRUE(d.request_failed()); |
| 4807 | EXPECT_FALSE(d.received_data_before_response()); |
| 4808 | EXPECT_EQ(0, d.bytes_received()); |
| 4809 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
| 4810 | EXPECT_EQ(ERR_FILE_NOT_FOUND, r.status().error()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4811 | } |
| 4812 | } |
| 4813 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4814 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
| 4815 | ASSERT_TRUE(test_server_.Start()); |
| 4816 | |
| 4817 | TestDelegate d; |
| 4818 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4819 | URLRequest r( |
| 4820 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4821 | r.EnableChunkedUpload(); |
| 4822 | r.set_method("POST"); |
| 4823 | AddChunksToUpload(&r); |
| 4824 | r.Start(); |
| 4825 | EXPECT_TRUE(r.is_pending()); |
| 4826 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4827 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4828 | |
| 4829 | VerifyReceivedDataMatchesChunks(&r, &d); |
| 4830 | } |
| 4831 | } |
| 4832 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4833 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
| 4834 | ASSERT_TRUE(test_server_.Start()); |
| 4835 | |
| 4836 | TestDelegate d; |
| 4837 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4838 | URLRequest r( |
| 4839 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4840 | r.EnableChunkedUpload(); |
| 4841 | r.set_method("POST"); |
| 4842 | r.Start(); |
| 4843 | EXPECT_TRUE(r.is_pending()); |
| 4844 | AddChunksToUpload(&r); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4845 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4846 | |
| 4847 | VerifyReceivedDataMatchesChunks(&r, &d); |
| 4848 | } |
| 4849 | } |
| 4850 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4851 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
| 4852 | ASSERT_TRUE(test_server_.Start()); |
| 4853 | |
| 4854 | TestDelegate d; |
| 4855 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4856 | URLRequest r( |
| 4857 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4858 | r.EnableChunkedUpload(); |
| 4859 | r.set_method("POST"); |
| 4860 | r.Start(); |
| 4861 | EXPECT_TRUE(r.is_pending()); |
| 4862 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4863 | base::RunLoop().RunUntilIdle(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4864 | AddChunksToUpload(&r); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4865 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4866 | |
| 4867 | VerifyReceivedDataMatchesChunks(&r, &d); |
| 4868 | } |
| 4869 | } |
| 4870 | |
| 4871 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
| 4872 | ASSERT_TRUE(test_server_.Start()); |
| 4873 | |
| 4874 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4875 | URLRequest req(test_server_.GetURL("files/with-headers.html"), |
| 4876 | DEFAULT_PRIORITY, |
| 4877 | &d, |
| 4878 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4879 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4880 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4881 | |
| 4882 | const HttpResponseHeaders* headers = req.response_headers(); |
| 4883 | |
| 4884 | // Simple sanity check that response_info() accesses the same data. |
| 4885 | EXPECT_EQ(headers, req.response_info().headers.get()); |
| 4886 | |
| 4887 | std::string header; |
| 4888 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 4889 | EXPECT_EQ("private", header); |
| 4890 | |
| 4891 | header.clear(); |
| 4892 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 4893 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 4894 | |
| 4895 | // The response has two "X-Multiple-Entries" headers. |
| 4896 | // This verfies our output has them concatenated together. |
| 4897 | header.clear(); |
| 4898 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 4899 | EXPECT_EQ("a, b", header); |
| 4900 | } |
| 4901 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4902 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 4903 | SpawnedTestServer::SSLOptions ssl_options; |
| 4904 | SpawnedTestServer https_test_server( |
| 4905 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4906 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4907 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4908 | ASSERT_TRUE(https_test_server.Start()); |
| 4909 | |
| 4910 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4911 | URLRequest request(https_test_server.GetURL("files/hsts-headers.html"), |
| 4912 | DEFAULT_PRIORITY, |
| 4913 | &d, |
| 4914 | &default_context_); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4915 | request.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4916 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4917 | |
| 4918 | TransportSecurityState* security_state = |
| 4919 | default_context_.transport_security_state(); |
| 4920 | bool sni_available = true; |
| 4921 | TransportSecurityState::DomainState domain_state; |
| 4922 | EXPECT_TRUE(security_state->GetDomainState( |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 4923 | SpawnedTestServer::kLocalhost, sni_available, &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4924 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
| 4925 | domain_state.upgrade_mode); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 4926 | EXPECT_TRUE(domain_state.sts_include_subdomains); |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 4927 | EXPECT_FALSE(domain_state.pkp_include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 4928 | #if defined(OS_ANDROID) |
| 4929 | // Android's CertVerifyProc does not (yet) handle pins. |
| 4930 | #else |
| 4931 | EXPECT_FALSE(domain_state.HasPublicKeyPins()); |
| 4932 | #endif |
| 4933 | } |
| 4934 | |
| 4935 | // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will |
| 4936 | // reject HPKP headers, and a test setting only HPKP headers will fail (no |
| 4937 | // DomainState present because header rejected). |
| 4938 | #if defined(OS_ANDROID) |
| 4939 | #define MAYBE_ProcessPKP DISABLED_ProcessPKP |
| 4940 | #else |
| 4941 | #define MAYBE_ProcessPKP ProcessPKP |
| 4942 | #endif |
| 4943 | |
| 4944 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 4945 | // validity/expiration. |
| 4946 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) { |
| 4947 | SpawnedTestServer::SSLOptions ssl_options; |
| 4948 | SpawnedTestServer https_test_server( |
| 4949 | SpawnedTestServer::TYPE_HTTPS, |
| 4950 | ssl_options, |
| 4951 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 4952 | ASSERT_TRUE(https_test_server.Start()); |
| 4953 | |
| 4954 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4955 | URLRequest request(https_test_server.GetURL("files/hpkp-headers.html"), |
| 4956 | DEFAULT_PRIORITY, |
| 4957 | &d, |
| 4958 | &default_context_); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 4959 | request.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4960 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 4961 | |
| 4962 | TransportSecurityState* security_state = |
| 4963 | default_context_.transport_security_state(); |
| 4964 | bool sni_available = true; |
| 4965 | TransportSecurityState::DomainState domain_state; |
| 4966 | EXPECT_TRUE(security_state->GetDomainState( |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 4967 | SpawnedTestServer::kLocalhost, sni_available, &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 4968 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT, |
| 4969 | domain_state.upgrade_mode); |
| 4970 | EXPECT_FALSE(domain_state.sts_include_subdomains); |
| 4971 | EXPECT_FALSE(domain_state.pkp_include_subdomains); |
| 4972 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 4973 | EXPECT_NE(domain_state.upgrade_expiry, |
| 4974 | domain_state.dynamic_spki_hashes_expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4975 | } |
| 4976 | |
| 4977 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 4978 | SpawnedTestServer::SSLOptions ssl_options; |
| 4979 | SpawnedTestServer https_test_server( |
| 4980 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4981 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4982 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4983 | ASSERT_TRUE(https_test_server.Start()); |
| 4984 | |
| 4985 | TestDelegate d; |
| 4986 | URLRequest request( |
| 4987 | https_test_server.GetURL("files/hsts-multiple-headers.html"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 4988 | DEFAULT_PRIORITY, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4989 | &d, |
| 4990 | &default_context_); |
| 4991 | request.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4992 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4993 | |
| 4994 | // We should have set parameters from the first header, not the second. |
| 4995 | TransportSecurityState* security_state = |
| 4996 | default_context_.transport_security_state(); |
| 4997 | bool sni_available = true; |
| 4998 | TransportSecurityState::DomainState domain_state; |
| 4999 | EXPECT_TRUE(security_state->GetDomainState( |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 5000 | SpawnedTestServer::kLocalhost, sni_available, &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5001 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
| 5002 | domain_state.upgrade_mode); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5003 | EXPECT_FALSE(domain_state.sts_include_subdomains); |
| 5004 | EXPECT_FALSE(domain_state.pkp_include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5005 | } |
| 5006 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5007 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 5008 | SpawnedTestServer::SSLOptions ssl_options; |
| 5009 | SpawnedTestServer https_test_server( |
| 5010 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5011 | ssl_options, |
| 5012 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5013 | ASSERT_TRUE(https_test_server.Start()); |
| 5014 | |
| 5015 | TestDelegate d; |
| 5016 | URLRequest request( |
| 5017 | https_test_server.GetURL("files/hsts-and-hpkp-headers.html"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5018 | DEFAULT_PRIORITY, |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5019 | &d, |
| 5020 | &default_context_); |
| 5021 | request.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5022 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5023 | |
| 5024 | // We should have set parameters from the first header, not the second. |
| 5025 | TransportSecurityState* security_state = |
| 5026 | default_context_.transport_security_state(); |
| 5027 | bool sni_available = true; |
| 5028 | TransportSecurityState::DomainState domain_state; |
| 5029 | EXPECT_TRUE(security_state->GetDomainState( |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 5030 | SpawnedTestServer::kLocalhost, sni_available, &domain_state)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5031 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
| 5032 | domain_state.upgrade_mode); |
| 5033 | #if defined(OS_ANDROID) |
| 5034 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5035 | #else |
| 5036 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5037 | #endif |
| 5038 | EXPECT_NE(domain_state.upgrade_expiry, |
| 5039 | domain_state.dynamic_spki_hashes_expiry); |
| 5040 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5041 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 5042 | // the *second* such header, and we MUST process only the first. |
| 5043 | EXPECT_FALSE(domain_state.sts_include_subdomains); |
| 5044 | // includeSubdomains does not occur in the test HPKP header. |
| 5045 | EXPECT_FALSE(domain_state.pkp_include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5046 | } |
| 5047 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5048 | // Tests that when multiple HPKP headers are present, asserting different |
| 5049 | // policies, that only the first such policy is processed. |
| 5050 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
| 5051 | SpawnedTestServer::SSLOptions ssl_options; |
| 5052 | SpawnedTestServer https_test_server( |
| 5053 | SpawnedTestServer::TYPE_HTTPS, |
| 5054 | ssl_options, |
| 5055 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5056 | ASSERT_TRUE(https_test_server.Start()); |
| 5057 | |
| 5058 | TestDelegate d; |
| 5059 | URLRequest request( |
| 5060 | https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5061 | DEFAULT_PRIORITY, |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5062 | &d, |
| 5063 | &default_context_); |
| 5064 | request.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5065 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5066 | |
| 5067 | TransportSecurityState* security_state = |
| 5068 | default_context_.transport_security_state(); |
| 5069 | bool sni_available = true; |
| 5070 | TransportSecurityState::DomainState domain_state; |
| 5071 | EXPECT_TRUE(security_state->GetDomainState( |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 5072 | SpawnedTestServer::kLocalhost, sni_available, &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5073 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
| 5074 | domain_state.upgrade_mode); |
| 5075 | #if defined(OS_ANDROID) |
| 5076 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5077 | #else |
| 5078 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5079 | #endif |
| 5080 | EXPECT_NE(domain_state.upgrade_expiry, |
| 5081 | domain_state.dynamic_spki_hashes_expiry); |
| 5082 | |
| 5083 | EXPECT_TRUE(domain_state.sts_include_subdomains); |
| 5084 | EXPECT_FALSE(domain_state.pkp_include_subdomains); |
| 5085 | } |
| 5086 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5087 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
| 5088 | ASSERT_TRUE(test_server_.Start()); |
| 5089 | |
| 5090 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5091 | URLRequest req(test_server_.GetURL("files/content-type-normalization.html"), |
| 5092 | DEFAULT_PRIORITY, |
| 5093 | &d, |
| 5094 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5095 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5096 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5097 | |
| 5098 | std::string mime_type; |
| 5099 | req.GetMimeType(&mime_type); |
| 5100 | EXPECT_EQ("text/html", mime_type); |
| 5101 | |
| 5102 | std::string charset; |
| 5103 | req.GetCharset(&charset); |
| 5104 | EXPECT_EQ("utf-8", charset); |
| 5105 | req.Cancel(); |
| 5106 | } |
| 5107 | |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5108 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictRedirects) { |
| 5109 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 5110 | GURL file_url("file:///foo.txt"); |
| 5111 | GURL data_url("data:,foo"); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5112 | FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current()); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5113 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 5114 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5115 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5116 | |
| 5117 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
| 5118 | EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url)); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5119 | EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5120 | } |
| 5121 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5122 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5123 | ASSERT_TRUE(test_server_.Start()); |
| 5124 | |
| 5125 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5126 | URLRequest req(test_server_.GetURL("files/redirect-to-file.html"), |
| 5127 | DEFAULT_PRIORITY, |
| 5128 | &d, |
| 5129 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5130 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5131 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5132 | |
| 5133 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 5134 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error()); |
| 5135 | } |
| 5136 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5137 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
| 5138 | ASSERT_TRUE(test_server_.Start()); |
| 5139 | |
| 5140 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5141 | URLRequest req(test_server_.GetURL("files/redirect-to-data.html"), |
| 5142 | DEFAULT_PRIORITY, |
| 5143 | &d, |
| 5144 | &default_context_); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5145 | req.Start(); |
| 5146 | base::MessageLoop::current()->Run(); |
| 5147 | |
| 5148 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 5149 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error()); |
| 5150 | } |
| 5151 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5152 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
| 5153 | ASSERT_TRUE(test_server_.Start()); |
| 5154 | |
| 5155 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5156 | URLRequest req(test_server_.GetURL("files/redirect-to-invalid-url.html"), |
| 5157 | DEFAULT_PRIORITY, |
| 5158 | &d, |
| 5159 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5160 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5161 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5162 | |
| 5163 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 5164 | EXPECT_EQ(ERR_INVALID_URL, req.status().error()); |
| 5165 | } |
| 5166 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame^] | 5167 | // Make sure redirects are cached, despite not reading their bodies. |
| 5168 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
| 5169 | ASSERT_TRUE(test_server_.Start()); |
| 5170 | GURL redirect_url = |
| 5171 | test_server_.GetURL("files/redirect302-to-echo-cacheable"); |
| 5172 | |
| 5173 | { |
| 5174 | TestDelegate d; |
| 5175 | URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_); |
| 5176 | req.Start(); |
| 5177 | base::RunLoop().Run(); |
| 5178 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 5179 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5180 | EXPECT_EQ(test_server_.GetURL("echo"), req.url()); |
| 5181 | } |
| 5182 | |
| 5183 | { |
| 5184 | TestDelegate d; |
| 5185 | d.set_quit_on_redirect(true); |
| 5186 | URLRequest req(redirect_url, DEFAULT_PRIORITY, &d, &default_context_); |
| 5187 | req.Start(); |
| 5188 | base::RunLoop().Run(); |
| 5189 | |
| 5190 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5191 | EXPECT_EQ(0, d.response_started_count()); |
| 5192 | EXPECT_TRUE(req.was_cached()); |
| 5193 | |
| 5194 | req.FollowDeferredRedirect(); |
| 5195 | base::RunLoop().Run(); |
| 5196 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5197 | EXPECT_EQ(1, d.response_started_count()); |
| 5198 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 5199 | EXPECT_EQ(test_server_.GetURL("echo"), req.url()); |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 5204 | // when the headers are read, since the body won't have been read. |
| 5205 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
| 5206 | ASSERT_TRUE(test_server_.Start()); |
| 5207 | // URL that is normally cached. |
| 5208 | GURL initial_url = test_server_.GetURL("cachetime"); |
| 5209 | |
| 5210 | { |
| 5211 | // Set up the TestNetworkDelegate tp force a redirect. |
| 5212 | GURL redirect_to_url = test_server_.GetURL("echo"); |
| 5213 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 5214 | redirect_to_url); |
| 5215 | |
| 5216 | TestDelegate d; |
| 5217 | URLRequest req(initial_url, DEFAULT_PRIORITY, &d, &default_context_); |
| 5218 | req.Start(); |
| 5219 | base::RunLoop().Run(); |
| 5220 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 5221 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5222 | EXPECT_EQ(redirect_to_url, req.url()); |
| 5223 | } |
| 5224 | |
| 5225 | { |
| 5226 | TestDelegate d; |
| 5227 | URLRequest req(initial_url, DEFAULT_PRIORITY, &d, &default_context_); |
| 5228 | req.Start(); |
| 5229 | base::RunLoop().Run(); |
| 5230 | |
| 5231 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 5232 | EXPECT_FALSE(req.was_cached()); |
| 5233 | EXPECT_EQ(0, d.received_redirect_count()); |
| 5234 | EXPECT_EQ(initial_url, req.url()); |
| 5235 | } |
| 5236 | } |
| 5237 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5238 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
| 5239 | ASSERT_TRUE(test_server_.Start()); |
| 5240 | |
| 5241 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5242 | URLRequest req(test_server_.GetURL("echoheader?Referer"), |
| 5243 | DEFAULT_PRIORITY, |
| 5244 | &d, |
| 5245 | &default_context_); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5246 | req.SetReferrer("http://user:[email protected]/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5247 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5248 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5249 | |
| 5250 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 5251 | } |
| 5252 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5253 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
| 5254 | ASSERT_TRUE(test_server_.Start()); |
| 5255 | |
| 5256 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5257 | URLRequest req(test_server_.GetURL("echoheader?Referer"), |
| 5258 | DEFAULT_PRIORITY, |
| 5259 | &d, |
| 5260 | &default_context_); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5261 | req.SetReferrer("http://foo.com/test#fragment"); |
| 5262 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5263 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5264 | |
| 5265 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 5266 | } |
| 5267 | |
| 5268 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
| 5269 | ASSERT_TRUE(test_server_.Start()); |
| 5270 | |
| 5271 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5272 | URLRequest req(test_server_.GetURL("echoheader?Referer"), |
| 5273 | DEFAULT_PRIORITY, |
| 5274 | &d, |
| 5275 | &default_context_); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5276 | req.SetReferrer("http://foo.com/test#fragment"); |
| 5277 | req.SetReferrer(""); |
| 5278 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5279 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5280 | |
| 5281 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 5282 | } |
| 5283 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5284 | // Defer network start and then resume, checking that the request was a success |
| 5285 | // and bytes were received. |
| 5286 | TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) { |
| 5287 | ASSERT_TRUE(test_server_.Start()); |
| 5288 | |
| 5289 | TestDelegate d; |
| 5290 | { |
| 5291 | d.set_quit_on_network_start(true); |
| 5292 | GURL test_url(test_server_.GetURL("echo")); |
| 5293 | URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_); |
| 5294 | |
| 5295 | req.Start(); |
| 5296 | base::RunLoop().Run(); |
| 5297 | |
| 5298 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5299 | EXPECT_EQ(0, d.response_started_count()); |
| 5300 | |
| 5301 | req.ResumeNetworkStart(); |
| 5302 | base::RunLoop().Run(); |
| 5303 | |
| 5304 | EXPECT_EQ(1, d.response_started_count()); |
| 5305 | EXPECT_NE(0, d.bytes_received()); |
| 5306 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 5307 | } |
| 5308 | } |
| 5309 | |
| 5310 | // Check that OnBeforeNetworkStart is only called once even if there is a |
| 5311 | // redirect. |
| 5312 | TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) { |
| 5313 | ASSERT_TRUE(test_server_.Start()); |
| 5314 | |
| 5315 | TestDelegate d; |
| 5316 | { |
| 5317 | d.set_quit_on_redirect(true); |
| 5318 | d.set_quit_on_network_start(true); |
| 5319 | URLRequest req(test_server_.GetURL("server-redirect?echo"), |
| 5320 | DEFAULT_PRIORITY, |
| 5321 | &d, |
| 5322 | &default_context_); |
| 5323 | |
| 5324 | req.Start(); |
| 5325 | base::RunLoop().Run(); |
| 5326 | |
| 5327 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5328 | EXPECT_EQ(0, d.response_started_count()); |
| 5329 | EXPECT_EQ(0, d.received_redirect_count()); |
| 5330 | |
| 5331 | req.ResumeNetworkStart(); |
| 5332 | base::RunLoop().Run(); |
| 5333 | |
| 5334 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5335 | req.FollowDeferredRedirect(); |
| 5336 | base::RunLoop().Run(); |
| 5337 | |
| 5338 | // Check that the redirect's new network transaction does not get propagated |
| 5339 | // to a second OnBeforeNetworkStart() notification. |
| 5340 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5341 | |
| 5342 | EXPECT_EQ(1, d.response_started_count()); |
| 5343 | EXPECT_NE(0, d.bytes_received()); |
| 5344 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 5345 | } |
| 5346 | } |
| 5347 | |
| 5348 | // Cancel the request after learning that the request would use the network. |
| 5349 | TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) { |
| 5350 | ASSERT_TRUE(test_server_.Start()); |
| 5351 | |
| 5352 | TestDelegate d; |
| 5353 | { |
| 5354 | d.set_quit_on_network_start(true); |
| 5355 | GURL test_url(test_server_.GetURL("echo")); |
| 5356 | URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_); |
| 5357 | |
| 5358 | req.Start(); |
| 5359 | base::RunLoop().Run(); |
| 5360 | |
| 5361 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5362 | EXPECT_EQ(0, d.response_started_count()); |
| 5363 | |
| 5364 | req.Cancel(); |
| 5365 | base::RunLoop().Run(); |
| 5366 | |
| 5367 | EXPECT_EQ(1, d.response_started_count()); |
| 5368 | EXPECT_EQ(0, d.bytes_received()); |
| 5369 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 5370 | } |
| 5371 | } |
| 5372 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5373 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
| 5374 | ASSERT_TRUE(test_server_.Start()); |
| 5375 | |
| 5376 | TestDelegate d; |
| 5377 | { |
| 5378 | d.set_cancel_in_received_redirect(true); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5379 | URLRequest req(test_server_.GetURL("files/redirect-test.html"), |
| 5380 | DEFAULT_PRIORITY, |
| 5381 | &d, |
| 5382 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5383 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5384 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5385 | |
| 5386 | EXPECT_EQ(1, d.response_started_count()); |
| 5387 | EXPECT_EQ(0, d.bytes_received()); |
| 5388 | EXPECT_FALSE(d.received_data_before_response()); |
| 5389 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 5390 | } |
| 5391 | } |
| 5392 | |
| 5393 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
| 5394 | ASSERT_TRUE(test_server_.Start()); |
| 5395 | |
| 5396 | TestDelegate d; |
| 5397 | { |
| 5398 | d.set_quit_on_redirect(true); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5399 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5400 | URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5401 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5402 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5403 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5404 | |
| 5405 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5406 | |
| 5407 | req.FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5408 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5409 | |
| 5410 | EXPECT_EQ(1, d.response_started_count()); |
| 5411 | EXPECT_FALSE(d.received_data_before_response()); |
| 5412 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 5413 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5414 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5415 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 5416 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 5417 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 5418 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 5419 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 5420 | |
| 5421 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 5422 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5423 | EXPECT_EQ(contents, d.data_received()); |
| 5424 | } |
| 5425 | } |
| 5426 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5427 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
| 5428 | ASSERT_TRUE(test_server_.Start()); |
| 5429 | |
| 5430 | TestDelegate d; |
| 5431 | { |
| 5432 | d.set_quit_on_redirect(true); |
| 5433 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5434 | URLRequest req(test_url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5435 | |
| 5436 | EXPECT_FALSE(d.have_full_request_headers()); |
| 5437 | |
| 5438 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5439 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5440 | |
| 5441 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5442 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5443 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5444 | d.ClearFullRequestHeaders(); |
| 5445 | |
| 5446 | req.FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5447 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5448 | |
| 5449 | GURL target_url(test_server_.GetURL("files/with-headers.html")); |
| 5450 | EXPECT_EQ(1, d.response_started_count()); |
| 5451 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5452 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 5453 | EXPECT_FALSE(d.received_data_before_response()); |
| 5454 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 5455 | |
| 5456 | base::FilePath path; |
| 5457 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 5458 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 5459 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 5460 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 5461 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 5462 | |
| 5463 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 5464 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5465 | EXPECT_EQ(contents, d.data_received()); |
| 5466 | } |
| 5467 | } |
| 5468 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5469 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
| 5470 | ASSERT_TRUE(test_server_.Start()); |
| 5471 | |
| 5472 | TestDelegate d; |
| 5473 | { |
| 5474 | d.set_quit_on_redirect(true); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5475 | URLRequest req(test_server_.GetURL("files/redirect-test.html"), |
| 5476 | DEFAULT_PRIORITY, |
| 5477 | &d, |
| 5478 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5479 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5480 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5481 | |
| 5482 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5483 | |
| 5484 | req.Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5485 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5486 | |
| 5487 | EXPECT_EQ(1, d.response_started_count()); |
| 5488 | EXPECT_EQ(0, d.bytes_received()); |
| 5489 | EXPECT_FALSE(d.received_data_before_response()); |
| 5490 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 5491 | } |
| 5492 | } |
| 5493 | |
| 5494 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
| 5495 | ASSERT_TRUE(test_server_.Start()); |
| 5496 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5497 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5498 | { |
| 5499 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5500 | URLRequest req(test_server_.GetURL("echoheadercache?foo"), |
| 5501 | DEFAULT_PRIORITY, |
| 5502 | &d, |
| 5503 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5504 | HttpRequestHeaders headers; |
| 5505 | headers.SetHeader("foo", "1"); |
| 5506 | req.SetExtraRequestHeaders(headers); |
| 5507 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5508 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5509 | |
| 5510 | LoadTimingInfo load_timing_info; |
| 5511 | req.GetLoadTimingInfo(&load_timing_info); |
| 5512 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5513 | } |
| 5514 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5515 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5516 | { |
| 5517 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5518 | URLRequest req(test_server_.GetURL("echoheadercache?foo"), |
| 5519 | DEFAULT_PRIORITY, |
| 5520 | &d, |
| 5521 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5522 | HttpRequestHeaders headers; |
| 5523 | headers.SetHeader("foo", "1"); |
| 5524 | req.SetExtraRequestHeaders(headers); |
| 5525 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5526 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5527 | |
| 5528 | EXPECT_TRUE(req.was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5529 | |
| 5530 | LoadTimingInfo load_timing_info; |
| 5531 | req.GetLoadTimingInfo(&load_timing_info); |
| 5532 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5533 | } |
| 5534 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5535 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5536 | { |
| 5537 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5538 | URLRequest req(test_server_.GetURL("echoheadercache?foo"), |
| 5539 | DEFAULT_PRIORITY, |
| 5540 | &d, |
| 5541 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5542 | HttpRequestHeaders headers; |
| 5543 | headers.SetHeader("foo", "2"); |
| 5544 | req.SetExtraRequestHeaders(headers); |
| 5545 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5546 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5547 | |
| 5548 | EXPECT_FALSE(req.was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5549 | |
| 5550 | LoadTimingInfo load_timing_info; |
| 5551 | req.GetLoadTimingInfo(&load_timing_info); |
| 5552 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
| 5557 | ASSERT_TRUE(test_server_.Start()); |
| 5558 | |
| 5559 | // populate the cache |
| 5560 | { |
| 5561 | TestDelegate d; |
| 5562 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5563 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5564 | URLRequest r(test_server_.GetURL("auth-basic"), |
| 5565 | DEFAULT_PRIORITY, |
| 5566 | &d, |
| 5567 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5568 | r.Start(); |
| 5569 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5570 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5571 | |
| 5572 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5573 | } |
| 5574 | |
| 5575 | // repeat request with end-to-end validation. since auth-basic results in a |
| 5576 | // cachable page, we expect this test to result in a 304. in which case, the |
| 5577 | // response should be fetched from the cache. |
| 5578 | { |
| 5579 | TestDelegate d; |
| 5580 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5581 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5582 | URLRequest r(test_server_.GetURL("auth-basic"), |
| 5583 | DEFAULT_PRIORITY, |
| 5584 | &d, |
| 5585 | &default_context_); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 5586 | r.SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5587 | r.Start(); |
| 5588 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5589 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5590 | |
| 5591 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5592 | |
| 5593 | // Should be the same cached document. |
| 5594 | EXPECT_TRUE(r.was_cached()); |
| 5595 | } |
| 5596 | } |
| 5597 | |
| 5598 | // Check that Set-Cookie headers in 401 responses are respected. |
| 5599 | // http://crbug.com/6450 |
| 5600 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
| 5601 | ASSERT_TRUE(test_server_.Start()); |
| 5602 | |
| 5603 | GURL url_requiring_auth = |
| 5604 | test_server_.GetURL("auth-basic?set-cookie-if-challenged"); |
| 5605 | |
| 5606 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 5607 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 5608 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5609 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5610 | TestURLRequestContext context(true); |
| 5611 | context.set_network_delegate(&network_delegate); |
| 5612 | context.Init(); |
| 5613 | |
| 5614 | TestDelegate d; |
| 5615 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5616 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5617 | URLRequest r(url_requiring_auth, DEFAULT_PRIORITY, &d, &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5618 | r.Start(); |
| 5619 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5620 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5621 | |
| 5622 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5623 | |
| 5624 | // Make sure we sent the cookie in the restarted transaction. |
| 5625 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 5626 | != std::string::npos); |
| 5627 | } |
| 5628 | |
| 5629 | // Same test as above, except this time the restart is initiated earlier |
| 5630 | // (without user intervention since identity is embedded in the URL). |
| 5631 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5632 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5633 | TestURLRequestContext context(true); |
| 5634 | context.set_network_delegate(&network_delegate); |
| 5635 | context.Init(); |
| 5636 | |
| 5637 | TestDelegate d; |
| 5638 | |
| 5639 | GURL::Replacements replacements; |
| 5640 | std::string username("user2"); |
| 5641 | std::string password("secret"); |
| 5642 | replacements.SetUsernameStr(username); |
| 5643 | replacements.SetPasswordStr(password); |
| 5644 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 5645 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5646 | URLRequest r(url_with_identity, DEFAULT_PRIORITY, &d, &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5647 | r.Start(); |
| 5648 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5649 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5650 | |
| 5651 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 5652 | |
| 5653 | // Make sure we sent the cookie in the restarted transaction. |
| 5654 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 5655 | != std::string::npos); |
| 5656 | } |
| 5657 | } |
| 5658 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5659 | // Tests that load timing works as expected with auth and the cache. |
| 5660 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
| 5661 | ASSERT_TRUE(test_server_.Start()); |
| 5662 | |
| 5663 | // populate the cache |
| 5664 | { |
| 5665 | TestDelegate d; |
| 5666 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5667 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5668 | URLRequest r(test_server_.GetURL("auth-basic"), |
| 5669 | DEFAULT_PRIORITY, |
| 5670 | &d, |
| 5671 | &default_context_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5672 | r.Start(); |
| 5673 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5674 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5675 | |
| 5676 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5677 | |
| 5678 | LoadTimingInfo load_timing_info_before_auth; |
| 5679 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 5680 | &load_timing_info_before_auth)); |
| 5681 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 5682 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5683 | |
| 5684 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 5685 | r.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5686 | // The test server does not support keep alive sockets, so the second |
| 5687 | // request with auth should use a new socket. |
| 5688 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5689 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 5690 | load_timing_info.socket_log_id); |
| 5691 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 5692 | load_timing_info.connect_timing.connect_start); |
| 5693 | } |
| 5694 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5695 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 5696 | // cachable page, we expect this test to result in a 304. In which case, the |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5697 | // response should be fetched from the cache. |
| 5698 | { |
| 5699 | TestDelegate d; |
| 5700 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5701 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5702 | URLRequest r(test_server_.GetURL("auth-basic"), |
| 5703 | DEFAULT_PRIORITY, |
| 5704 | &d, |
| 5705 | &default_context_); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 5706 | r.SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5707 | r.Start(); |
| 5708 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5709 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5710 | |
| 5711 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5712 | |
| 5713 | // Should be the same cached document. |
| 5714 | EXPECT_TRUE(r.was_cached()); |
| 5715 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5716 | // Since there was a request that went over the wire, the load timing |
| 5717 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5718 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 5719 | r.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5720 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5721 | } |
| 5722 | } |
| 5723 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5724 | // In this test, we do a POST which the server will 302 redirect. |
| 5725 | // The subsequent transaction should use GET, and should not send the |
| 5726 | // Content-Type header. |
| 5727 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 5728 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
| 5729 | ASSERT_TRUE(test_server_.Start()); |
| 5730 | |
| 5731 | const char kData[] = "hello world"; |
| 5732 | |
| 5733 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5734 | URLRequest req(test_server_.GetURL("files/redirect-to-echoall"), |
| 5735 | DEFAULT_PRIORITY, |
| 5736 | &d, |
| 5737 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5738 | req.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 5739 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5740 | |
| 5741 | // Set headers (some of which are specific to the POST). |
| 5742 | HttpRequestHeaders headers; |
| 5743 | headers.AddHeadersFromString( |
| 5744 | "Content-Type: multipart/form-data; " |
| 5745 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 5746 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 5747 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 5748 | "Accept-Language: en-US,en\r\n" |
| 5749 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 5750 | "Content-Length: 11\r\n" |
| 5751 | "Origin: http://localhost:1337/"); |
| 5752 | req.SetExtraRequestHeaders(headers); |
| 5753 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5754 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5755 | |
| 5756 | std::string mime_type; |
| 5757 | req.GetMimeType(&mime_type); |
| 5758 | EXPECT_EQ("text/html", mime_type); |
| 5759 | |
| 5760 | const std::string& data = d.data_received(); |
| 5761 | |
| 5762 | // Check that the post-specific headers were stripped: |
| 5763 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 5764 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 5765 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 5766 | |
| 5767 | // These extra request headers should not have been stripped. |
| 5768 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 5769 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 5770 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 5771 | } |
| 5772 | |
| 5773 | // The following tests check that we handle mutating the request method for |
| 5774 | // HTTP redirects as expected. |
| 5775 | // See http://crbug.com/56373 and http://crbug.com/102130. |
| 5776 | |
| 5777 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
| 5778 | ASSERT_TRUE(test_server_.Start()); |
| 5779 | |
| 5780 | const GURL url = test_server_.GetURL("files/redirect301-to-echo"); |
| 5781 | |
| 5782 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 5783 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 5784 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 5785 | } |
| 5786 | |
| 5787 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
| 5788 | ASSERT_TRUE(test_server_.Start()); |
| 5789 | |
| 5790 | const GURL url = test_server_.GetURL("files/redirect302-to-echo"); |
| 5791 | |
| 5792 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 5793 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 5794 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 5795 | } |
| 5796 | |
| 5797 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
| 5798 | ASSERT_TRUE(test_server_.Start()); |
| 5799 | |
| 5800 | const GURL url = test_server_.GetURL("files/redirect303-to-echo"); |
| 5801 | |
| 5802 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 5803 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 5804 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 5805 | } |
| 5806 | |
| 5807 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
| 5808 | ASSERT_TRUE(test_server_.Start()); |
| 5809 | |
| 5810 | const GURL url = test_server_.GetURL("files/redirect307-to-echo"); |
| 5811 | |
| 5812 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 5813 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 5814 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 5815 | } |
| 5816 | |
| 5817 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
| 5818 | ASSERT_TRUE(test_server_.Start()); |
| 5819 | |
| 5820 | const char kData[] = "hello world"; |
| 5821 | |
| 5822 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5823 | URLRequest req(test_server_.GetURL("empty.html"), |
| 5824 | DEFAULT_PRIORITY, |
| 5825 | &d, |
| 5826 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5827 | req.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 5828 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5829 | HttpRequestHeaders headers; |
| 5830 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 5831 | base::UintToString(arraysize(kData) - 1)); |
| 5832 | req.SetExtraRequestHeaders(headers); |
| 5833 | |
| 5834 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 5835 | &req, &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 5836 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5837 | AddTestInterceptor()->set_main_intercept_job(job); |
| 5838 | |
| 5839 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5840 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5841 | EXPECT_EQ("GET", req.method()); |
| 5842 | } |
| 5843 | |
| 5844 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 5845 | ASSERT_TRUE(test_server_.Start()); |
| 5846 | |
| 5847 | const char kData[] = "hello world"; |
| 5848 | |
| 5849 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5850 | URLRequest req(test_server_.GetURL("empty.html"), |
| 5851 | DEFAULT_PRIORITY, |
| 5852 | &d, |
| 5853 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5854 | req.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 5855 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5856 | HttpRequestHeaders headers; |
| 5857 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 5858 | base::UintToString(arraysize(kData) - 1)); |
| 5859 | req.SetExtraRequestHeaders(headers); |
| 5860 | |
| 5861 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 5862 | &req, &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 5863 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
| 5864 | "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5865 | AddTestInterceptor()->set_main_intercept_job(job); |
| 5866 | |
| 5867 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5868 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5869 | EXPECT_EQ("POST", req.method()); |
| 5870 | EXPECT_EQ(kData, d.data_received()); |
| 5871 | } |
| 5872 | |
| 5873 | // Check that default A-L header is sent. |
| 5874 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
| 5875 | ASSERT_TRUE(test_server_.Start()); |
| 5876 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 5877 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5878 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5879 | TestURLRequestContext context(true); |
| 5880 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 5881 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5882 | context.Init(); |
| 5883 | |
| 5884 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5885 | URLRequest req(test_server_.GetURL("echoheader?Accept-Language"), |
| 5886 | DEFAULT_PRIORITY, |
| 5887 | &d, |
| 5888 | &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5889 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5890 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5891 | EXPECT_EQ("en", d.data_received()); |
| 5892 | } |
| 5893 | |
| 5894 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 5895 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
| 5896 | ASSERT_TRUE(test_server_.Start()); |
| 5897 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 5898 | std::string empty_string; // Avoid most vexing parse on line below. |
| 5899 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5900 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5901 | TestURLRequestContext context(true); |
| 5902 | context.set_network_delegate(&network_delegate); |
| 5903 | context.Init(); |
| 5904 | // We override the language after initialization because empty entries |
| 5905 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 5906 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5907 | |
| 5908 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5909 | URLRequest req(test_server_.GetURL("echoheader?Accept-Language"), |
| 5910 | DEFAULT_PRIORITY, |
| 5911 | &d, |
| 5912 | &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5913 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5914 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5915 | EXPECT_EQ("None", d.data_received()); |
| 5916 | } |
| 5917 | |
| 5918 | // Check that if request overrides the A-L header, the default is not appended. |
| 5919 | // See http://crbug.com/20894 |
| 5920 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
| 5921 | ASSERT_TRUE(test_server_.Start()); |
| 5922 | |
| 5923 | TestDelegate d; |
| 5924 | URLRequest req(test_server_.GetURL("echoheader?Accept-Language"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5925 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5926 | &d, |
| 5927 | &default_context_); |
| 5928 | HttpRequestHeaders headers; |
| 5929 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
| 5930 | req.SetExtraRequestHeaders(headers); |
| 5931 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5932 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5933 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 5934 | } |
| 5935 | |
| 5936 | // Check that default A-E header is sent. |
| 5937 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
| 5938 | ASSERT_TRUE(test_server_.Start()); |
| 5939 | |
| 5940 | TestDelegate d; |
| 5941 | URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5942 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5943 | &d, |
| 5944 | &default_context_); |
| 5945 | HttpRequestHeaders headers; |
| 5946 | req.SetExtraRequestHeaders(headers); |
| 5947 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5948 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5949 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 5950 | } |
| 5951 | |
| 5952 | // Check that if request overrides the A-E header, the default is not appended. |
| 5953 | // See http://crbug.com/47381 |
| 5954 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
| 5955 | ASSERT_TRUE(test_server_.Start()); |
| 5956 | |
| 5957 | TestDelegate d; |
| 5958 | URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5959 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5960 | &d, |
| 5961 | &default_context_); |
| 5962 | HttpRequestHeaders headers; |
| 5963 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
| 5964 | req.SetExtraRequestHeaders(headers); |
| 5965 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5966 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5967 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 5968 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 5969 | } |
| 5970 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 5971 | // Check that setting the A-C header sends the proper header. |
| 5972 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5973 | ASSERT_TRUE(test_server_.Start()); |
| 5974 | |
| 5975 | TestDelegate d; |
| 5976 | URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5977 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5978 | &d, |
| 5979 | &default_context_); |
| 5980 | HttpRequestHeaders headers; |
| 5981 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
| 5982 | req.SetExtraRequestHeaders(headers); |
| 5983 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5984 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5985 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 5986 | } |
| 5987 | |
| 5988 | // Check that default User-Agent header is sent. |
| 5989 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
| 5990 | ASSERT_TRUE(test_server_.Start()); |
| 5991 | |
| 5992 | TestDelegate d; |
| 5993 | URLRequest req(test_server_.GetURL("echoheader?User-Agent"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 5994 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5995 | &d, |
| 5996 | &default_context_); |
| 5997 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5998 | base::RunLoop().Run(); |
[email protected] | aa05127 | 2014-03-10 05:56:56 | [diff] [blame] | 5999 | EXPECT_EQ(req.context()->http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 6000 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | // Check that if request overrides the User-Agent header, |
| 6004 | // the default is not appended. |
| 6005 | TEST_F(URLRequestTestHTTP, OverrideUserAgent) { |
| 6006 | ASSERT_TRUE(test_server_.Start()); |
| 6007 | |
| 6008 | TestDelegate d; |
| 6009 | URLRequest req(test_server_.GetURL("echoheader?User-Agent"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6010 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6011 | &d, |
| 6012 | &default_context_); |
| 6013 | HttpRequestHeaders headers; |
| 6014 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
| 6015 | req.SetExtraRequestHeaders(headers); |
| 6016 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6017 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 6018 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6019 | } |
| 6020 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6021 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 6022 | // User-Agent header to be sent but does not send the Accept-Language and |
| 6023 | // Accept-Charset headers. |
| 6024 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
| 6025 | ASSERT_TRUE(test_server_.Start()); |
| 6026 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6027 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6028 | TestURLRequestContext context(true); |
| 6029 | context.set_network_delegate(&network_delegate); |
| 6030 | context.Init(); |
| 6031 | // We override the HttpUserAgentSettings after initialization because empty |
| 6032 | // entries get overridden by Init(). |
| 6033 | context.set_http_user_agent_settings(NULL); |
| 6034 | |
| 6035 | struct { |
| 6036 | const char* request; |
| 6037 | const char* expected_response; |
| 6038 | } tests[] = { { "echoheader?Accept-Language", "None" }, |
| 6039 | { "echoheader?Accept-Charset", "None" }, |
| 6040 | { "echoheader?User-Agent", "" } }; |
| 6041 | |
| 6042 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { |
| 6043 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6044 | URLRequest req( |
| 6045 | test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, &context); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6046 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6047 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6048 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 6049 | << " Request = \"" << tests[i].request << "\""; |
| 6050 | } |
| 6051 | } |
| 6052 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6053 | // Make sure that URLRequest passes on its priority updates to |
| 6054 | // newly-created jobs after the first one. |
| 6055 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
| 6056 | ASSERT_TRUE(test_server_.Start()); |
| 6057 | |
| 6058 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6059 | URLRequest req(test_server_.GetURL("empty.html"), |
| 6060 | DEFAULT_PRIORITY, |
| 6061 | &d, |
| 6062 | &default_context_); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6063 | EXPECT_EQ(DEFAULT_PRIORITY, req.priority()); |
| 6064 | |
| 6065 | scoped_refptr<URLRequestRedirectJob> redirect_job = |
| 6066 | new URLRequestRedirectJob( |
| 6067 | &req, &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6068 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6069 | AddTestInterceptor()->set_main_intercept_job(redirect_job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6070 | |
| 6071 | req.SetPriority(LOW); |
| 6072 | req.Start(); |
| 6073 | EXPECT_TRUE(req.is_pending()); |
| 6074 | |
| 6075 | scoped_refptr<URLRequestTestJob> job = |
| 6076 | new URLRequestTestJob(&req, &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6077 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6078 | |
| 6079 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6080 | base::RunLoop().Run(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6081 | EXPECT_EQ(LOW, job->priority()); |
| 6082 | } |
| 6083 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6084 | // Check that creating a network request while entering/exiting suspend mode |
| 6085 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 6086 | // does not return an HttpTransaction. |
| 6087 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 6088 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6089 | HttpNetworkSession::Params params; |
| 6090 | params.host_resolver = default_context_.host_resolver(); |
| 6091 | params.cert_verifier = default_context_.cert_verifier(); |
| 6092 | params.transport_security_state = default_context_.transport_security_state(); |
| 6093 | params.proxy_service = default_context_.proxy_service(); |
| 6094 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6095 | params.http_auth_handler_factory = |
| 6096 | default_context_.http_auth_handler_factory(); |
| 6097 | params.network_delegate = &default_network_delegate_; |
| 6098 | params.http_server_properties = default_context_.http_server_properties(); |
| 6099 | scoped_ptr<HttpNetworkLayer> network_layer( |
| 6100 | new HttpNetworkLayer(new HttpNetworkSession(params))); |
| 6101 | network_layer->OnSuspend(); |
| 6102 | |
| 6103 | HttpCache http_cache(network_layer.release(), default_context_.net_log(), |
| 6104 | HttpCache::DefaultBackend::InMemory(0)); |
| 6105 | |
| 6106 | TestURLRequestContext context(true); |
| 6107 | context.set_http_transaction_factory(&http_cache); |
| 6108 | context.Init(); |
| 6109 | |
| 6110 | TestDelegate d; |
| 6111 | URLRequest req(GURL("http://127.0.0.1/"), |
| 6112 | DEFAULT_PRIORITY, |
| 6113 | &d, |
| 6114 | &context); |
| 6115 | req.Start(); |
| 6116 | base::RunLoop().Run(); |
| 6117 | |
| 6118 | EXPECT_TRUE(d.request_failed()); |
| 6119 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 6120 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req.status().error()); |
| 6121 | } |
| 6122 | |
| 6123 | // Check that creating a network request while entering/exiting suspend mode |
| 6124 | // fails as it should in the case there is no cache. This is the only case |
| 6125 | // where an HttpTransactionFactory does not return an HttpTransaction. |
| 6126 | TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) { |
| 6127 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6128 | HttpNetworkSession::Params params; |
| 6129 | params.host_resolver = default_context_.host_resolver(); |
| 6130 | params.cert_verifier = default_context_.cert_verifier(); |
| 6131 | params.transport_security_state = default_context_.transport_security_state(); |
| 6132 | params.proxy_service = default_context_.proxy_service(); |
| 6133 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6134 | params.http_auth_handler_factory = |
| 6135 | default_context_.http_auth_handler_factory(); |
| 6136 | params.network_delegate = &default_network_delegate_; |
| 6137 | params.http_server_properties = default_context_.http_server_properties(); |
| 6138 | HttpNetworkLayer network_layer(new HttpNetworkSession(params)); |
| 6139 | network_layer.OnSuspend(); |
| 6140 | |
| 6141 | TestURLRequestContext context(true); |
| 6142 | context.set_http_transaction_factory(&network_layer); |
| 6143 | context.Init(); |
| 6144 | |
| 6145 | TestDelegate d; |
| 6146 | URLRequest req(GURL("http://127.0.0.1/"), |
| 6147 | DEFAULT_PRIORITY, |
| 6148 | &d, |
| 6149 | &context); |
| 6150 | req.Start(); |
| 6151 | base::RunLoop().Run(); |
| 6152 | |
| 6153 | EXPECT_TRUE(d.request_failed()); |
| 6154 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 6155 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req.status().error()); |
| 6156 | } |
| 6157 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 6158 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 6159 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6160 | HTTPSRequestTest() : default_context_(true) { |
| 6161 | default_context_.set_network_delegate(&default_network_delegate_); |
| 6162 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 6163 | } |
| 6164 | virtual ~HTTPSRequestTest() {} |
| 6165 | |
| 6166 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6167 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6168 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6169 | }; |
| 6170 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 6171 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6172 | SpawnedTestServer test_server( |
| 6173 | SpawnedTestServer::TYPE_HTTPS, |
| 6174 | SpawnedTestServer::kLocalhost, |
| 6175 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6176 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6177 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6178 | TestDelegate d; |
| 6179 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6180 | URLRequest r(test_server.GetURL(std::string()), |
| 6181 | DEFAULT_PRIORITY, |
| 6182 | &d, |
| 6183 | &default_context_); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6184 | r.Start(); |
| 6185 | EXPECT_TRUE(r.is_pending()); |
| 6186 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6187 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6188 | |
| 6189 | EXPECT_EQ(1, d.response_started_count()); |
| 6190 | EXPECT_FALSE(d.received_data_before_response()); |
| 6191 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6192 | CheckSSLInfo(r.ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 6193 | EXPECT_EQ(test_server.host_port_pair().host(), |
| 6194 | r.GetSocketAddress().host()); |
| 6195 | EXPECT_EQ(test_server.host_port_pair().port(), |
| 6196 | r.GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6197 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6198 | } |
| 6199 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 6200 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6201 | SpawnedTestServer::SSLOptions ssl_options( |
| 6202 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6203 | SpawnedTestServer test_server( |
| 6204 | SpawnedTestServer::TYPE_HTTPS, |
| 6205 | ssl_options, |
| 6206 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6207 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6208 | |
| 6209 | bool err_allowed = true; |
| 6210 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 6211 | TestDelegate d; |
| 6212 | { |
| 6213 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6214 | URLRequest r(test_server.GetURL(std::string()), |
| 6215 | DEFAULT_PRIORITY, |
| 6216 | &d, |
| 6217 | &default_context_); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6218 | |
| 6219 | r.Start(); |
| 6220 | EXPECT_TRUE(r.is_pending()); |
| 6221 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6222 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6223 | |
| 6224 | EXPECT_EQ(1, d.response_started_count()); |
| 6225 | EXPECT_FALSE(d.received_data_before_response()); |
| 6226 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6227 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6228 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6229 | CheckSSLInfo(r.ssl_info()); |
| 6230 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6231 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6232 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6233 | } |
| 6234 | } |
| 6235 | } |
| 6236 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 6237 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6238 | SpawnedTestServer::SSLOptions ssl_options( |
| 6239 | SpawnedTestServer::SSLOptions::CERT_EXPIRED); |
| 6240 | SpawnedTestServer test_server( |
| 6241 | SpawnedTestServer::TYPE_HTTPS, |
| 6242 | ssl_options, |
| 6243 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6244 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6245 | |
| 6246 | // Iterate from false to true, just so that we do the opposite of the |
| 6247 | // previous test in order to increase test coverage. |
| 6248 | bool err_allowed = false; |
| 6249 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 6250 | TestDelegate d; |
| 6251 | { |
| 6252 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6253 | URLRequest r(test_server.GetURL(std::string()), |
| 6254 | DEFAULT_PRIORITY, |
| 6255 | &d, |
| 6256 | &default_context_); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6257 | |
| 6258 | r.Start(); |
| 6259 | EXPECT_TRUE(r.is_pending()); |
| 6260 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6261 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6262 | |
| 6263 | EXPECT_EQ(1, d.response_started_count()); |
| 6264 | EXPECT_FALSE(d.received_data_before_response()); |
| 6265 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6266 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6267 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6268 | CheckSSLInfo(r.ssl_info()); |
| 6269 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6270 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6271 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6272 | } |
| 6273 | } |
| 6274 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 6275 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6276 | // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more |
| 6277 | // than necessary. |
| 6278 | TEST_F(HTTPSRequestTest, TLSv1Fallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6279 | // The OpenSSL library in use may not support TLS 1.1. |
| 6280 | #if !defined(USE_OPENSSL) |
[email protected] | c091d360 | 2014-03-24 02:32:48 | [diff] [blame] | 6281 | EXPECT_GT(kDefaultSSLVersionMax, SSL_PROTOCOL_VERSION_TLS1); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6282 | #endif |
[email protected] | c091d360 | 2014-03-24 02:32:48 | [diff] [blame] | 6283 | if (kDefaultSSLVersionMax <= SSL_PROTOCOL_VERSION_TLS1) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6284 | return; |
| 6285 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6286 | SpawnedTestServer::SSLOptions ssl_options( |
| 6287 | SpawnedTestServer::SSLOptions::CERT_OK); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6288 | ssl_options.tls_intolerant = |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6289 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 6290 | SpawnedTestServer test_server( |
| 6291 | SpawnedTestServer::TYPE_HTTPS, |
| 6292 | ssl_options, |
| 6293 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6294 | ASSERT_TRUE(test_server.Start()); |
| 6295 | |
| 6296 | TestDelegate d; |
| 6297 | TestURLRequestContext context(true); |
| 6298 | context.Init(); |
| 6299 | d.set_allow_certificate_errors(true); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6300 | URLRequest r( |
| 6301 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6302 | r.Start(); |
| 6303 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6304 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6305 | |
| 6306 | EXPECT_EQ(1, d.response_started_count()); |
| 6307 | EXPECT_NE(0, d.bytes_received()); |
| 6308 | EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_TLS1), |
| 6309 | SSLConnectionStatusToVersion(r.ssl_info().connection_status)); |
| 6310 | EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK); |
| 6311 | } |
| 6312 | |
[email protected] | 974fbdfb | 2013-12-11 20:16:13 | [diff] [blame] | 6313 | // Tests that we don't fallback with servers that implement TLS_FALLBACK_SCSV. |
| 6314 | #if defined(USE_OPENSSL) |
| 6315 | TEST_F(HTTPSRequestTest, DISABLED_FallbackSCSV) { |
| 6316 | #else |
| 6317 | TEST_F(HTTPSRequestTest, FallbackSCSV) { |
| 6318 | #endif |
| 6319 | SpawnedTestServer::SSLOptions ssl_options( |
| 6320 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 6321 | // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger |
| 6322 | // a version fallback. |
| 6323 | ssl_options.tls_intolerant = |
| 6324 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 6325 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 6326 | // connections are rejected. |
| 6327 | ssl_options.fallback_scsv_enabled = true; |
| 6328 | |
| 6329 | SpawnedTestServer test_server( |
| 6330 | SpawnedTestServer::TYPE_HTTPS, |
| 6331 | ssl_options, |
| 6332 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 6333 | ASSERT_TRUE(test_server.Start()); |
| 6334 | |
| 6335 | TestDelegate d; |
| 6336 | TestURLRequestContext context(true); |
| 6337 | context.Init(); |
| 6338 | d.set_allow_certificate_errors(true); |
| 6339 | URLRequest r( |
| 6340 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); |
| 6341 | r.Start(); |
| 6342 | |
| 6343 | base::RunLoop().Run(); |
| 6344 | |
| 6345 | EXPECT_EQ(1, d.response_started_count()); |
| 6346 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version |
| 6347 | // intolerance. If the fallback SCSV is processed when the original error |
| 6348 | // that caused the fallback should be returned, which should be |
| 6349 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH. |
| 6350 | EXPECT_EQ(ERR_SSL_VERSION_OR_CIPHER_MISMATCH, r.status().error()); |
| 6351 | } |
| 6352 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6353 | // This tests that a load of www.google.com with a certificate error sets |
| 6354 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 6355 | // the interstitial to be fatal. |
| 6356 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6357 | SpawnedTestServer::SSLOptions ssl_options( |
| 6358 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6359 | SpawnedTestServer test_server( |
| 6360 | SpawnedTestServer::TYPE_HTTPS, |
| 6361 | ssl_options, |
| 6362 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6363 | ASSERT_TRUE(test_server.Start()); |
| 6364 | |
| 6365 | // We require that the URL be www.google.com in order to pick up the |
| 6366 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 6367 | // have to use a MockHostResolver in order to direct www.google.com to the |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6368 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6369 | |
| 6370 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6371 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6372 | TestURLRequestContext context(true); |
| 6373 | context.set_network_delegate(&network_delegate); |
| 6374 | context.set_host_resolver(&host_resolver); |
| 6375 | TransportSecurityState transport_security_state; |
| 6376 | context.set_transport_security_state(&transport_security_state); |
| 6377 | context.Init(); |
| 6378 | |
| 6379 | TestDelegate d; |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 6380 | URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d", |
| 6381 | test_server.host_port_pair().port())), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6382 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6383 | &d, |
| 6384 | &context); |
| 6385 | |
| 6386 | r.Start(); |
| 6387 | EXPECT_TRUE(r.is_pending()); |
| 6388 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6389 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6390 | |
| 6391 | EXPECT_EQ(1, d.response_started_count()); |
| 6392 | EXPECT_FALSE(d.received_data_before_response()); |
| 6393 | EXPECT_TRUE(d.have_certificate_errors()); |
| 6394 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 6395 | } |
| 6396 | |
| 6397 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 6398 | // TransportSecurityState. |
| 6399 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 6400 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 6401 | // matter. It just has to be any error. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6402 | SpawnedTestServer::SSLOptions ssl_options( |
| 6403 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6404 | SpawnedTestServer test_server( |
| 6405 | SpawnedTestServer::TYPE_HTTPS, |
| 6406 | ssl_options, |
| 6407 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6408 | ASSERT_TRUE(test_server.Start()); |
| 6409 | |
| 6410 | // We require that the URL be www.google.com in order to pick up the |
| 6411 | // preloaded and dynamic HSTS and public key pin entries in the |
| 6412 | // TransportSecurityState. This means that we have to use a |
| 6413 | // MockHostResolver in order to direct www.google.com to the testserver. |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6414 | // By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6415 | |
| 6416 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6417 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6418 | TestURLRequestContext context(true); |
| 6419 | context.set_network_delegate(&network_delegate); |
| 6420 | context.set_host_resolver(&host_resolver); |
| 6421 | TransportSecurityState transport_security_state; |
| 6422 | TransportSecurityState::DomainState domain_state; |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 6423 | EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true, |
| 6424 | &domain_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6425 | context.set_transport_security_state(&transport_security_state); |
| 6426 | context.Init(); |
| 6427 | |
| 6428 | TestDelegate d; |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 6429 | URLRequest r(GURL(base::StringPrintf("https://www.google.com:%d", |
| 6430 | test_server.host_port_pair().port())), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6431 | DEFAULT_PRIORITY, |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6432 | &d, |
| 6433 | &context); |
| 6434 | |
| 6435 | r.Start(); |
| 6436 | EXPECT_TRUE(r.is_pending()); |
| 6437 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6438 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6439 | |
| 6440 | EXPECT_EQ(1, d.response_started_count()); |
| 6441 | EXPECT_FALSE(d.received_data_before_response()); |
| 6442 | EXPECT_TRUE(d.have_certificate_errors()); |
| 6443 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 6444 | |
| 6445 | // Get a fresh copy of the state, and check that it hasn't been updated. |
| 6446 | TransportSecurityState::DomainState new_domain_state; |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 6447 | EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true, |
| 6448 | &new_domain_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6449 | EXPECT_EQ(new_domain_state.upgrade_mode, domain_state.upgrade_mode); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 6450 | EXPECT_EQ(new_domain_state.sts_include_subdomains, |
| 6451 | domain_state.sts_include_subdomains); |
| 6452 | EXPECT_EQ(new_domain_state.pkp_include_subdomains, |
| 6453 | domain_state.pkp_include_subdomains); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6454 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.static_spki_hashes, |
| 6455 | domain_state.static_spki_hashes)); |
| 6456 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes, |
| 6457 | domain_state.dynamic_spki_hashes)); |
| 6458 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_static_spki_hashes, |
| 6459 | domain_state.bad_static_spki_hashes)); |
| 6460 | } |
| 6461 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6462 | // Make sure HSTS preserves a POST request's method and body. |
| 6463 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 6464 | static const char kData[] = "hello world"; |
| 6465 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6466 | SpawnedTestServer::SSLOptions ssl_options( |
| 6467 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 6468 | SpawnedTestServer test_server( |
| 6469 | SpawnedTestServer::TYPE_HTTPS, |
| 6470 | ssl_options, |
| 6471 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6472 | ASSERT_TRUE(test_server.Start()); |
| 6473 | |
| 6474 | |
| 6475 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 6476 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6477 | // the SpawnedTestServer. By default, MockHostResolver maps all hosts |
| 6478 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6479 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6480 | |
| 6481 | // Force https for www.somewhere.com. |
| 6482 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 6483 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 6484 | bool include_subdomains = false; |
| 6485 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 6486 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6487 | |
| 6488 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 6489 | |
| 6490 | TestURLRequestContext context(true); |
| 6491 | context.set_host_resolver(&host_resolver); |
| 6492 | context.set_transport_security_state(&transport_security_state); |
| 6493 | context.set_network_delegate(&network_delegate); |
| 6494 | context.Init(); |
| 6495 | |
| 6496 | TestDelegate d; |
| 6497 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 6498 | // cause a certificate error. Ignore the error. |
| 6499 | d.set_allow_certificate_errors(true); |
| 6500 | |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 6501 | URLRequest req(GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
| 6502 | test_server.host_port_pair().port())), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6503 | DEFAULT_PRIORITY, |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6504 | &d, |
| 6505 | &context); |
| 6506 | req.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 6507 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6508 | |
| 6509 | req.Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6510 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6511 | |
| 6512 | EXPECT_EQ("https", req.url().scheme()); |
| 6513 | EXPECT_EQ("POST", req.method()); |
| 6514 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 6515 | |
| 6516 | LoadTimingInfo load_timing_info; |
| 6517 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 6518 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 6519 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6520 | } |
| 6521 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6522 | TEST_F(HTTPSRequestTest, SSLv3Fallback) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6523 | SpawnedTestServer::SSLOptions ssl_options( |
| 6524 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 6525 | ssl_options.tls_intolerant = |
| 6526 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 6527 | SpawnedTestServer test_server( |
| 6528 | SpawnedTestServer::TYPE_HTTPS, |
| 6529 | ssl_options, |
| 6530 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6531 | ASSERT_TRUE(test_server.Start()); |
| 6532 | |
| 6533 | TestDelegate d; |
| 6534 | TestURLRequestContext context(true); |
| 6535 | context.Init(); |
| 6536 | d.set_allow_certificate_errors(true); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6537 | URLRequest r( |
| 6538 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6539 | r.Start(); |
| 6540 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6541 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6542 | |
| 6543 | EXPECT_EQ(1, d.response_started_count()); |
| 6544 | EXPECT_NE(0, d.bytes_received()); |
| 6545 | EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_SSL3), |
| 6546 | SSLConnectionStatusToVersion(r.ssl_info().connection_status)); |
| 6547 | EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK); |
| 6548 | } |
| 6549 | |
| 6550 | namespace { |
| 6551 | |
| 6552 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 6553 | public: |
| 6554 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 6555 | } |
| 6556 | virtual void OnCertificateRequested( |
| 6557 | URLRequest* request, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 6558 | SSLCertRequestInfo* cert_request_info) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6559 | on_certificate_requested_count_++; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6560 | base::MessageLoop::current()->Quit(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6561 | } |
| 6562 | int on_certificate_requested_count() { |
| 6563 | return on_certificate_requested_count_; |
| 6564 | } |
| 6565 | private: |
| 6566 | int on_certificate_requested_count_; |
| 6567 | }; |
| 6568 | |
| 6569 | } // namespace |
| 6570 | |
| 6571 | // TODO(davidben): Test the rest of the code. Specifically, |
| 6572 | // - Filtering which certificates to select. |
| 6573 | // - Sending a certificate back. |
| 6574 | // - Getting a certificate request in an SSL renegotiation sending the |
| 6575 | // HTTP request. |
| 6576 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6577 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6578 | ssl_options.request_client_certificate = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6579 | SpawnedTestServer test_server( |
| 6580 | SpawnedTestServer::TYPE_HTTPS, |
| 6581 | ssl_options, |
| 6582 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6583 | ASSERT_TRUE(test_server.Start()); |
| 6584 | |
| 6585 | SSLClientAuthTestDelegate d; |
| 6586 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6587 | URLRequest r(test_server.GetURL(std::string()), |
| 6588 | DEFAULT_PRIORITY, |
| 6589 | &d, |
| 6590 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6591 | |
| 6592 | r.Start(); |
| 6593 | EXPECT_TRUE(r.is_pending()); |
| 6594 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6595 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6596 | |
| 6597 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 6598 | EXPECT_FALSE(d.received_data_before_response()); |
| 6599 | EXPECT_EQ(0, d.bytes_received()); |
| 6600 | |
| 6601 | // Send no certificate. |
| 6602 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 6603 | // all platforms so we can test sending a cert as well. |
| 6604 | r.ContinueWithCertificate(NULL); |
| 6605 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6606 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6607 | |
| 6608 | EXPECT_EQ(1, d.response_started_count()); |
| 6609 | EXPECT_FALSE(d.received_data_before_response()); |
| 6610 | EXPECT_NE(0, d.bytes_received()); |
| 6611 | } |
| 6612 | } |
| 6613 | |
| 6614 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 6615 | // Test that we attempt a session resume when making two connections to the |
| 6616 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6617 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6618 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6619 | SpawnedTestServer test_server( |
| 6620 | SpawnedTestServer::TYPE_HTTPS, |
| 6621 | ssl_options, |
| 6622 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6623 | ASSERT_TRUE(test_server.Start()); |
| 6624 | |
| 6625 | SSLClientSocket::ClearSessionCache(); |
| 6626 | |
| 6627 | { |
| 6628 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6629 | URLRequest r(test_server.GetURL("ssl-session-cache"), |
| 6630 | DEFAULT_PRIORITY, |
| 6631 | &d, |
| 6632 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6633 | |
| 6634 | r.Start(); |
| 6635 | EXPECT_TRUE(r.is_pending()); |
| 6636 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6637 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6638 | |
| 6639 | EXPECT_EQ(1, d.response_started_count()); |
| 6640 | } |
| 6641 | |
| 6642 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 6643 | CloseAllConnections(); |
| 6644 | |
| 6645 | { |
| 6646 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6647 | URLRequest r(test_server.GetURL("ssl-session-cache"), |
| 6648 | DEFAULT_PRIORITY, |
| 6649 | &d, |
| 6650 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6651 | |
| 6652 | r.Start(); |
| 6653 | EXPECT_TRUE(r.is_pending()); |
| 6654 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6655 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6656 | |
| 6657 | // The response will look like; |
| 6658 | // insert abc |
| 6659 | // lookup abc |
| 6660 | // insert xyz |
| 6661 | // |
| 6662 | // With a newline at the end which makes the split think that there are |
| 6663 | // four lines. |
| 6664 | |
| 6665 | EXPECT_EQ(1, d.response_started_count()); |
| 6666 | std::vector<std::string> lines; |
| 6667 | base::SplitString(d.data_received(), '\n', &lines); |
| 6668 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 6669 | |
| 6670 | std::string session_id; |
| 6671 | |
| 6672 | for (size_t i = 0; i < 2; i++) { |
| 6673 | std::vector<std::string> parts; |
| 6674 | base::SplitString(lines[i], '\t', &parts); |
| 6675 | ASSERT_EQ(2u, parts.size()); |
| 6676 | if (i == 0) { |
| 6677 | EXPECT_EQ("insert", parts[0]); |
| 6678 | session_id = parts[1]; |
| 6679 | } else { |
| 6680 | EXPECT_EQ("lookup", parts[0]); |
| 6681 | EXPECT_EQ(session_id, parts[1]); |
| 6682 | } |
| 6683 | } |
| 6684 | } |
| 6685 | } |
| 6686 | |
| 6687 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 6688 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 6689 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6690 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6691 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6692 | SpawnedTestServer test_server( |
| 6693 | SpawnedTestServer::TYPE_HTTPS, |
| 6694 | ssl_options, |
| 6695 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6696 | ASSERT_TRUE(test_server.Start()); |
| 6697 | |
| 6698 | SSLClientSocket::ClearSessionCache(); |
| 6699 | |
| 6700 | { |
| 6701 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6702 | URLRequest r(test_server.GetURL("ssl-session-cache"), |
| 6703 | DEFAULT_PRIORITY, |
| 6704 | &d, |
| 6705 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6706 | |
| 6707 | r.Start(); |
| 6708 | EXPECT_TRUE(r.is_pending()); |
| 6709 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6710 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6711 | |
| 6712 | EXPECT_EQ(1, d.response_started_count()); |
| 6713 | } |
| 6714 | |
| 6715 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 6716 | HttpNetworkSession::Params params; |
| 6717 | params.host_resolver = default_context_.host_resolver(); |
| 6718 | params.cert_verifier = default_context_.cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 6719 | params.transport_security_state = default_context_.transport_security_state(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6720 | params.proxy_service = default_context_.proxy_service(); |
| 6721 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6722 | params.http_auth_handler_factory = |
| 6723 | default_context_.http_auth_handler_factory(); |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 6724 | params.network_delegate = &default_network_delegate_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6725 | params.http_server_properties = default_context_.http_server_properties(); |
| 6726 | params.ssl_session_cache_shard = "alternate"; |
| 6727 | |
| 6728 | scoped_ptr<net::HttpCache> cache(new net::HttpCache( |
| 6729 | new net::HttpNetworkSession(params), |
| 6730 | net::HttpCache::DefaultBackend::InMemory(0))); |
| 6731 | |
| 6732 | default_context_.set_http_transaction_factory(cache.get()); |
| 6733 | |
| 6734 | { |
| 6735 | TestDelegate d; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6736 | URLRequest r(test_server.GetURL("ssl-session-cache"), |
| 6737 | DEFAULT_PRIORITY, |
| 6738 | &d, |
| 6739 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6740 | |
| 6741 | r.Start(); |
| 6742 | EXPECT_TRUE(r.is_pending()); |
| 6743 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6744 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6745 | |
| 6746 | // The response will look like; |
| 6747 | // insert abc |
| 6748 | // insert xyz |
| 6749 | // |
| 6750 | // With a newline at the end which makes the split think that there are |
| 6751 | // three lines. |
| 6752 | |
| 6753 | EXPECT_EQ(1, d.response_started_count()); |
| 6754 | std::vector<std::string> lines; |
| 6755 | base::SplitString(d.data_received(), '\n', &lines); |
| 6756 | ASSERT_EQ(3u, lines.size()); |
| 6757 | |
| 6758 | std::string session_id; |
| 6759 | for (size_t i = 0; i < 2; i++) { |
| 6760 | std::vector<std::string> parts; |
| 6761 | base::SplitString(lines[i], '\t', &parts); |
| 6762 | ASSERT_EQ(2u, parts.size()); |
| 6763 | EXPECT_EQ("insert", parts[0]); |
| 6764 | if (i == 0) { |
| 6765 | session_id = parts[1]; |
| 6766 | } else { |
| 6767 | EXPECT_NE(session_id, parts[1]); |
| 6768 | } |
| 6769 | } |
| 6770 | } |
| 6771 | } |
| 6772 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 6773 | class HTTPSSessionTest : public testing::Test { |
| 6774 | public: |
| 6775 | HTTPSSessionTest() : default_context_(true) { |
| 6776 | cert_verifier_.set_default_result(net::OK); |
| 6777 | |
| 6778 | default_context_.set_network_delegate(&default_network_delegate_); |
| 6779 | default_context_.set_cert_verifier(&cert_verifier_); |
| 6780 | default_context_.Init(); |
| 6781 | } |
| 6782 | virtual ~HTTPSSessionTest() {} |
| 6783 | |
| 6784 | protected: |
| 6785 | MockCertVerifier cert_verifier_; |
| 6786 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 6787 | TestURLRequestContext default_context_; |
| 6788 | }; |
| 6789 | |
| 6790 | // Tests that session resumption is not attempted if an invalid certificate |
| 6791 | // is presented. |
| 6792 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 6793 | SpawnedTestServer::SSLOptions ssl_options; |
| 6794 | ssl_options.record_resume = true; |
| 6795 | SpawnedTestServer test_server( |
| 6796 | SpawnedTestServer::TYPE_HTTPS, |
| 6797 | ssl_options, |
| 6798 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 6799 | ASSERT_TRUE(test_server.Start()); |
| 6800 | |
| 6801 | SSLClientSocket::ClearSessionCache(); |
| 6802 | |
| 6803 | // Simulate the certificate being expired and attempt a connection. |
| 6804 | cert_verifier_.set_default_result(net::ERR_CERT_DATE_INVALID); |
| 6805 | { |
| 6806 | TestDelegate d; |
| 6807 | URLRequest r(test_server.GetURL("ssl-session-cache"), |
| 6808 | DEFAULT_PRIORITY, |
| 6809 | &d, |
| 6810 | &default_context_); |
| 6811 | |
| 6812 | r.Start(); |
| 6813 | EXPECT_TRUE(r.is_pending()); |
| 6814 | |
| 6815 | base::RunLoop().Run(); |
| 6816 | |
| 6817 | EXPECT_EQ(1, d.response_started_count()); |
| 6818 | } |
| 6819 | |
| 6820 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 6821 | CloseAllConnections(); |
| 6822 | |
| 6823 | // Now change the certificate to be acceptable (so that the response is |
| 6824 | // loaded), and ensure that no session id is presented to the peer. |
| 6825 | cert_verifier_.set_default_result(net::OK); |
| 6826 | { |
| 6827 | TestDelegate d; |
| 6828 | URLRequest r(test_server.GetURL("ssl-session-cache"), |
| 6829 | DEFAULT_PRIORITY, |
| 6830 | &d, |
| 6831 | &default_context_); |
| 6832 | |
| 6833 | r.Start(); |
| 6834 | EXPECT_TRUE(r.is_pending()); |
| 6835 | |
| 6836 | base::RunLoop().Run(); |
| 6837 | |
| 6838 | // The response will look like; |
| 6839 | // insert abc |
| 6840 | // insert xyz |
| 6841 | // |
| 6842 | // With a newline at the end which makes the split think that there are |
| 6843 | // three lines. |
| 6844 | // |
| 6845 | // If a session was presented (eg: a bug), then the response would look |
| 6846 | // like; |
| 6847 | // insert abc |
| 6848 | // lookup abc |
| 6849 | // insert xyz |
| 6850 | |
| 6851 | EXPECT_EQ(1, d.response_started_count()); |
| 6852 | std::vector<std::string> lines; |
| 6853 | base::SplitString(d.data_received(), '\n', &lines); |
| 6854 | ASSERT_EQ(3u, lines.size()) << d.data_received(); |
| 6855 | |
| 6856 | std::string session_id; |
| 6857 | for (size_t i = 0; i < 2; i++) { |
| 6858 | std::vector<std::string> parts; |
| 6859 | base::SplitString(lines[i], '\t', &parts); |
| 6860 | ASSERT_EQ(2u, parts.size()); |
| 6861 | EXPECT_EQ("insert", parts[0]); |
| 6862 | if (i == 0) { |
| 6863 | session_id = parts[1]; |
| 6864 | } else { |
| 6865 | EXPECT_NE(session_id, parts[1]); |
| 6866 | } |
| 6867 | } |
| 6868 | } |
| 6869 | } |
| 6870 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6871 | class TestSSLConfigService : public SSLConfigService { |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6872 | public: |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 6873 | TestSSLConfigService(bool ev_enabled, |
| 6874 | bool online_rev_checking, |
| 6875 | bool rev_checking_required_local_anchors) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6876 | : ev_enabled_(ev_enabled), |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 6877 | online_rev_checking_(online_rev_checking), |
| 6878 | rev_checking_required_local_anchors_( |
| 6879 | rev_checking_required_local_anchors) {} |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6880 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 6881 | // SSLConfigService: |
| 6882 | virtual void GetSSLConfig(SSLConfig* config) OVERRIDE { |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6883 | *config = SSLConfig(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6884 | config->rev_checking_enabled = online_rev_checking_; |
| 6885 | config->verify_ev_cert = ev_enabled_; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 6886 | config->rev_checking_required_local_anchors = |
| 6887 | rev_checking_required_local_anchors_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6888 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6889 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 6890 | protected: |
| 6891 | virtual ~TestSSLConfigService() {} |
| 6892 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6893 | private: |
| 6894 | const bool ev_enabled_; |
| 6895 | const bool online_rev_checking_; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 6896 | const bool rev_checking_required_local_anchors_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6897 | }; |
| 6898 | |
| 6899 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 6900 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 6901 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6902 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 6903 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 6904 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 6905 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 6906 | // testserver. |
| 6907 | static const SHA256HashValue kOCSPTestCertSPKI = { { |
| 6908 | 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, |
| 6909 | 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, |
| 6910 | 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, |
| 6911 | 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, |
| 6912 | } }; |
| 6913 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6914 | // This is the policy OID contained in the certificates that testserver |
| 6915 | // generates. |
| 6916 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 6917 | |
| 6918 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 6919 | public: |
| 6920 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6921 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 6922 | ev_test_policy_( |
| 6923 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 6924 | kOCSPTestCertFingerprint, |
| 6925 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6926 | } |
| 6927 | |
| 6928 | virtual void SetUp() OVERRIDE { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6929 | SetupContext(&context_); |
| 6930 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6931 | |
| 6932 | scoped_refptr<net::X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 6933 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6934 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6935 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6936 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 6937 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6938 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6939 | EnsureNSSHttpIOInit(); |
| 6940 | #endif |
| 6941 | } |
| 6942 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6943 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6944 | CertStatus* out_cert_status) { |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 6945 | // We always overwrite out_cert_status. |
| 6946 | *out_cert_status = 0; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6947 | SpawnedTestServer test_server( |
| 6948 | SpawnedTestServer::TYPE_HTTPS, |
| 6949 | ssl_options, |
| 6950 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6951 | ASSERT_TRUE(test_server.Start()); |
| 6952 | |
| 6953 | TestDelegate d; |
| 6954 | d.set_allow_certificate_errors(true); |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 6955 | URLRequest r( |
| 6956 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, &context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6957 | r.Start(); |
| 6958 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6959 | base::RunLoop().Run(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6960 | |
| 6961 | EXPECT_EQ(1, d.response_started_count()); |
| 6962 | *out_cert_status = r.ssl_info().cert_status; |
| 6963 | } |
| 6964 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 6965 | virtual ~HTTPSOCSPTest() { |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 6966 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6967 | ShutdownNSSHttpIO(); |
| 6968 | #endif |
| 6969 | } |
| 6970 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6971 | protected: |
| 6972 | // SetupContext configures the URLRequestContext that will be used for making |
| 6973 | // connetions to testserver. This can be overridden in test subclasses for |
| 6974 | // different behaviour. |
| 6975 | virtual void SetupContext(URLRequestContext* context) { |
| 6976 | context->set_ssl_config_service( |
| 6977 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 6978 | true /* online revocation checking */, |
| 6979 | false /* require rev. checking for local |
| 6980 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6981 | } |
| 6982 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6983 | scoped_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6984 | TestURLRequestContext context_; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 6985 | scoped_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 6986 | }; |
| 6987 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6988 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 6989 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 6990 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 6991 | // have that ability on other platforms. |
| 6992 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 6993 | #else |
| 6994 | return 0; |
| 6995 | #endif |
| 6996 | } |
| 6997 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 6998 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 6999 | // operating system supports revocation checking and can distinguish between |
| 7000 | // situations where a given certificate lacks any revocation information (eg: |
| 7001 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 7002 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 7003 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 7004 | // skipped. |
| 7005 | static bool SystemSupportsHardFailRevocationChecking() { |
| 7006 | #if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS) |
| 7007 | return true; |
| 7008 | #else |
| 7009 | return false; |
| 7010 | #endif |
| 7011 | } |
| 7012 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7013 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 7014 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 7015 | // several tests are effected because our testing EV certificate won't be |
| 7016 | // recognised as EV. |
| 7017 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 7018 | #if defined(USE_OPENSSL) |
| 7019 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 7020 | return false; |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 7021 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7022 | // On OS X, we use the system to tell us whether a certificate is EV or not |
| 7023 | // and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 7024 | return false; |
| 7025 | #else |
| 7026 | return true; |
| 7027 | #endif |
| 7028 | } |
| 7029 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7030 | static bool SystemSupportsOCSP() { |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 7031 | #if defined(USE_OPENSSL) |
| 7032 | // http://crbug.com/117478 - OpenSSL does not support OCSP. |
| 7033 | return false; |
| 7034 | #elif defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7035 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 7036 | #elif defined(OS_ANDROID) |
| 7037 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 7038 | return false; |
| 7039 | #else |
| 7040 | return true; |
| 7041 | #endif |
| 7042 | } |
| 7043 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7044 | TEST_F(HTTPSOCSPTest, Valid) { |
| 7045 | if (!SystemSupportsOCSP()) { |
| 7046 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7047 | return; |
| 7048 | } |
| 7049 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7050 | SpawnedTestServer::SSLOptions ssl_options( |
| 7051 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7052 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7053 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7054 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7055 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7056 | |
| 7057 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7058 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7059 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7060 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7061 | |
| 7062 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7063 | } |
| 7064 | |
| 7065 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 7066 | if (!SystemSupportsOCSP()) { |
| 7067 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7068 | return; |
| 7069 | } |
| 7070 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7071 | SpawnedTestServer::SSLOptions ssl_options( |
| 7072 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7073 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7074 | |
| 7075 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7076 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7077 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 7078 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7079 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 7080 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7081 | #endif |
| 7082 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7083 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7084 | } |
| 7085 | |
| 7086 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 7087 | if (!SystemSupportsOCSP()) { |
| 7088 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7089 | return; |
| 7090 | } |
| 7091 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7092 | SpawnedTestServer::SSLOptions ssl_options( |
| 7093 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7094 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7095 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7096 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7097 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7098 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7099 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7100 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7101 | |
| 7102 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 7103 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7104 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7105 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7106 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7107 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 7108 | protected: |
| 7109 | virtual void SetupContext(URLRequestContext* context) OVERRIDE { |
| 7110 | context->set_ssl_config_service( |
| 7111 | new TestSSLConfigService(false /* check for EV */, |
| 7112 | false /* online revocation checking */, |
| 7113 | true /* require rev. checking for local |
| 7114 | anchors */)); |
| 7115 | } |
| 7116 | }; |
| 7117 | |
| 7118 | |
| 7119 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 7120 | if (!SystemSupportsOCSP()) { |
| 7121 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7122 | return; |
| 7123 | } |
| 7124 | |
| 7125 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 7126 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 7127 | << "revocation checking"; |
| 7128 | return; |
| 7129 | } |
| 7130 | |
| 7131 | SpawnedTestServer::SSLOptions ssl_options( |
| 7132 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7133 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 7134 | |
| 7135 | CertStatus cert_status; |
| 7136 | DoConnection(ssl_options, &cert_status); |
| 7137 | |
| 7138 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 7139 | cert_status & CERT_STATUS_REVOKED); |
| 7140 | |
| 7141 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 7142 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7143 | } |
| 7144 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7145 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 7146 | protected: |
| 7147 | virtual void SetupContext(URLRequestContext* context) OVERRIDE { |
| 7148 | context->set_ssl_config_service( |
| 7149 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7150 | false /* online revocation checking */, |
| 7151 | false /* require rev. checking for local |
| 7152 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7153 | } |
| 7154 | }; |
| 7155 | |
| 7156 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 7157 | if (!SystemSupportsOCSP()) { |
| 7158 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7159 | return; |
| 7160 | } |
| 7161 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7162 | SpawnedTestServer::SSLOptions ssl_options( |
| 7163 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7164 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7165 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7166 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7167 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7168 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7169 | |
| 7170 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7171 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 7172 | |
| 7173 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7174 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7175 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7176 | } |
| 7177 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 7178 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 7179 | if (!SystemSupportsOCSP()) { |
| 7180 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7181 | return; |
| 7182 | } |
| 7183 | |
| 7184 | SpawnedTestServer::SSLOptions ssl_options( |
| 7185 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7186 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 7187 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7188 | |
| 7189 | CertStatus cert_status; |
| 7190 | DoConnection(ssl_options, &cert_status); |
| 7191 | |
| 7192 | // Currently only works for Windows. When using NSS or OS X, it's not |
| 7193 | // possible to determine whether the check failed because of actual |
| 7194 | // revocation or because there was an OCSP failure. |
| 7195 | #if defined(OS_WIN) |
| 7196 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7197 | #else |
| 7198 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7199 | #endif |
| 7200 | |
| 7201 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7202 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7203 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7204 | } |
| 7205 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7206 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 7207 | if (!SystemSupportsOCSP()) { |
| 7208 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7209 | return; |
| 7210 | } |
| 7211 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7212 | SpawnedTestServer::SSLOptions ssl_options( |
| 7213 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7214 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7215 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7216 | |
| 7217 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7218 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7219 | |
| 7220 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7221 | |
| 7222 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7223 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7224 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7225 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7226 | } |
| 7227 | |
| 7228 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 7229 | if (!SystemSupportsOCSP()) { |
| 7230 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7231 | return; |
| 7232 | } |
| 7233 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7234 | SpawnedTestServer::SSLOptions ssl_options( |
| 7235 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7236 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7237 | SSLConfigService::SetCRLSet( |
| 7238 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7239 | |
| 7240 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7241 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7242 | |
| 7243 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7244 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 7245 | |
| 7246 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7247 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7248 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7249 | } |
| 7250 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7251 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 7252 | if (!SystemSupportsOCSP()) { |
| 7253 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7254 | return; |
| 7255 | } |
| 7256 | |
| 7257 | SpawnedTestServer::SSLOptions ssl_options( |
| 7258 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7259 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 7260 | SSLConfigService::SetCRLSet( |
| 7261 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 7262 | false, &kOCSPTestCertSPKI, ""))); |
| 7263 | |
| 7264 | CertStatus cert_status; |
| 7265 | DoConnection(ssl_options, &cert_status); |
| 7266 | |
| 7267 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 7268 | // revocation check for EV. |
| 7269 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7270 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7271 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 7272 | EXPECT_FALSE( |
| 7273 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7274 | } |
| 7275 | |
| 7276 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 7277 | if (!SystemSupportsOCSP()) { |
| 7278 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7279 | return; |
| 7280 | } |
| 7281 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7282 | SpawnedTestServer::SSLOptions ssl_options( |
| 7283 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7284 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7285 | SSLConfigService::SetCRLSet( |
| 7286 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 7287 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7288 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7289 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7290 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7291 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 7292 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 7293 | // test. |
| 7294 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7295 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7296 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7297 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7298 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7299 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7300 | } |
| 7301 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7302 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 7303 | // Test that when EV verification is requested, but online revocation |
| 7304 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 7305 | // no revocation checking actually happens. |
| 7306 | if (!SystemSupportsOCSP()) { |
| 7307 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7308 | return; |
| 7309 | } |
| 7310 | |
| 7311 | // Unmark the certificate's OID as EV, which should disable revocation |
| 7312 | // checking (as per the user preference) |
| 7313 | ev_test_policy_.reset(); |
| 7314 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7315 | SpawnedTestServer::SSLOptions ssl_options( |
| 7316 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7317 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7318 | SSLConfigService::SetCRLSet( |
| 7319 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7320 | |
| 7321 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7322 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7323 | |
| 7324 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7325 | |
| 7326 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7327 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7328 | } |
| 7329 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7330 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 7331 | protected: |
| 7332 | virtual void SetupContext(URLRequestContext* context) OVERRIDE { |
| 7333 | context->set_ssl_config_service( |
| 7334 | new TestSSLConfigService(false /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7335 | false /* online revocation checking */, |
| 7336 | false /* require rev. checking for local |
| 7337 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7338 | } |
| 7339 | }; |
| 7340 | |
| 7341 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7342 | SpawnedTestServer::SSLOptions ssl_options( |
| 7343 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7344 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7345 | SSLConfigService::SetCRLSet( |
| 7346 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7347 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7348 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7349 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7350 | |
| 7351 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 7352 | // we don't fall back to online revocation checks. |
| 7353 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7354 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7355 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7356 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7357 | |
| 7358 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
| 7359 | #if defined(USE_OPENSSL) |
| 7360 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 7361 | return; |
| 7362 | #endif |
| 7363 | |
| 7364 | SpawnedTestServer::SSLOptions ssl_options( |
| 7365 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7366 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 7367 | ssl_options.cert_serial = 10; |
| 7368 | SSLConfigService::SetCRLSet( |
| 7369 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 7370 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 7371 | |
| 7372 | CertStatus cert_status = 0; |
| 7373 | DoConnection(ssl_options, &cert_status); |
| 7374 | |
| 7375 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 7376 | // reflected without online revocation checking. |
| 7377 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7378 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7379 | EXPECT_FALSE( |
| 7380 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7381 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7382 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7383 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7384 | #if !defined(DISABLE_FTP_SUPPORT) |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7385 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7386 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 7387 | URLRequestTestFTP() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7388 | : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7389 | base::FilePath()) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7390 | } |
| 7391 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7392 | protected: |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7393 | SpawnedTestServer test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7394 | }; |
| 7395 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7396 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 7397 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7398 | ASSERT_TRUE(test_server_.Start()); |
| 7399 | |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 7400 | URLRequestJobFactoryImpl job_factory; |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7401 | FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7402 | |
| 7403 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7404 | job_factory.SetProtocolHandler( |
| 7405 | "ftp", |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7406 | new FtpProtocolHandler(&ftp_transaction_factory)); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7407 | default_context_.set_job_factory(&job_factory); |
| 7408 | |
| 7409 | TestDelegate d; |
| 7410 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7411 | URLRequest r(url, DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7412 | r.Start(); |
| 7413 | EXPECT_TRUE(r.is_pending()); |
| 7414 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7415 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7416 | |
| 7417 | EXPECT_FALSE(r.is_pending()); |
| 7418 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
| 7419 | EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error()); |
| 7420 | } |
| 7421 | } |
| 7422 | |
[email protected] | 5accf733 | 2009-11-24 03:41:38 | [diff] [blame] | 7423 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7424 | TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7425 | ASSERT_TRUE(test_server_.Start()); |
| 7426 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7427 | TestDelegate d; |
| 7428 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7429 | URLRequest r( |
| 7430 | test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, &default_context_); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7431 | r.Start(); |
| 7432 | EXPECT_TRUE(r.is_pending()); |
| 7433 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7434 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7435 | |
| 7436 | EXPECT_FALSE(r.is_pending()); |
| 7437 | EXPECT_EQ(1, d.response_started_count()); |
| 7438 | EXPECT_FALSE(d.received_data_before_response()); |
| 7439 | EXPECT_LT(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7440 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 7441 | r.GetSocketAddress().host()); |
| 7442 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 7443 | r.GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7444 | } |
| 7445 | } |
| 7446 | |
[email protected] | 7df7001 | 2010-02-04 00:09:55 | [diff] [blame] | 7447 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7448 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7449 | ASSERT_TRUE(test_server_.Start()); |
| 7450 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7451 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7452 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 7453 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7454 | TestDelegate d; |
| 7455 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7456 | URLRequest r(test_server_.GetURL("/LICENSE"), |
| 7457 | DEFAULT_PRIORITY, |
| 7458 | &d, |
| 7459 | &default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7460 | r.Start(); |
| 7461 | EXPECT_TRUE(r.is_pending()); |
| 7462 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7463 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7464 | |
| 7465 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7466 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7467 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 7468 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7469 | EXPECT_EQ(1, d.response_started_count()); |
| 7470 | EXPECT_FALSE(d.received_data_before_response()); |
| 7471 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7472 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 7473 | r.GetSocketAddress().host()); |
| 7474 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 7475 | r.GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7476 | } |
| 7477 | } |
| 7478 | |
[email protected] | e9ecbd1 | 2009-12-20 18:44:40 | [diff] [blame] | 7479 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7480 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7481 | ASSERT_TRUE(test_server_.Start()); |
| 7482 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7483 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7484 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 7485 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7486 | TestDelegate d; |
| 7487 | { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7488 | URLRequest r( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7489 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7490 | DEFAULT_PRIORITY, |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 7491 | &d, |
| 7492 | &default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7493 | r.Start(); |
| 7494 | EXPECT_TRUE(r.is_pending()); |
| 7495 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7496 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7497 | |
| 7498 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7499 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7500 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 7501 | EXPECT_FALSE(r.is_pending()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7502 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 7503 | r.GetSocketAddress().host()); |
| 7504 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 7505 | r.GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7506 | EXPECT_EQ(1, d.response_started_count()); |
| 7507 | EXPECT_FALSE(d.received_data_before_response()); |
| 7508 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7509 | |
| 7510 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 7511 | r.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7512 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7513 | } |
| 7514 | } |
| 7515 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 7516 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7517 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7518 | ASSERT_TRUE(test_server_.Start()); |
| 7519 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7520 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7521 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 7522 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7523 | TestDelegate d; |
| 7524 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7525 | URLRequest r(test_server_.GetURLWithUserAndPassword( |
| 7526 | "/LICENSE", "chrome", "wrong_password"), |
| 7527 | DEFAULT_PRIORITY, |
| 7528 | &d, |
| 7529 | &default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7530 | r.Start(); |
| 7531 | EXPECT_TRUE(r.is_pending()); |
| 7532 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7533 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7534 | |
| 7535 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7536 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7537 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 7538 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7539 | EXPECT_EQ(1, d.response_started_count()); |
| 7540 | EXPECT_FALSE(d.received_data_before_response()); |
| 7541 | EXPECT_EQ(d.bytes_received(), 0); |
| 7542 | } |
| 7543 | } |
| 7544 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 7545 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7546 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7547 | ASSERT_TRUE(test_server_.Start()); |
| 7548 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7549 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7550 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 7551 | app_path = app_path.AppendASCII("LICENSE"); |
| 7552 | TestDelegate d; |
| 7553 | // Set correct login credentials. The delegate will be asked for them when |
| 7554 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7555 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7556 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7557 | URLRequest r(test_server_.GetURLWithUserAndPassword( |
| 7558 | "/LICENSE", "chrome", "wrong_password"), |
| 7559 | DEFAULT_PRIORITY, |
| 7560 | &d, |
| 7561 | &default_context_); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7562 | r.Start(); |
| 7563 | EXPECT_TRUE(r.is_pending()); |
| 7564 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7565 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7566 | |
| 7567 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7568 | base::GetFileSize(app_path, &file_size); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7569 | |
| 7570 | EXPECT_FALSE(r.is_pending()); |
| 7571 | EXPECT_EQ(1, d.response_started_count()); |
| 7572 | EXPECT_FALSE(d.received_data_before_response()); |
| 7573 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 7574 | } |
| 7575 | } |
| 7576 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 7577 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7578 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7579 | ASSERT_TRUE(test_server_.Start()); |
| 7580 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7581 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7582 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 7583 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7584 | TestDelegate d; |
| 7585 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7586 | URLRequest r(test_server_.GetURLWithUserAndPassword( |
| 7587 | "/LICENSE", "wrong_user", "chrome"), |
| 7588 | DEFAULT_PRIORITY, |
| 7589 | &d, |
| 7590 | &default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7591 | r.Start(); |
| 7592 | EXPECT_TRUE(r.is_pending()); |
| 7593 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7594 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7595 | |
| 7596 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7597 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7598 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 7599 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7600 | EXPECT_EQ(1, d.response_started_count()); |
| 7601 | EXPECT_FALSE(d.received_data_before_response()); |
| 7602 | EXPECT_EQ(d.bytes_received(), 0); |
| 7603 | } |
| 7604 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7605 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 7606 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7607 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7608 | ASSERT_TRUE(test_server_.Start()); |
| 7609 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7610 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7611 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 7612 | app_path = app_path.AppendASCII("LICENSE"); |
| 7613 | TestDelegate d; |
| 7614 | // Set correct login credentials. The delegate will be asked for them when |
| 7615 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7616 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7617 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7618 | URLRequest r(test_server_.GetURLWithUserAndPassword( |
| 7619 | "/LICENSE", "wrong_user", "chrome"), |
| 7620 | DEFAULT_PRIORITY, |
| 7621 | &d, |
| 7622 | &default_context_); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7623 | r.Start(); |
| 7624 | EXPECT_TRUE(r.is_pending()); |
| 7625 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7626 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7627 | |
| 7628 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7629 | base::GetFileSize(app_path, &file_size); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7630 | |
| 7631 | EXPECT_FALSE(r.is_pending()); |
| 7632 | EXPECT_EQ(1, d.response_started_count()); |
| 7633 | EXPECT_FALSE(d.received_data_before_response()); |
| 7634 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 7635 | } |
| 7636 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7637 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 7638 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7639 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7640 | ASSERT_TRUE(test_server_.Start()); |
| 7641 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7642 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7643 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 7644 | app_path = app_path.AppendASCII("LICENSE"); |
| 7645 | |
| 7646 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 7647 | { |
| 7648 | // Pass correct login identity in the URL. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7649 | URLRequest r( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7650 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
| 7651 | DEFAULT_PRIORITY, |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 7652 | d.get(), |
| 7653 | &default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7654 | r.Start(); |
| 7655 | EXPECT_TRUE(r.is_pending()); |
| 7656 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7657 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7658 | |
| 7659 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7660 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7661 | |
| 7662 | EXPECT_FALSE(r.is_pending()); |
| 7663 | EXPECT_EQ(1, d->response_started_count()); |
| 7664 | EXPECT_FALSE(d->received_data_before_response()); |
| 7665 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 7666 | } |
| 7667 | |
| 7668 | d.reset(new TestDelegate); |
| 7669 | { |
| 7670 | // This request should use cached identity from previous request. |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7671 | URLRequest r(test_server_.GetURL("/LICENSE"), |
| 7672 | DEFAULT_PRIORITY, |
| 7673 | d.get(), |
| 7674 | &default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7675 | r.Start(); |
| 7676 | EXPECT_TRUE(r.is_pending()); |
| 7677 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7678 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7679 | |
| 7680 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7681 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7682 | |
| 7683 | EXPECT_FALSE(r.is_pending()); |
| 7684 | EXPECT_EQ(1, d->response_started_count()); |
| 7685 | EXPECT_FALSE(d->received_data_before_response()); |
| 7686 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 7687 | } |
| 7688 | } |
| 7689 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 7690 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7691 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7692 | ASSERT_TRUE(test_server_.Start()); |
| 7693 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7694 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7695 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 7696 | app_path = app_path.AppendASCII("LICENSE"); |
| 7697 | |
| 7698 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 7699 | // Set correct login credentials. The delegate will be asked for them when |
| 7700 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7701 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7702 | { |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7703 | URLRequest r(test_server_.GetURLWithUserAndPassword( |
| 7704 | "/LICENSE", "chrome", "wrong_password"), |
| 7705 | DEFAULT_PRIORITY, |
| 7706 | d.get(), |
| 7707 | &default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7708 | r.Start(); |
| 7709 | EXPECT_TRUE(r.is_pending()); |
| 7710 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7711 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7712 | |
| 7713 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7714 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7715 | |
| 7716 | EXPECT_FALSE(r.is_pending()); |
| 7717 | EXPECT_EQ(1, d->response_started_count()); |
| 7718 | EXPECT_FALSE(d->received_data_before_response()); |
| 7719 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 7720 | } |
| 7721 | |
| 7722 | // Use a new delegate without explicit credentials. The cached ones should be |
| 7723 | // used. |
| 7724 | d.reset(new TestDelegate); |
| 7725 | { |
| 7726 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 7727 | // ones. |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7728 | URLRequest r(test_server_.GetURL("/LICENSE"), |
| 7729 | DEFAULT_PRIORITY, |
| 7730 | d.get(), |
| 7731 | &default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7732 | r.Start(); |
| 7733 | EXPECT_TRUE(r.is_pending()); |
| 7734 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7735 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7736 | |
| 7737 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7738 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7739 | |
| 7740 | EXPECT_FALSE(r.is_pending()); |
| 7741 | EXPECT_EQ(1, d->response_started_count()); |
| 7742 | EXPECT_FALSE(d->received_data_before_response()); |
| 7743 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 7744 | } |
| 7745 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7746 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 7747 | |
| 7748 | } // namespace net |