[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" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 68 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 69 | #include "net/url_request/url_request.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 70 | #include "net/url_request/url_request_http_job.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 71 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 72 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 73 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 74 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 75 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 76 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 77 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 78 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 79 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 80 | #include "net/url_request/file_protocol_handler.h" |
| 81 | #include "net/url_request/url_request_file_dir_job.h" |
| 82 | #endif |
| 83 | |
| 84 | #if !defined(DISABLE_FTP_SUPPORT) |
| 85 | #include "net/url_request/ftp_protocol_handler.h" |
| 86 | #endif |
| 87 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 88 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 89 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 90 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 91 | #include "base/win/windows_version.h" |
| 92 | #endif |
| 93 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 94 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 95 | using base::Time; |
| 96 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 97 | namespace net { |
| 98 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 99 | namespace { |
| 100 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 101 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 102 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 103 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 104 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 105 | // Tests load timing information in the case a fresh connection was used, with |
| 106 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 107 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 108 | int connect_timing_flags) { |
| 109 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 110 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 111 | |
| 112 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 113 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 114 | |
| 115 | EXPECT_LE(load_timing_info.request_start, |
| 116 | load_timing_info.connect_timing.connect_start); |
| 117 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 118 | connect_timing_flags); |
| 119 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 120 | load_timing_info.send_start); |
| 121 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 122 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 123 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 124 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 125 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 126 | } |
| 127 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 128 | // Same as above, but with proxy times. |
| 129 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 130 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 131 | int connect_timing_flags) { |
| 132 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 133 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 134 | |
| 135 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 136 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 137 | |
| 138 | EXPECT_LE(load_timing_info.request_start, |
| 139 | load_timing_info.proxy_resolve_start); |
| 140 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 141 | load_timing_info.proxy_resolve_end); |
| 142 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 143 | load_timing_info.connect_timing.connect_start); |
| 144 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 145 | connect_timing_flags); |
| 146 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 147 | load_timing_info.send_start); |
| 148 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 149 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 150 | } |
| 151 | |
| 152 | // Same as above, but with a reused socket and proxy times. |
| 153 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 154 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 155 | EXPECT_TRUE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 156 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 157 | |
| 158 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 159 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 160 | |
| 161 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 162 | |
| 163 | EXPECT_LE(load_timing_info.request_start, |
| 164 | load_timing_info.proxy_resolve_start); |
| 165 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 166 | load_timing_info.proxy_resolve_end); |
| 167 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 168 | load_timing_info.send_start); |
| 169 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 170 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 171 | } |
| 172 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 173 | // Tests load timing information in the case of a cache hit, when no cache |
| 174 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 175 | base::StringPiece TestNetResourceProvider(int key) { |
| 176 | return "header"; |
| 177 | } |
| 178 | |
| 179 | void FillBuffer(char* buffer, size_t len) { |
| 180 | static bool called = false; |
| 181 | if (!called) { |
| 182 | called = true; |
| 183 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 184 | srand(seed); |
| 185 | } |
| 186 | |
| 187 | for (size_t i = 0; i < len; i++) { |
| 188 | buffer[i] = static_cast<char>(rand()); |
| 189 | if (!buffer[i]) |
| 190 | buffer[i] = 'g'; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | #if !defined(OS_IOS) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 195 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 196 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 197 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 198 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 199 | |
| 200 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 201 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 202 | |
| 203 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 204 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 205 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 206 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 207 | |
| 208 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 209 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 210 | } |
| 211 | |
| 212 | // Tests load timing in the case that there is no HTTP response. This can be |
| 213 | // used to test in the case of errors or non-HTTP requests. |
| 214 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 215 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 216 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 217 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 218 | |
| 219 | // Only the request times should be non-null. |
| 220 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 221 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 222 | |
| 223 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 224 | |
| 225 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 226 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 227 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 228 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 229 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 230 | } |
| 231 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 232 | // Do a case-insensitive search through |haystack| for |needle|. |
| 233 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 234 | std::string::const_iterator it = |
| 235 | std::search(haystack.begin(), |
| 236 | haystack.end(), |
| 237 | needle, |
| 238 | needle + strlen(needle), |
[email protected] | 07f1cee | 2010-11-03 03:53:35 | [diff] [blame] | 239 | base::CaseInsensitiveCompare<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 240 | return it != haystack.end(); |
| 241 | } |
| 242 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 243 | UploadDataStream* CreateSimpleUploadData(const char* data) { |
| 244 | scoped_ptr<UploadElementReader> reader( |
| 245 | new UploadBytesElementReader(data, strlen(data))); |
| 246 | return UploadDataStream::CreateWithReader(reader.Pass(), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 247 | } |
| 248 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 249 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 250 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 251 | // -1 means unknown. 0 means no encryption. |
| 252 | EXPECT_GT(ssl_info.security_bits, 0); |
| 253 | |
| 254 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 255 | int cipher_suite = SSLConnectionStatusToCipherSuite( |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 256 | ssl_info.connection_status); |
| 257 | EXPECT_NE(0, cipher_suite); |
| 258 | } |
| 259 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 260 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 261 | const GURL& host_url) { |
| 262 | std::string sent_value; |
| 263 | |
| 264 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 265 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 266 | |
| 267 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 268 | EXPECT_EQ("keep-alive", sent_value); |
| 269 | } |
| 270 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 271 | bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) { |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 272 | size_t size = a.size(); |
| 273 | |
| 274 | if (size != b.size()) |
| 275 | return false; |
| 276 | |
| 277 | for (size_t i = 0; i < size; ++i) { |
| 278 | if (!a[i].Equals(b[i])) |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | return true; |
| 283 | } |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 284 | #endif // !defined(OS_IOS) |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 285 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 286 | // A network delegate that allows the user to choose a subset of request stages |
| 287 | // to block in. When blocking, the delegate can do one of the following: |
| 288 | // * synchronously return a pre-specified error code, or |
| 289 | // * asynchronously return that value via an automatically called callback, |
| 290 | // or |
| 291 | // * block and wait for the user to do a callback. |
| 292 | // Additionally, the user may also specify a redirect URL -- then each request |
| 293 | // with the current URL different from the redirect target will be redirected |
| 294 | // to that target, in the on-before-URL-request stage, independent of whether |
| 295 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 296 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 297 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 298 | // Stages in which the delegate can block. |
| 299 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 300 | NOT_BLOCKED = 0, |
| 301 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 302 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 303 | ON_HEADERS_RECEIVED = 1 << 2, |
| 304 | ON_AUTH_REQUIRED = 1 << 3 |
| 305 | }; |
| 306 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 307 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 308 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 309 | enum BlockMode { |
| 310 | SYNCHRONOUS, // No callback, returns specified return values. |
| 311 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 312 | // specified return codes. |
| 313 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 314 | // |auth_retval_| are ignored. In every blocking stage the |
| 315 | // message loop is quit. |
| 316 | }; |
| 317 | |
| 318 | // Creates a delegate which does not block at all. |
| 319 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 320 | |
| 321 | // For users to trigger a callback returning |response|. |
| 322 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 323 | // Only call if |block_mode_| == USER_CALLBACK. |
| 324 | void DoCallback(int response); |
| 325 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 326 | |
| 327 | // Setters. |
| 328 | void set_retval(int retval) { |
| 329 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 330 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 331 | ASSERT_NE(OK, retval); |
| 332 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 333 | } |
| 334 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 335 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 336 | // |auth_credentials_| will be passed with the response. |
| 337 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 338 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 339 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 340 | auth_retval_ = auth_retval; |
| 341 | } |
| 342 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 343 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 346 | void set_redirect_url(const GURL& url) { |
| 347 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 350 | void set_block_on(int block_on) { |
| 351 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 354 | // Allows the user to check in which state did we block. |
| 355 | Stage stage_blocked_for_callback() const { |
| 356 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 357 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 361 | void RunCallback(int response, const CompletionCallback& callback); |
| 362 | void RunAuthCallback(AuthRequiredResponse response, |
| 363 | const AuthCallback& callback); |
| 364 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 365 | // TestNetworkDelegate implementation. |
| 366 | virtual int OnBeforeURLRequest(URLRequest* request, |
| 367 | const CompletionCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 368 | GURL* new_url) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 369 | |
| 370 | virtual int OnBeforeSendHeaders(URLRequest* request, |
| 371 | const CompletionCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 372 | HttpRequestHeaders* headers) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 373 | |
| 374 | virtual int OnHeadersReceived( |
| 375 | URLRequest* request, |
| 376 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 377 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 378 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 379 | GURL* allowed_unsafe_redirect_url) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 380 | |
| 381 | virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 382 | URLRequest* request, |
| 383 | const AuthChallengeInfo& auth_info, |
| 384 | const AuthCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 385 | AuthCredentials* credentials) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 386 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 387 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 388 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 389 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 390 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 391 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 392 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 393 | |
| 394 | // Configuration parameters, can be adjusted by public methods: |
| 395 | const BlockMode block_mode_; |
| 396 | |
| 397 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 398 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 399 | int retval_; // To be returned in non-auth stages. |
| 400 | AuthRequiredResponse auth_retval_; |
| 401 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 402 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 403 | int block_on_; // Bit mask: in which stages to block. |
| 404 | |
| 405 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 406 | // callback. |
| 407 | AuthCredentials auth_credentials_; |
| 408 | AuthCredentials* target_auth_credentials_; |
| 409 | |
| 410 | // Internal variables, not set by not the user: |
| 411 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 412 | // USER_CALLBACK). |
| 413 | Stage stage_blocked_for_callback_; |
| 414 | |
| 415 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 416 | CompletionCallback callback_; |
| 417 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 418 | |
| 419 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 420 | |
| 421 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 422 | }; |
| 423 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 424 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 425 | : block_mode_(block_mode), |
| 426 | retval_(OK), |
| 427 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 428 | block_on_(0), |
| 429 | target_auth_credentials_(NULL), |
| 430 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 431 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 435 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 436 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 437 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 438 | CompletionCallback callback = callback_; |
| 439 | Reset(); |
| 440 | RunCallback(response, callback); |
| 441 | } |
| 442 | |
| 443 | void BlockingNetworkDelegate::DoAuthCallback( |
| 444 | NetworkDelegate::AuthRequiredResponse response) { |
| 445 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 446 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 447 | AuthCallback auth_callback = auth_callback_; |
| 448 | Reset(); |
| 449 | RunAuthCallback(response, auth_callback); |
| 450 | } |
| 451 | |
| 452 | void BlockingNetworkDelegate::RunCallback(int response, |
| 453 | const CompletionCallback& callback) { |
| 454 | callback.Run(response); |
| 455 | } |
| 456 | |
| 457 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 458 | const AuthCallback& callback) { |
| 459 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 460 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 461 | *target_auth_credentials_ = auth_credentials_; |
| 462 | } |
| 463 | callback.Run(response); |
| 464 | } |
| 465 | |
| 466 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 467 | URLRequest* request, |
| 468 | const CompletionCallback& callback, |
| 469 | GURL* new_url) { |
| 470 | if (redirect_url_ == request->url()) |
| 471 | return OK; // We've already seen this request and redirected elsewhere. |
| 472 | |
| 473 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 474 | |
| 475 | if (!redirect_url_.is_empty()) |
| 476 | *new_url = redirect_url_; |
| 477 | |
| 478 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 479 | } |
| 480 | |
| 481 | int BlockingNetworkDelegate::OnBeforeSendHeaders( |
| 482 | URLRequest* request, |
| 483 | const CompletionCallback& callback, |
| 484 | HttpRequestHeaders* headers) { |
| 485 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 486 | |
| 487 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 488 | } |
| 489 | |
| 490 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 491 | URLRequest* request, |
| 492 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 493 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 494 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 495 | GURL* allowed_unsafe_redirect_url) { |
| 496 | TestNetworkDelegate::OnHeadersReceived(request, |
| 497 | callback, |
| 498 | original_response_headers, |
| 499 | override_response_headers, |
| 500 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 501 | |
| 502 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 503 | } |
| 504 | |
| 505 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 506 | URLRequest* request, |
| 507 | const AuthChallengeInfo& auth_info, |
| 508 | const AuthCallback& callback, |
| 509 | AuthCredentials* credentials) { |
| 510 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 511 | credentials); |
| 512 | // Check that the user has provided callback for the previous blocked stage. |
| 513 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 514 | |
| 515 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 516 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 517 | } |
| 518 | |
| 519 | target_auth_credentials_ = credentials; |
| 520 | |
| 521 | switch (block_mode_) { |
| 522 | case SYNCHRONOUS: |
| 523 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 524 | *target_auth_credentials_ = auth_credentials_; |
| 525 | return auth_retval_; |
| 526 | |
| 527 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 528 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 529 | FROM_HERE, |
| 530 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 531 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 532 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 533 | |
| 534 | case USER_CALLBACK: |
| 535 | auth_callback_ = callback; |
| 536 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 537 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 538 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 539 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 540 | } |
| 541 | NOTREACHED(); |
| 542 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 543 | } |
| 544 | |
| 545 | void BlockingNetworkDelegate::Reset() { |
| 546 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 547 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 548 | callback_.Reset(); |
| 549 | auth_callback_.Reset(); |
| 550 | } |
| 551 | |
| 552 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 553 | BlockingNetworkDelegate::Stage stage, |
| 554 | const CompletionCallback& callback) { |
| 555 | // Check that the user has provided callback for the previous blocked stage. |
| 556 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 557 | |
| 558 | if ((block_on_ & stage) == 0) { |
| 559 | return OK; |
| 560 | } |
| 561 | |
| 562 | switch (block_mode_) { |
| 563 | case SYNCHRONOUS: |
| 564 | EXPECT_NE(OK, retval_); |
| 565 | return retval_; |
| 566 | |
| 567 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 568 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 569 | FROM_HERE, |
| 570 | base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 571 | weak_factory_.GetWeakPtr(), retval_, callback)); |
| 572 | return ERR_IO_PENDING; |
| 573 | |
| 574 | case USER_CALLBACK: |
| 575 | callback_ = callback; |
| 576 | stage_blocked_for_callback_ = stage; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 577 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 578 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 579 | return ERR_IO_PENDING; |
| 580 | } |
| 581 | NOTREACHED(); |
| 582 | return 0; |
| 583 | } |
| 584 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 585 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 586 | public: |
| 587 | // Does not own |delegate|. |
| 588 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 589 | NetworkDelegate* delegate) |
| 590 | : TestURLRequestContext(true) { |
| 591 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 592 | set_network_delegate(delegate); |
| 593 | Init(); |
| 594 | } |
| 595 | virtual ~TestURLRequestContextWithProxy() {} |
| 596 | }; |
| 597 | |
| 598 | } // namespace |
| 599 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 600 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 601 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 602 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 603 | URLRequestTest() : default_context_(true) { |
| 604 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 605 | default_context_.set_net_log(&net_log_); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 606 | job_factory_.SetProtocolHandler("data", new DataProtocolHandler); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 607 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 608 | job_factory_.SetProtocolHandler( |
| 609 | "file", new FileProtocolHandler(base::MessageLoopProxy::current())); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 610 | #endif |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 611 | default_context_.set_job_factory(&job_factory_); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 612 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 613 | } |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 614 | virtual ~URLRequestTest() { |
| 615 | // URLRequestJobs may post clean-up tasks on destruction. |
| 616 | base::RunLoop().RunUntilIdle(); |
| 617 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 618 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 619 | // Adds the TestJobInterceptor to the default context. |
| 620 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 621 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 622 | job_factory_.SetProtocolHandler("http", NULL); |
| 623 | job_factory_.SetProtocolHandler("http", protocol_handler_); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 624 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 625 | } |
| 626 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 627 | protected: |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 628 | CapturingNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 629 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 630 | URLRequestJobFactoryImpl job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 631 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 632 | }; |
| 633 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 634 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 635 | TestDelegate d; |
| 636 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 637 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 638 | GURL("about:blank"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 639 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 640 | r->Start(); |
| 641 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 642 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 643 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 644 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 645 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 646 | EXPECT_FALSE(d.received_data_before_response()); |
| 647 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 648 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 649 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 650 | |
| 651 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 652 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 657 | TestDelegate d; |
| 658 | { |
| 659 | // Use our nice little Chrome logo. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 660 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 661 | GURL( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 662 | "data:image/png;base64," |
| 663 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3" |
| 664 | "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD" |
| 665 | "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t" |
| 666 | "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9" |
| 667 | "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1" |
| 668 | "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z" |
| 669 | "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW" |
| 670 | "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW" |
| 671 | "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb" |
| 672 | "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5" |
| 673 | "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV" |
| 674 | "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq" |
| 675 | "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F" |
| 676 | "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB" |
| 677 | "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM" |
| 678 | "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm" |
| 679 | "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En" |
| 680 | "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 681 | DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 682 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 683 | r->Start(); |
| 684 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 685 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 686 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 687 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 688 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 689 | EXPECT_FALSE(d.received_data_before_response()); |
| 690 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 691 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 692 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 693 | |
| 694 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 695 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 696 | } |
| 697 | } |
| 698 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 699 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 700 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 701 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 702 | PathService::Get(base::FILE_EXE, &app_path); |
| 703 | GURL app_url = FilePathToFileURL(app_path); |
| 704 | |
| 705 | TestDelegate d; |
| 706 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 707 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 708 | app_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 709 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 710 | r->Start(); |
| 711 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 712 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 713 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 714 | |
| 715 | int64 file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 716 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 717 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 718 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 719 | EXPECT_EQ(1, d.response_started_count()); |
| 720 | EXPECT_FALSE(d.received_data_before_response()); |
| 721 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 722 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 723 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 724 | |
| 725 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 726 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 730 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 731 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 732 | PathService::Get(base::FILE_EXE, &app_path); |
| 733 | GURL app_url = FilePathToFileURL(app_path); |
| 734 | |
| 735 | TestDelegate d; |
| 736 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 737 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 738 | app_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 739 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 740 | r->Start(); |
| 741 | EXPECT_TRUE(r->is_pending()); |
| 742 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 743 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 744 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 745 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 746 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 747 | } |
| 748 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 749 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 750 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 751 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 752 | FillBuffer(buffer.get(), buffer_size); |
| 753 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 754 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 755 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 756 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 757 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 758 | |
| 759 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 760 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 761 | |
| 762 | const size_t first_byte_position = 500; |
| 763 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 764 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 765 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 766 | buffer.get() + last_byte_position + 1); |
| 767 | |
| 768 | TestDelegate d; |
| 769 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 770 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 771 | temp_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 772 | |
| 773 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 774 | headers.SetHeader( |
| 775 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 776 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 777 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 778 | r->SetExtraRequestHeaders(headers); |
| 779 | r->Start(); |
| 780 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 781 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 782 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 783 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 784 | EXPECT_EQ(1, d.response_started_count()); |
| 785 | EXPECT_FALSE(d.received_data_before_response()); |
| 786 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 787 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 788 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 789 | } |
| 790 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 791 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 795 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 796 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 797 | FillBuffer(buffer.get(), buffer_size); |
| 798 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 799 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 800 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 801 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 802 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 803 | |
| 804 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 805 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 806 | |
| 807 | const size_t first_byte_position = 500; |
| 808 | const size_t last_byte_position = buffer_size - 1; |
| 809 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 810 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 811 | buffer.get() + last_byte_position + 1); |
| 812 | |
| 813 | TestDelegate d; |
| 814 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 815 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 816 | temp_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 817 | |
| 818 | HttpRequestHeaders headers; |
| 819 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 820 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 821 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 822 | r->SetExtraRequestHeaders(headers); |
| 823 | r->Start(); |
| 824 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 825 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 826 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 827 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 828 | EXPECT_EQ(1, d.response_started_count()); |
| 829 | EXPECT_FALSE(d.received_data_before_response()); |
| 830 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 831 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 832 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 833 | } |
| 834 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 835 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 839 | const size_t buffer_size = 400000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 840 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 841 | FillBuffer(buffer.get(), buffer_size); |
| 842 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 843 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 844 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 845 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 846 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 847 | |
| 848 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 849 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 850 | |
| 851 | TestDelegate d; |
| 852 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 853 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 854 | temp_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 855 | |
| 856 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 857 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 858 | r->SetExtraRequestHeaders(headers); |
| 859 | r->Start(); |
| 860 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 861 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 862 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 863 | EXPECT_TRUE(d.request_failed()); |
| 864 | } |
| 865 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 866 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 867 | } |
| 868 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 869 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 870 | base::ScopedTempDir temp_dir; |
| 871 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 872 | base::FilePath test_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 873 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file)); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 874 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 875 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 876 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 877 | |
| 878 | { |
| 879 | TestDelegate d; |
| 880 | TestNetworkDelegate network_delegate; |
| 881 | network_delegate.set_can_access_files(true); |
| 882 | default_context_.set_network_delegate(&network_delegate); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 883 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 884 | test_file_url, DEFAULT_PRIORITY, &d, NULL)); |
| 885 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 886 | base::RunLoop().Run(); |
| 887 | EXPECT_FALSE(d.request_failed()); |
| 888 | EXPECT_EQ(test_data, d.data_received()); |
| 889 | } |
| 890 | |
| 891 | { |
| 892 | TestDelegate d; |
| 893 | TestNetworkDelegate network_delegate; |
| 894 | network_delegate.set_can_access_files(false); |
| 895 | default_context_.set_network_delegate(&network_delegate); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 896 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 897 | test_file_url, DEFAULT_PRIORITY, &d, NULL)); |
| 898 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 899 | base::RunLoop().Run(); |
| 900 | EXPECT_TRUE(d.request_failed()); |
| 901 | EXPECT_EQ("", d.data_received()); |
| 902 | } |
| 903 | } |
| 904 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 905 | |
| 906 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 907 | // Put in mock resource provider. |
| 908 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 909 | |
| 910 | TestDelegate d; |
| 911 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 912 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 913 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 914 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 915 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 916 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 917 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 918 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d, NULL)); |
| 919 | req->Start(); |
| 920 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 921 | |
| 922 | d.set_cancel_in_received_data_pending(true); |
| 923 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 924 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | // Take out mock resource provider. |
| 928 | NetModule::SetResourceProvider(NULL); |
| 929 | } |
| 930 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 931 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 932 | // Verify the general sanity of the the output of the file: |
| 933 | // directory lister by checking for the output of a known existing |
| 934 | // file. |
| 935 | const char sentinel_name[] = "filedir-sentinel"; |
| 936 | |
| 937 | base::FilePath path; |
| 938 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 939 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 940 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 941 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 942 | |
| 943 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 944 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 945 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d, NULL)); |
| 946 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 947 | base::RunLoop().Run(); |
| 948 | |
| 949 | // Generate entry for the sentinel file. |
| 950 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 951 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 952 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 953 | EXPECT_GT(info.size, 0); |
| 954 | std::string sentinel_output = GetDirectoryListingEntry( |
| 955 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 956 | std::string(sentinel_name), |
| 957 | false /* is_dir */, |
| 958 | info.size, |
| 959 | info.last_modified); |
| 960 | |
| 961 | ASSERT_LT(0, d.bytes_received()); |
| 962 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 963 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 964 | // Check for the entry generated for the "sentinel" file. |
| 965 | const std::string& data = d.data_received(); |
| 966 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 967 | } |
| 968 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 969 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 970 | // There is an implicit redirect when loading a file path that matches a |
| 971 | // directory and does not end with a slash. Ensure that following such |
| 972 | // redirects does not crash. See http://crbug.com/18686. |
| 973 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 974 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 975 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 976 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 977 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 978 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 979 | |
| 980 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 981 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 982 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d, NULL)); |
| 983 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 984 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 985 | |
| 986 | ASSERT_EQ(1, d.received_redirect_count()); |
| 987 | ASSERT_LT(0, d.bytes_received()); |
| 988 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 989 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | #if defined(OS_WIN) |
| 993 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 994 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 995 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 996 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 997 | GURL("file:///"), DEFAULT_PRIORITY, &d, NULL)); |
| 998 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 999 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1000 | |
| 1001 | ASSERT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1002 | ASSERT_FALSE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1003 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1004 | #endif // defined(OS_WIN) |
| 1005 | |
| 1006 | #endif // !defined(DISABLE_FILE_SUPPORT) |
| 1007 | |
| 1008 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1009 | TestDelegate d; |
| 1010 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1011 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1012 | GURL("invalid url"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1013 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1014 | r->Start(); |
| 1015 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1016 | |
| 1017 | base::RunLoop().Run(); |
| 1018 | EXPECT_TRUE(d.request_failed()); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | #if defined(OS_WIN) |
| 1023 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1024 | base::FilePath app_path; |
| 1025 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1026 | app_path = app_path.AppendASCII("net"); |
| 1027 | app_path = app_path.AppendASCII("data"); |
| 1028 | app_path = app_path.AppendASCII("url_request_unittest"); |
| 1029 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1030 | |
| 1031 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1032 | |
| 1033 | base::win::ScopedCOMInitializer com_initializer; |
| 1034 | |
| 1035 | // Temporarily create a shortcut for test |
| 1036 | { |
| 1037 | base::win::ScopedComPtr<IShellLink> shell; |
| 1038 | ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 1039 | CLSCTX_INPROC_SERVER))); |
| 1040 | base::win::ScopedComPtr<IPersistFile> persist; |
| 1041 | ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 1042 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1043 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1044 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1045 | } |
| 1046 | |
| 1047 | TestDelegate d; |
| 1048 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1049 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 1050 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d, |
| 1051 | NULL)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1052 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1053 | r->Start(); |
| 1054 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1055 | |
| 1056 | base::RunLoop().Run(); |
| 1057 | |
| 1058 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1059 | GetFileAttributesEx(app_path.value().c_str(), |
| 1060 | GetFileExInfoStandard, &data); |
| 1061 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1062 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1063 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1064 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 1065 | scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
| 1066 | DWORD read_size; |
| 1067 | BOOL result; |
| 1068 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1069 | &read_size, NULL); |
| 1070 | std::string content(buffer.get(), read_size); |
| 1071 | CloseHandle(file); |
| 1072 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1073 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1074 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1075 | EXPECT_EQ(content, d.data_received()); |
| 1076 | } |
| 1077 | |
| 1078 | // Clean the shortcut |
| 1079 | DeleteFile(lnk_path.c_str()); |
| 1080 | } |
| 1081 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1082 | |
| 1083 | // Custom URLRequestJobs for use with interceptor tests |
| 1084 | class RestartTestJob : public URLRequestTestJob { |
| 1085 | public: |
| 1086 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1087 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1088 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1089 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1090 | this->NotifyRestartRequired(); |
| 1091 | } |
| 1092 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1093 | virtual ~RestartTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1094 | }; |
| 1095 | |
| 1096 | class CancelTestJob : public URLRequestTestJob { |
| 1097 | public: |
| 1098 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1099 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1100 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1101 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1102 | request_->Cancel(); |
| 1103 | } |
| 1104 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1105 | virtual ~CancelTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1106 | }; |
| 1107 | |
| 1108 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1109 | public: |
| 1110 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1111 | NetworkDelegate* network_delegate) |
| 1112 | : URLRequestTestJob(request, network_delegate, true) { |
| 1113 | } |
| 1114 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1115 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1116 | request_->Cancel(); |
| 1117 | this->NotifyRestartRequired(); |
| 1118 | } |
| 1119 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1120 | virtual ~CancelThenRestartTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1121 | }; |
| 1122 | |
| 1123 | // An Interceptor for use with interceptor tests |
| 1124 | class TestInterceptor : URLRequest::Interceptor { |
| 1125 | public: |
| 1126 | TestInterceptor() |
| 1127 | : intercept_main_request_(false), restart_main_request_(false), |
| 1128 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1129 | simulate_main_network_error_(false), |
| 1130 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1131 | intercept_final_response_(false), cancel_final_request_(false), |
| 1132 | did_intercept_main_(false), did_restart_main_(false), |
| 1133 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1134 | did_simulate_error_main_(false), |
| 1135 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1136 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1137 | URLRequest::Deprecated::RegisterRequestInterceptor(this); |
| 1138 | } |
| 1139 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1140 | virtual ~TestInterceptor() { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1141 | URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
| 1142 | } |
| 1143 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1144 | virtual URLRequestJob* MaybeIntercept( |
| 1145 | URLRequest* request, |
| 1146 | NetworkDelegate* network_delegate) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1147 | if (restart_main_request_) { |
| 1148 | restart_main_request_ = false; |
| 1149 | did_restart_main_ = true; |
| 1150 | return new RestartTestJob(request, network_delegate); |
| 1151 | } |
| 1152 | if (cancel_main_request_) { |
| 1153 | cancel_main_request_ = false; |
| 1154 | did_cancel_main_ = true; |
| 1155 | return new CancelTestJob(request, network_delegate); |
| 1156 | } |
| 1157 | if (cancel_then_restart_main_request_) { |
| 1158 | cancel_then_restart_main_request_ = false; |
| 1159 | did_cancel_then_restart_main_ = true; |
| 1160 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1161 | } |
| 1162 | if (simulate_main_network_error_) { |
| 1163 | simulate_main_network_error_ = false; |
| 1164 | did_simulate_error_main_ = true; |
| 1165 | // will error since the requeted url is not one of its canned urls |
| 1166 | return new URLRequestTestJob(request, network_delegate, true); |
| 1167 | } |
| 1168 | if (!intercept_main_request_) |
| 1169 | return NULL; |
| 1170 | intercept_main_request_ = false; |
| 1171 | did_intercept_main_ = true; |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1172 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1173 | network_delegate, |
| 1174 | main_headers_, |
| 1175 | main_data_, |
| 1176 | true); |
| 1177 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1178 | return job; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | virtual URLRequestJob* MaybeInterceptRedirect( |
| 1182 | URLRequest* request, |
| 1183 | NetworkDelegate* network_delegate, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1184 | const GURL& location) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1185 | if (cancel_redirect_request_) { |
| 1186 | cancel_redirect_request_ = false; |
| 1187 | did_cancel_redirect_ = true; |
| 1188 | return new CancelTestJob(request, network_delegate); |
| 1189 | } |
| 1190 | if (!intercept_redirect_) |
| 1191 | return NULL; |
| 1192 | intercept_redirect_ = false; |
| 1193 | did_intercept_redirect_ = true; |
| 1194 | return new URLRequestTestJob(request, |
| 1195 | network_delegate, |
| 1196 | redirect_headers_, |
| 1197 | redirect_data_, |
| 1198 | true); |
| 1199 | } |
| 1200 | |
| 1201 | virtual URLRequestJob* MaybeInterceptResponse( |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1202 | URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1203 | if (cancel_final_request_) { |
| 1204 | cancel_final_request_ = false; |
| 1205 | did_cancel_final_ = true; |
| 1206 | return new CancelTestJob(request, network_delegate); |
| 1207 | } |
| 1208 | if (!intercept_final_response_) |
| 1209 | return NULL; |
| 1210 | intercept_final_response_ = false; |
| 1211 | did_intercept_final_ = true; |
| 1212 | return new URLRequestTestJob(request, |
| 1213 | network_delegate, |
| 1214 | final_headers_, |
| 1215 | final_data_, |
| 1216 | true); |
| 1217 | } |
| 1218 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1219 | // Whether to intercept the main request, and if so the response to return and |
| 1220 | // the LoadTimingInfo to use. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1221 | bool intercept_main_request_; |
| 1222 | std::string main_headers_; |
| 1223 | std::string main_data_; |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1224 | LoadTimingInfo main_request_load_timing_info_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1225 | |
| 1226 | // Other actions we take at MaybeIntercept time |
| 1227 | bool restart_main_request_; |
| 1228 | bool cancel_main_request_; |
| 1229 | bool cancel_then_restart_main_request_; |
| 1230 | bool simulate_main_network_error_; |
| 1231 | |
| 1232 | // Whether to intercept redirects, and if so the response to return. |
| 1233 | bool intercept_redirect_; |
| 1234 | std::string redirect_headers_; |
| 1235 | std::string redirect_data_; |
| 1236 | |
| 1237 | // Other actions we can take at MaybeInterceptRedirect time |
| 1238 | bool cancel_redirect_request_; |
| 1239 | |
| 1240 | // Whether to intercept final response, and if so the response to return. |
| 1241 | bool intercept_final_response_; |
| 1242 | std::string final_headers_; |
| 1243 | std::string final_data_; |
| 1244 | |
| 1245 | // Other actions we can take at MaybeInterceptResponse time |
| 1246 | bool cancel_final_request_; |
| 1247 | |
| 1248 | // If we did something or not |
| 1249 | bool did_intercept_main_; |
| 1250 | bool did_restart_main_; |
| 1251 | bool did_cancel_main_; |
| 1252 | bool did_cancel_then_restart_main_; |
| 1253 | bool did_simulate_error_main_; |
| 1254 | bool did_intercept_redirect_; |
| 1255 | bool did_cancel_redirect_; |
| 1256 | bool did_intercept_final_; |
| 1257 | bool did_cancel_final_; |
| 1258 | |
| 1259 | // Static getters for canned response header and data strings |
| 1260 | |
| 1261 | static std::string ok_data() { |
| 1262 | return URLRequestTestJob::test_data_1(); |
| 1263 | } |
| 1264 | |
| 1265 | static std::string ok_headers() { |
| 1266 | return URLRequestTestJob::test_headers(); |
| 1267 | } |
| 1268 | |
| 1269 | static std::string redirect_data() { |
| 1270 | return std::string(); |
| 1271 | } |
| 1272 | |
| 1273 | static std::string redirect_headers() { |
| 1274 | return URLRequestTestJob::test_redirect_headers(); |
| 1275 | } |
| 1276 | |
| 1277 | static std::string error_data() { |
| 1278 | return std::string("ohhh nooooo mr. bill!"); |
| 1279 | } |
| 1280 | |
| 1281 | static std::string error_headers() { |
| 1282 | return URLRequestTestJob::test_error_headers(); |
| 1283 | } |
| 1284 | }; |
| 1285 | |
| 1286 | TEST_F(URLRequestTest, Intercept) { |
| 1287 | TestInterceptor interceptor; |
| 1288 | |
| 1289 | // intercept the main request and respond with a simple response |
| 1290 | interceptor.intercept_main_request_ = true; |
| 1291 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1292 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1293 | |
| 1294 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1295 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1296 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1297 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1298 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1299 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1300 | req->SetUserData(NULL, user_data0); |
| 1301 | req->SetUserData(&user_data1, user_data1); |
| 1302 | req->SetUserData(&user_data2, user_data2); |
| 1303 | req->set_method("GET"); |
| 1304 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1305 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1306 | |
| 1307 | // Make sure we can retrieve our specific user data |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1308 | EXPECT_EQ(user_data0, req->GetUserData(NULL)); |
| 1309 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1310 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1311 | |
| 1312 | // Check the interceptor got called as expected |
| 1313 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1314 | |
| 1315 | // Check we got one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1316 | EXPECT_TRUE(req->status().is_success()); |
| 1317 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1318 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1319 | EXPECT_EQ(1, d.response_started_count()); |
| 1320 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1321 | } |
| 1322 | |
| 1323 | TEST_F(URLRequestTest, InterceptRedirect) { |
| 1324 | TestInterceptor interceptor; |
| 1325 | |
| 1326 | // intercept the main request and respond with a redirect |
| 1327 | interceptor.intercept_main_request_ = true; |
| 1328 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1329 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1330 | |
| 1331 | // intercept that redirect and respond a final OK response |
| 1332 | interceptor.intercept_redirect_ = true; |
| 1333 | interceptor.redirect_headers_ = TestInterceptor::ok_headers(); |
| 1334 | interceptor.redirect_data_ = TestInterceptor::ok_data(); |
| 1335 | |
| 1336 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1337 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1338 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1339 | req->set_method("GET"); |
| 1340 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1341 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1342 | |
| 1343 | // Check the interceptor got called as expected |
| 1344 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1345 | EXPECT_TRUE(interceptor.did_intercept_redirect_); |
| 1346 | |
| 1347 | // Check we got one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1348 | EXPECT_TRUE(req->status().is_success()); |
| 1349 | if (req->status().is_success()) { |
| 1350 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1351 | } |
| 1352 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1353 | EXPECT_EQ(1, d.response_started_count()); |
| 1354 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1355 | } |
| 1356 | |
| 1357 | TEST_F(URLRequestTest, InterceptServerError) { |
| 1358 | TestInterceptor interceptor; |
| 1359 | |
| 1360 | // intercept the main request to generate a server error response |
| 1361 | interceptor.intercept_main_request_ = true; |
| 1362 | interceptor.main_headers_ = TestInterceptor::error_headers(); |
| 1363 | interceptor.main_data_ = TestInterceptor::error_data(); |
| 1364 | |
| 1365 | // intercept that error and respond with an OK response |
| 1366 | interceptor.intercept_final_response_ = true; |
| 1367 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1368 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1369 | |
| 1370 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1371 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1372 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1373 | req->set_method("GET"); |
| 1374 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1375 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1376 | |
| 1377 | // Check the interceptor got called as expected |
| 1378 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1379 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1380 | |
| 1381 | // Check we got one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1382 | EXPECT_TRUE(req->status().is_success()); |
| 1383 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1384 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1385 | EXPECT_EQ(1, d.response_started_count()); |
| 1386 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1387 | } |
| 1388 | |
| 1389 | TEST_F(URLRequestTest, InterceptNetworkError) { |
| 1390 | TestInterceptor interceptor; |
| 1391 | |
| 1392 | // intercept the main request to simulate a network error |
| 1393 | interceptor.simulate_main_network_error_ = true; |
| 1394 | |
| 1395 | // intercept that error and respond with an OK response |
| 1396 | interceptor.intercept_final_response_ = true; |
| 1397 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1398 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1399 | |
| 1400 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1401 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1402 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1403 | req->set_method("GET"); |
| 1404 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1405 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1406 | |
| 1407 | // Check the interceptor got called as expected |
| 1408 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1409 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1410 | |
| 1411 | // Check we received one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1412 | EXPECT_TRUE(req->status().is_success()); |
| 1413 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1414 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1415 | EXPECT_EQ(1, d.response_started_count()); |
| 1416 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1417 | } |
| 1418 | |
| 1419 | TEST_F(URLRequestTest, InterceptRestartRequired) { |
| 1420 | TestInterceptor interceptor; |
| 1421 | |
| 1422 | // restart the main request |
| 1423 | interceptor.restart_main_request_ = true; |
| 1424 | |
| 1425 | // then intercept the new main request and respond with an OK response |
| 1426 | interceptor.intercept_main_request_ = true; |
| 1427 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1428 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1429 | |
| 1430 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1431 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1432 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1433 | req->set_method("GET"); |
| 1434 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1435 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1436 | |
| 1437 | // Check the interceptor got called as expected |
| 1438 | EXPECT_TRUE(interceptor.did_restart_main_); |
| 1439 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1440 | |
| 1441 | // Check we received one good response |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1442 | EXPECT_TRUE(req->status().is_success()); |
| 1443 | if (req->status().is_success()) { |
| 1444 | EXPECT_EQ(200, req->response_headers()->response_code()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1445 | } |
| 1446 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1447 | EXPECT_EQ(1, d.response_started_count()); |
| 1448 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1449 | } |
| 1450 | |
| 1451 | TEST_F(URLRequestTest, InterceptRespectsCancelMain) { |
| 1452 | TestInterceptor interceptor; |
| 1453 | |
| 1454 | // intercept the main request and cancel from within the restarted job |
| 1455 | interceptor.cancel_main_request_ = true; |
| 1456 | |
| 1457 | // setup to intercept final response and override it with an OK response |
| 1458 | interceptor.intercept_final_response_ = true; |
| 1459 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1460 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1461 | |
| 1462 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1463 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1464 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1465 | req->set_method("GET"); |
| 1466 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1467 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1468 | |
| 1469 | // Check the interceptor got called as expected |
| 1470 | EXPECT_TRUE(interceptor.did_cancel_main_); |
| 1471 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1472 | |
| 1473 | // Check we see a canceled request |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1474 | EXPECT_FALSE(req->status().is_success()); |
| 1475 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) { |
| 1479 | TestInterceptor interceptor; |
| 1480 | |
| 1481 | // intercept the main request and respond with a redirect |
| 1482 | interceptor.intercept_main_request_ = true; |
| 1483 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1484 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1485 | |
| 1486 | // intercept the redirect and cancel from within that job |
| 1487 | interceptor.cancel_redirect_request_ = true; |
| 1488 | |
| 1489 | // setup to intercept final response and override it with an OK response |
| 1490 | interceptor.intercept_final_response_ = true; |
| 1491 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1492 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1493 | |
| 1494 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1495 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1496 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1497 | req->set_method("GET"); |
| 1498 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1499 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1500 | |
| 1501 | // Check the interceptor got called as expected |
| 1502 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1503 | EXPECT_TRUE(interceptor.did_cancel_redirect_); |
| 1504 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1505 | |
| 1506 | // Check we see a canceled request |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1507 | EXPECT_FALSE(req->status().is_success()); |
| 1508 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1509 | } |
| 1510 | |
| 1511 | TEST_F(URLRequestTest, InterceptRespectsCancelFinal) { |
| 1512 | TestInterceptor interceptor; |
| 1513 | |
| 1514 | // intercept the main request to simulate a network error |
| 1515 | interceptor.simulate_main_network_error_ = true; |
| 1516 | |
| 1517 | // setup to intercept final response and cancel from within that job |
| 1518 | interceptor.cancel_final_request_ = true; |
| 1519 | |
| 1520 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1521 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1522 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1523 | req->set_method("GET"); |
| 1524 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1525 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1526 | |
| 1527 | // Check the interceptor got called as expected |
| 1528 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1529 | EXPECT_TRUE(interceptor.did_cancel_final_); |
| 1530 | |
| 1531 | // Check we see a canceled request |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1532 | EXPECT_FALSE(req->status().is_success()); |
| 1533 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { |
| 1537 | TestInterceptor interceptor; |
| 1538 | |
| 1539 | // intercept the main request and cancel then restart from within that job |
| 1540 | interceptor.cancel_then_restart_main_request_ = true; |
| 1541 | |
| 1542 | // setup to intercept final response and override it with an OK response |
| 1543 | interceptor.intercept_final_response_ = true; |
| 1544 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1545 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1546 | |
| 1547 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1548 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1549 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1550 | req->set_method("GET"); |
| 1551 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1552 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1553 | |
| 1554 | // Check the interceptor got called as expected |
| 1555 | EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); |
| 1556 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1557 | |
| 1558 | // Check we see a canceled request |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1559 | EXPECT_FALSE(req->status().is_success()); |
| 1560 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1561 | } |
| 1562 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1563 | LoadTimingInfo RunLoadTimingTest(const LoadTimingInfo& job_load_timing, |
| 1564 | URLRequestContext* context) { |
| 1565 | TestInterceptor interceptor; |
| 1566 | interceptor.intercept_main_request_ = true; |
| 1567 | interceptor.main_request_load_timing_info_ = job_load_timing; |
| 1568 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1569 | scoped_ptr<URLRequest> req(context->CreateRequest( |
| 1570 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1571 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1572 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1573 | |
| 1574 | LoadTimingInfo resulting_load_timing; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1575 | req->GetLoadTimingInfo(&resulting_load_timing); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1576 | |
| 1577 | // None of these should be modified by the URLRequest. |
| 1578 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1579 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1580 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1581 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1582 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1583 | resulting_load_timing.receive_headers_end); |
| 1584 | |
| 1585 | return resulting_load_timing; |
| 1586 | } |
| 1587 | |
| 1588 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 1589 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 1590 | // or SSL times, and |used_proxy| is used for proxy times. |
| 1591 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 1592 | int connect_time_flags, |
| 1593 | bool used_proxy) { |
| 1594 | LoadTimingInfo load_timing; |
| 1595 | load_timing.socket_log_id = 1; |
| 1596 | |
| 1597 | if (used_proxy) { |
| 1598 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1599 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1600 | } |
| 1601 | |
| 1602 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 1603 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 1604 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 1605 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 1606 | } |
| 1607 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 1608 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 1609 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 1610 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 1611 | } |
| 1612 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 1613 | |
| 1614 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1615 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1616 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1617 | return load_timing; |
| 1618 | } |
| 1619 | |
| 1620 | // Same as above, but in the case of a reused socket. |
| 1621 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 1622 | bool used_proxy) { |
| 1623 | LoadTimingInfo load_timing; |
| 1624 | load_timing.socket_log_id = 1; |
| 1625 | load_timing.socket_reused = true; |
| 1626 | |
| 1627 | if (used_proxy) { |
| 1628 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1629 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1630 | } |
| 1631 | |
| 1632 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1633 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1634 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1635 | return load_timing; |
| 1636 | } |
| 1637 | |
| 1638 | // Basic test that the intercept + load timing tests work. |
| 1639 | TEST_F(URLRequestTest, InterceptLoadTiming) { |
| 1640 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1641 | LoadTimingInfo job_load_timing = |
| 1642 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1643 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1644 | LoadTimingInfo load_timing_result = |
| 1645 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1646 | |
| 1647 | // Nothing should have been changed by the URLRequest. |
| 1648 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1649 | load_timing_result.proxy_resolve_start); |
| 1650 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1651 | load_timing_result.proxy_resolve_end); |
| 1652 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1653 | load_timing_result.connect_timing.dns_start); |
| 1654 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1655 | load_timing_result.connect_timing.dns_end); |
| 1656 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1657 | load_timing_result.connect_timing.connect_start); |
| 1658 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1659 | load_timing_result.connect_timing.connect_end); |
| 1660 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1661 | load_timing_result.connect_timing.ssl_start); |
| 1662 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1663 | load_timing_result.connect_timing.ssl_end); |
| 1664 | |
| 1665 | // Redundant sanity check. |
| 1666 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1667 | } |
| 1668 | |
| 1669 | // Another basic test, with proxy and SSL times, but no DNS times. |
| 1670 | TEST_F(URLRequestTest, InterceptLoadTimingProxy) { |
| 1671 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1672 | LoadTimingInfo job_load_timing = |
| 1673 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1674 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1675 | LoadTimingInfo load_timing_result = |
| 1676 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1677 | |
| 1678 | // Nothing should have been changed by the URLRequest. |
| 1679 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1680 | load_timing_result.proxy_resolve_start); |
| 1681 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1682 | load_timing_result.proxy_resolve_end); |
| 1683 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1684 | load_timing_result.connect_timing.dns_start); |
| 1685 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1686 | load_timing_result.connect_timing.dns_end); |
| 1687 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1688 | load_timing_result.connect_timing.connect_start); |
| 1689 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1690 | load_timing_result.connect_timing.connect_end); |
| 1691 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1692 | load_timing_result.connect_timing.ssl_start); |
| 1693 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1694 | load_timing_result.connect_timing.ssl_end); |
| 1695 | |
| 1696 | // Redundant sanity check. |
| 1697 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1698 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 1699 | } |
| 1700 | |
| 1701 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1702 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 1703 | // the case of reusing a SPDY session. The connected socket is not considered |
| 1704 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1705 | // |
| 1706 | // To mix things up from the test above, assumes DNS times but no SSL times. |
| 1707 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolution) { |
| 1708 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1709 | LoadTimingInfo job_load_timing = |
| 1710 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 1711 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 1712 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 1713 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 1714 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 1715 | job_load_timing.connect_timing.connect_start = |
| 1716 | now - base::TimeDelta::FromDays(2); |
| 1717 | job_load_timing.connect_timing.connect_end = |
| 1718 | now - base::TimeDelta::FromDays(1); |
| 1719 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1720 | LoadTimingInfo load_timing_result = |
| 1721 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1722 | |
| 1723 | // Proxy times, connect times, and DNS times should all be replaced with |
| 1724 | // request_start. |
| 1725 | EXPECT_EQ(load_timing_result.request_start, |
| 1726 | load_timing_result.proxy_resolve_start); |
| 1727 | EXPECT_EQ(load_timing_result.request_start, |
| 1728 | load_timing_result.proxy_resolve_end); |
| 1729 | EXPECT_EQ(load_timing_result.request_start, |
| 1730 | load_timing_result.connect_timing.dns_start); |
| 1731 | EXPECT_EQ(load_timing_result.request_start, |
| 1732 | load_timing_result.connect_timing.dns_end); |
| 1733 | EXPECT_EQ(load_timing_result.request_start, |
| 1734 | load_timing_result.connect_timing.connect_start); |
| 1735 | EXPECT_EQ(load_timing_result.request_start, |
| 1736 | load_timing_result.connect_timing.connect_end); |
| 1737 | |
| 1738 | // Other times should have been left null. |
| 1739 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1740 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 1741 | } |
| 1742 | |
| 1743 | // Same as above, but in the reused case. |
| 1744 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyProxyResolutionReused) { |
| 1745 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1746 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 1747 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 1748 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 1749 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1750 | LoadTimingInfo load_timing_result = |
| 1751 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1752 | |
| 1753 | // Proxy times and connect times should all be replaced with request_start. |
| 1754 | EXPECT_EQ(load_timing_result.request_start, |
| 1755 | load_timing_result.proxy_resolve_start); |
| 1756 | EXPECT_EQ(load_timing_result.request_start, |
| 1757 | load_timing_result.proxy_resolve_end); |
| 1758 | |
| 1759 | // Other times should have been left null. |
| 1760 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 1761 | } |
| 1762 | |
| 1763 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1764 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 1765 | // not considered reused in this test (May be a preconnect). |
| 1766 | // |
| 1767 | // To mix things up, the request has SSL times, but no DNS times. |
| 1768 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnect) { |
| 1769 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1770 | LoadTimingInfo job_load_timing = |
| 1771 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 1772 | job_load_timing.connect_timing.connect_start = |
| 1773 | now - base::TimeDelta::FromDays(1); |
| 1774 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 1775 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 1776 | job_load_timing.connect_timing.connect_end = |
| 1777 | now - base::TimeDelta::FromDays(4); |
| 1778 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1779 | LoadTimingInfo load_timing_result = |
| 1780 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1781 | |
| 1782 | // Connect times, and SSL times should be replaced with request_start. |
| 1783 | EXPECT_EQ(load_timing_result.request_start, |
| 1784 | load_timing_result.connect_timing.connect_start); |
| 1785 | EXPECT_EQ(load_timing_result.request_start, |
| 1786 | load_timing_result.connect_timing.ssl_start); |
| 1787 | EXPECT_EQ(load_timing_result.request_start, |
| 1788 | load_timing_result.connect_timing.ssl_end); |
| 1789 | EXPECT_EQ(load_timing_result.request_start, |
| 1790 | load_timing_result.connect_timing.connect_end); |
| 1791 | |
| 1792 | // Other times should have been left null. |
| 1793 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 1794 | } |
| 1795 | |
| 1796 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1797 | // |request_start|, due to reusing a connected socket in the case that there |
| 1798 | // are also proxy times. The connected socket is not considered reused in this |
| 1799 | // test (May be a preconnect). |
| 1800 | // |
| 1801 | // In this test, there are no SSL or DNS times. |
| 1802 | TEST_F(URLRequestTest, InterceptLoadTimingEarlyConnectWithProxy) { |
| 1803 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1804 | LoadTimingInfo job_load_timing = |
| 1805 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 1806 | job_load_timing.connect_timing.connect_start = |
| 1807 | now - base::TimeDelta::FromDays(1); |
| 1808 | job_load_timing.connect_timing.connect_end = |
| 1809 | now - base::TimeDelta::FromDays(2); |
| 1810 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1811 | LoadTimingInfo load_timing_result = |
| 1812 | RunLoadTimingTest(job_load_timing, &default_context_); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1813 | |
| 1814 | // Connect times should be replaced with proxy_resolve_end. |
| 1815 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1816 | load_timing_result.connect_timing.connect_start); |
| 1817 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1818 | load_timing_result.connect_timing.connect_end); |
| 1819 | |
| 1820 | // Other times should have been left null. |
| 1821 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1822 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 1823 | } |
| 1824 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1825 | // Check that two different URL requests have different identifiers. |
| 1826 | TEST_F(URLRequestTest, Identifiers) { |
| 1827 | TestDelegate d; |
| 1828 | TestURLRequestContext context; |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1829 | TestURLRequest req( |
| 1830 | GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context); |
| 1831 | TestURLRequest other_req( |
| 1832 | GURL("http://example.com"), DEFAULT_PRIORITY, &d, &context); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1833 | |
| 1834 | ASSERT_NE(req.identifier(), other_req.identifier()); |
| 1835 | } |
| 1836 | |
| 1837 | // Check that a failure to connect to the proxy is reported to the network |
| 1838 | // delegate. |
| 1839 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
| 1840 | MockHostResolver host_resolver; |
| 1841 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 1842 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 1843 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1844 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 1845 | |
| 1846 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1847 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 1848 | GURL("http://example.com"), DEFAULT_PRIORITY, &d, NULL)); |
| 1849 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1850 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1851 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1852 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1853 | |
| 1854 | // Check we see a failed request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1855 | EXPECT_FALSE(req->status().is_success()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 1856 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1857 | EXPECT_TRUE(req->proxy_server().IsEmpty()); |
| 1858 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 1859 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1860 | |
| 1861 | EXPECT_EQ(1, network_delegate.error_count()); |
| 1862 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
| 1863 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 1864 | } |
| 1865 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 1866 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1867 | // content is empty. |
| 1868 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 1869 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1870 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1871 | GURL("data:,"), DEFAULT_PRIORITY, &d, NULL)); |
| 1872 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1873 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1874 | EXPECT_EQ("", d.data_received()); |
| 1875 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 1876 | } |
| 1877 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1878 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 1879 | // correctly, both before and after start. |
| 1880 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 1881 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1882 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1883 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1884 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1885 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1886 | req->SetPriority(LOW); |
| 1887 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1888 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1889 | req->Start(); |
| 1890 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1891 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1892 | req->SetPriority(MEDIUM); |
| 1893 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 1897 | // Start on it. |
| 1898 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 1899 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1900 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1901 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
| 1902 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1903 | |
| 1904 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1905 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1906 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1907 | EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 1908 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1909 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1910 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1911 | req->Start(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1912 | EXPECT_EQ(LOW, job->priority()); |
| 1913 | } |
| 1914 | |
| 1915 | // Make sure that URLRequest passes on its priority updates to its |
| 1916 | // job. |
| 1917 | TEST_F(URLRequestTest, SetJobPriority) { |
| 1918 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1919 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1920 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1921 | |
| 1922 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1923 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1924 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1925 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1926 | req->SetPriority(LOW); |
| 1927 | req->Start(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1928 | EXPECT_EQ(LOW, job->priority()); |
| 1929 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1930 | req->SetPriority(MEDIUM); |
| 1931 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 1932 | EXPECT_EQ(MEDIUM, job->priority()); |
| 1933 | } |
| 1934 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1935 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 1936 | // is MAXIMUM_PRIORITY. |
| 1937 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 1938 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1939 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 1940 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d, NULL)); |
| 1941 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1942 | |
| 1943 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1944 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1945 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
| 1946 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1947 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 1948 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1949 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1950 | req->SetPriority(MAXIMUM_PRIORITY); |
| 1951 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1952 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1953 | req->Start(); |
| 1954 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 1955 | EXPECT_EQ(MAXIMUM_PRIORITY, job->priority()); |
| 1956 | } |
| 1957 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1958 | // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666). |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1959 | #if !defined(OS_IOS) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1960 | // A subclass of SpawnedTestServer that uses a statically-configured hostname. |
| 1961 | // This is to work around mysterious failures in chrome_frame_net_tests. See: |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1962 | // http://crbug.com/114369 |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 1963 | // TODO(erikwright): remove or update as needed; see http://crbug.com/334634. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1964 | class LocalHttpTestServer : public SpawnedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1965 | public: |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1966 | explicit LocalHttpTestServer(const base::FilePath& document_root) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1967 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 1968 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 1969 | document_root) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1970 | LocalHttpTestServer() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 1971 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 1972 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 1973 | base::FilePath()) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1974 | }; |
| 1975 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 1976 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1977 | LocalHttpTestServer test_server; |
| 1978 | ASSERT_TRUE(test_server.Start()); |
| 1979 | |
| 1980 | TestURLRequestContext context; |
| 1981 | scoped_refptr<DelayedCookieMonster> delayed_cm = |
| 1982 | new DelayedCookieMonster(); |
| 1983 | scoped_refptr<CookieStore> cookie_store = delayed_cm; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1984 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1985 | |
| 1986 | // Set up a cookie. |
| 1987 | { |
| 1988 | TestNetworkDelegate network_delegate; |
| 1989 | context.set_network_delegate(&network_delegate); |
| 1990 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1991 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 1992 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
| 1993 | &d, NULL)); |
| 1994 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1995 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1996 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1997 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1998 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 1999 | } |
| 2000 | |
| 2001 | // Verify that the cookie is set. |
| 2002 | { |
| 2003 | TestNetworkDelegate network_delegate; |
| 2004 | context.set_network_delegate(&network_delegate); |
| 2005 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2006 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2007 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2008 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2009 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2010 | |
| 2011 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2012 | != std::string::npos); |
| 2013 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2014 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2015 | } |
| 2016 | } |
| 2017 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2018 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2019 | LocalHttpTestServer test_server; |
| 2020 | ASSERT_TRUE(test_server.Start()); |
| 2021 | |
| 2022 | // Set up a cookie. |
| 2023 | { |
| 2024 | TestNetworkDelegate network_delegate; |
| 2025 | default_context_.set_network_delegate(&network_delegate); |
| 2026 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2027 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2028 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
| 2029 | &d, NULL)); |
| 2030 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2031 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2032 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2033 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2034 | } |
| 2035 | |
| 2036 | // Verify that the cookie is set. |
| 2037 | { |
| 2038 | TestNetworkDelegate network_delegate; |
| 2039 | default_context_.set_network_delegate(&network_delegate); |
| 2040 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2041 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2042 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2043 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2044 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2045 | |
| 2046 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2047 | != std::string::npos); |
| 2048 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2049 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2050 | } |
| 2051 | |
| 2052 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2053 | { |
| 2054 | TestNetworkDelegate network_delegate; |
| 2055 | default_context_.set_network_delegate(&network_delegate); |
| 2056 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2057 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2058 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2059 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2060 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2061 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2062 | |
| 2063 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2064 | == std::string::npos); |
| 2065 | |
| 2066 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2067 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2068 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2073 | LocalHttpTestServer test_server; |
| 2074 | ASSERT_TRUE(test_server.Start()); |
| 2075 | |
| 2076 | // Set up a cookie. |
| 2077 | { |
| 2078 | TestNetworkDelegate network_delegate; |
| 2079 | default_context_.set_network_delegate(&network_delegate); |
| 2080 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2081 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2082 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
| 2083 | &d, NULL)); |
| 2084 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2085 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2086 | |
| 2087 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2088 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2089 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2090 | } |
| 2091 | |
| 2092 | // Try to set-up another cookie and update the previous cookie. |
| 2093 | { |
| 2094 | TestNetworkDelegate network_delegate; |
| 2095 | default_context_.set_network_delegate(&network_delegate); |
| 2096 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2097 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2098 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2099 | DEFAULT_PRIORITY, &d, NULL)); |
| 2100 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2101 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2102 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2103 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2104 | |
| 2105 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2106 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2107 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2108 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2109 | } |
| 2110 | |
| 2111 | // Verify the cookies weren't saved or updated. |
| 2112 | { |
| 2113 | TestNetworkDelegate network_delegate; |
| 2114 | default_context_.set_network_delegate(&network_delegate); |
| 2115 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2116 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2117 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2118 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2119 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2120 | |
| 2121 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2122 | == std::string::npos); |
| 2123 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2124 | != std::string::npos); |
| 2125 | |
| 2126 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2127 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2128 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2133 | LocalHttpTestServer test_server; |
| 2134 | ASSERT_TRUE(test_server.Start()); |
| 2135 | |
| 2136 | // Set up a cookie. |
| 2137 | { |
| 2138 | TestNetworkDelegate network_delegate; |
| 2139 | default_context_.set_network_delegate(&network_delegate); |
| 2140 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2141 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2142 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
| 2143 | &d, NULL)); |
| 2144 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2145 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2146 | |
| 2147 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2148 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2149 | } |
| 2150 | |
| 2151 | // Verify that the cookie is set. |
| 2152 | { |
| 2153 | TestNetworkDelegate network_delegate; |
| 2154 | default_context_.set_network_delegate(&network_delegate); |
| 2155 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2156 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2157 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2158 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2159 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2160 | |
| 2161 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2162 | != std::string::npos); |
| 2163 | |
| 2164 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2165 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2166 | } |
| 2167 | |
| 2168 | // Verify that the cookie isn't sent. |
| 2169 | { |
| 2170 | TestNetworkDelegate network_delegate; |
| 2171 | default_context_.set_network_delegate(&network_delegate); |
| 2172 | TestDelegate d; |
| 2173 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2174 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2175 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2176 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2177 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2178 | |
| 2179 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2180 | == std::string::npos); |
| 2181 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2182 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2183 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2188 | LocalHttpTestServer test_server; |
| 2189 | ASSERT_TRUE(test_server.Start()); |
| 2190 | |
| 2191 | // Set up a cookie. |
| 2192 | { |
| 2193 | TestNetworkDelegate network_delegate; |
| 2194 | default_context_.set_network_delegate(&network_delegate); |
| 2195 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2196 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2197 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
| 2198 | &d, NULL)); |
| 2199 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2200 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2201 | |
| 2202 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2203 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2204 | } |
| 2205 | |
| 2206 | // Try to set-up another cookie and update the previous cookie. |
| 2207 | { |
| 2208 | TestNetworkDelegate network_delegate; |
| 2209 | default_context_.set_network_delegate(&network_delegate); |
| 2210 | TestDelegate d; |
| 2211 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2212 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2213 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2214 | DEFAULT_PRIORITY, &d, NULL)); |
| 2215 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2216 | |
[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(2, network_delegate.blocked_set_cookie_count()); |
| 2221 | } |
| 2222 | |
| 2223 | // Verify the cookies weren't saved or updated. |
| 2224 | { |
| 2225 | TestNetworkDelegate network_delegate; |
| 2226 | default_context_.set_network_delegate(&network_delegate); |
| 2227 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2228 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2229 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2230 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2231 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2232 | |
| 2233 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2234 | == std::string::npos); |
| 2235 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2236 | != std::string::npos); |
| 2237 | |
| 2238 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2239 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2240 | } |
| 2241 | } |
| 2242 | |
| 2243 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2244 | LocalHttpTestServer test_server; |
| 2245 | ASSERT_TRUE(test_server.Start()); |
| 2246 | |
| 2247 | // Set up an empty cookie. |
| 2248 | { |
| 2249 | TestNetworkDelegate network_delegate; |
| 2250 | default_context_.set_network_delegate(&network_delegate); |
| 2251 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2252 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2253 | test_server.GetURL("set-cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2254 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2255 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2256 | |
| 2257 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2258 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2259 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2260 | } |
| 2261 | } |
| 2262 | |
| 2263 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2264 | LocalHttpTestServer test_server; |
| 2265 | ASSERT_TRUE(test_server.Start()); |
| 2266 | |
| 2267 | // Set up a cookie. |
| 2268 | { |
| 2269 | TestNetworkDelegate network_delegate; |
| 2270 | default_context_.set_network_delegate(&network_delegate); |
| 2271 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2272 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2273 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
| 2274 | &d, NULL)); |
| 2275 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2276 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2277 | |
| 2278 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2279 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2280 | } |
| 2281 | |
| 2282 | // Verify that the cookie is set. |
| 2283 | { |
| 2284 | TestNetworkDelegate network_delegate; |
| 2285 | default_context_.set_network_delegate(&network_delegate); |
| 2286 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2287 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2288 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2289 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2290 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2291 | |
| 2292 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2293 | != std::string::npos); |
| 2294 | |
| 2295 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2296 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2297 | } |
| 2298 | |
| 2299 | // Verify that the cookie isn't sent. |
| 2300 | { |
| 2301 | TestNetworkDelegate network_delegate; |
| 2302 | default_context_.set_network_delegate(&network_delegate); |
| 2303 | TestDelegate d; |
| 2304 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2305 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2306 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2307 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2308 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2309 | |
| 2310 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2311 | == std::string::npos); |
| 2312 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2313 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2314 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2319 | LocalHttpTestServer test_server; |
| 2320 | ASSERT_TRUE(test_server.Start()); |
| 2321 | |
| 2322 | // Set up a cookie. |
| 2323 | { |
| 2324 | TestNetworkDelegate network_delegate; |
| 2325 | default_context_.set_network_delegate(&network_delegate); |
| 2326 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2327 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2328 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
| 2329 | &d, NULL)); |
| 2330 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2331 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2332 | |
| 2333 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2334 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2335 | } |
| 2336 | |
| 2337 | // Try to set-up another cookie and update the previous cookie. |
| 2338 | { |
| 2339 | TestNetworkDelegate network_delegate; |
| 2340 | default_context_.set_network_delegate(&network_delegate); |
| 2341 | TestDelegate d; |
| 2342 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2343 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2344 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2345 | DEFAULT_PRIORITY, &d, NULL)); |
| 2346 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2347 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2348 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2349 | |
| 2350 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2351 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2352 | } |
| 2353 | |
| 2354 | // Verify the cookies weren't saved or updated. |
| 2355 | { |
| 2356 | TestNetworkDelegate network_delegate; |
| 2357 | default_context_.set_network_delegate(&network_delegate); |
| 2358 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2359 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2360 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2361 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2362 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2363 | |
| 2364 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2365 | == std::string::npos); |
| 2366 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2367 | != std::string::npos); |
| 2368 | |
| 2369 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2370 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2371 | } |
| 2372 | } |
| 2373 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2374 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 2375 | // value for the |fixed_date| argument given to the constructor. |
| 2376 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 2377 | public: |
| 2378 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 2379 | : fixed_date_(fixed_date) {} |
| 2380 | virtual ~FixedDateNetworkDelegate() {} |
| 2381 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2382 | // NetworkDelegate implementation |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2383 | virtual int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2384 | URLRequest* request, |
| 2385 | const CompletionCallback& callback, |
| 2386 | const HttpResponseHeaders* original_response_headers, |
| 2387 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2388 | GURL* allowed_unsafe_redirect_url) OVERRIDE; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2389 | |
| 2390 | private: |
| 2391 | std::string fixed_date_; |
| 2392 | |
| 2393 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 2394 | }; |
| 2395 | |
| 2396 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2397 | URLRequest* request, |
| 2398 | const CompletionCallback& callback, |
| 2399 | const HttpResponseHeaders* original_response_headers, |
| 2400 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2401 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2402 | HttpResponseHeaders* new_response_headers = |
| 2403 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2404 | |
| 2405 | new_response_headers->RemoveHeader("Date"); |
| 2406 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 2407 | |
| 2408 | *override_response_headers = new_response_headers; |
| 2409 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 2410 | callback, |
| 2411 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2412 | override_response_headers, |
| 2413 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2414 | } |
| 2415 | |
| 2416 | // Test that cookie expiration times are adjusted for server/client clock |
| 2417 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 2418 | // headers by defaulting to GMT. (crbug.com/135131) |
| 2419 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 2420 | LocalHttpTestServer test_server; |
| 2421 | ASSERT_TRUE(test_server.Start()); |
| 2422 | |
| 2423 | // Set up an expired cookie. |
| 2424 | { |
| 2425 | TestNetworkDelegate network_delegate; |
| 2426 | default_context_.set_network_delegate(&network_delegate); |
| 2427 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2428 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2429 | test_server.GetURL( |
| 2430 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2431 | DEFAULT_PRIORITY, &d, NULL)); |
| 2432 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2433 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2434 | } |
| 2435 | // Verify that the cookie is not set. |
| 2436 | { |
| 2437 | TestNetworkDelegate network_delegate; |
| 2438 | default_context_.set_network_delegate(&network_delegate); |
| 2439 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2440 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2441 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2442 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2443 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2444 | |
| 2445 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 2446 | } |
| 2447 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 2448 | { |
| 2449 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 2450 | default_context_.set_network_delegate(&network_delegate); |
| 2451 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2452 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2453 | test_server.GetURL( |
| 2454 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2455 | DEFAULT_PRIORITY, &d, NULL)); |
| 2456 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2457 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2458 | } |
| 2459 | // Verify that the cookie is set. |
| 2460 | { |
| 2461 | TestNetworkDelegate network_delegate; |
| 2462 | default_context_.set_network_delegate(&network_delegate); |
| 2463 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2464 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2465 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d, NULL)); |
| 2466 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2467 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2468 | |
| 2469 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 2470 | } |
| 2471 | } |
| 2472 | |
| 2473 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2474 | // Check that it is impossible to change the referrer in the extra headers of |
| 2475 | // an URLRequest. |
| 2476 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 2477 | LocalHttpTestServer test_server; |
| 2478 | ASSERT_TRUE(test_server.Start()); |
| 2479 | |
| 2480 | // If extra headers contain referer and the request contains a referer, |
| 2481 | // only the latter shall be respected. |
| 2482 | { |
| 2483 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2484 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2485 | test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
| 2486 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2487 | |
| 2488 | HttpRequestHeaders headers; |
| 2489 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2490 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2491 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2492 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2493 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2494 | |
| 2495 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 2496 | } |
| 2497 | |
| 2498 | // If extra headers contain a referer but the request does not, no referer |
| 2499 | // shall be sent in the header. |
| 2500 | { |
| 2501 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2502 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2503 | test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2504 | |
| 2505 | HttpRequestHeaders headers; |
| 2506 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2507 | req->SetExtraRequestHeaders(headers); |
| 2508 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2509 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2510 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2511 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2512 | |
| 2513 | EXPECT_EQ("None", d.data_received()); |
| 2514 | } |
| 2515 | } |
| 2516 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2517 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2518 | public: |
| 2519 | URLRequestTestHTTP() |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 2520 | : test_server_(base::FilePath(FILE_PATH_LITERAL( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2521 | "net/data/url_request_unittest"))) { |
| 2522 | } |
| 2523 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2524 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2525 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 2526 | // |request_method| is the method to use for the initial request. |
| 2527 | // |redirect_method| is the method that is expected to be used for the second |
| 2528 | // request, after redirection. |
| 2529 | // If |include_data| is true, data is uploaded with the request. The |
| 2530 | // response body is expected to match it exactly, if and only if |
| 2531 | // |request_method| == |redirect_method|. |
| 2532 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 2533 | const std::string& request_method, |
| 2534 | const std::string& redirect_method, |
| 2535 | bool include_data) { |
| 2536 | static const char kData[] = "hello world"; |
| 2537 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2538 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2539 | redirect_url, DEFAULT_PRIORITY, &d, NULL)); |
| 2540 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2541 | if (include_data) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2542 | req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2543 | HttpRequestHeaders headers; |
| 2544 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 2545 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2546 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2547 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2548 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2549 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2550 | EXPECT_EQ(redirect_method, req->method()); |
| 2551 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 2552 | EXPECT_EQ(OK, req->status().error()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2553 | if (include_data) { |
| 2554 | if (request_method == redirect_method) { |
| 2555 | EXPECT_EQ(kData, d.data_received()); |
| 2556 | } else { |
| 2557 | EXPECT_NE(kData, d.data_received()); |
| 2558 | } |
| 2559 | } |
| 2560 | if (HasFailure()) |
| 2561 | LOG(WARNING) << "Request method was: " << request_method; |
| 2562 | } |
| 2563 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2564 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2565 | const int kMsgSize = 20000; // multiple of 10 |
| 2566 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 2567 | char* uploadBytes = new char[kMsgSize+1]; |
| 2568 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2569 | char marker = 'a'; |
| 2570 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 2571 | memcpy(ptr, "----------", 10); |
| 2572 | ptr += 10; |
| 2573 | if (idx % 100 == 0) { |
| 2574 | ptr--; |
| 2575 | *ptr++ = marker; |
| 2576 | if (++marker > 'z') |
| 2577 | marker = 'a'; |
| 2578 | } |
| 2579 | } |
| 2580 | uploadBytes[kMsgSize] = '\0'; |
| 2581 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2582 | for (int i = 0; i < kIterations; ++i) { |
| 2583 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2584 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 2585 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 2586 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2587 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2588 | r->set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes))); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2589 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2590 | r->Start(); |
| 2591 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2592 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2593 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2594 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2595 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2596 | << "request failed: " << r->status().status() |
| 2597 | << ", os error: " << r->status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2598 | |
| 2599 | EXPECT_FALSE(d.received_data_before_response()); |
| 2600 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2601 | } |
| 2602 | delete[] uploadBytes; |
| 2603 | } |
| 2604 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2605 | void AddChunksToUpload(URLRequest* r) { |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 2606 | r->AppendChunkToUpload("a", 1, false); |
| 2607 | r->AppendChunkToUpload("bcd", 3, false); |
| 2608 | r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); |
| 2609 | r->AppendChunkToUpload("\r\n\r\n", 4, false); |
| 2610 | r->AppendChunkToUpload("0", 1, false); |
| 2611 | r->AppendChunkToUpload("2323", 4, true); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2612 | } |
| 2613 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2614 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2615 | // This should match the chunks sent by AddChunksToUpload(). |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2616 | const std::string expected_data = |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2617 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 2618 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2619 | ASSERT_EQ(1, d->response_started_count()) |
| 2620 | << "request failed: " << r->status().status() |
| 2621 | << ", os error: " << r->status().error(); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2622 | |
| 2623 | EXPECT_FALSE(d->received_data_before_response()); |
| 2624 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2625 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 2626 | EXPECT_EQ(expected_data, d->data_received()); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2627 | } |
| 2628 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2629 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2630 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2631 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 2632 | test_server_.GetURL("set-many-cookies?" + |
| 2633 | base::IntToString(num_cookies)), |
| 2634 | DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2635 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2636 | r->Start(); |
| 2637 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2638 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2639 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2640 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2641 | bool is_success = r->status().is_success(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2642 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2643 | if (!is_success) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2644 | EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2645 | // The test server appears to be unable to handle subsequent requests |
| 2646 | // after this error is triggered. Force it to restart. |
| 2647 | EXPECT_TRUE(test_server_.Stop()); |
| 2648 | EXPECT_TRUE(test_server_.Start()); |
| 2649 | } |
| 2650 | |
| 2651 | return is_success; |
| 2652 | } |
| 2653 | |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2654 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2655 | }; |
| 2656 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2657 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 2658 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 2659 | // The HTTPTestServer will return a 302 response, which we should not |
| 2660 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2661 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2662 | ASSERT_TRUE(test_server_.Start()); |
| 2663 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2664 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2665 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2666 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2667 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2668 | TestDelegate d; |
| 2669 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2670 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2671 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, NULL)); |
| 2672 | r->Start(); |
| 2673 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2674 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2675 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2676 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2677 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2678 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2679 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 2680 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 2681 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2682 | // We should not have followed the redirect. |
| 2683 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2684 | } |
| 2685 | } |
| 2686 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2687 | // This is the same as the previous test, but checks that the network delegate |
| 2688 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 2689 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2690 | ASSERT_TRUE(test_server_.Start()); |
| 2691 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2692 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2693 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2694 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2695 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2696 | TestDelegate d; |
| 2697 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2698 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2699 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d, NULL)); |
| 2700 | r->Start(); |
| 2701 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2702 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2703 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2704 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2705 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2706 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2707 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 2708 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2709 | EXPECT_EQ(1, d.response_started_count()); |
| 2710 | // We should not have followed the redirect. |
| 2711 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2712 | |
| 2713 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2714 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2718 | // Tests that we can block and asynchronously return OK in various stages. |
| 2719 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 2720 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 2721 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 2722 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 2723 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 2724 | }; |
| 2725 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 2726 | |
| 2727 | ASSERT_TRUE(test_server_.Start()); |
| 2728 | |
| 2729 | TestDelegate d; |
| 2730 | BlockingNetworkDelegate network_delegate( |
| 2731 | BlockingNetworkDelegate::USER_CALLBACK); |
| 2732 | network_delegate.set_block_on( |
| 2733 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 2734 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 2735 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 2736 | |
| 2737 | TestURLRequestContext context(true); |
| 2738 | context.set_network_delegate(&network_delegate); |
| 2739 | context.Init(); |
| 2740 | |
| 2741 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2742 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2743 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2744 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2745 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2746 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2747 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2748 | EXPECT_EQ(blocking_stages[i], |
| 2749 | network_delegate.stage_blocked_for_callback()); |
| 2750 | network_delegate.DoCallback(OK); |
| 2751 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2752 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2753 | EXPECT_EQ(200, r->GetResponseCode()); |
| 2754 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2755 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2756 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2757 | } |
| 2758 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2759 | } |
| 2760 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2761 | // Tests that the network delegate can block and cancel a request. |
| 2762 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
| 2763 | ASSERT_TRUE(test_server_.Start()); |
| 2764 | |
| 2765 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2766 | BlockingNetworkDelegate network_delegate( |
| 2767 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2768 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 2769 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2770 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2771 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2772 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2773 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2774 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2775 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2776 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2777 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2778 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2779 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2780 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2781 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2782 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2783 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 2784 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2785 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2786 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2787 | } |
| 2788 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2789 | } |
| 2790 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2791 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 2792 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 2793 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 2794 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 2795 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 2796 | BlockingNetworkDelegate::Stage stage, |
| 2797 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2798 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2799 | BlockingNetworkDelegate network_delegate(block_mode); |
| 2800 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 2801 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2802 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2803 | TestURLRequestContext context(true); |
| 2804 | context.set_network_delegate(&network_delegate); |
| 2805 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2806 | |
| 2807 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2808 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2809 | url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2810 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2811 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2812 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2813 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2814 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2815 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2816 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 2817 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2818 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2819 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2820 | } |
| 2821 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2822 | } |
| 2823 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2824 | // The following 3 tests check that the network delegate can cancel a request |
| 2825 | // synchronously in various stages of the request. |
| 2826 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
| 2827 | ASSERT_TRUE(test_server_.Start()); |
| 2828 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2829 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2830 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
| 2834 | ASSERT_TRUE(test_server_.Start()); |
| 2835 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2836 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2837 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
| 2841 | ASSERT_TRUE(test_server_.Start()); |
| 2842 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2843 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2844 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | // The following 3 tests check that the network delegate can cancel a request |
| 2848 | // asynchronously in various stages of the request. |
| 2849 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
| 2850 | ASSERT_TRUE(test_server_.Start()); |
| 2851 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2852 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2853 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
| 2857 | ASSERT_TRUE(test_server_.Start()); |
| 2858 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2859 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2860 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
| 2864 | ASSERT_TRUE(test_server_.Start()); |
| 2865 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2866 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2867 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2868 | } |
| 2869 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2870 | // Tests that the network delegate can block and redirect a request to a new |
| 2871 | // URL. |
| 2872 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
| 2873 | ASSERT_TRUE(test_server_.Start()); |
| 2874 | |
| 2875 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2876 | BlockingNetworkDelegate network_delegate( |
| 2877 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2878 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2879 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2880 | network_delegate.set_redirect_url(redirect_url); |
| 2881 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2882 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2883 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2884 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2885 | { |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 2886 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2887 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2888 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2889 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2890 | // Quit after hitting the redirect, so can check the headers. |
| 2891 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2892 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2893 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2894 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2895 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2896 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 2897 | EXPECT_EQ(307, r->GetResponseCode()); |
| 2898 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2899 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2900 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 2901 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2902 | EXPECT_EQ(redirect_url, GURL(location)); |
| 2903 | |
| 2904 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2905 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2906 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2907 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 2908 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 2909 | EXPECT_EQ( |
| 2910 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 2911 | EXPECT_TRUE( |
| 2912 | network_delegate.last_observed_proxy().Equals( |
| 2913 | test_server_.host_port_pair())); |
| 2914 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2915 | EXPECT_EQ(0, r->status().error()); |
| 2916 | EXPECT_EQ(redirect_url, r->url()); |
| 2917 | EXPECT_EQ(original_url, r->original_url()); |
| 2918 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2919 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2920 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2921 | } |
| 2922 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2923 | } |
| 2924 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2925 | // Tests that the network delegate can block and redirect a request to a new |
| 2926 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 2927 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
| 2928 | ASSERT_TRUE(test_server_.Start()); |
| 2929 | |
| 2930 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2931 | BlockingNetworkDelegate network_delegate( |
| 2932 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2933 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2934 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2935 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2936 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2937 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2938 | |
| 2939 | { |
| 2940 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2941 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2942 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2943 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2944 | // Quit after hitting the redirect, so can check the headers. |
| 2945 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2946 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2947 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2948 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2949 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2950 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 2951 | EXPECT_EQ(307, r->GetResponseCode()); |
| 2952 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2953 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2954 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 2955 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2956 | EXPECT_EQ(redirect_url, GURL(location)); |
| 2957 | |
| 2958 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2959 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 2960 | base::RunLoop().Run(); |
| 2961 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2962 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 2963 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 2964 | EXPECT_EQ( |
| 2965 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 2966 | EXPECT_TRUE( |
| 2967 | network_delegate.last_observed_proxy().Equals( |
| 2968 | test_server_.host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2969 | EXPECT_EQ(0, r->status().error()); |
| 2970 | EXPECT_EQ(redirect_url, r->url()); |
| 2971 | EXPECT_EQ(original_url, r->original_url()); |
| 2972 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2973 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2974 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2975 | } |
| 2976 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2977 | } |
| 2978 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2979 | // Tests that redirects caused by the network delegate preserve POST data. |
| 2980 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 2981 | ASSERT_TRUE(test_server_.Start()); |
| 2982 | |
| 2983 | const char kData[] = "hello world"; |
| 2984 | |
| 2985 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2986 | BlockingNetworkDelegate network_delegate( |
| 2987 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2988 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2989 | GURL redirect_url(test_server_.GetURL("echo")); |
| 2990 | network_delegate.set_redirect_url(redirect_url); |
| 2991 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2992 | TestURLRequestContext context(true); |
| 2993 | context.set_network_delegate(&network_delegate); |
| 2994 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2995 | |
| 2996 | { |
| 2997 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2998 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 2999 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 3000 | r->set_method("POST"); |
| 3001 | r->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3002 | HttpRequestHeaders headers; |
| 3003 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3004 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3005 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3006 | |
| 3007 | // Quit after hitting the redirect, so can check the headers. |
| 3008 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3009 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3010 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3011 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3012 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3013 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3014 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3015 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3016 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3017 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3018 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3019 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3020 | |
| 3021 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3022 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3023 | base::RunLoop().Run(); |
| 3024 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3025 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3026 | EXPECT_EQ(0, r->status().error()); |
| 3027 | EXPECT_EQ(redirect_url, r->url()); |
| 3028 | EXPECT_EQ(original_url, r->original_url()); |
| 3029 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3030 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3031 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3032 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3033 | EXPECT_EQ(kData, d.data_received()); |
| 3034 | } |
| 3035 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3036 | } |
| 3037 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3038 | // Tests that the network delegate can block and redirect a request to a new |
| 3039 | // URL during OnHeadersReceived. |
| 3040 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
| 3041 | ASSERT_TRUE(test_server_.Start()); |
| 3042 | |
| 3043 | TestDelegate d; |
| 3044 | BlockingNetworkDelegate network_delegate( |
| 3045 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3046 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3047 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 3048 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 3049 | |
| 3050 | TestURLRequestContextWithProxy context( |
| 3051 | test_server_.host_port_pair().ToString(), &network_delegate); |
| 3052 | |
| 3053 | { |
| 3054 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3055 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3056 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3057 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3058 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3059 | base::RunLoop().Run(); |
| 3060 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3061 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3062 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3063 | EXPECT_EQ( |
| 3064 | 2, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 3065 | EXPECT_TRUE( |
| 3066 | network_delegate.last_observed_proxy().Equals( |
| 3067 | test_server_.host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3068 | |
| 3069 | EXPECT_EQ(OK, r->status().error()); |
| 3070 | EXPECT_EQ(redirect_url, r->url()); |
| 3071 | EXPECT_EQ(original_url, r->original_url()); |
| 3072 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3073 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 3074 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3075 | } |
| 3076 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3077 | } |
| 3078 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3079 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3080 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 3081 | // handle the challenge, and is passing the buck along to the |
| 3082 | // URLRequest::Delegate. |
| 3083 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 3084 | ASSERT_TRUE(test_server_.Start()); |
| 3085 | |
| 3086 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3087 | BlockingNetworkDelegate network_delegate( |
| 3088 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3089 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3090 | TestURLRequestContext context(true); |
| 3091 | context.set_network_delegate(&network_delegate); |
| 3092 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3093 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3094 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3095 | |
| 3096 | { |
| 3097 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3098 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3099 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3100 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3101 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3102 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3103 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3104 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3105 | EXPECT_EQ(0, r->status().error()); |
| 3106 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3107 | EXPECT_TRUE(d.auth_required_called()); |
| 3108 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3109 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3110 | } |
| 3111 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3112 | } |
| 3113 | |
| 3114 | TEST_F(URLRequestTestHTTP, |
| 3115 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
| 3116 | ASSERT_TRUE(test_server_.Start()); |
| 3117 | |
| 3118 | TestDelegate d; |
| 3119 | BlockingNetworkDelegate network_delegate( |
| 3120 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3121 | |
| 3122 | TestURLRequestContext context(true); |
| 3123 | context.set_network_delegate(&network_delegate); |
| 3124 | context.Init(); |
| 3125 | |
| 3126 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3127 | |
| 3128 | { |
| 3129 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3130 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3131 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3132 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3133 | |
| 3134 | { |
| 3135 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3136 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3137 | EXPECT_FALSE(headers.HasHeader("Authorization")); |
| 3138 | } |
| 3139 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3140 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3141 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3142 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3143 | EXPECT_EQ(0, r->status().error()); |
| 3144 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3145 | EXPECT_TRUE(d.auth_required_called()); |
| 3146 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3147 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3148 | } |
| 3149 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3150 | } |
| 3151 | |
| 3152 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 3153 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3154 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 3155 | ASSERT_TRUE(test_server_.Start()); |
| 3156 | |
| 3157 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3158 | BlockingNetworkDelegate network_delegate( |
| 3159 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3160 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3161 | network_delegate.set_auth_retval( |
| 3162 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3163 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3164 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3165 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3166 | TestURLRequestContext context(true); |
| 3167 | context.set_network_delegate(&network_delegate); |
| 3168 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3169 | |
| 3170 | { |
| 3171 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3172 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3173 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3174 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3175 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3176 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3177 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3178 | EXPECT_EQ(0, r->status().error()); |
| 3179 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3180 | EXPECT_FALSE(d.auth_required_called()); |
| 3181 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3182 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3183 | } |
| 3184 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3185 | } |
| 3186 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3187 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 3188 | // headers (for the first or second request) when called at the proper times. |
| 3189 | TEST_F(URLRequestTestHTTP, |
| 3190 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
| 3191 | ASSERT_TRUE(test_server_.Start()); |
| 3192 | |
| 3193 | TestDelegate d; |
| 3194 | BlockingNetworkDelegate network_delegate( |
| 3195 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3196 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 3197 | network_delegate.set_auth_retval( |
| 3198 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3199 | |
| 3200 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 3201 | |
| 3202 | TestURLRequestContext context(true); |
| 3203 | context.set_network_delegate(&network_delegate); |
| 3204 | context.Init(); |
| 3205 | |
| 3206 | { |
| 3207 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3208 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3209 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3210 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3211 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3212 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3213 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3214 | EXPECT_EQ(0, r->status().error()); |
| 3215 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3216 | EXPECT_FALSE(d.auth_required_called()); |
| 3217 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3218 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3219 | |
| 3220 | { |
| 3221 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3222 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3223 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 3224 | } |
| 3225 | } |
| 3226 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3227 | } |
| 3228 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3229 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3230 | // by cancelling authentication. |
| 3231 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 3232 | ASSERT_TRUE(test_server_.Start()); |
| 3233 | |
| 3234 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3235 | BlockingNetworkDelegate network_delegate( |
| 3236 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3237 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3238 | network_delegate.set_auth_retval( |
| 3239 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3240 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3241 | TestURLRequestContext context(true); |
| 3242 | context.set_network_delegate(&network_delegate); |
| 3243 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3244 | |
| 3245 | { |
| 3246 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3247 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3248 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3249 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3250 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3251 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3252 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3253 | EXPECT_EQ(OK, r->status().error()); |
| 3254 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3255 | EXPECT_FALSE(d.auth_required_called()); |
| 3256 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3257 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3258 | } |
| 3259 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3260 | } |
| 3261 | |
| 3262 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3263 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 3264 | // to handle the challenge, and is passing the buck along to the |
| 3265 | // URLRequest::Delegate. |
| 3266 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 3267 | ASSERT_TRUE(test_server_.Start()); |
| 3268 | |
| 3269 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3270 | BlockingNetworkDelegate network_delegate( |
| 3271 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3272 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3273 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3274 | TestURLRequestContext context(true); |
| 3275 | context.set_network_delegate(&network_delegate); |
| 3276 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3277 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3278 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3279 | |
| 3280 | { |
| 3281 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3282 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3283 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3284 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3285 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3286 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3287 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3288 | EXPECT_EQ(0, r->status().error()); |
| 3289 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3290 | EXPECT_TRUE(d.auth_required_called()); |
| 3291 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3292 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3293 | } |
| 3294 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3295 | } |
| 3296 | |
| 3297 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3298 | // by setting credentials. |
| 3299 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 3300 | ASSERT_TRUE(test_server_.Start()); |
| 3301 | |
| 3302 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3303 | BlockingNetworkDelegate network_delegate( |
| 3304 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3305 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3306 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3307 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3308 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3309 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3310 | network_delegate.set_auth_credentials(auth_credentials); |
| 3311 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3312 | TestURLRequestContext context(true); |
| 3313 | context.set_network_delegate(&network_delegate); |
| 3314 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3315 | |
| 3316 | { |
| 3317 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3318 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3319 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3320 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3321 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3322 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3323 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3324 | EXPECT_EQ(0, r->status().error()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3325 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3326 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3327 | EXPECT_FALSE(d.auth_required_called()); |
| 3328 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3329 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3330 | } |
| 3331 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3332 | } |
| 3333 | |
| 3334 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3335 | // by cancelling authentication. |
| 3336 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 3337 | ASSERT_TRUE(test_server_.Start()); |
| 3338 | |
| 3339 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3340 | BlockingNetworkDelegate network_delegate( |
| 3341 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3342 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3343 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3344 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3345 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3346 | TestURLRequestContext context(true); |
| 3347 | context.set_network_delegate(&network_delegate); |
| 3348 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3349 | |
| 3350 | { |
| 3351 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3352 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3353 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 3354 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3355 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3356 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3357 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3358 | EXPECT_EQ(OK, r->status().error()); |
| 3359 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3360 | EXPECT_FALSE(d.auth_required_called()); |
| 3361 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3362 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3363 | } |
| 3364 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3365 | } |
| 3366 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3367 | // Tests that we can handle when a network request was canceled while we were |
| 3368 | // waiting for the network delegate. |
| 3369 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 3370 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 3371 | ASSERT_TRUE(test_server_.Start()); |
| 3372 | |
| 3373 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3374 | BlockingNetworkDelegate network_delegate( |
| 3375 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3376 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3377 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3378 | TestURLRequestContext context(true); |
| 3379 | context.set_network_delegate(&network_delegate); |
| 3380 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3381 | |
| 3382 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3383 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3384 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3385 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3386 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3387 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3388 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3389 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3390 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3391 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3392 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3393 | // Ensure that network delegate is notified. |
| 3394 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3395 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3396 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3397 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3398 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3399 | } |
| 3400 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3401 | } |
| 3402 | |
| 3403 | // Tests that we can handle when a network request was canceled while we were |
| 3404 | // waiting for the network delegate. |
| 3405 | // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 3406 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
| 3407 | ASSERT_TRUE(test_server_.Start()); |
| 3408 | |
| 3409 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3410 | BlockingNetworkDelegate network_delegate( |
| 3411 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3412 | network_delegate.set_block_on( |
| 3413 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3414 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3415 | TestURLRequestContext context(true); |
| 3416 | context.set_network_delegate(&network_delegate); |
| 3417 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3418 | |
| 3419 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3420 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3421 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3422 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3423 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3424 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3425 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3426 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3427 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3428 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3429 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3430 | // Ensure that network delegate is notified. |
| 3431 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3432 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3433 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3434 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3435 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3436 | } |
| 3437 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3438 | } |
| 3439 | |
| 3440 | // Tests that we can handle when a network request was canceled while we were |
| 3441 | // waiting for the network delegate. |
| 3442 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 3443 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 3444 | ASSERT_TRUE(test_server_.Start()); |
| 3445 | |
| 3446 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3447 | BlockingNetworkDelegate network_delegate( |
| 3448 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3449 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3450 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3451 | TestURLRequestContext context(true); |
| 3452 | context.set_network_delegate(&network_delegate); |
| 3453 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3454 | |
| 3455 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3456 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3457 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3458 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3459 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3460 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3461 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 3462 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3463 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3464 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3465 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3466 | // Ensure that network delegate is notified. |
| 3467 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3468 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3469 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3470 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3471 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3472 | } |
| 3473 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3474 | } |
| 3475 | |
| 3476 | // Tests that we can handle when a network request was canceled while we were |
| 3477 | // waiting for the network delegate. |
| 3478 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 3479 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3480 | ASSERT_TRUE(test_server_.Start()); |
| 3481 | |
| 3482 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3483 | BlockingNetworkDelegate network_delegate( |
| 3484 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3485 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3486 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3487 | TestURLRequestContext context(true); |
| 3488 | context.set_network_delegate(&network_delegate); |
| 3489 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3490 | |
| 3491 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3492 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3493 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3494 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3495 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3496 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3497 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 3498 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3499 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3500 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3501 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3502 | // Ensure that network delegate is notified. |
| 3503 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3504 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3505 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3506 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3507 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3508 | } |
| 3509 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3510 | } |
| 3511 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3512 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 3513 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 3514 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3515 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3516 | ASSERT_TRUE(test_server_.Start()); |
| 3517 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3518 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3519 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3520 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3521 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3522 | TestDelegate d; |
| 3523 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3524 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3525 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3526 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3527 | r->Start(); |
| 3528 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3529 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3530 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3531 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3532 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3533 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3534 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3535 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3539 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3540 | ASSERT_TRUE(test_server_.Start()); |
| 3541 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3542 | TestDelegate d; |
| 3543 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3544 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3545 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3546 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3547 | r->Start(); |
| 3548 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3549 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3550 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3551 | |
| 3552 | EXPECT_EQ(1, d.response_started_count()); |
| 3553 | EXPECT_FALSE(d.received_data_before_response()); |
| 3554 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3555 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3556 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3557 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3558 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 3559 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 3560 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3561 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3562 | } |
| 3563 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3564 | // This test has the server send a large number of cookies to the client. |
| 3565 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 3566 | // search is used to estimate that maximum number of cookies that are accepted |
| 3567 | // by the browser. Beyond the maximum number, the request will fail with |
| 3568 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 3569 | #if defined(OS_WIN) |
| 3570 | // http://crbug.com/177916 |
| 3571 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 3572 | #else |
| 3573 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 3574 | #endif // defined(OS_WIN) |
| 3575 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3576 | ASSERT_TRUE(test_server_.Start()); |
| 3577 | |
| 3578 | int lower_bound = 0; |
| 3579 | int upper_bound = 1; |
| 3580 | |
| 3581 | // Double the number of cookies until the response header limits are |
| 3582 | // exceeded. |
| 3583 | while (DoManyCookiesRequest(upper_bound)) { |
| 3584 | lower_bound = upper_bound; |
| 3585 | upper_bound *= 2; |
| 3586 | ASSERT_LT(upper_bound, 1000000); |
| 3587 | } |
| 3588 | |
| 3589 | int tolerance = upper_bound * 0.005; |
| 3590 | if (tolerance < 2) |
| 3591 | tolerance = 2; |
| 3592 | |
| 3593 | // Perform a binary search to find the highest possible number of cookies, |
| 3594 | // within the desired tolerance. |
| 3595 | while (upper_bound - lower_bound >= tolerance) { |
| 3596 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 3597 | |
| 3598 | if (DoManyCookiesRequest(num_cookies)) |
| 3599 | lower_bound = num_cookies; |
| 3600 | else |
| 3601 | upper_bound = num_cookies; |
| 3602 | } |
| 3603 | // Success: the test did not crash. |
| 3604 | } |
| 3605 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3606 | TEST_F(URLRequestTestHTTP, GetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3607 | ASSERT_TRUE(test_server_.Start()); |
| 3608 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3609 | TestDelegate d; |
| 3610 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3611 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3612 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3613 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3614 | r->Start(); |
| 3615 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3616 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3617 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3618 | |
| 3619 | EXPECT_EQ(1, d.response_started_count()); |
| 3620 | EXPECT_FALSE(d.received_data_before_response()); |
| 3621 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3622 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3623 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3624 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3625 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3626 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 3627 | } |
| 3628 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3629 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
| 3630 | ASSERT_TRUE(test_server_.Start()); |
| 3631 | |
| 3632 | TestDelegate d; |
| 3633 | { |
| 3634 | GURL test_url(test_server_.GetURL(std::string())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3635 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3636 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3637 | |
| 3638 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3639 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3640 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3641 | r->Start(); |
| 3642 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3643 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3644 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3645 | |
| 3646 | EXPECT_EQ(1, d.response_started_count()); |
| 3647 | EXPECT_FALSE(d.received_data_before_response()); |
| 3648 | EXPECT_NE(0, d.bytes_received()); |
| 3649 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3650 | r->GetSocketAddress().host()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3651 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3652 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3653 | |
| 3654 | EXPECT_TRUE(d.have_full_request_headers()); |
| 3655 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 3656 | } |
| 3657 | } |
| 3658 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3659 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
| 3660 | ASSERT_TRUE(test_server_.Start()); |
| 3661 | |
| 3662 | TestDelegate d; |
| 3663 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3664 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 3665 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3666 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3667 | r->Start(); |
| 3668 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3669 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3670 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3671 | |
| 3672 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3673 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3674 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 3675 | |
| 3676 | EXPECT_EQ(1, d.response_started_count()); |
| 3677 | EXPECT_FALSE(d.received_data_before_response()); |
| 3678 | EXPECT_NE(0, d.bytes_received()); |
| 3679 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3680 | r->GetSocketAddress().host()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3681 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3682 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3683 | } |
| 3684 | } |
| 3685 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3686 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
| 3687 | ASSERT_TRUE(test_server_.Start()); |
| 3688 | |
| 3689 | // Parameter that specifies the Content-Length field in the response: |
| 3690 | // C - Compressed length. |
| 3691 | // U - Uncompressed length. |
| 3692 | // L - Large length (larger than both C & U). |
| 3693 | // M - Medium length (between C & U). |
| 3694 | // S - Small length (smaller than both C & U). |
| 3695 | const char test_parameters[] = "CULMS"; |
| 3696 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 3697 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 3698 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3699 | // S has too little data, but we seem to accept it. |
| 3700 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 3701 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3702 | |
| 3703 | for (int i = 0; i < num_tests ; i++) { |
| 3704 | TestDelegate d; |
| 3705 | { |
| 3706 | std::string test_file = |
| 3707 | base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c", |
| 3708 | test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3709 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3710 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3711 | TestURLRequestContext context(true); |
| 3712 | context.set_network_delegate(&network_delegate); |
| 3713 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3714 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3715 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 3716 | test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d, NULL)); |
| 3717 | r->Start(); |
| 3718 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3719 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3720 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3721 | |
| 3722 | EXPECT_EQ(1, d.response_started_count()); |
| 3723 | EXPECT_FALSE(d.received_data_before_response()); |
| 3724 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3725 | << " status = " << r->status().status() |
| 3726 | << " error = " << r->status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3727 | if (test_expect_success[i]) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3728 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3729 | << " Parameter = \"" << test_file << "\""; |
| 3730 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3731 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 3732 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 3733 | << " Parameter = \"" << test_file << "\""; |
| 3734 | } |
| 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3739 | TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3740 | ASSERT_TRUE(test_server_.Start()); |
| 3741 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 3742 | SpawnedTestServer https_test_server( |
| 3743 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::kLocalhost, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3744 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3745 | ASSERT_TRUE(https_test_server.Start()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3746 | |
| 3747 | // An https server is sent a request with an https referer, |
| 3748 | // and responds with a redirect to an http url. The http |
| 3749 | // server should not be sent the referer. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3750 | GURL http_destination = test_server_.GetURL(std::string()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3751 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3752 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3753 | https_test_server.GetURL("server-redirect?" + http_destination.spec()), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3754 | DEFAULT_PRIORITY, &d, NULL)); |
| 3755 | req->SetReferrer("https://www.referrer.com/"); |
| 3756 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3757 | base::RunLoop().Run(); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3758 | |
| 3759 | EXPECT_EQ(1, d.response_started_count()); |
| 3760 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3761 | EXPECT_EQ(http_destination, req->url()); |
| 3762 | EXPECT_EQ(std::string(), req->referrer()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 3763 | } |
| 3764 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3765 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
| 3766 | ASSERT_TRUE(test_server_.Start()); |
| 3767 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3768 | GURL destination_url = test_server_.GetURL(std::string()); |
| 3769 | GURL original_url = |
| 3770 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3771 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3772 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3773 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 3774 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3775 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3776 | |
| 3777 | EXPECT_EQ(1, d.response_started_count()); |
| 3778 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3779 | EXPECT_EQ(destination_url, req->url()); |
| 3780 | EXPECT_EQ(original_url, req->original_url()); |
| 3781 | ASSERT_EQ(2U, req->url_chain().size()); |
| 3782 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 3783 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3784 | |
| 3785 | LoadTimingInfo load_timing_info_before_redirect; |
| 3786 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 3787 | &load_timing_info_before_redirect)); |
| 3788 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 3789 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 3790 | |
| 3791 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3792 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3793 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 3794 | |
| 3795 | // Check that a new socket was used on redirect, since the server does not |
| 3796 | // supposed keep-alive sockets, and that the times before the redirect are |
| 3797 | // before the ones recorded for the second request. |
| 3798 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 3799 | load_timing_info.socket_log_id); |
| 3800 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 3801 | load_timing_info.connect_timing.connect_start); |
| 3802 | } |
| 3803 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3804 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
| 3805 | ASSERT_TRUE(test_server_.Start()); |
| 3806 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3807 | GURL destination_url = test_server_.GetURL(std::string()); |
| 3808 | GURL middle_redirect_url = |
| 3809 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3810 | GURL original_url = test_server_.GetURL( |
| 3811 | "server-redirect?" + middle_redirect_url.spec()); |
| 3812 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3813 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 3814 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 3815 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3816 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3817 | |
| 3818 | EXPECT_EQ(1, d.response_started_count()); |
| 3819 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3820 | EXPECT_EQ(destination_url, req->url()); |
| 3821 | EXPECT_EQ(original_url, req->original_url()); |
| 3822 | ASSERT_EQ(3U, req->url_chain().size()); |
| 3823 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 3824 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 3825 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3826 | } |
| 3827 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3828 | // First and second pieces of information logged by delegates to URLRequests. |
| 3829 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 3830 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 3831 | |
| 3832 | // Logs delegate information to a URLRequest. The first string is logged |
| 3833 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 3834 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 3835 | // another asynchronous call is used to clear the delegate information |
| 3836 | // before calling a callback. The object then deletes itself. |
| 3837 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 3838 | public: |
| 3839 | typedef base::Callback<void()> Callback; |
| 3840 | |
| 3841 | // Each time delegate information is added to the URLRequest, the resulting |
| 3842 | // load state is checked. The expected load state after each request is |
| 3843 | // passed in as an argument. |
| 3844 | static void Run(URLRequest* url_request, |
| 3845 | LoadState expected_first_load_state, |
| 3846 | LoadState expected_second_load_state, |
| 3847 | LoadState expected_third_load_state, |
| 3848 | const Callback& callback) { |
| 3849 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 3850 | url_request, |
| 3851 | expected_first_load_state, |
| 3852 | expected_second_load_state, |
| 3853 | expected_third_load_state, |
| 3854 | callback); |
| 3855 | logger->Start(); |
| 3856 | } |
| 3857 | |
| 3858 | // Checks that the log entries, starting with log_position, contain the |
| 3859 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 3860 | // recorded. Returns the index of entry after the expected number of |
| 3861 | // events this logged, or entries.size() if there aren't enough entries. |
| 3862 | static size_t CheckDelegateInfo( |
| 3863 | const CapturingNetLog::CapturedEntryList& entries, size_t log_position) { |
| 3864 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 3865 | if (log_position + 3 >= entries.size()) { |
| 3866 | ADD_FAILURE() << "Not enough log entries"; |
| 3867 | return entries.size(); |
| 3868 | } |
| 3869 | std::string delegate_info; |
| 3870 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3871 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 3872 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 3873 | &delegate_info)); |
| 3874 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 3875 | |
| 3876 | ++log_position; |
| 3877 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3878 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 3879 | |
| 3880 | ++log_position; |
| 3881 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3882 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 3883 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 3884 | &delegate_info)); |
| 3885 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 3886 | |
| 3887 | ++log_position; |
| 3888 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 3889 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 3890 | |
| 3891 | return log_position + 1; |
| 3892 | } |
| 3893 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 3894 | // Find delegate request begin and end messages for OnBeforeNetworkStart. |
| 3895 | // Returns the position of the end message. |
| 3896 | static size_t ExpectBeforeNetworkEvents( |
| 3897 | const CapturingNetLog::CapturedEntryList& entries, |
| 3898 | size_t log_position) { |
| 3899 | log_position = |
| 3900 | ExpectLogContainsSomewhereAfter(entries, |
| 3901 | log_position, |
| 3902 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 3903 | NetLog::PHASE_BEGIN); |
| 3904 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 3905 | entries[log_position + 1].type); |
| 3906 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase); |
| 3907 | return log_position + 1; |
| 3908 | } |
| 3909 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3910 | private: |
| 3911 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 3912 | |
| 3913 | AsyncDelegateLogger(URLRequest* url_request, |
| 3914 | LoadState expected_first_load_state, |
| 3915 | LoadState expected_second_load_state, |
| 3916 | LoadState expected_third_load_state, |
| 3917 | const Callback& callback) |
| 3918 | : url_request_(url_request), |
| 3919 | expected_first_load_state_(expected_first_load_state), |
| 3920 | expected_second_load_state_(expected_second_load_state), |
| 3921 | expected_third_load_state_(expected_third_load_state), |
| 3922 | callback_(callback) { |
| 3923 | } |
| 3924 | |
| 3925 | ~AsyncDelegateLogger() {} |
| 3926 | |
| 3927 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3928 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3929 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3930 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 3931 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
| 3932 | base::MessageLoop::current()->PostTask( |
| 3933 | FROM_HERE, |
| 3934 | base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
| 3935 | } |
| 3936 | |
| 3937 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3938 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3939 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3940 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 3941 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 3942 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 3943 | } else { |
| 3944 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 3945 | } |
| 3946 | base::MessageLoop::current()->PostTask( |
| 3947 | FROM_HERE, |
| 3948 | base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
| 3949 | } |
| 3950 | |
| 3951 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 3952 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3953 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 3954 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 3955 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 3956 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 3957 | callback_.Run(); |
| 3958 | } |
| 3959 | |
| 3960 | URLRequest* url_request_; |
| 3961 | const int expected_first_load_state_; |
| 3962 | const int expected_second_load_state_; |
| 3963 | const int expected_third_load_state_; |
| 3964 | const Callback callback_; |
| 3965 | |
| 3966 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 3967 | }; |
| 3968 | |
| 3969 | // NetworkDelegate that logs delegate information before a request is started, |
| 3970 | // before headers are sent, when headers are read, and when auth information |
| 3971 | // is requested. Uses AsyncDelegateLogger. |
| 3972 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 3973 | public: |
| 3974 | AsyncLoggingNetworkDelegate() {} |
| 3975 | virtual ~AsyncLoggingNetworkDelegate() {} |
| 3976 | |
| 3977 | // NetworkDelegate implementation. |
| 3978 | virtual int OnBeforeURLRequest(URLRequest* request, |
| 3979 | const CompletionCallback& callback, |
| 3980 | GURL* new_url) OVERRIDE { |
| 3981 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 3982 | return RunCallbackAsynchronously(request, callback); |
| 3983 | } |
| 3984 | |
| 3985 | virtual int OnBeforeSendHeaders(URLRequest* request, |
| 3986 | const CompletionCallback& callback, |
| 3987 | HttpRequestHeaders* headers) OVERRIDE { |
| 3988 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 3989 | return RunCallbackAsynchronously(request, callback); |
| 3990 | } |
| 3991 | |
| 3992 | virtual int OnHeadersReceived( |
| 3993 | URLRequest* request, |
| 3994 | const CompletionCallback& callback, |
| 3995 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3996 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 3997 | GURL* allowed_unsafe_redirect_url) OVERRIDE { |
| 3998 | TestNetworkDelegate::OnHeadersReceived(request, |
| 3999 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4000 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4001 | override_response_headers, |
| 4002 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4003 | return RunCallbackAsynchronously(request, callback); |
| 4004 | } |
| 4005 | |
| 4006 | virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 4007 | URLRequest* request, |
| 4008 | const AuthChallengeInfo& auth_info, |
| 4009 | const AuthCallback& callback, |
| 4010 | AuthCredentials* credentials) OVERRIDE { |
| 4011 | AsyncDelegateLogger::Run( |
| 4012 | request, |
| 4013 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4014 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4015 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4016 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 4017 | callback, credentials)); |
| 4018 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 4019 | } |
| 4020 | |
| 4021 | private: |
| 4022 | static int RunCallbackAsynchronously( |
| 4023 | URLRequest* request, |
| 4024 | const CompletionCallback& callback) { |
| 4025 | AsyncDelegateLogger::Run( |
| 4026 | request, |
| 4027 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4028 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4029 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4030 | base::Bind(callback, OK)); |
| 4031 | return ERR_IO_PENDING; |
| 4032 | } |
| 4033 | |
| 4034 | static void SetAuthAndResume(const AuthCallback& callback, |
| 4035 | AuthCredentials* credentials) { |
| 4036 | *credentials = AuthCredentials(kUser, kSecret); |
| 4037 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4038 | } |
| 4039 | |
| 4040 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 4041 | }; |
| 4042 | |
| 4043 | // URLRequest::Delegate that logs delegate information when the headers |
| 4044 | // are received, when each read completes, and during redirects. Uses |
| 4045 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 4046 | // |
| 4047 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 4048 | // advancing to the next step in most cases, as well as cancellation. |
| 4049 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 4050 | public: |
| 4051 | enum CancelStage { |
| 4052 | NO_CANCEL = 0, |
| 4053 | CANCEL_ON_RECEIVED_REDIRECT, |
| 4054 | CANCEL_ON_RESPONSE_STARTED, |
| 4055 | CANCEL_ON_READ_COMPLETED |
| 4056 | }; |
| 4057 | |
| 4058 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 4059 | : cancel_stage_(cancel_stage) { |
| 4060 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 4061 | set_cancel_in_received_redirect(true); |
| 4062 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 4063 | set_cancel_in_response_started(true); |
| 4064 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 4065 | set_cancel_in_received_data(true); |
| 4066 | } |
| 4067 | virtual ~AsyncLoggingUrlRequestDelegate() {} |
| 4068 | |
| 4069 | // URLRequest::Delegate implementation: |
| 4070 | void virtual OnReceivedRedirect(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4071 | const RedirectInfo& redirect_info, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4072 | bool* defer_redirect) OVERRIDE { |
| 4073 | *defer_redirect = true; |
| 4074 | AsyncDelegateLogger::Run( |
| 4075 | request, |
| 4076 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4077 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4078 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4079 | base::Bind( |
| 4080 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4081 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | virtual void OnResponseStarted(URLRequest* request) OVERRIDE { |
| 4085 | AsyncDelegateLogger::Run( |
| 4086 | request, |
| 4087 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4088 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4089 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4090 | base::Bind( |
| 4091 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 4092 | base::Unretained(this), request)); |
| 4093 | } |
| 4094 | |
| 4095 | virtual void OnReadCompleted(URLRequest* request, |
| 4096 | int bytes_read) OVERRIDE { |
| 4097 | AsyncDelegateLogger::Run( |
| 4098 | request, |
| 4099 | LOAD_STATE_IDLE, |
| 4100 | LOAD_STATE_IDLE, |
| 4101 | LOAD_STATE_IDLE, |
| 4102 | base::Bind( |
| 4103 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 4104 | base::Unretained(this), request, bytes_read)); |
| 4105 | } |
| 4106 | |
| 4107 | private: |
| 4108 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4109 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4110 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4111 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4112 | // FollowDeferredRedirect should not be called after cancellation. |
| 4113 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 4114 | return; |
| 4115 | if (!defer_redirect) |
| 4116 | request->FollowDeferredRedirect(); |
| 4117 | } |
| 4118 | |
| 4119 | void OnResponseStartedLoggingComplete(URLRequest* request) { |
| 4120 | // The parent class continues the request. |
| 4121 | TestDelegate::OnResponseStarted(request); |
| 4122 | } |
| 4123 | |
| 4124 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 4125 | // The parent class continues the request. |
| 4126 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 4127 | } |
| 4128 | |
| 4129 | const CancelStage cancel_stage_; |
| 4130 | |
| 4131 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 4132 | }; |
| 4133 | |
| 4134 | // Tests handling of delegate info before a request starts. |
| 4135 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
| 4136 | ASSERT_TRUE(test_server_.Start()); |
| 4137 | |
| 4138 | TestDelegate request_delegate; |
| 4139 | TestURLRequestContext context(true); |
| 4140 | context.set_network_delegate(NULL); |
| 4141 | context.set_net_log(&net_log_); |
| 4142 | context.Init(); |
| 4143 | |
| 4144 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4145 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4146 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &request_delegate, |
| 4147 | NULL)); |
| 4148 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4149 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4150 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4151 | |
| 4152 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4153 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4154 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4155 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4156 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4157 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4158 | |
| 4159 | base::RunLoop().Run(); |
| 4160 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4161 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4162 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | CapturingNetLog::CapturedEntryList entries; |
| 4166 | net_log_.GetEntries(&entries); |
| 4167 | size_t log_position = ExpectLogContainsSomewhereAfter( |
| 4168 | entries, |
| 4169 | 0, |
| 4170 | NetLog::TYPE_DELEGATE_INFO, |
| 4171 | NetLog::PHASE_BEGIN); |
| 4172 | |
| 4173 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 4174 | |
| 4175 | // Nothing else should add any delegate info to the request. |
| 4176 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4177 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4178 | } |
| 4179 | |
| 4180 | // Tests handling of delegate info from a network delegate. |
| 4181 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
| 4182 | ASSERT_TRUE(test_server_.Start()); |
| 4183 | |
| 4184 | TestDelegate request_delegate; |
| 4185 | AsyncLoggingNetworkDelegate network_delegate; |
| 4186 | TestURLRequestContext context(true); |
| 4187 | context.set_network_delegate(&network_delegate); |
| 4188 | context.set_net_log(&net_log_); |
| 4189 | context.Init(); |
| 4190 | |
| 4191 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4192 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4193 | test_server_.GetURL("simple.html"), DEFAULT_PRIORITY, &request_delegate, |
| 4194 | NULL)); |
| 4195 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4196 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4197 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4198 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4199 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4200 | base::RunLoop().Run(); |
| 4201 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4202 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4203 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4204 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4205 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4206 | } |
| 4207 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4208 | |
| 4209 | size_t log_position = 0; |
| 4210 | CapturingNetLog::CapturedEntryList entries; |
| 4211 | net_log_.GetEntries(&entries); |
| 4212 | for (size_t i = 0; i < 3; ++i) { |
| 4213 | log_position = ExpectLogContainsSomewhereAfter( |
| 4214 | entries, |
| 4215 | log_position + 1, |
| 4216 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4217 | NetLog::PHASE_BEGIN); |
| 4218 | |
| 4219 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4220 | log_position + 1); |
| 4221 | |
| 4222 | ASSERT_LT(log_position, entries.size()); |
| 4223 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4224 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4225 | |
| 4226 | if (i == 1) { |
| 4227 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4228 | entries, log_position + 1); |
| 4229 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4233 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4234 | } |
| 4235 | |
| 4236 | // Tests handling of delegate info from a network delegate in the case of an |
| 4237 | // HTTP redirect. |
| 4238 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
| 4239 | ASSERT_TRUE(test_server_.Start()); |
| 4240 | |
| 4241 | TestDelegate request_delegate; |
| 4242 | AsyncLoggingNetworkDelegate network_delegate; |
| 4243 | TestURLRequestContext context(true); |
| 4244 | context.set_network_delegate(&network_delegate); |
| 4245 | context.set_net_log(&net_log_); |
| 4246 | context.Init(); |
| 4247 | |
| 4248 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4249 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4250 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
| 4251 | &request_delegate, NULL)); |
| 4252 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4253 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4254 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4255 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4256 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4257 | base::RunLoop().Run(); |
| 4258 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4259 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4260 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4261 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4262 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4263 | } |
| 4264 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4265 | |
| 4266 | size_t log_position = 0; |
| 4267 | CapturingNetLog::CapturedEntryList entries; |
| 4268 | net_log_.GetEntries(&entries); |
| 4269 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
| 4270 | // OnBeforeSendHeaders, and OnHeadersReceived. |
| 4271 | for (size_t i = 0; i < 3; ++i) { |
| 4272 | log_position = ExpectLogContainsSomewhereAfter( |
| 4273 | entries, |
| 4274 | log_position + 1, |
| 4275 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4276 | NetLog::PHASE_BEGIN); |
| 4277 | |
| 4278 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4279 | log_position + 1); |
| 4280 | |
| 4281 | ASSERT_LT(log_position, entries.size()); |
| 4282 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4283 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4284 | |
| 4285 | if (i == 1) { |
| 4286 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4287 | entries, log_position + 1); |
| 4288 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | // The URLRequest::Delegate then gets informed about the redirect. |
| 4292 | log_position = ExpectLogContainsSomewhereAfter( |
| 4293 | entries, |
| 4294 | log_position + 1, |
| 4295 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4296 | NetLog::PHASE_BEGIN); |
| 4297 | |
| 4298 | // The NetworkDelegate logged information in the same three events as before. |
| 4299 | for (size_t i = 0; i < 3; ++i) { |
| 4300 | log_position = ExpectLogContainsSomewhereAfter( |
| 4301 | entries, |
| 4302 | log_position + 1, |
| 4303 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4304 | NetLog::PHASE_BEGIN); |
| 4305 | |
| 4306 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4307 | log_position + 1); |
| 4308 | |
| 4309 | ASSERT_LT(log_position, entries.size()); |
| 4310 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4311 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4312 | } |
| 4313 | |
| 4314 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4315 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4316 | } |
| 4317 | |
| 4318 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 4319 | // AUTH. |
| 4320 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
| 4321 | ASSERT_TRUE(test_server_.Start()); |
| 4322 | |
| 4323 | TestDelegate request_delegate; |
| 4324 | AsyncLoggingNetworkDelegate network_delegate; |
| 4325 | TestURLRequestContext context(true); |
| 4326 | context.set_network_delegate(&network_delegate); |
| 4327 | context.set_net_log(&net_log_); |
| 4328 | context.Init(); |
| 4329 | |
| 4330 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4331 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4332 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &request_delegate, |
| 4333 | NULL)); |
| 4334 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4335 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4336 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4337 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4338 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4339 | base::RunLoop().Run(); |
| 4340 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4341 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4342 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4343 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4344 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4345 | } |
| 4346 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4347 | |
| 4348 | size_t log_position = 0; |
| 4349 | CapturingNetLog::CapturedEntryList entries; |
| 4350 | net_log_.GetEntries(&entries); |
| 4351 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
| 4352 | // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in |
| 4353 | // OnBeforeURLRequest and OnBeforeSendHeaders. |
| 4354 | for (size_t i = 0; i < 6; ++i) { |
| 4355 | log_position = ExpectLogContainsSomewhereAfter( |
| 4356 | entries, |
| 4357 | log_position + 1, |
| 4358 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4359 | NetLog::PHASE_BEGIN); |
| 4360 | |
| 4361 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4362 | log_position + 1); |
| 4363 | |
| 4364 | ASSERT_LT(log_position, entries.size()); |
| 4365 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4366 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4367 | |
| 4368 | if (i == 1) { |
| 4369 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4370 | entries, log_position + 1); |
| 4371 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4375 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4376 | } |
| 4377 | |
| 4378 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 4379 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
| 4380 | ASSERT_TRUE(test_server_.Start()); |
| 4381 | |
| 4382 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4383 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4384 | TestURLRequestContext context(true); |
| 4385 | context.set_network_delegate(NULL); |
| 4386 | context.set_net_log(&net_log_); |
| 4387 | context.Init(); |
| 4388 | |
| 4389 | { |
| 4390 | // A chunked response with delays between chunks is used to make sure that |
| 4391 | // attempts by the URLRequest delegate to log information while reading the |
| 4392 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4393 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4394 | // that it occurs. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4395 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4396 | test_server_.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
| 4397 | &request_delegate, NULL)); |
| 4398 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4399 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4400 | base::RunLoop().Run(); |
| 4401 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4402 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4403 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | CapturingNetLog::CapturedEntryList entries; |
| 4407 | net_log_.GetEntries(&entries); |
| 4408 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4409 | size_t log_position = 0; |
| 4410 | |
| 4411 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4412 | entries, log_position); |
| 4413 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4414 | // The delegate info should only have been logged on header complete. Other |
| 4415 | // times it should silently be ignored. |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4416 | log_position = |
| 4417 | ExpectLogContainsSomewhereAfter(entries, |
| 4418 | log_position + 1, |
| 4419 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4420 | NetLog::PHASE_BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4421 | |
| 4422 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4423 | log_position + 1); |
| 4424 | |
| 4425 | ASSERT_LT(log_position, entries.size()); |
| 4426 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4427 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4428 | |
| 4429 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4430 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4431 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4432 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4433 | } |
| 4434 | |
| 4435 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4436 | // an HTTP redirect. |
| 4437 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
| 4438 | ASSERT_TRUE(test_server_.Start()); |
| 4439 | |
| 4440 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4441 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4442 | TestURLRequestContext context(true); |
| 4443 | context.set_network_delegate(NULL); |
| 4444 | context.set_net_log(&net_log_); |
| 4445 | context.Init(); |
| 4446 | |
| 4447 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4448 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4449 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
| 4450 | &request_delegate, NULL)); |
| 4451 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4452 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4453 | base::RunLoop().Run(); |
| 4454 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4455 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4456 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4457 | } |
| 4458 | |
| 4459 | CapturingNetLog::CapturedEntryList entries; |
| 4460 | net_log_.GetEntries(&entries); |
| 4461 | |
| 4462 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 4463 | // OnResponseStarted. |
| 4464 | size_t log_position = 0; |
| 4465 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4466 | if (i == 0) { |
| 4467 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4468 | entries, log_position) + 1; |
| 4469 | } |
| 4470 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4471 | log_position = ExpectLogContainsSomewhereAfter( |
| 4472 | entries, |
| 4473 | log_position, |
| 4474 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4475 | NetLog::PHASE_BEGIN); |
| 4476 | |
| 4477 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4478 | log_position + 1); |
| 4479 | |
| 4480 | ASSERT_LT(log_position, entries.size()); |
| 4481 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4482 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4483 | } |
| 4484 | |
| 4485 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4486 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4487 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4488 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4489 | } |
| 4490 | |
| 4491 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4492 | // an HTTP redirect, with cancellation at various points. |
| 4493 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
| 4494 | ASSERT_TRUE(test_server_.Start()); |
| 4495 | |
| 4496 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 4497 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 4498 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 4499 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 4500 | }; |
| 4501 | |
| 4502 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 4503 | ++test_case) { |
| 4504 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
| 4505 | TestURLRequestContext context(true); |
| 4506 | CapturingNetLog net_log; |
| 4507 | context.set_network_delegate(NULL); |
| 4508 | context.set_net_log(&net_log); |
| 4509 | context.Init(); |
| 4510 | |
| 4511 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4512 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4513 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
| 4514 | &request_delegate, NULL)); |
| 4515 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4516 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4517 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4518 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | CapturingNetLog::CapturedEntryList entries; |
| 4522 | net_log.GetEntries(&entries); |
| 4523 | |
| 4524 | // Delegate info is always logged in both OnReceivedRedirect and |
| 4525 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 4526 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 4527 | // still currently supported in that call. |
| 4528 | size_t log_position = 0; |
| 4529 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4530 | if (i == 0) { |
| 4531 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4532 | entries, log_position) + 1; |
| 4533 | } |
| 4534 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4535 | log_position = ExpectLogContainsSomewhereAfter( |
| 4536 | entries, |
| 4537 | log_position, |
| 4538 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4539 | NetLog::PHASE_BEGIN); |
| 4540 | |
| 4541 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4542 | log_position + 1); |
| 4543 | |
| 4544 | ASSERT_LT(log_position, entries.size()); |
| 4545 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4546 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4547 | } |
| 4548 | |
| 4549 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4550 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4551 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4552 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4553 | } |
| 4554 | } |
| 4555 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4556 | namespace { |
| 4557 | |
| 4558 | const char kExtraHeader[] = "Allow-Snafu"; |
| 4559 | const char kExtraValue[] = "fubar"; |
| 4560 | |
| 4561 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4562 | virtual void OnReceivedRedirect(URLRequest* request, |
| 4563 | const RedirectInfo& redirect_info, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 4564 | bool* defer_redirect) OVERRIDE { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4565 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4566 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 4567 | } |
| 4568 | }; |
| 4569 | |
| 4570 | } // namespace |
| 4571 | |
| 4572 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
| 4573 | ASSERT_TRUE(test_server_.Start()); |
| 4574 | |
| 4575 | GURL destination_url = test_server_.GetURL( |
| 4576 | "echoheader?" + std::string(kExtraHeader)); |
| 4577 | GURL original_url = test_server_.GetURL( |
| 4578 | "server-redirect?" + destination_url.spec()); |
| 4579 | RedirectWithAdditionalHeadersDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4580 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 4581 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 4582 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4583 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4584 | |
| 4585 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4586 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4587 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 4588 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4589 | EXPECT_FALSE(req->is_pending()); |
| 4590 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4591 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 4592 | } |
| 4593 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4594 | namespace { |
| 4595 | |
| 4596 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 4597 | |
| 4598 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4599 | virtual void OnReceivedRedirect(URLRequest* request, |
| 4600 | const RedirectInfo& redirect_info, |
| 4601 | bool* defer_redirect) OVERRIDE { |
| 4602 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4603 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 4604 | } |
| 4605 | }; |
| 4606 | |
| 4607 | } // namespace |
| 4608 | |
| 4609 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
| 4610 | ASSERT_TRUE(test_server_.Start()); |
| 4611 | |
| 4612 | GURL destination_url = test_server_.GetURL( |
| 4613 | "echoheader?" + std::string(kExtraHeaderToRemove)); |
| 4614 | GURL original_url = test_server_.GetURL( |
| 4615 | "server-redirect?" + destination_url.spec()); |
| 4616 | RedirectWithHeaderRemovalDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4617 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 4618 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
| 4619 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 4620 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4621 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4622 | |
| 4623 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4624 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4625 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4626 | EXPECT_FALSE(req->is_pending()); |
| 4627 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4628 | EXPECT_EQ("None", d.data_received()); |
| 4629 | } |
| 4630 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4631 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 4632 | TestDelegate d; |
| 4633 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4634 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4635 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4636 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4637 | r->Start(); |
| 4638 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4639 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4640 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4641 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4642 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4643 | |
| 4644 | // We expect to receive OnResponseStarted even though the request has been |
| 4645 | // cancelled. |
| 4646 | EXPECT_EQ(1, d.response_started_count()); |
| 4647 | EXPECT_EQ(0, d.bytes_received()); |
| 4648 | EXPECT_FALSE(d.received_data_before_response()); |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
| 4653 | ASSERT_TRUE(test_server_.Start()); |
| 4654 | |
| 4655 | TestDelegate d; |
| 4656 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4657 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4658 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4659 | |
| 4660 | d.set_cancel_in_response_started(true); |
| 4661 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4662 | r->Start(); |
| 4663 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4664 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4665 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4666 | |
| 4667 | EXPECT_EQ(1, d.response_started_count()); |
| 4668 | EXPECT_EQ(0, d.bytes_received()); |
| 4669 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4670 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
| 4675 | ASSERT_TRUE(test_server_.Start()); |
| 4676 | |
| 4677 | TestDelegate d; |
| 4678 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4679 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4680 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4681 | |
| 4682 | d.set_cancel_in_received_data(true); |
| 4683 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4684 | r->Start(); |
| 4685 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4686 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4687 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4688 | |
| 4689 | EXPECT_EQ(1, d.response_started_count()); |
| 4690 | // There is no guarantee about how much data was received |
| 4691 | // before the cancel was issued. It could have been 0 bytes, |
| 4692 | // or it could have been all the bytes. |
| 4693 | // EXPECT_EQ(0, d.bytes_received()); |
| 4694 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4695 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4696 | } |
| 4697 | } |
| 4698 | |
| 4699 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
| 4700 | ASSERT_TRUE(test_server_.Start()); |
| 4701 | |
| 4702 | TestDelegate d; |
| 4703 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4704 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4705 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4706 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4707 | r->Start(); |
| 4708 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4709 | |
| 4710 | // The request will be implicitly canceled when it is destroyed. The |
| 4711 | // test delegate must not post a quit message when this happens because |
| 4712 | // this test doesn't actually have a message loop. The quit message would |
| 4713 | // get put on this thread's message queue and the next test would exit |
| 4714 | // early, causing problems. |
| 4715 | d.set_quit_on_complete(false); |
| 4716 | } |
| 4717 | // expect things to just cleanup properly. |
| 4718 | |
| 4719 | // we won't actually get a received reponse here because we've never run the |
| 4720 | // message loop |
| 4721 | EXPECT_FALSE(d.received_data_before_response()); |
| 4722 | EXPECT_EQ(0, d.bytes_received()); |
| 4723 | } |
| 4724 | |
| 4725 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
| 4726 | ASSERT_TRUE(test_server_.Start()); |
| 4727 | |
| 4728 | // populate cache |
| 4729 | { |
| 4730 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4731 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4732 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d, NULL)); |
| 4733 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4734 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4735 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | // cancel read from cache (see bug 990242) |
| 4739 | { |
| 4740 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4741 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4742 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d, NULL)); |
| 4743 | r->Start(); |
| 4744 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4745 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4746 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4747 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4748 | EXPECT_EQ(1, d.response_started_count()); |
| 4749 | EXPECT_EQ(0, d.bytes_received()); |
| 4750 | EXPECT_FALSE(d.received_data_before_response()); |
| 4751 | } |
| 4752 | } |
| 4753 | |
| 4754 | TEST_F(URLRequestTestHTTP, PostTest) { |
| 4755 | ASSERT_TRUE(test_server_.Start()); |
| 4756 | HTTPUploadDataOperationTest("POST"); |
| 4757 | } |
| 4758 | |
| 4759 | TEST_F(URLRequestTestHTTP, PutTest) { |
| 4760 | ASSERT_TRUE(test_server_.Start()); |
| 4761 | HTTPUploadDataOperationTest("PUT"); |
| 4762 | } |
| 4763 | |
| 4764 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
| 4765 | ASSERT_TRUE(test_server_.Start()); |
| 4766 | |
| 4767 | TestDelegate d; |
| 4768 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4769 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4770 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4771 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4772 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4773 | r->Start(); |
| 4774 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4775 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4776 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4777 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4778 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4779 | << "request failed: " << r->status().status() |
| 4780 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4781 | |
| 4782 | EXPECT_FALSE(d.received_data_before_response()); |
| 4783 | EXPECT_TRUE(d.data_received().empty()); |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
| 4788 | ASSERT_TRUE(test_server_.Start()); |
| 4789 | |
| 4790 | TestDelegate d; |
| 4791 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4792 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4793 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4794 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4795 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4796 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4797 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 4798 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4799 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 4800 | ScopedVector<UploadElementReader> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4801 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4802 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4803 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 4804 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 4805 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 4806 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 4807 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 4808 | element_readers.push_back( |
| 4809 | new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 4810 | path, |
| 4811 | 0, |
| 4812 | kuint64max, |
| 4813 | base::Time())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4814 | r->set_upload(make_scoped_ptr( |
[email protected] | 96c77a7 | 2013-09-24 09:49:20 | [diff] [blame] | 4815 | new UploadDataStream(element_readers.Pass(), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4816 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4817 | r->Start(); |
| 4818 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4819 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4820 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4821 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4822 | int64 size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 4823 | ASSERT_EQ(true, base::GetFileSize(path, &size)); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 4824 | scoped_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4825 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 4826 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4827 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4828 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4829 | << "request failed: " << r->status().status() |
| 4830 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4831 | |
| 4832 | EXPECT_FALSE(d.received_data_before_response()); |
| 4833 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4834 | EXPECT_EQ(size, d.bytes_received()); |
| 4835 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4836 | } |
| 4837 | } |
| 4838 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4839 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
| 4840 | ASSERT_TRUE(test_server_.Start()); |
| 4841 | |
| 4842 | TestDelegate d; |
| 4843 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4844 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4845 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4846 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4847 | |
| 4848 | ScopedVector<UploadElementReader> element_readers; |
| 4849 | |
| 4850 | element_readers.push_back(new UploadFileElementReader( |
| 4851 | base::MessageLoopProxy::current().get(), |
| 4852 | base::FilePath(FILE_PATH_LITERAL( |
| 4853 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
| 4854 | 0, |
| 4855 | kuint64max, |
| 4856 | base::Time())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4857 | r->set_upload(make_scoped_ptr( |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4858 | new UploadDataStream(element_readers.Pass(), 0))); |
| 4859 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4860 | r->Start(); |
| 4861 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4862 | |
| 4863 | base::RunLoop().Run(); |
| 4864 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4865 | EXPECT_TRUE(d.request_failed()); |
| 4866 | EXPECT_FALSE(d.received_data_before_response()); |
| 4867 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4868 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 4869 | EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 4870 | } |
| 4871 | } |
| 4872 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4873 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
| 4874 | ASSERT_TRUE(test_server_.Start()); |
| 4875 | |
| 4876 | TestDelegate d; |
| 4877 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4878 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4879 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4880 | r->EnableChunkedUpload(); |
| 4881 | r->set_method("POST"); |
| 4882 | AddChunksToUpload(r.get()); |
| 4883 | r->Start(); |
| 4884 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4885 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4886 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4887 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4888 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4889 | } |
| 4890 | } |
| 4891 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4892 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
| 4893 | ASSERT_TRUE(test_server_.Start()); |
| 4894 | |
| 4895 | TestDelegate d; |
| 4896 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4897 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4898 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4899 | r->EnableChunkedUpload(); |
| 4900 | r->set_method("POST"); |
| 4901 | r->Start(); |
| 4902 | EXPECT_TRUE(r->is_pending()); |
| 4903 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4904 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4905 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4906 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 4907 | } |
| 4908 | } |
| 4909 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4910 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
| 4911 | ASSERT_TRUE(test_server_.Start()); |
| 4912 | |
| 4913 | TestDelegate d; |
| 4914 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4915 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 4916 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d, NULL)); |
| 4917 | r->EnableChunkedUpload(); |
| 4918 | r->set_method("POST"); |
| 4919 | r->Start(); |
| 4920 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4921 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4922 | base::RunLoop().RunUntilIdle(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4923 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4924 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4925 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4926 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4927 | } |
| 4928 | } |
| 4929 | |
| 4930 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
| 4931 | ASSERT_TRUE(test_server_.Start()); |
| 4932 | |
| 4933 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4934 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 4935 | test_server_.GetURL("files/with-headers.html"), DEFAULT_PRIORITY, &d, |
| 4936 | NULL)); |
| 4937 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4938 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4939 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4940 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4941 | |
| 4942 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4943 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4944 | |
| 4945 | std::string header; |
| 4946 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 4947 | EXPECT_EQ("private", header); |
| 4948 | |
| 4949 | header.clear(); |
| 4950 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 4951 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 4952 | |
| 4953 | // The response has two "X-Multiple-Entries" headers. |
| 4954 | // This verfies our output has them concatenated together. |
| 4955 | header.clear(); |
| 4956 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 4957 | EXPECT_EQ("a, b", header); |
| 4958 | } |
| 4959 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4960 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 4961 | SpawnedTestServer::SSLOptions ssl_options; |
| 4962 | SpawnedTestServer https_test_server( |
| 4963 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4964 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4965 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4966 | ASSERT_TRUE(https_test_server.Start()); |
| 4967 | |
| 4968 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4969 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 4970 | https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY, &d, |
| 4971 | NULL)); |
| 4972 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4973 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4974 | |
| 4975 | TransportSecurityState* security_state = |
| 4976 | default_context_.transport_security_state(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4977 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 4978 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 4979 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 4980 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 4981 | domain_state.sts.upgrade_mode); |
| 4982 | EXPECT_TRUE(domain_state.sts.include_subdomains); |
| 4983 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 4984 | #if defined(OS_ANDROID) |
| 4985 | // Android's CertVerifyProc does not (yet) handle pins. |
| 4986 | #else |
| 4987 | EXPECT_FALSE(domain_state.HasPublicKeyPins()); |
| 4988 | #endif |
| 4989 | } |
| 4990 | |
| 4991 | // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will |
| 4992 | // reject HPKP headers, and a test setting only HPKP headers will fail (no |
| 4993 | // DomainState present because header rejected). |
| 4994 | #if defined(OS_ANDROID) |
| 4995 | #define MAYBE_ProcessPKP DISABLED_ProcessPKP |
| 4996 | #else |
| 4997 | #define MAYBE_ProcessPKP ProcessPKP |
| 4998 | #endif |
| 4999 | |
| 5000 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 5001 | // validity/expiration. |
| 5002 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) { |
| 5003 | SpawnedTestServer::SSLOptions ssl_options; |
| 5004 | SpawnedTestServer https_test_server( |
| 5005 | SpawnedTestServer::TYPE_HTTPS, |
| 5006 | ssl_options, |
| 5007 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5008 | ASSERT_TRUE(https_test_server.Start()); |
| 5009 | |
| 5010 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5011 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 5012 | https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY, &d, |
| 5013 | NULL)); |
| 5014 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5015 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5016 | |
| 5017 | TransportSecurityState* security_state = |
| 5018 | default_context_.transport_security_state(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5019 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5020 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 5021 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5022 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5023 | domain_state.sts.upgrade_mode); |
| 5024 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
| 5025 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5026 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5027 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 5031 | SpawnedTestServer::SSLOptions ssl_options; |
| 5032 | SpawnedTestServer https_test_server( |
| 5033 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5034 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5035 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5036 | ASSERT_TRUE(https_test_server.Start()); |
| 5037 | |
| 5038 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5039 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5040 | https_test_server.GetURL("files/hsts-multiple-headers.html"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5041 | DEFAULT_PRIORITY, &d, NULL)); |
| 5042 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5043 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5044 | |
| 5045 | // We should have set parameters from the first header, not the second. |
| 5046 | TransportSecurityState* security_state = |
| 5047 | default_context_.transport_security_state(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5048 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5049 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 5050 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5051 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5052 | domain_state.sts.upgrade_mode); |
| 5053 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
| 5054 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5055 | } |
| 5056 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5057 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 5058 | SpawnedTestServer::SSLOptions ssl_options; |
| 5059 | SpawnedTestServer https_test_server( |
| 5060 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5061 | ssl_options, |
| 5062 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5063 | ASSERT_TRUE(https_test_server.Start()); |
| 5064 | |
| 5065 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5066 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5067 | https_test_server.GetURL("files/hsts-and-hpkp-headers.html"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5068 | DEFAULT_PRIORITY, &d, NULL)); |
| 5069 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5070 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5071 | |
| 5072 | // We should have set parameters from the first header, not the second. |
| 5073 | TransportSecurityState* security_state = |
| 5074 | default_context_.transport_security_state(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5075 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5076 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 5077 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5078 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5079 | domain_state.sts.upgrade_mode); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5080 | #if defined(OS_ANDROID) |
| 5081 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5082 | #else |
| 5083 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5084 | #endif |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5085 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5086 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5087 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 5088 | // the *second* such header, and we MUST process only the first. |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5089 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5090 | // includeSubdomains does not occur in the test HPKP header. |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5091 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5092 | } |
| 5093 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5094 | // Tests that when multiple HPKP headers are present, asserting different |
| 5095 | // policies, that only the first such policy is processed. |
| 5096 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
| 5097 | SpawnedTestServer::SSLOptions ssl_options; |
| 5098 | SpawnedTestServer https_test_server( |
| 5099 | SpawnedTestServer::TYPE_HTTPS, |
| 5100 | ssl_options, |
| 5101 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5102 | ASSERT_TRUE(https_test_server.Start()); |
| 5103 | |
| 5104 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5105 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5106 | https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5107 | DEFAULT_PRIORITY, &d, NULL)); |
| 5108 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5109 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5110 | |
| 5111 | TransportSecurityState* security_state = |
| 5112 | default_context_.transport_security_state(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5113 | TransportSecurityState::DomainState domain_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5114 | EXPECT_TRUE(security_state->GetDynamicDomainState( |
| 5115 | SpawnedTestServer::kLocalhost, &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5116 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5117 | domain_state.sts.upgrade_mode); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5118 | #if defined(OS_ANDROID) |
| 5119 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5120 | #else |
| 5121 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5122 | #endif |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5123 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5124 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5125 | EXPECT_TRUE(domain_state.sts.include_subdomains); |
| 5126 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5127 | } |
| 5128 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5129 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
| 5130 | ASSERT_TRUE(test_server_.Start()); |
| 5131 | |
| 5132 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5133 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5134 | test_server_.GetURL("files/content-type-normalization.html"), |
| 5135 | DEFAULT_PRIORITY, &d, NULL)); |
| 5136 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5137 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5138 | |
| 5139 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5140 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5141 | EXPECT_EQ("text/html", mime_type); |
| 5142 | |
| 5143 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5144 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5145 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5146 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5147 | } |
| 5148 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5149 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5150 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5151 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5152 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5153 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5154 | |
| 5155 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5156 | EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5157 | } |
| 5158 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5159 | #if !defined(DISABLE_FILE_SUPPORT) |
| 5160 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 5161 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 5162 | GURL file_url("file:///foo.txt"); |
| 5163 | FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current()); |
| 5164 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 5165 | |
| 5166 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
| 5167 | EXPECT_FALSE(job_factory_.IsSafeRedirectTarget(file_url)); |
| 5168 | } |
| 5169 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5170 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5171 | ASSERT_TRUE(test_server_.Start()); |
| 5172 | |
| 5173 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5174 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5175 | test_server_.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY, &d, |
| 5176 | NULL)); |
| 5177 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5178 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5179 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5180 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5181 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5182 | } |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5183 | #endif // !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5184 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5185 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
| 5186 | ASSERT_TRUE(test_server_.Start()); |
| 5187 | |
| 5188 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5189 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5190 | test_server_.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY, &d, |
| 5191 | NULL)); |
| 5192 | req->Start(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5193 | base::MessageLoop::current()->Run(); |
| 5194 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5195 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5196 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5197 | } |
| 5198 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5199 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
| 5200 | ASSERT_TRUE(test_server_.Start()); |
| 5201 | |
| 5202 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5203 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5204 | test_server_.GetURL("files/redirect-to-invalid-url.html"), |
| 5205 | DEFAULT_PRIORITY, &d, NULL)); |
| 5206 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5207 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5208 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5209 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5210 | EXPECT_EQ(ERR_INVALID_URL, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5211 | } |
| 5212 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5213 | // Make sure redirects are cached, despite not reading their bodies. |
| 5214 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
| 5215 | ASSERT_TRUE(test_server_.Start()); |
| 5216 | GURL redirect_url = |
| 5217 | test_server_.GetURL("files/redirect302-to-echo-cacheable"); |
| 5218 | |
| 5219 | { |
| 5220 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5221 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5222 | redirect_url, DEFAULT_PRIORITY, &d, NULL)); |
| 5223 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5224 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5225 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5226 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5227 | EXPECT_EQ(test_server_.GetURL("echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | { |
| 5231 | TestDelegate d; |
| 5232 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5233 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5234 | redirect_url, DEFAULT_PRIORITY, &d, NULL)); |
| 5235 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5236 | base::RunLoop().Run(); |
| 5237 | |
| 5238 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5239 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5240 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5241 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5242 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5243 | base::RunLoop().Run(); |
| 5244 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5245 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5246 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 5247 | EXPECT_EQ(test_server_.GetURL("echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 5252 | // when the headers are read, since the body won't have been read. |
| 5253 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
| 5254 | ASSERT_TRUE(test_server_.Start()); |
| 5255 | // URL that is normally cached. |
| 5256 | GURL initial_url = test_server_.GetURL("cachetime"); |
| 5257 | |
| 5258 | { |
| 5259 | // Set up the TestNetworkDelegate tp force a redirect. |
| 5260 | GURL redirect_to_url = test_server_.GetURL("echo"); |
| 5261 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 5262 | redirect_to_url); |
| 5263 | |
| 5264 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5265 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5266 | initial_url, DEFAULT_PRIORITY, &d, NULL)); |
| 5267 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5268 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5269 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5270 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5271 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5272 | } |
| 5273 | |
| 5274 | { |
| 5275 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5276 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5277 | initial_url, DEFAULT_PRIORITY, &d, NULL)); |
| 5278 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5279 | base::RunLoop().Run(); |
| 5280 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5281 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 5282 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5283 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5284 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5288 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 5289 | // safe. |
| 5290 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
| 5291 | ASSERT_TRUE(test_server_.Start()); |
| 5292 | |
| 5293 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 5294 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5295 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5296 | |
| 5297 | TestDelegate d; |
| 5298 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5299 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5300 | test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5301 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5302 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5303 | base::RunLoop().Run(); |
| 5304 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5305 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5306 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5307 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5308 | EXPECT_EQ(OK, r->status().error()); |
| 5309 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5310 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 5311 | } |
| 5312 | } |
| 5313 | |
| 5314 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 5315 | // some other URL to the whitelist. |
| 5316 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
| 5317 | ASSERT_TRUE(test_server_.Start()); |
| 5318 | |
| 5319 | GURL unsafe_url("data:text/html,something"); |
| 5320 | GURL different_unsafe_url("data:text/html,something-else"); |
| 5321 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5322 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 5323 | different_unsafe_url); |
| 5324 | |
| 5325 | TestDelegate d; |
| 5326 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5327 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5328 | test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5329 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5330 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5331 | base::RunLoop().Run(); |
| 5332 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5333 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 5334 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5335 | } |
| 5336 | } |
| 5337 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5338 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 5339 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 5340 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
| 5341 | ASSERT_TRUE(test_server_.Start()); |
| 5342 | |
| 5343 | GURL original_url(test_server_.GetURL("original#fragment1")); |
| 5344 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 5345 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 5346 | |
| 5347 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5348 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5349 | |
| 5350 | TestDelegate d; |
| 5351 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5352 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5353 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5354 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5355 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5356 | base::RunLoop().Run(); |
| 5357 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5358 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5359 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5360 | EXPECT_EQ(OK, r->status().error()); |
| 5361 | EXPECT_EQ(original_url, r->original_url()); |
| 5362 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5363 | } |
| 5364 | } |
| 5365 | |
| 5366 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 5367 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5368 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5369 | ASSERT_TRUE(test_server_.Start()); |
| 5370 | |
| 5371 | GURL original_url(test_server_.GetURL("original#expected-fragment")); |
| 5372 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
| 5373 | GURL redirect_url(test_server_.GetURL("target")); |
| 5374 | GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment")); |
| 5375 | |
| 5376 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 5377 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5378 | |
| 5379 | TestDelegate d; |
| 5380 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5381 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5382 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5383 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5384 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5385 | base::RunLoop().Run(); |
| 5386 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5387 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5388 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5389 | EXPECT_EQ(OK, r->status().error()); |
| 5390 | EXPECT_EQ(original_url, r->original_url()); |
| 5391 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5392 | } |
| 5393 | } |
| 5394 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5395 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 5396 | // URL should not be changed. In particular, the reference fragment should not |
| 5397 | // be modified. |
| 5398 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
| 5399 | ASSERT_TRUE(test_server_.Start()); |
| 5400 | |
| 5401 | GURL original_url(test_server_.GetURL("original#should-not-be-appended")); |
| 5402 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 5403 | |
| 5404 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 5405 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 5406 | |
| 5407 | TestDelegate d; |
| 5408 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5409 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5410 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5411 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5412 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5413 | base::RunLoop().Run(); |
| 5414 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5415 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5416 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5417 | EXPECT_EQ(OK, r->status().error()); |
| 5418 | EXPECT_EQ(original_url, r->original_url()); |
| 5419 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5420 | } |
| 5421 | } |
| 5422 | |
| 5423 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 5424 | // the reference fragment of the target URL must not be modified. |
| 5425 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
| 5426 | ASSERT_TRUE(test_server_.Start()); |
| 5427 | |
| 5428 | GURL original_url(test_server_.GetURL("original#should-not-be-appended")); |
| 5429 | GURL redirect_url(test_server_.GetURL("echo")); |
| 5430 | |
| 5431 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5432 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5433 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5434 | |
| 5435 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5436 | r.get(), &default_network_delegate_, redirect_url, |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5437 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
| 5438 | AddTestInterceptor()->set_main_intercept_job(job); |
| 5439 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5440 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5441 | base::RunLoop().Run(); |
| 5442 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5443 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5444 | EXPECT_EQ(OK, r->status().error()); |
| 5445 | EXPECT_EQ(original_url, r->original_url()); |
| 5446 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5447 | } |
| 5448 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5449 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
| 5450 | ASSERT_TRUE(test_server_.Start()); |
| 5451 | |
| 5452 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5453 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5454 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
| 5455 | req->SetReferrer("http://user:[email protected]/"); |
| 5456 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5457 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5458 | |
| 5459 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 5460 | } |
| 5461 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5462 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
| 5463 | ASSERT_TRUE(test_server_.Start()); |
| 5464 | |
| 5465 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5466 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5467 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
| 5468 | req->SetReferrer("http://foo.com/test#fragment"); |
| 5469 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5470 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5471 | |
| 5472 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 5473 | } |
| 5474 | |
| 5475 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
| 5476 | ASSERT_TRUE(test_server_.Start()); |
| 5477 | |
| 5478 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5479 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5480 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d, NULL)); |
| 5481 | req->SetReferrer("http://foo.com/test#fragment"); |
| 5482 | req->SetReferrer(""); |
| 5483 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5484 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5485 | |
| 5486 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 5487 | } |
| 5488 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5489 | // Defer network start and then resume, checking that the request was a success |
| 5490 | // and bytes were received. |
| 5491 | TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) { |
| 5492 | ASSERT_TRUE(test_server_.Start()); |
| 5493 | |
| 5494 | TestDelegate d; |
| 5495 | { |
| 5496 | d.set_quit_on_network_start(true); |
| 5497 | GURL test_url(test_server_.GetURL("echo")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5498 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5499 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5500 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5501 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5502 | base::RunLoop().Run(); |
| 5503 | |
| 5504 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5505 | EXPECT_EQ(0, d.response_started_count()); |
| 5506 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5507 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5508 | base::RunLoop().Run(); |
| 5509 | |
| 5510 | EXPECT_EQ(1, d.response_started_count()); |
| 5511 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5512 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5513 | } |
| 5514 | } |
| 5515 | |
| 5516 | // Check that OnBeforeNetworkStart is only called once even if there is a |
| 5517 | // redirect. |
| 5518 | TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) { |
| 5519 | ASSERT_TRUE(test_server_.Start()); |
| 5520 | |
| 5521 | TestDelegate d; |
| 5522 | { |
| 5523 | d.set_quit_on_redirect(true); |
| 5524 | d.set_quit_on_network_start(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5525 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5526 | test_server_.GetURL("server-redirect?echo"), DEFAULT_PRIORITY, &d, |
| 5527 | NULL)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5528 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5529 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5530 | base::RunLoop().Run(); |
| 5531 | |
| 5532 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5533 | EXPECT_EQ(0, d.response_started_count()); |
| 5534 | EXPECT_EQ(0, d.received_redirect_count()); |
| 5535 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5536 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5537 | base::RunLoop().Run(); |
| 5538 | |
| 5539 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5540 | req->FollowDeferredRedirect(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5541 | base::RunLoop().Run(); |
| 5542 | |
| 5543 | // Check that the redirect's new network transaction does not get propagated |
| 5544 | // to a second OnBeforeNetworkStart() notification. |
| 5545 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5546 | |
| 5547 | EXPECT_EQ(1, d.response_started_count()); |
| 5548 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5549 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5550 | } |
| 5551 | } |
| 5552 | |
| 5553 | // Cancel the request after learning that the request would use the network. |
| 5554 | TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) { |
| 5555 | ASSERT_TRUE(test_server_.Start()); |
| 5556 | |
| 5557 | TestDelegate d; |
| 5558 | { |
| 5559 | d.set_quit_on_network_start(true); |
| 5560 | GURL test_url(test_server_.GetURL("echo")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5561 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5562 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5563 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5564 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5565 | base::RunLoop().Run(); |
| 5566 | |
| 5567 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5568 | EXPECT_EQ(0, d.response_started_count()); |
| 5569 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5570 | req->Cancel(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5571 | base::RunLoop().Run(); |
| 5572 | |
| 5573 | EXPECT_EQ(1, d.response_started_count()); |
| 5574 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5575 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5576 | } |
| 5577 | } |
| 5578 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5579 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
| 5580 | ASSERT_TRUE(test_server_.Start()); |
| 5581 | |
| 5582 | TestDelegate d; |
| 5583 | { |
| 5584 | d.set_cancel_in_received_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5585 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5586 | test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 5587 | NULL)); |
| 5588 | req->Start(); |
[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_EQ(1, d.response_started_count()); |
| 5592 | EXPECT_EQ(0, d.bytes_received()); |
| 5593 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5594 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5595 | } |
| 5596 | } |
| 5597 | |
| 5598 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
| 5599 | ASSERT_TRUE(test_server_.Start()); |
| 5600 | |
| 5601 | TestDelegate d; |
| 5602 | { |
| 5603 | d.set_quit_on_redirect(true); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5604 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5605 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5606 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5607 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5608 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5609 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5610 | |
| 5611 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5612 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5613 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5614 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5615 | |
| 5616 | EXPECT_EQ(1, d.response_started_count()); |
| 5617 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5618 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5619 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5620 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5621 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 5622 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 5623 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 5624 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 5625 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 5626 | |
| 5627 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 5628 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5629 | EXPECT_EQ(contents, d.data_received()); |
| 5630 | } |
| 5631 | } |
| 5632 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5633 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
| 5634 | ASSERT_TRUE(test_server_.Start()); |
| 5635 | |
| 5636 | TestDelegate d; |
| 5637 | { |
| 5638 | d.set_quit_on_redirect(true); |
| 5639 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5640 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5641 | test_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5642 | |
| 5643 | EXPECT_FALSE(d.have_full_request_headers()); |
| 5644 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5645 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5646 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5647 | |
| 5648 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5649 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5650 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 5651 | d.ClearFullRequestHeaders(); |
| 5652 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5653 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5654 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5655 | |
| 5656 | GURL target_url(test_server_.GetURL("files/with-headers.html")); |
| 5657 | EXPECT_EQ(1, d.response_started_count()); |
| 5658 | EXPECT_TRUE(d.have_full_request_headers()); |
| 5659 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 5660 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5661 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5662 | |
| 5663 | base::FilePath path; |
| 5664 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 5665 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 5666 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 5667 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 5668 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 5669 | |
| 5670 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 5671 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5672 | EXPECT_EQ(contents, d.data_received()); |
| 5673 | } |
| 5674 | } |
| 5675 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5676 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
| 5677 | ASSERT_TRUE(test_server_.Start()); |
| 5678 | |
| 5679 | TestDelegate d; |
| 5680 | { |
| 5681 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5682 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5683 | test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d, |
| 5684 | NULL)); |
| 5685 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5686 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5687 | |
| 5688 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5689 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5690 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5691 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5692 | |
| 5693 | EXPECT_EQ(1, d.response_started_count()); |
| 5694 | EXPECT_EQ(0, d.bytes_received()); |
| 5695 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5696 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5697 | } |
| 5698 | } |
| 5699 | |
| 5700 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
| 5701 | ASSERT_TRUE(test_server_.Start()); |
| 5702 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5703 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5704 | { |
| 5705 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5706 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5707 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d, |
| 5708 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5709 | HttpRequestHeaders headers; |
| 5710 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5711 | req->SetExtraRequestHeaders(headers); |
| 5712 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5713 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5714 | |
| 5715 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5716 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5717 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5718 | } |
| 5719 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5720 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5721 | { |
| 5722 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5723 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5724 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d, |
| 5725 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5726 | HttpRequestHeaders headers; |
| 5727 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5728 | req->SetExtraRequestHeaders(headers); |
| 5729 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5730 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5731 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5732 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5733 | |
| 5734 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5735 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5736 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5737 | } |
| 5738 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5739 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5740 | { |
| 5741 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5742 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5743 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d, |
| 5744 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5745 | HttpRequestHeaders headers; |
| 5746 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5747 | req->SetExtraRequestHeaders(headers); |
| 5748 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5749 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5750 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5751 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5752 | |
| 5753 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5754 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5755 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5756 | } |
| 5757 | } |
| 5758 | |
| 5759 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
| 5760 | ASSERT_TRUE(test_server_.Start()); |
| 5761 | |
| 5762 | // populate the cache |
| 5763 | { |
| 5764 | TestDelegate d; |
| 5765 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5766 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5767 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5768 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
| 5769 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5770 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5771 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5772 | |
| 5773 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5774 | } |
| 5775 | |
| 5776 | // repeat request with end-to-end validation. since auth-basic results in a |
| 5777 | // cachable page, we expect this test to result in a 304. in which case, the |
| 5778 | // response should be fetched from the cache. |
| 5779 | { |
| 5780 | TestDelegate d; |
| 5781 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5782 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5783 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5784 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
| 5785 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 5786 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5787 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5788 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5789 | |
| 5790 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5791 | |
| 5792 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5793 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5794 | } |
| 5795 | } |
| 5796 | |
| 5797 | // Check that Set-Cookie headers in 401 responses are respected. |
| 5798 | // http://crbug.com/6450 |
| 5799 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
| 5800 | ASSERT_TRUE(test_server_.Start()); |
| 5801 | |
| 5802 | GURL url_requiring_auth = |
| 5803 | test_server_.GetURL("auth-basic?set-cookie-if-challenged"); |
| 5804 | |
| 5805 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 5806 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 5807 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5808 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5809 | TestURLRequestContext context(true); |
| 5810 | context.set_network_delegate(&network_delegate); |
| 5811 | context.Init(); |
| 5812 | |
| 5813 | TestDelegate d; |
| 5814 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5815 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5816 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 5817 | url_requiring_auth, DEFAULT_PRIORITY, &d, NULL)); |
| 5818 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5819 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5820 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5821 | |
| 5822 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5823 | |
| 5824 | // Make sure we sent the cookie in the restarted transaction. |
| 5825 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 5826 | != std::string::npos); |
| 5827 | } |
| 5828 | |
| 5829 | // Same test as above, except this time the restart is initiated earlier |
| 5830 | // (without user intervention since identity is embedded in the URL). |
| 5831 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 5832 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5833 | TestURLRequestContext context(true); |
| 5834 | context.set_network_delegate(&network_delegate); |
| 5835 | context.Init(); |
| 5836 | |
| 5837 | TestDelegate d; |
| 5838 | |
| 5839 | GURL::Replacements replacements; |
| 5840 | std::string username("user2"); |
| 5841 | std::string password("secret"); |
| 5842 | replacements.SetUsernameStr(username); |
| 5843 | replacements.SetPasswordStr(password); |
| 5844 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 5845 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5846 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 5847 | url_with_identity, DEFAULT_PRIORITY, &d, NULL)); |
| 5848 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5849 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5850 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5851 | |
| 5852 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 5853 | |
| 5854 | // Make sure we sent the cookie in the restarted transaction. |
| 5855 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 5856 | != std::string::npos); |
| 5857 | } |
| 5858 | } |
| 5859 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5860 | // Tests that load timing works as expected with auth and the cache. |
| 5861 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
| 5862 | ASSERT_TRUE(test_server_.Start()); |
| 5863 | |
| 5864 | // populate the cache |
| 5865 | { |
| 5866 | TestDelegate d; |
| 5867 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5868 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5869 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5870 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
| 5871 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5872 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5873 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5874 | |
| 5875 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5876 | |
| 5877 | LoadTimingInfo load_timing_info_before_auth; |
| 5878 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 5879 | &load_timing_info_before_auth)); |
| 5880 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 5881 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 5882 | |
| 5883 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5884 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5885 | // The test server does not support keep alive sockets, so the second |
| 5886 | // request with auth should use a new socket. |
| 5887 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 5888 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 5889 | load_timing_info.socket_log_id); |
| 5890 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 5891 | load_timing_info.connect_timing.connect_start); |
| 5892 | } |
| 5893 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5894 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 5895 | // 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] | 5896 | // response should be fetched from the cache. |
| 5897 | { |
| 5898 | TestDelegate d; |
| 5899 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 5900 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5901 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 5902 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d, NULL)); |
| 5903 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 5904 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5905 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5906 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5907 | |
| 5908 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 5909 | |
| 5910 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5911 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5912 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5913 | // Since there was a request that went over the wire, the load timing |
| 5914 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5915 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5916 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 5917 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5918 | } |
| 5919 | } |
| 5920 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5921 | // In this test, we do a POST which the server will 302 redirect. |
| 5922 | // The subsequent transaction should use GET, and should not send the |
| 5923 | // Content-Type header. |
| 5924 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 5925 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
| 5926 | ASSERT_TRUE(test_server_.Start()); |
| 5927 | |
| 5928 | const char kData[] = "hello world"; |
| 5929 | |
| 5930 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5931 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5932 | test_server_.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY, &d, |
| 5933 | NULL)); |
| 5934 | req->set_method("POST"); |
| 5935 | req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5936 | |
| 5937 | // Set headers (some of which are specific to the POST). |
| 5938 | HttpRequestHeaders headers; |
| 5939 | headers.AddHeadersFromString( |
| 5940 | "Content-Type: multipart/form-data; " |
| 5941 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 5942 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 5943 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 5944 | "Accept-Language: en-US,en\r\n" |
| 5945 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 5946 | "Content-Length: 11\r\n" |
| 5947 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5948 | req->SetExtraRequestHeaders(headers); |
| 5949 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5950 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5951 | |
| 5952 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5953 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5954 | EXPECT_EQ("text/html", mime_type); |
| 5955 | |
| 5956 | const std::string& data = d.data_received(); |
| 5957 | |
| 5958 | // Check that the post-specific headers were stripped: |
| 5959 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 5960 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 5961 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 5962 | |
| 5963 | // These extra request headers should not have been stripped. |
| 5964 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 5965 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 5966 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 5967 | } |
| 5968 | |
| 5969 | // The following tests check that we handle mutating the request method for |
| 5970 | // HTTP redirects as expected. |
| 5971 | // See http://crbug.com/56373 and http://crbug.com/102130. |
| 5972 | |
| 5973 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
| 5974 | ASSERT_TRUE(test_server_.Start()); |
| 5975 | |
| 5976 | const GURL url = test_server_.GetURL("files/redirect301-to-echo"); |
| 5977 | |
| 5978 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 5979 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 5980 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 5981 | } |
| 5982 | |
| 5983 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
| 5984 | ASSERT_TRUE(test_server_.Start()); |
| 5985 | |
| 5986 | const GURL url = test_server_.GetURL("files/redirect302-to-echo"); |
| 5987 | |
| 5988 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 5989 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 5990 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 5991 | } |
| 5992 | |
| 5993 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
| 5994 | ASSERT_TRUE(test_server_.Start()); |
| 5995 | |
| 5996 | const GURL url = test_server_.GetURL("files/redirect303-to-echo"); |
| 5997 | |
| 5998 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 5999 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 6000 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 6001 | } |
| 6002 | |
| 6003 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
| 6004 | ASSERT_TRUE(test_server_.Start()); |
| 6005 | |
| 6006 | const GURL url = test_server_.GetURL("files/redirect307-to-echo"); |
| 6007 | |
| 6008 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 6009 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6010 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 6011 | } |
| 6012 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6013 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
| 6014 | ASSERT_TRUE(test_server_.Start()); |
| 6015 | |
| 6016 | const GURL url = test_server_.GetURL("files/redirect308-to-echo"); |
| 6017 | |
| 6018 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 6019 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6020 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 6021 | } |
| 6022 | |
| 6023 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 6024 | // Certain legacy apis that pre-date the response code expect this behavior |
| 6025 | // (Like Google Drive). |
| 6026 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
| 6027 | ASSERT_TRUE(test_server_.Start()); |
| 6028 | |
| 6029 | TestDelegate d; |
| 6030 | const GURL url = test_server_.GetURL("files/308-without-location-header"); |
| 6031 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6032 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 6033 | url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6034 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6035 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6036 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6037 | EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
| 6038 | EXPECT_EQ(OK, request->status().error()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6039 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6040 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6041 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 6042 | } |
| 6043 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6044 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
| 6045 | ASSERT_TRUE(test_server_.Start()); |
| 6046 | |
| 6047 | GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment")); |
| 6048 | GURL expected_url(test_server_.GetURL("echo#fragment")); |
| 6049 | |
| 6050 | TestDelegate d; |
| 6051 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6052 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6053 | original_url, DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6054 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6055 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6056 | base::RunLoop().Run(); |
| 6057 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6058 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6059 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6060 | EXPECT_EQ(OK, r->status().error()); |
| 6061 | EXPECT_EQ(original_url, r->original_url()); |
| 6062 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6063 | } |
| 6064 | } |
| 6065 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6066 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
| 6067 | ASSERT_TRUE(test_server_.Start()); |
| 6068 | |
| 6069 | GURL url(test_server_.GetURL("files/redirect302-to-echo")); |
| 6070 | GURL first_party_url("http://example.com"); |
| 6071 | |
| 6072 | TestDelegate d; |
| 6073 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6074 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6075 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 6076 | r->set_first_party_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6077 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6078 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6079 | base::RunLoop().Run(); |
| 6080 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6081 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6082 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6083 | EXPECT_EQ(OK, r->status().error()); |
| 6084 | EXPECT_EQ(first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6085 | } |
| 6086 | } |
| 6087 | |
| 6088 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
| 6089 | ASSERT_TRUE(test_server_.Start()); |
| 6090 | |
| 6091 | GURL url(test_server_.GetURL("files/redirect302-to-echo")); |
| 6092 | GURL original_first_party_url("http://example.com"); |
| 6093 | GURL expected_first_party_url(test_server_.GetURL("echo")); |
| 6094 | |
| 6095 | TestDelegate d; |
| 6096 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6097 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6098 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 6099 | r->set_first_party_for_cookies(original_first_party_url); |
| 6100 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6101 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 6102 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6103 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6104 | base::RunLoop().Run(); |
| 6105 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6106 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6107 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6108 | EXPECT_EQ(OK, r->status().error()); |
| 6109 | EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6110 | } |
| 6111 | } |
| 6112 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6113 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
| 6114 | ASSERT_TRUE(test_server_.Start()); |
| 6115 | |
| 6116 | const char kData[] = "hello world"; |
| 6117 | |
| 6118 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6119 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6120 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL)); |
| 6121 | req->set_method("POST"); |
| 6122 | req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6123 | HttpRequestHeaders headers; |
| 6124 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 6125 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6126 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6127 | |
| 6128 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6129 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6130 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6131 | AddTestInterceptor()->set_main_intercept_job(job); |
| 6132 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6133 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6134 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6135 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 6139 | ASSERT_TRUE(test_server_.Start()); |
| 6140 | |
| 6141 | const char kData[] = "hello world"; |
| 6142 | |
| 6143 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6144 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6145 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL)); |
| 6146 | req->set_method("POST"); |
| 6147 | req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6148 | HttpRequestHeaders headers; |
| 6149 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 6150 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6151 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6152 | |
| 6153 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6154 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6155 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
| 6156 | "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6157 | AddTestInterceptor()->set_main_intercept_job(job); |
| 6158 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6159 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6160 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6161 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6162 | EXPECT_EQ(kData, d.data_received()); |
| 6163 | } |
| 6164 | |
| 6165 | // Check that default A-L header is sent. |
| 6166 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
| 6167 | ASSERT_TRUE(test_server_.Start()); |
| 6168 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 6169 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6170 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6171 | TestURLRequestContext context(true); |
| 6172 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6173 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6174 | context.Init(); |
| 6175 | |
| 6176 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6177 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6178 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d, |
| 6179 | NULL)); |
| 6180 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6181 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6182 | EXPECT_EQ("en", d.data_received()); |
| 6183 | } |
| 6184 | |
| 6185 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 6186 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
| 6187 | ASSERT_TRUE(test_server_.Start()); |
| 6188 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 6189 | std::string empty_string; // Avoid most vexing parse on line below. |
| 6190 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6191 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6192 | TestURLRequestContext context(true); |
| 6193 | context.set_network_delegate(&network_delegate); |
| 6194 | context.Init(); |
| 6195 | // We override the language after initialization because empty entries |
| 6196 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6197 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6198 | |
| 6199 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6200 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6201 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d, |
| 6202 | NULL)); |
| 6203 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6204 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6205 | EXPECT_EQ("None", d.data_received()); |
| 6206 | } |
| 6207 | |
| 6208 | // Check that if request overrides the A-L header, the default is not appended. |
| 6209 | // See http://crbug.com/20894 |
| 6210 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
| 6211 | ASSERT_TRUE(test_server_.Start()); |
| 6212 | |
| 6213 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6214 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6215 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d, |
| 6216 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6217 | HttpRequestHeaders headers; |
| 6218 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6219 | req->SetExtraRequestHeaders(headers); |
| 6220 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6221 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6222 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 6223 | } |
| 6224 | |
| 6225 | // Check that default A-E header is sent. |
| 6226 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
| 6227 | ASSERT_TRUE(test_server_.Start()); |
| 6228 | |
| 6229 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6230 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6231 | test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d, |
| 6232 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6233 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6234 | req->SetExtraRequestHeaders(headers); |
| 6235 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6236 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6237 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 6238 | } |
| 6239 | |
| 6240 | // Check that if request overrides the A-E header, the default is not appended. |
| 6241 | // See http://crbug.com/47381 |
| 6242 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
| 6243 | ASSERT_TRUE(test_server_.Start()); |
| 6244 | |
| 6245 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6246 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6247 | test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d, |
| 6248 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6249 | HttpRequestHeaders headers; |
| 6250 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6251 | req->SetExtraRequestHeaders(headers); |
| 6252 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6253 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6254 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 6255 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 6256 | } |
| 6257 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 6258 | // Check that setting the A-C header sends the proper header. |
| 6259 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6260 | ASSERT_TRUE(test_server_.Start()); |
| 6261 | |
| 6262 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6263 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6264 | test_server_.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY, &d, |
| 6265 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6266 | HttpRequestHeaders headers; |
| 6267 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6268 | req->SetExtraRequestHeaders(headers); |
| 6269 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6270 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6271 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 6272 | } |
| 6273 | |
| 6274 | // Check that default User-Agent header is sent. |
| 6275 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
| 6276 | ASSERT_TRUE(test_server_.Start()); |
| 6277 | |
| 6278 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6279 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6280 | test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d, |
| 6281 | NULL)); |
| 6282 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6283 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6284 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 6285 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6286 | } |
| 6287 | |
| 6288 | // Check that if request overrides the User-Agent header, |
| 6289 | // the default is not appended. |
| 6290 | TEST_F(URLRequestTestHTTP, OverrideUserAgent) { |
| 6291 | ASSERT_TRUE(test_server_.Start()); |
| 6292 | |
| 6293 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6294 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6295 | test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d, |
| 6296 | NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6297 | HttpRequestHeaders headers; |
| 6298 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6299 | req->SetExtraRequestHeaders(headers); |
| 6300 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6301 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 6302 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6303 | } |
| 6304 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6305 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 6306 | // User-Agent header to be sent but does not send the Accept-Language and |
| 6307 | // Accept-Charset headers. |
| 6308 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
| 6309 | ASSERT_TRUE(test_server_.Start()); |
| 6310 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6311 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6312 | TestURLRequestContext context(true); |
| 6313 | context.set_network_delegate(&network_delegate); |
| 6314 | context.Init(); |
| 6315 | // We override the HttpUserAgentSettings after initialization because empty |
| 6316 | // entries get overridden by Init(). |
| 6317 | context.set_http_user_agent_settings(NULL); |
| 6318 | |
| 6319 | struct { |
| 6320 | const char* request; |
| 6321 | const char* expected_response; |
| 6322 | } tests[] = { { "echoheader?Accept-Language", "None" }, |
| 6323 | { "echoheader?Accept-Charset", "None" }, |
| 6324 | { "echoheader?User-Agent", "" } }; |
| 6325 | |
| 6326 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { |
| 6327 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6328 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6329 | test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d, NULL)); |
| 6330 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6331 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6332 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 6333 | << " Request = \"" << tests[i].request << "\""; |
| 6334 | } |
| 6335 | } |
| 6336 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6337 | // Make sure that URLRequest passes on its priority updates to |
| 6338 | // newly-created jobs after the first one. |
| 6339 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
| 6340 | ASSERT_TRUE(test_server_.Start()); |
| 6341 | |
| 6342 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6343 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 6344 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d, NULL)); |
| 6345 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6346 | |
| 6347 | scoped_refptr<URLRequestRedirectJob> redirect_job = |
| 6348 | new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6349 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6350 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6351 | AddTestInterceptor()->set_main_intercept_job(redirect_job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6352 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6353 | req->SetPriority(LOW); |
| 6354 | req->Start(); |
| 6355 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6356 | |
| 6357 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6358 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6359 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6360 | |
| 6361 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6362 | base::RunLoop().Run(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6363 | EXPECT_EQ(LOW, job->priority()); |
| 6364 | } |
| 6365 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6366 | // Check that creating a network request while entering/exiting suspend mode |
| 6367 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 6368 | // does not return an HttpTransaction. |
| 6369 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 6370 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6371 | HttpNetworkSession::Params params; |
| 6372 | params.host_resolver = default_context_.host_resolver(); |
| 6373 | params.cert_verifier = default_context_.cert_verifier(); |
| 6374 | params.transport_security_state = default_context_.transport_security_state(); |
| 6375 | params.proxy_service = default_context_.proxy_service(); |
| 6376 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6377 | params.http_auth_handler_factory = |
| 6378 | default_context_.http_auth_handler_factory(); |
| 6379 | params.network_delegate = &default_network_delegate_; |
| 6380 | params.http_server_properties = default_context_.http_server_properties(); |
| 6381 | scoped_ptr<HttpNetworkLayer> network_layer( |
| 6382 | new HttpNetworkLayer(new HttpNetworkSession(params))); |
| 6383 | network_layer->OnSuspend(); |
| 6384 | |
| 6385 | HttpCache http_cache(network_layer.release(), default_context_.net_log(), |
| 6386 | HttpCache::DefaultBackend::InMemory(0)); |
| 6387 | |
| 6388 | TestURLRequestContext context(true); |
| 6389 | context.set_http_transaction_factory(&http_cache); |
| 6390 | context.Init(); |
| 6391 | |
| 6392 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6393 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6394 | GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL)); |
| 6395 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6396 | base::RunLoop().Run(); |
| 6397 | |
| 6398 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6399 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6400 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6401 | } |
| 6402 | |
| 6403 | // Check that creating a network request while entering/exiting suspend mode |
| 6404 | // fails as it should in the case there is no cache. This is the only case |
| 6405 | // where an HttpTransactionFactory does not return an HttpTransaction. |
| 6406 | TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) { |
| 6407 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6408 | HttpNetworkSession::Params params; |
| 6409 | params.host_resolver = default_context_.host_resolver(); |
| 6410 | params.cert_verifier = default_context_.cert_verifier(); |
| 6411 | params.transport_security_state = default_context_.transport_security_state(); |
| 6412 | params.proxy_service = default_context_.proxy_service(); |
| 6413 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6414 | params.http_auth_handler_factory = |
| 6415 | default_context_.http_auth_handler_factory(); |
| 6416 | params.network_delegate = &default_network_delegate_; |
| 6417 | params.http_server_properties = default_context_.http_server_properties(); |
| 6418 | HttpNetworkLayer network_layer(new HttpNetworkSession(params)); |
| 6419 | network_layer.OnSuspend(); |
| 6420 | |
| 6421 | TestURLRequestContext context(true); |
| 6422 | context.set_http_transaction_factory(&network_layer); |
| 6423 | context.Init(); |
| 6424 | |
| 6425 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6426 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6427 | GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d, NULL)); |
| 6428 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6429 | base::RunLoop().Run(); |
| 6430 | |
| 6431 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6432 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6433 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6434 | } |
| 6435 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 6436 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 6437 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6438 | HTTPSRequestTest() : default_context_(true) { |
| 6439 | default_context_.set_network_delegate(&default_network_delegate_); |
| 6440 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 6441 | } |
| 6442 | virtual ~HTTPSRequestTest() {} |
| 6443 | |
| 6444 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6445 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 6446 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6447 | }; |
| 6448 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 6449 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6450 | SpawnedTestServer test_server( |
| 6451 | SpawnedTestServer::TYPE_HTTPS, |
| 6452 | SpawnedTestServer::kLocalhost, |
| 6453 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6454 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6455 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6456 | TestDelegate d; |
| 6457 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6458 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6459 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
| 6460 | r->Start(); |
| 6461 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6462 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6463 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6464 | |
| 6465 | EXPECT_EQ(1, d.response_started_count()); |
| 6466 | EXPECT_FALSE(d.received_data_before_response()); |
| 6467 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6468 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 6469 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6470 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 6471 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6472 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6473 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 6474 | } |
| 6475 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 6476 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6477 | SpawnedTestServer::SSLOptions ssl_options( |
| 6478 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6479 | SpawnedTestServer test_server( |
| 6480 | SpawnedTestServer::TYPE_HTTPS, |
| 6481 | ssl_options, |
| 6482 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6483 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6484 | |
| 6485 | bool err_allowed = true; |
| 6486 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 6487 | TestDelegate d; |
| 6488 | { |
| 6489 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6490 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6491 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6492 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6493 | r->Start(); |
| 6494 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6495 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6496 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6497 | |
| 6498 | EXPECT_EQ(1, d.response_started_count()); |
| 6499 | EXPECT_FALSE(d.received_data_before_response()); |
| 6500 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6501 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6502 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6503 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6504 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6505 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6506 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6507 | } |
| 6508 | } |
| 6509 | } |
| 6510 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 6511 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6512 | SpawnedTestServer::SSLOptions ssl_options( |
| 6513 | SpawnedTestServer::SSLOptions::CERT_EXPIRED); |
| 6514 | SpawnedTestServer test_server( |
| 6515 | SpawnedTestServer::TYPE_HTTPS, |
| 6516 | ssl_options, |
| 6517 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 6518 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6519 | |
| 6520 | // Iterate from false to true, just so that we do the opposite of the |
| 6521 | // previous test in order to increase test coverage. |
| 6522 | bool err_allowed = false; |
| 6523 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 6524 | TestDelegate d; |
| 6525 | { |
| 6526 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6527 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6528 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6529 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6530 | r->Start(); |
| 6531 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6532 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6533 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6534 | |
| 6535 | EXPECT_EQ(1, d.response_started_count()); |
| 6536 | EXPECT_FALSE(d.received_data_before_response()); |
| 6537 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6538 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6539 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6540 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6541 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6542 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 6543 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 6544 | } |
| 6545 | } |
| 6546 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 6547 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6548 | // This tests that a load of www.google.com with a certificate error sets |
| 6549 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 6550 | // the interstitial to be fatal. |
| 6551 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6552 | SpawnedTestServer::SSLOptions ssl_options( |
| 6553 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6554 | SpawnedTestServer test_server( |
| 6555 | SpawnedTestServer::TYPE_HTTPS, |
| 6556 | ssl_options, |
| 6557 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6558 | ASSERT_TRUE(test_server.Start()); |
| 6559 | |
| 6560 | // We require that the URL be www.google.com in order to pick up the |
| 6561 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 6562 | // 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] | 6563 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6564 | |
| 6565 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6566 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6567 | TestURLRequestContext context(true); |
| 6568 | context.set_network_delegate(&network_delegate); |
| 6569 | context.set_host_resolver(&host_resolver); |
| 6570 | TransportSecurityState transport_security_state; |
| 6571 | context.set_transport_security_state(&transport_security_state); |
| 6572 | context.Init(); |
| 6573 | |
| 6574 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6575 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 6576 | GURL(base::StringPrintf("https://www.google.com:%d", |
| 6577 | test_server.host_port_pair().port())), |
| 6578 | DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6579 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6580 | r->Start(); |
| 6581 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6582 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6583 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6584 | |
| 6585 | EXPECT_EQ(1, d.response_started_count()); |
| 6586 | EXPECT_FALSE(d.received_data_before_response()); |
| 6587 | EXPECT_TRUE(d.have_certificate_errors()); |
| 6588 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 6589 | } |
| 6590 | |
| 6591 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 6592 | // TransportSecurityState. |
| 6593 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 6594 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 6595 | // matter. It just has to be any error. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6596 | SpawnedTestServer::SSLOptions ssl_options( |
| 6597 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 6598 | SpawnedTestServer test_server( |
| 6599 | SpawnedTestServer::TYPE_HTTPS, |
| 6600 | ssl_options, |
| 6601 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6602 | ASSERT_TRUE(test_server.Start()); |
| 6603 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6604 | // We require that the URL be www.google.com in order to pick up the static |
| 6605 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 6606 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 6607 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6608 | |
| 6609 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6610 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6611 | TestURLRequestContext context(true); |
| 6612 | context.set_network_delegate(&network_delegate); |
| 6613 | context.set_host_resolver(&host_resolver); |
| 6614 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6615 | |
| 6616 | TransportSecurityState::DomainState static_domain_state; |
| 6617 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
| 6618 | "www.google.com", true, &static_domain_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6619 | context.set_transport_security_state(&transport_security_state); |
| 6620 | context.Init(); |
| 6621 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6622 | TransportSecurityState::DomainState dynamic_domain_state; |
| 6623 | EXPECT_FALSE(transport_security_state.GetDynamicDomainState( |
| 6624 | "www.google.com", &dynamic_domain_state)); |
| 6625 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6626 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6627 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 6628 | GURL(base::StringPrintf("https://www.google.com:%d", |
| 6629 | test_server.host_port_pair().port())), |
| 6630 | DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6631 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6632 | r->Start(); |
| 6633 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6634 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6635 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6636 | |
| 6637 | EXPECT_EQ(1, d.response_started_count()); |
| 6638 | EXPECT_FALSE(d.received_data_before_response()); |
| 6639 | EXPECT_TRUE(d.have_certificate_errors()); |
| 6640 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 6641 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 6642 | // Get a fresh copy of the states, and check that they haven't changed. |
| 6643 | TransportSecurityState::DomainState new_static_domain_state; |
| 6644 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
| 6645 | "www.google.com", true, &new_static_domain_state)); |
| 6646 | TransportSecurityState::DomainState new_dynamic_domain_state; |
| 6647 | EXPECT_FALSE(transport_security_state.GetDynamicDomainState( |
| 6648 | "www.google.com", &new_dynamic_domain_state)); |
| 6649 | |
| 6650 | EXPECT_EQ(new_static_domain_state.sts.upgrade_mode, |
| 6651 | static_domain_state.sts.upgrade_mode); |
| 6652 | EXPECT_EQ(new_static_domain_state.sts.include_subdomains, |
| 6653 | static_domain_state.sts.include_subdomains); |
| 6654 | EXPECT_EQ(new_static_domain_state.pkp.include_subdomains, |
| 6655 | static_domain_state.pkp.include_subdomains); |
| 6656 | EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes, |
| 6657 | static_domain_state.pkp.spki_hashes)); |
| 6658 | EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes, |
| 6659 | static_domain_state.pkp.bad_spki_hashes)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6660 | } |
| 6661 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6662 | // Make sure HSTS preserves a POST request's method and body. |
| 6663 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 6664 | static const char kData[] = "hello world"; |
| 6665 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6666 | SpawnedTestServer::SSLOptions ssl_options( |
| 6667 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 6668 | SpawnedTestServer test_server( |
| 6669 | SpawnedTestServer::TYPE_HTTPS, |
| 6670 | ssl_options, |
| 6671 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6672 | ASSERT_TRUE(test_server.Start()); |
| 6673 | |
| 6674 | |
| 6675 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 6676 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6677 | // the SpawnedTestServer. By default, MockHostResolver maps all hosts |
| 6678 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6679 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6680 | |
| 6681 | // Force https for www.somewhere.com. |
| 6682 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 6683 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 6684 | bool include_subdomains = false; |
| 6685 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 6686 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6687 | |
| 6688 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 6689 | |
| 6690 | TestURLRequestContext context(true); |
| 6691 | context.set_host_resolver(&host_resolver); |
| 6692 | context.set_transport_security_state(&transport_security_state); |
| 6693 | context.set_network_delegate(&network_delegate); |
| 6694 | context.Init(); |
| 6695 | |
| 6696 | TestDelegate d; |
| 6697 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 6698 | // cause a certificate error. Ignore the error. |
| 6699 | d.set_allow_certificate_errors(true); |
| 6700 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6701 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 6702 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
| 6703 | test_server.host_port_pair().port())), |
| 6704 | DEFAULT_PRIORITY, &d, NULL)); |
| 6705 | req->set_method("POST"); |
| 6706 | req->set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6707 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6708 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6709 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6710 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6711 | EXPECT_EQ("https", req->url().scheme()); |
| 6712 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6713 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 6714 | |
| 6715 | LoadTimingInfo load_timing_info; |
| 6716 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 6717 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 6718 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 6719 | } |
| 6720 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6721 | namespace { |
| 6722 | |
| 6723 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 6724 | public: |
| 6725 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 6726 | } |
| 6727 | virtual void OnCertificateRequested( |
| 6728 | URLRequest* request, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 6729 | SSLCertRequestInfo* cert_request_info) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6730 | on_certificate_requested_count_++; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 6731 | base::MessageLoop::current()->Quit(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6732 | } |
| 6733 | int on_certificate_requested_count() { |
| 6734 | return on_certificate_requested_count_; |
| 6735 | } |
| 6736 | private: |
| 6737 | int on_certificate_requested_count_; |
| 6738 | }; |
| 6739 | |
| 6740 | } // namespace |
| 6741 | |
| 6742 | // TODO(davidben): Test the rest of the code. Specifically, |
| 6743 | // - Filtering which certificates to select. |
| 6744 | // - Sending a certificate back. |
| 6745 | // - Getting a certificate request in an SSL renegotiation sending the |
| 6746 | // HTTP request. |
| 6747 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6748 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6749 | ssl_options.request_client_certificate = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6750 | SpawnedTestServer test_server( |
| 6751 | SpawnedTestServer::TYPE_HTTPS, |
| 6752 | ssl_options, |
| 6753 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6754 | ASSERT_TRUE(test_server.Start()); |
| 6755 | |
| 6756 | SSLClientAuthTestDelegate d; |
| 6757 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6758 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6759 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6760 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6761 | r->Start(); |
| 6762 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6763 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6764 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6765 | |
| 6766 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 6767 | EXPECT_FALSE(d.received_data_before_response()); |
| 6768 | EXPECT_EQ(0, d.bytes_received()); |
| 6769 | |
| 6770 | // Send no certificate. |
| 6771 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 6772 | // all platforms so we can test sending a cert as well. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6773 | r->ContinueWithCertificate(NULL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6774 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6775 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6776 | |
| 6777 | EXPECT_EQ(1, d.response_started_count()); |
| 6778 | EXPECT_FALSE(d.received_data_before_response()); |
| 6779 | EXPECT_NE(0, d.bytes_received()); |
| 6780 | } |
| 6781 | } |
| 6782 | |
| 6783 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 6784 | // Test that we attempt a session resume when making two connections to the |
| 6785 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6786 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6787 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6788 | SpawnedTestServer test_server( |
| 6789 | SpawnedTestServer::TYPE_HTTPS, |
| 6790 | ssl_options, |
| 6791 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6792 | ASSERT_TRUE(test_server.Start()); |
| 6793 | |
| 6794 | SSLClientSocket::ClearSessionCache(); |
| 6795 | |
| 6796 | { |
| 6797 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6798 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6799 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6800 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6801 | r->Start(); |
| 6802 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6803 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6804 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6805 | |
| 6806 | EXPECT_EQ(1, d.response_started_count()); |
| 6807 | } |
| 6808 | |
| 6809 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 6810 | CloseAllConnections(); |
| 6811 | |
| 6812 | { |
| 6813 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6814 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6815 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6816 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6817 | r->Start(); |
| 6818 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6819 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6820 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6821 | |
| 6822 | // The response will look like; |
| 6823 | // insert abc |
| 6824 | // lookup abc |
| 6825 | // insert xyz |
| 6826 | // |
| 6827 | // With a newline at the end which makes the split think that there are |
| 6828 | // four lines. |
| 6829 | |
| 6830 | EXPECT_EQ(1, d.response_started_count()); |
| 6831 | std::vector<std::string> lines; |
| 6832 | base::SplitString(d.data_received(), '\n', &lines); |
| 6833 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 6834 | |
| 6835 | std::string session_id; |
| 6836 | |
| 6837 | for (size_t i = 0; i < 2; i++) { |
| 6838 | std::vector<std::string> parts; |
| 6839 | base::SplitString(lines[i], '\t', &parts); |
| 6840 | ASSERT_EQ(2u, parts.size()); |
| 6841 | if (i == 0) { |
| 6842 | EXPECT_EQ("insert", parts[0]); |
| 6843 | session_id = parts[1]; |
| 6844 | } else { |
| 6845 | EXPECT_EQ("lookup", parts[0]); |
| 6846 | EXPECT_EQ(session_id, parts[1]); |
| 6847 | } |
| 6848 | } |
| 6849 | } |
| 6850 | } |
| 6851 | |
| 6852 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 6853 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 6854 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6855 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6856 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 6857 | SpawnedTestServer test_server( |
| 6858 | SpawnedTestServer::TYPE_HTTPS, |
| 6859 | ssl_options, |
| 6860 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6861 | ASSERT_TRUE(test_server.Start()); |
| 6862 | |
| 6863 | SSLClientSocket::ClearSessionCache(); |
| 6864 | |
| 6865 | { |
| 6866 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6867 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6868 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6869 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6870 | r->Start(); |
| 6871 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6872 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6873 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6874 | |
| 6875 | EXPECT_EQ(1, d.response_started_count()); |
| 6876 | } |
| 6877 | |
| 6878 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 6879 | HttpNetworkSession::Params params; |
| 6880 | params.host_resolver = default_context_.host_resolver(); |
| 6881 | params.cert_verifier = default_context_.cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 6882 | params.transport_security_state = default_context_.transport_security_state(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6883 | params.proxy_service = default_context_.proxy_service(); |
| 6884 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6885 | params.http_auth_handler_factory = |
| 6886 | default_context_.http_auth_handler_factory(); |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 6887 | params.network_delegate = &default_network_delegate_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6888 | params.http_server_properties = default_context_.http_server_properties(); |
| 6889 | params.ssl_session_cache_shard = "alternate"; |
| 6890 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6891 | scoped_ptr<HttpCache> cache(new HttpCache( |
| 6892 | new HttpNetworkSession(params), |
| 6893 | HttpCache::DefaultBackend::InMemory(0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6894 | |
| 6895 | default_context_.set_http_transaction_factory(cache.get()); |
| 6896 | |
| 6897 | { |
| 6898 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6899 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 6900 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6901 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6902 | r->Start(); |
| 6903 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6904 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6905 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6906 | |
| 6907 | // The response will look like; |
| 6908 | // insert abc |
| 6909 | // insert xyz |
| 6910 | // |
| 6911 | // With a newline at the end which makes the split think that there are |
| 6912 | // three lines. |
| 6913 | |
| 6914 | EXPECT_EQ(1, d.response_started_count()); |
| 6915 | std::vector<std::string> lines; |
| 6916 | base::SplitString(d.data_received(), '\n', &lines); |
| 6917 | ASSERT_EQ(3u, lines.size()); |
| 6918 | |
| 6919 | std::string session_id; |
| 6920 | for (size_t i = 0; i < 2; i++) { |
| 6921 | std::vector<std::string> parts; |
| 6922 | base::SplitString(lines[i], '\t', &parts); |
| 6923 | ASSERT_EQ(2u, parts.size()); |
| 6924 | EXPECT_EQ("insert", parts[0]); |
| 6925 | if (i == 0) { |
| 6926 | session_id = parts[1]; |
| 6927 | } else { |
| 6928 | EXPECT_NE(session_id, parts[1]); |
| 6929 | } |
| 6930 | } |
| 6931 | } |
| 6932 | } |
| 6933 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 6934 | class HTTPSFallbackTest : public testing::Test { |
| 6935 | public: |
| 6936 | HTTPSFallbackTest() : context_(true) { |
| 6937 | context_.Init(); |
| 6938 | delegate_.set_allow_certificate_errors(true); |
| 6939 | } |
| 6940 | virtual ~HTTPSFallbackTest() {} |
| 6941 | |
| 6942 | protected: |
| 6943 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 6944 | DCHECK(!request_); |
| 6945 | SpawnedTestServer test_server( |
| 6946 | SpawnedTestServer::TYPE_HTTPS, |
| 6947 | ssl_options, |
| 6948 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 6949 | ASSERT_TRUE(test_server.Start()); |
| 6950 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6951 | request_ = context_.CreateRequest( |
| 6952 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate_, NULL); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 6953 | request_->Start(); |
| 6954 | |
| 6955 | base::RunLoop().Run(); |
| 6956 | } |
| 6957 | |
| 6958 | void ExpectConnection(int version) { |
| 6959 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 6960 | EXPECT_NE(0, delegate_.bytes_received()); |
| 6961 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 6962 | request_->ssl_info().connection_status)); |
| 6963 | EXPECT_TRUE(request_->ssl_info().connection_status & |
| 6964 | SSL_CONNECTION_VERSION_FALLBACK); |
| 6965 | } |
| 6966 | |
| 6967 | void ExpectFailure(int error) { |
| 6968 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 6969 | EXPECT_FALSE(request_->status().is_success()); |
| 6970 | EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status()); |
| 6971 | EXPECT_EQ(error, request_->status().error()); |
| 6972 | } |
| 6973 | |
| 6974 | private: |
| 6975 | TestDelegate delegate_; |
| 6976 | TestURLRequestContext context_; |
| 6977 | scoped_ptr<URLRequest> request_; |
| 6978 | }; |
| 6979 | |
| 6980 | // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more |
| 6981 | // than necessary. |
| 6982 | TEST_F(HTTPSFallbackTest, TLSv1Fallback) { |
| 6983 | SpawnedTestServer::SSLOptions ssl_options( |
| 6984 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 6985 | ssl_options.tls_intolerant = |
| 6986 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 6987 | |
| 6988 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 6989 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
| 6990 | } |
| 6991 | |
| 6992 | // This test is disabled on Android because the remote test server doesn't cause |
| 6993 | // a TCP reset. |
| 6994 | #if !defined(OS_ANDROID) |
| 6995 | // Tests fallback to TLS 1.0 on connection reset. |
| 6996 | TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) { |
| 6997 | SpawnedTestServer::SSLOptions ssl_options( |
| 6998 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 6999 | ssl_options.tls_intolerant = |
| 7000 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 7001 | ssl_options.tls_intolerance_type = |
| 7002 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 7003 | |
| 7004 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7005 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
| 7006 | } |
| 7007 | #endif // !OS_ANDROID |
| 7008 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7009 | // Tests that we don't fallback on handshake failure with servers that implement |
| 7010 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7011 | TEST_F(HTTPSFallbackTest, FallbackSCSV) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7012 | SpawnedTestServer::SSLOptions ssl_options( |
| 7013 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7014 | // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger |
| 7015 | // a version fallback. |
| 7016 | ssl_options.tls_intolerant = |
| 7017 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7018 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 7019 | // connections are rejected. |
| 7020 | ssl_options.fallback_scsv_enabled = true; |
| 7021 | |
| 7022 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7023 | |
| 7024 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version |
| 7025 | // intolerance. If the fallback SCSV is processed when the original error |
| 7026 | // that caused the fallback should be returned, which should be |
| 7027 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH. |
| 7028 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 7029 | } |
| 7030 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7031 | // Tests that we don't fallback on connection closed with servers that implement |
| 7032 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7033 | TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) { |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7034 | SpawnedTestServer::SSLOptions ssl_options( |
| 7035 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7036 | // Configure HTTPS server to be intolerant of TLS >= 1.0 in order to trigger |
| 7037 | // a version fallback. |
| 7038 | ssl_options.tls_intolerant = |
| 7039 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7040 | ssl_options.tls_intolerance_type = |
| 7041 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 7042 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 7043 | // connections are rejected. |
| 7044 | ssl_options.fallback_scsv_enabled = true; |
| 7045 | |
| 7046 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7047 | |
| 7048 | // The original error should be replayed on rejected fallback. |
| 7049 | ExpectFailure(ERR_CONNECTION_CLOSED); |
| 7050 | } |
| 7051 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7052 | // Tests that the SSLv3 fallback triggers on alert. |
| 7053 | TEST_F(HTTPSFallbackTest, SSLv3Fallback) { |
| 7054 | SpawnedTestServer::SSLOptions ssl_options( |
| 7055 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7056 | ssl_options.tls_intolerant = |
| 7057 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7058 | |
| 7059 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7060 | ExpectConnection(SSL_CONNECTION_VERSION_SSL3); |
| 7061 | } |
| 7062 | |
| 7063 | // Tests that the SSLv3 fallback triggers on closed connections. |
| 7064 | TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) { |
| 7065 | SpawnedTestServer::SSLOptions ssl_options( |
| 7066 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7067 | ssl_options.tls_intolerant = |
| 7068 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7069 | ssl_options.tls_intolerance_type = |
| 7070 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 7071 | |
| 7072 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7073 | ExpectConnection(SSL_CONNECTION_VERSION_SSL3); |
| 7074 | } |
| 7075 | |
| 7076 | // This test is disabled on Android because the remote test server doesn't cause |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 7077 | // a TCP reset. |
| 7078 | #if !defined(OS_ANDROID) |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7079 | // Tests that a reset connection does not fallback down to SSL3. |
| 7080 | TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) { |
| 7081 | SpawnedTestServer::SSLOptions ssl_options( |
| 7082 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7083 | ssl_options.tls_intolerant = |
| 7084 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 7085 | ssl_options.tls_intolerance_type = |
| 7086 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 7087 | |
| 7088 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7089 | ExpectFailure(ERR_CONNECTION_RESET); |
| 7090 | } |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 7091 | #endif // !OS_ANDROID |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7092 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7093 | class HTTPSSessionTest : public testing::Test { |
| 7094 | public: |
| 7095 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7096 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7097 | |
| 7098 | default_context_.set_network_delegate(&default_network_delegate_); |
| 7099 | default_context_.set_cert_verifier(&cert_verifier_); |
| 7100 | default_context_.Init(); |
| 7101 | } |
| 7102 | virtual ~HTTPSSessionTest() {} |
| 7103 | |
| 7104 | protected: |
| 7105 | MockCertVerifier cert_verifier_; |
| 7106 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 7107 | TestURLRequestContext default_context_; |
| 7108 | }; |
| 7109 | |
| 7110 | // Tests that session resumption is not attempted if an invalid certificate |
| 7111 | // is presented. |
| 7112 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 7113 | SpawnedTestServer::SSLOptions ssl_options; |
| 7114 | ssl_options.record_resume = true; |
| 7115 | SpawnedTestServer test_server( |
| 7116 | SpawnedTestServer::TYPE_HTTPS, |
| 7117 | ssl_options, |
| 7118 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7119 | ASSERT_TRUE(test_server.Start()); |
| 7120 | |
| 7121 | SSLClientSocket::ClearSessionCache(); |
| 7122 | |
| 7123 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7124 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7125 | { |
| 7126 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7127 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7128 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7129 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7130 | r->Start(); |
| 7131 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7132 | |
| 7133 | base::RunLoop().Run(); |
| 7134 | |
| 7135 | EXPECT_EQ(1, d.response_started_count()); |
| 7136 | } |
| 7137 | |
| 7138 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 7139 | CloseAllConnections(); |
| 7140 | |
| 7141 | // Now change the certificate to be acceptable (so that the response is |
| 7142 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7143 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7144 | { |
| 7145 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7146 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7147 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d, NULL)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7148 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7149 | r->Start(); |
| 7150 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 7151 | |
| 7152 | base::RunLoop().Run(); |
| 7153 | |
| 7154 | // The response will look like; |
| 7155 | // insert abc |
| 7156 | // insert xyz |
| 7157 | // |
| 7158 | // With a newline at the end which makes the split think that there are |
| 7159 | // three lines. |
| 7160 | // |
| 7161 | // If a session was presented (eg: a bug), then the response would look |
| 7162 | // like; |
| 7163 | // insert abc |
| 7164 | // lookup abc |
| 7165 | // insert xyz |
| 7166 | |
| 7167 | EXPECT_EQ(1, d.response_started_count()); |
| 7168 | std::vector<std::string> lines; |
| 7169 | base::SplitString(d.data_received(), '\n', &lines); |
| 7170 | ASSERT_EQ(3u, lines.size()) << d.data_received(); |
| 7171 | |
| 7172 | std::string session_id; |
| 7173 | for (size_t i = 0; i < 2; i++) { |
| 7174 | std::vector<std::string> parts; |
| 7175 | base::SplitString(lines[i], '\t', &parts); |
| 7176 | ASSERT_EQ(2u, parts.size()); |
| 7177 | EXPECT_EQ("insert", parts[0]); |
| 7178 | if (i == 0) { |
| 7179 | session_id = parts[1]; |
| 7180 | } else { |
| 7181 | EXPECT_NE(session_id, parts[1]); |
| 7182 | } |
| 7183 | } |
| 7184 | } |
| 7185 | } |
| 7186 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7187 | class TestSSLConfigService : public SSLConfigService { |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7188 | public: |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7189 | TestSSLConfigService(bool ev_enabled, |
| 7190 | bool online_rev_checking, |
| 7191 | bool rev_checking_required_local_anchors) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7192 | : ev_enabled_(ev_enabled), |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7193 | online_rev_checking_(online_rev_checking), |
| 7194 | rev_checking_required_local_anchors_( |
| 7195 | rev_checking_required_local_anchors) {} |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7196 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 7197 | // SSLConfigService: |
| 7198 | virtual void GetSSLConfig(SSLConfig* config) OVERRIDE { |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7199 | *config = SSLConfig(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7200 | config->rev_checking_enabled = online_rev_checking_; |
| 7201 | config->verify_ev_cert = ev_enabled_; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7202 | config->rev_checking_required_local_anchors = |
| 7203 | rev_checking_required_local_anchors_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7204 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7205 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 7206 | protected: |
| 7207 | virtual ~TestSSLConfigService() {} |
| 7208 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7209 | private: |
| 7210 | const bool ev_enabled_; |
| 7211 | const bool online_rev_checking_; |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7212 | const bool rev_checking_required_local_anchors_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7213 | }; |
| 7214 | |
| 7215 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 7216 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 7217 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7218 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 7219 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 7220 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7221 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 7222 | // testserver. |
| 7223 | static const SHA256HashValue kOCSPTestCertSPKI = { { |
| 7224 | 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, |
| 7225 | 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, |
| 7226 | 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, |
| 7227 | 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, |
| 7228 | } }; |
| 7229 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7230 | // This is the policy OID contained in the certificates that testserver |
| 7231 | // generates. |
| 7232 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 7233 | |
| 7234 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 7235 | public: |
| 7236 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7237 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7238 | ev_test_policy_( |
| 7239 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 7240 | kOCSPTestCertFingerprint, |
| 7241 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7242 | } |
| 7243 | |
| 7244 | virtual void SetUp() OVERRIDE { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7245 | SetupContext(&context_); |
| 7246 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7247 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7248 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7249 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame^] | 7250 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 7251 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7252 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 7253 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7254 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7255 | EnsureNSSHttpIOInit(); |
| 7256 | #endif |
| 7257 | } |
| 7258 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7259 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7260 | CertStatus* out_cert_status) { |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7261 | // We always overwrite out_cert_status. |
| 7262 | *out_cert_status = 0; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7263 | SpawnedTestServer test_server( |
| 7264 | SpawnedTestServer::TYPE_HTTPS, |
| 7265 | ssl_options, |
| 7266 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7267 | ASSERT_TRUE(test_server.Start()); |
| 7268 | |
| 7269 | TestDelegate d; |
| 7270 | d.set_allow_certificate_errors(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7271 | scoped_ptr<URLRequest> r(context_.CreateRequest( |
| 7272 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d, NULL)); |
| 7273 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7274 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7275 | base::RunLoop().Run(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7276 | |
| 7277 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7278 | *out_cert_status = r->ssl_info().cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7279 | } |
| 7280 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 7281 | virtual ~HTTPSOCSPTest() { |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 7282 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7283 | ShutdownNSSHttpIO(); |
| 7284 | #endif |
| 7285 | } |
| 7286 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7287 | protected: |
| 7288 | // SetupContext configures the URLRequestContext that will be used for making |
| 7289 | // connetions to testserver. This can be overridden in test subclasses for |
| 7290 | // different behaviour. |
| 7291 | virtual void SetupContext(URLRequestContext* context) { |
| 7292 | context->set_ssl_config_service( |
| 7293 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7294 | true /* online revocation checking */, |
| 7295 | false /* require rev. checking for local |
| 7296 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7297 | } |
| 7298 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7299 | scoped_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7300 | TestURLRequestContext context_; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7301 | scoped_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7302 | }; |
| 7303 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7304 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 7305 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7306 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 7307 | // have that ability on other platforms. |
| 7308 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 7309 | #else |
| 7310 | return 0; |
| 7311 | #endif |
| 7312 | } |
| 7313 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7314 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 7315 | // operating system supports revocation checking and can distinguish between |
| 7316 | // situations where a given certificate lacks any revocation information (eg: |
| 7317 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 7318 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 7319 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 7320 | // skipped. |
| 7321 | static bool SystemSupportsHardFailRevocationChecking() { |
| 7322 | #if defined(OS_WIN) || defined(USE_NSS) || defined(OS_IOS) |
| 7323 | return true; |
| 7324 | #else |
| 7325 | return false; |
| 7326 | #endif |
| 7327 | } |
| 7328 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7329 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 7330 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 7331 | // several tests are effected because our testing EV certificate won't be |
| 7332 | // recognised as EV. |
| 7333 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 7334 | #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 7335 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 7336 | return false; |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 7337 | #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 7338 | // On OS X and Android, we use the system to tell us whether a certificate is |
| 7339 | // EV or not and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 7340 | return false; |
| 7341 | #else |
| 7342 | return true; |
| 7343 | #endif |
| 7344 | } |
| 7345 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7346 | static bool SystemSupportsOCSP() { |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 7347 | #if defined(USE_OPENSSL) |
| 7348 | // http://crbug.com/117478 - OpenSSL does not support OCSP. |
| 7349 | return false; |
| 7350 | #elif defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7351 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 7352 | #elif defined(OS_ANDROID) |
| 7353 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 7354 | return false; |
| 7355 | #else |
| 7356 | return true; |
| 7357 | #endif |
| 7358 | } |
| 7359 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7360 | TEST_F(HTTPSOCSPTest, Valid) { |
| 7361 | if (!SystemSupportsOCSP()) { |
| 7362 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7363 | return; |
| 7364 | } |
| 7365 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7366 | SpawnedTestServer::SSLOptions ssl_options( |
| 7367 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7368 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7369 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7370 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7371 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7372 | |
| 7373 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7374 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7375 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7376 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7377 | |
| 7378 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7379 | } |
| 7380 | |
| 7381 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 7382 | if (!SystemSupportsOCSP()) { |
| 7383 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7384 | return; |
| 7385 | } |
| 7386 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7387 | SpawnedTestServer::SSLOptions ssl_options( |
| 7388 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7389 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7390 | |
| 7391 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7392 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7393 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 7394 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7395 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 7396 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7397 | #endif |
| 7398 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7399 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7400 | } |
| 7401 | |
| 7402 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 7403 | if (!SystemSupportsOCSP()) { |
| 7404 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7405 | return; |
| 7406 | } |
| 7407 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7408 | SpawnedTestServer::SSLOptions ssl_options( |
| 7409 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7410 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7411 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7412 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7413 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7414 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7415 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7416 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7417 | |
| 7418 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 7419 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7420 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7421 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7422 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7423 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 7424 | protected: |
| 7425 | virtual void SetupContext(URLRequestContext* context) OVERRIDE { |
| 7426 | context->set_ssl_config_service( |
| 7427 | new TestSSLConfigService(false /* check for EV */, |
| 7428 | false /* online revocation checking */, |
| 7429 | true /* require rev. checking for local |
| 7430 | anchors */)); |
| 7431 | } |
| 7432 | }; |
| 7433 | |
| 7434 | |
| 7435 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 7436 | if (!SystemSupportsOCSP()) { |
| 7437 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7438 | return; |
| 7439 | } |
| 7440 | |
| 7441 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 7442 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 7443 | << "revocation checking"; |
| 7444 | return; |
| 7445 | } |
| 7446 | |
| 7447 | SpawnedTestServer::SSLOptions ssl_options( |
| 7448 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7449 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 7450 | |
| 7451 | CertStatus cert_status; |
| 7452 | DoConnection(ssl_options, &cert_status); |
| 7453 | |
| 7454 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 7455 | cert_status & CERT_STATUS_REVOKED); |
| 7456 | |
| 7457 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 7458 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7459 | } |
| 7460 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7461 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 7462 | protected: |
| 7463 | virtual void SetupContext(URLRequestContext* context) OVERRIDE { |
| 7464 | context->set_ssl_config_service( |
| 7465 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7466 | false /* online revocation checking */, |
| 7467 | false /* require rev. checking for local |
| 7468 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7469 | } |
| 7470 | }; |
| 7471 | |
| 7472 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 7473 | if (!SystemSupportsOCSP()) { |
| 7474 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7475 | return; |
| 7476 | } |
| 7477 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7478 | SpawnedTestServer::SSLOptions ssl_options( |
| 7479 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7480 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7481 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7482 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7483 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7484 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7485 | |
| 7486 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7487 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 7488 | |
| 7489 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7490 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7491 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7492 | } |
| 7493 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 7494 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 7495 | if (!SystemSupportsOCSP()) { |
| 7496 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7497 | return; |
| 7498 | } |
| 7499 | |
| 7500 | SpawnedTestServer::SSLOptions ssl_options( |
| 7501 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7502 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 7503 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7504 | |
| 7505 | CertStatus cert_status; |
| 7506 | DoConnection(ssl_options, &cert_status); |
| 7507 | |
| 7508 | // Currently only works for Windows. When using NSS or OS X, it's not |
| 7509 | // possible to determine whether the check failed because of actual |
| 7510 | // revocation or because there was an OCSP failure. |
| 7511 | #if defined(OS_WIN) |
| 7512 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7513 | #else |
| 7514 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7515 | #endif |
| 7516 | |
| 7517 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7518 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7519 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7520 | } |
| 7521 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7522 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 7523 | if (!SystemSupportsOCSP()) { |
| 7524 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7525 | return; |
| 7526 | } |
| 7527 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7528 | SpawnedTestServer::SSLOptions ssl_options( |
| 7529 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7530 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7531 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 7532 | |
| 7533 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7534 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7535 | |
| 7536 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7537 | |
| 7538 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7539 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7540 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7541 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7542 | } |
| 7543 | |
| 7544 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 7545 | if (!SystemSupportsOCSP()) { |
| 7546 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7547 | return; |
| 7548 | } |
| 7549 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7550 | SpawnedTestServer::SSLOptions ssl_options( |
| 7551 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7552 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7553 | SSLConfigService::SetCRLSet( |
| 7554 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7555 | |
| 7556 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7557 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7558 | |
| 7559 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7560 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 7561 | |
| 7562 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7563 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7564 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7565 | } |
| 7566 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7567 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 7568 | if (!SystemSupportsOCSP()) { |
| 7569 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7570 | return; |
| 7571 | } |
| 7572 | |
| 7573 | SpawnedTestServer::SSLOptions ssl_options( |
| 7574 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7575 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 7576 | SSLConfigService::SetCRLSet( |
| 7577 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 7578 | false, &kOCSPTestCertSPKI, ""))); |
| 7579 | |
| 7580 | CertStatus cert_status; |
| 7581 | DoConnection(ssl_options, &cert_status); |
| 7582 | |
| 7583 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 7584 | // revocation check for EV. |
| 7585 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7586 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 7587 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 7588 | EXPECT_FALSE( |
| 7589 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7590 | } |
| 7591 | |
| 7592 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 7593 | if (!SystemSupportsOCSP()) { |
| 7594 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7595 | return; |
| 7596 | } |
| 7597 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7598 | SpawnedTestServer::SSLOptions ssl_options( |
| 7599 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7600 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7601 | SSLConfigService::SetCRLSet( |
| 7602 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 7603 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7604 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7605 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7606 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7607 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 7608 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 7609 | // test. |
| 7610 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 7611 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7612 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7613 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7614 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7615 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7616 | } |
| 7617 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7618 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 7619 | // Test that when EV verification is requested, but online revocation |
| 7620 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 7621 | // no revocation checking actually happens. |
| 7622 | if (!SystemSupportsOCSP()) { |
| 7623 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 7624 | return; |
| 7625 | } |
| 7626 | |
| 7627 | // Unmark the certificate's OID as EV, which should disable revocation |
| 7628 | // checking (as per the user preference) |
| 7629 | ev_test_policy_.reset(); |
| 7630 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7631 | SpawnedTestServer::SSLOptions ssl_options( |
| 7632 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7633 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7634 | SSLConfigService::SetCRLSet( |
| 7635 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7636 | |
| 7637 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7638 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 7639 | |
| 7640 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7641 | |
| 7642 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7643 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7644 | } |
| 7645 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7646 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 7647 | protected: |
| 7648 | virtual void SetupContext(URLRequestContext* context) OVERRIDE { |
| 7649 | context->set_ssl_config_service( |
| 7650 | new TestSSLConfigService(false /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 7651 | false /* online revocation checking */, |
| 7652 | false /* require rev. checking for local |
| 7653 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7654 | } |
| 7655 | }; |
| 7656 | |
| 7657 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7658 | SpawnedTestServer::SSLOptions ssl_options( |
| 7659 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7660 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7661 | SSLConfigService::SetCRLSet( |
| 7662 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 7663 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 7664 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 7665 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 7666 | |
| 7667 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 7668 | // we don't fall back to online revocation checks. |
| 7669 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7670 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7671 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 7672 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7673 | |
| 7674 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 7675 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 7676 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 7677 | return; |
| 7678 | #endif |
| 7679 | |
| 7680 | SpawnedTestServer::SSLOptions ssl_options( |
| 7681 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 7682 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 7683 | ssl_options.cert_serial = 10; |
| 7684 | SSLConfigService::SetCRLSet( |
| 7685 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 7686 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 7687 | |
| 7688 | CertStatus cert_status = 0; |
| 7689 | DoConnection(ssl_options, &cert_status); |
| 7690 | |
| 7691 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 7692 | // reflected without online revocation checking. |
| 7693 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 7694 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 7695 | EXPECT_FALSE( |
| 7696 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 7697 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7698 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 7699 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7700 | #if !defined(DISABLE_FTP_SUPPORT) |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7701 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7702 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 7703 | URLRequestTestFTP() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7704 | : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7705 | base::FilePath()) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7706 | } |
| 7707 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7708 | protected: |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7709 | SpawnedTestServer test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 7710 | }; |
| 7711 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7712 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 7713 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7714 | ASSERT_TRUE(test_server_.Start()); |
| 7715 | |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 7716 | URLRequestJobFactoryImpl job_factory; |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7717 | FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7718 | |
| 7719 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7720 | job_factory.SetProtocolHandler( |
| 7721 | "ftp", |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 7722 | new FtpProtocolHandler(&ftp_transaction_factory)); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7723 | default_context_.set_job_factory(&job_factory); |
| 7724 | |
| 7725 | TestDelegate d; |
| 7726 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7727 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7728 | url, DEFAULT_PRIORITY, &d, NULL)); |
| 7729 | r->Start(); |
| 7730 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7731 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7732 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7733 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7734 | EXPECT_FALSE(r->is_pending()); |
| 7735 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 7736 | EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 7737 | } |
| 7738 | } |
| 7739 | |
[email protected] | 5accf733 | 2009-11-24 03:41:38 | [diff] [blame] | 7740 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7741 | TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7742 | ASSERT_TRUE(test_server_.Start()); |
| 7743 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7744 | TestDelegate d; |
| 7745 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7746 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7747 | test_server_.GetURL("/"), DEFAULT_PRIORITY, &d, NULL)); |
| 7748 | r->Start(); |
| 7749 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7750 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7751 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7752 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7753 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7754 | EXPECT_EQ(1, d.response_started_count()); |
| 7755 | EXPECT_FALSE(d.received_data_before_response()); |
| 7756 | EXPECT_LT(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7757 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7758 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7759 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7760 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 7761 | } |
| 7762 | } |
| 7763 | |
[email protected] | 7df7001 | 2010-02-04 00:09:55 | [diff] [blame] | 7764 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7765 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7766 | ASSERT_TRUE(test_server_.Start()); |
| 7767 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7768 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7769 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 7770 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7771 | TestDelegate d; |
| 7772 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7773 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7774 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, &d, NULL)); |
| 7775 | r->Start(); |
| 7776 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7777 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7778 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7779 | |
| 7780 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7781 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7782 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7783 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7784 | EXPECT_EQ(1, d.response_started_count()); |
| 7785 | EXPECT_FALSE(d.received_data_before_response()); |
| 7786 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7787 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7788 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7789 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7790 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7791 | } |
| 7792 | } |
| 7793 | |
[email protected] | e9ecbd1 | 2009-12-20 18:44:40 | [diff] [blame] | 7794 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7795 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7796 | ASSERT_TRUE(test_server_.Start()); |
| 7797 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7798 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7799 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 7800 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7801 | TestDelegate d; |
| 7802 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7803 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7804 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7805 | DEFAULT_PRIORITY, &d, NULL)); |
| 7806 | r->Start(); |
| 7807 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7808 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7809 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7810 | |
| 7811 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7812 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7813 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7814 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7815 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7816 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7817 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7818 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7819 | EXPECT_EQ(1, d.response_started_count()); |
| 7820 | EXPECT_FALSE(d.received_data_before_response()); |
| 7821 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 7822 | |
| 7823 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7824 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 7825 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7826 | } |
| 7827 | } |
| 7828 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 7829 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7830 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7831 | ASSERT_TRUE(test_server_.Start()); |
| 7832 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7833 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7834 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 7835 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7836 | TestDelegate d; |
| 7837 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7838 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7839 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 7840 | "wrong_password"), |
| 7841 | DEFAULT_PRIORITY, &d, NULL)); |
| 7842 | r->Start(); |
| 7843 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7844 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7845 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7846 | |
| 7847 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7848 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7849 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7850 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7851 | EXPECT_EQ(1, d.response_started_count()); |
| 7852 | EXPECT_FALSE(d.received_data_before_response()); |
| 7853 | EXPECT_EQ(d.bytes_received(), 0); |
| 7854 | } |
| 7855 | } |
| 7856 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 7857 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7858 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7859 | ASSERT_TRUE(test_server_.Start()); |
| 7860 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7861 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7862 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 7863 | app_path = app_path.AppendASCII("LICENSE"); |
| 7864 | TestDelegate d; |
| 7865 | // Set correct login credentials. The delegate will be asked for them when |
| 7866 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7867 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7868 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7869 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7870 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 7871 | "wrong_password"), |
| 7872 | DEFAULT_PRIORITY, &d, NULL)); |
| 7873 | r->Start(); |
| 7874 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7875 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7876 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7877 | |
| 7878 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7879 | base::GetFileSize(app_path, &file_size); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7880 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7881 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7882 | EXPECT_EQ(1, d.response_started_count()); |
| 7883 | EXPECT_FALSE(d.received_data_before_response()); |
| 7884 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 7885 | } |
| 7886 | } |
| 7887 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 7888 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7889 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7890 | ASSERT_TRUE(test_server_.Start()); |
| 7891 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7892 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7893 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 7894 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7895 | TestDelegate d; |
| 7896 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7897 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7898 | test_server_.GetURLWithUserAndPassword("/LICENSE", "wrong_user", |
| 7899 | "chrome"), |
| 7900 | DEFAULT_PRIORITY, &d, NULL)); |
| 7901 | r->Start(); |
| 7902 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7903 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7904 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7905 | |
| 7906 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7907 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7908 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7909 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 7910 | EXPECT_EQ(1, d.response_started_count()); |
| 7911 | EXPECT_FALSE(d.received_data_before_response()); |
| 7912 | EXPECT_EQ(d.bytes_received(), 0); |
| 7913 | } |
| 7914 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7915 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 7916 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7917 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7918 | ASSERT_TRUE(test_server_.Start()); |
| 7919 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7920 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7921 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 7922 | app_path = app_path.AppendASCII("LICENSE"); |
| 7923 | TestDelegate d; |
| 7924 | // Set correct login credentials. The delegate will be asked for them when |
| 7925 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 7926 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7927 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7928 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7929 | test_server_.GetURLWithUserAndPassword("/LICENSE", "wrong_user", |
| 7930 | "chrome"), |
| 7931 | DEFAULT_PRIORITY, &d, NULL)); |
| 7932 | r->Start(); |
| 7933 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7934 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7935 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7936 | |
| 7937 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7938 | base::GetFileSize(app_path, &file_size); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7939 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7940 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 7941 | EXPECT_EQ(1, d.response_started_count()); |
| 7942 | EXPECT_FALSE(d.received_data_before_response()); |
| 7943 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 7944 | } |
| 7945 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7946 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 7947 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7948 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7949 | ASSERT_TRUE(test_server_.Start()); |
| 7950 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7951 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7952 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 7953 | app_path = app_path.AppendASCII("LICENSE"); |
| 7954 | |
| 7955 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 7956 | { |
| 7957 | // Pass correct login identity in the URL. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7958 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 7959 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7960 | DEFAULT_PRIORITY, d.get(), NULL)); |
| 7961 | r->Start(); |
| 7962 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7963 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7964 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7965 | |
| 7966 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7967 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7968 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7969 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7970 | EXPECT_EQ(1, d->response_started_count()); |
| 7971 | EXPECT_FALSE(d->received_data_before_response()); |
| 7972 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 7973 | } |
| 7974 | |
| 7975 | d.reset(new TestDelegate); |
| 7976 | { |
| 7977 | // This request should use cached identity from previous request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7978 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7979 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get(), NULL)); |
| 7980 | r->Start(); |
| 7981 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7982 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7983 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7984 | |
| 7985 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 7986 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7987 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7988 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 7989 | EXPECT_EQ(1, d->response_started_count()); |
| 7990 | EXPECT_FALSE(d->received_data_before_response()); |
| 7991 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 7992 | } |
| 7993 | } |
| 7994 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 7995 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 7996 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7997 | ASSERT_TRUE(test_server_.Start()); |
| 7998 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 7999 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8000 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 8001 | app_path = app_path.AppendASCII("LICENSE"); |
| 8002 | |
| 8003 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 8004 | // Set correct login credentials. The delegate will be asked for them when |
| 8005 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 8006 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8007 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8008 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8009 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 8010 | "wrong_password"), |
| 8011 | DEFAULT_PRIORITY, d.get(), NULL)); |
| 8012 | r->Start(); |
| 8013 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8014 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8015 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8016 | |
| 8017 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8018 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8019 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8020 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8021 | EXPECT_EQ(1, d->response_started_count()); |
| 8022 | EXPECT_FALSE(d->received_data_before_response()); |
| 8023 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 8024 | } |
| 8025 | |
| 8026 | // Use a new delegate without explicit credentials. The cached ones should be |
| 8027 | // used. |
| 8028 | d.reset(new TestDelegate); |
| 8029 | { |
| 8030 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 8031 | // ones. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8032 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8033 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get(), NULL)); |
| 8034 | r->Start(); |
| 8035 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8036 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8037 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8038 | |
| 8039 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8040 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8041 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8042 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 8043 | EXPECT_EQ(1, d->response_started_count()); |
| 8044 | EXPECT_FALSE(d->received_data_before_response()); |
| 8045 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 8046 | } |
| 8047 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8048 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 8049 | |
| 8050 | } // namespace net |