[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> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 13 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 14 | #include "base/basictypes.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 15 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 16 | #include "base/compiler_specific.h" |
thestig | d8df033 | 2014-09-04 06:33:29 | [diff] [blame] | 17 | #include "base/files/file_util.h" |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 18 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 19 | #include "base/format_macros.h" |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 20 | #include "base/memory/scoped_ptr.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" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 32 | #include "net/base/chunked_upload_data_stream.h" |
| 33 | #include "net/base/elements_upload_data_stream.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 35 | #include "net/base/load_timing_info.h" |
| 36 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 37 | #include "net/base/net_errors.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 38 | #include "net/base/net_module.h" |
| 39 | #include "net/base/net_util.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 40 | #include "net/base/request_priority.h" |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 41 | #include "net/base/test_data_directory.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 42 | #include "net/base/upload_bytes_element_reader.h" |
| 43 | #include "net/base/upload_data_stream.h" |
| 44 | #include "net/base/upload_file_element_reader.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 45 | #include "net/cert/ev_root_ca_metadata.h" |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 46 | #include "net/cert/mock_cert_verifier.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 47 | #include "net/cert/test_root_certs.h" |
eroman | ed744f3 | 2015-04-09 06:35:49 | [diff] [blame] | 48 | #include "net/cert_net/nss_ocsp.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" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 61 | #include "net/log/net_log.h" |
| 62 | #include "net/log/net_log_unittest.h" |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 63 | #include "net/log/test_net_log.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 64 | #include "net/proxy/proxy_service.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 65 | #include "net/socket/ssl_client_socket.h" |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 66 | #include "net/ssl/ssl_cipher_suite_names.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 67 | #include "net/ssl/ssl_connection_status_flags.h" |
[email protected] | 6e7845ae | 2013-03-29 21:48:11 | [diff] [blame] | 68 | #include "net/test/cert_test_util.h" |
[email protected] | 89b3252 | 2013-05-07 20:04:21 | [diff] [blame] | 69 | #include "net/test/spawned_test_server/spawned_test_server.h" |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 70 | #include "net/url_request/data_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 71 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 72 | #include "net/url_request/url_request.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 73 | #include "net/url_request/url_request_http_job.h" |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 74 | #include "net/url_request/url_request_intercepting_job_factory.h" |
| 75 | #include "net/url_request/url_request_interceptor.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 76 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 77 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 78 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 79 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 80 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 81 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 82 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 83 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 84 | #include "net/base/filename_util.h" |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 85 | #include "net/url_request/file_protocol_handler.h" |
| 86 | #include "net/url_request/url_request_file_dir_job.h" |
| 87 | #endif |
| 88 | |
| 89 | #if !defined(DISABLE_FTP_SUPPORT) |
| 90 | #include "net/url_request/ftp_protocol_handler.h" |
| 91 | #endif |
| 92 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 93 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 94 | #include "base/win/scoped_com_initializer.h" |
[email protected] | aed9efb | 2013-04-13 01:20:56 | [diff] [blame] | 95 | #include "base/win/scoped_comptr.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 96 | #include "base/win/windows_version.h" |
| 97 | #endif |
| 98 | |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 99 | using base::ASCIIToUTF16; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 100 | using base::Time; |
halton.huo | e4e4574 | 2014-12-08 07:55:46 | [diff] [blame] | 101 | using std::string; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 102 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 103 | namespace net { |
| 104 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 105 | namespace { |
| 106 | |
[email protected] | 42cba2fb | 2013-03-29 19:58:57 | [diff] [blame] | 107 | const base::string16 kChrome(ASCIIToUTF16("chrome")); |
| 108 | const base::string16 kSecret(ASCIIToUTF16("secret")); |
| 109 | const base::string16 kUser(ASCIIToUTF16("user")); |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 110 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 111 | // Tests load timing information in the case a fresh connection was used, with |
| 112 | // no proxy. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 113 | void TestLoadTimingNotReused(const LoadTimingInfo& load_timing_info, |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 114 | int connect_timing_flags) { |
| 115 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 116 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 117 | |
| 118 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 119 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 120 | |
| 121 | EXPECT_LE(load_timing_info.request_start, |
| 122 | load_timing_info.connect_timing.connect_start); |
| 123 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 124 | connect_timing_flags); |
| 125 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 126 | load_timing_info.send_start); |
| 127 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 128 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 129 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 130 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 131 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 132 | } |
| 133 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 134 | // Same as above, but with proxy times. |
| 135 | void TestLoadTimingNotReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 136 | const LoadTimingInfo& load_timing_info, |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 137 | int connect_timing_flags) { |
| 138 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 139 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 140 | |
| 141 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 142 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 143 | |
| 144 | EXPECT_LE(load_timing_info.request_start, |
| 145 | load_timing_info.proxy_resolve_start); |
| 146 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 147 | load_timing_info.proxy_resolve_end); |
| 148 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 149 | load_timing_info.connect_timing.connect_start); |
| 150 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 151 | connect_timing_flags); |
| 152 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 153 | load_timing_info.send_start); |
| 154 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 155 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 156 | } |
| 157 | |
| 158 | // Same as above, but with a reused socket and proxy times. |
| 159 | void TestLoadTimingReusedWithProxy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 160 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 161 | EXPECT_TRUE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 162 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 163 | |
| 164 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 165 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 166 | |
| 167 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 168 | |
| 169 | EXPECT_LE(load_timing_info.request_start, |
| 170 | load_timing_info.proxy_resolve_start); |
| 171 | EXPECT_LE(load_timing_info.proxy_resolve_start, |
| 172 | load_timing_info.proxy_resolve_end); |
| 173 | EXPECT_LE(load_timing_info.proxy_resolve_end, |
| 174 | load_timing_info.send_start); |
| 175 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 176 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 177 | } |
| 178 | |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 179 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 180 | // Tests load timing information in the case of a cache hit, when no cache |
| 181 | // validation request was sent over the wire. |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 182 | base::StringPiece TestNetResourceProvider(int key) { |
| 183 | return "header"; |
| 184 | } |
| 185 | |
| 186 | void FillBuffer(char* buffer, size_t len) { |
| 187 | static bool called = false; |
| 188 | if (!called) { |
| 189 | called = true; |
| 190 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 191 | srand(seed); |
| 192 | } |
| 193 | |
| 194 | for (size_t i = 0; i < len; i++) { |
| 195 | buffer[i] = static_cast<char>(rand()); |
| 196 | if (!buffer[i]) |
| 197 | buffer[i] = 'g'; |
| 198 | } |
| 199 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 200 | #endif |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 201 | |
| 202 | #if !defined(OS_IOS) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 203 | void TestLoadTimingCacheHitNoNetwork( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 204 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 205 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 206 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 207 | |
| 208 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 209 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 210 | |
| 211 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 212 | EXPECT_LE(load_timing_info.request_start, load_timing_info.send_start); |
| 213 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 214 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 215 | |
| 216 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 217 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 218 | } |
| 219 | |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 220 | #if !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 221 | // Tests load timing in the case that there is no HTTP response. This can be |
| 222 | // used to test in the case of errors or non-HTTP requests. |
| 223 | void TestLoadTimingNoHttpResponse( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 224 | const LoadTimingInfo& load_timing_info) { |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 225 | EXPECT_FALSE(load_timing_info.socket_reused); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 226 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 227 | |
| 228 | // Only the request times should be non-null. |
| 229 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 230 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 231 | |
| 232 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 233 | |
| 234 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 235 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 236 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 237 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 238 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 239 | } |
xunjieli | a688820 | 2015-04-14 21:34:25 | [diff] [blame] | 240 | #endif |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 241 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 242 | // Do a case-insensitive search through |haystack| for |needle|. |
| 243 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 244 | std::string::const_iterator it = |
| 245 | std::search(haystack.begin(), |
| 246 | haystack.end(), |
| 247 | needle, |
| 248 | needle + strlen(needle), |
[email protected] | 07f1cee | 2010-11-03 03:53:35 | [diff] [blame] | 249 | base::CaseInsensitiveCompare<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 250 | return it != haystack.end(); |
| 251 | } |
| 252 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 253 | scoped_ptr<UploadDataStream> CreateSimpleUploadData(const char* data) { |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 254 | scoped_ptr<UploadElementReader> reader( |
| 255 | new UploadBytesElementReader(data, strlen(data))); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 256 | return ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 257 | } |
| 258 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 259 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 260 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 261 | // -1 means unknown. 0 means no encryption. |
| 262 | EXPECT_GT(ssl_info.security_bits, 0); |
| 263 | |
| 264 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 265 | uint16 cipher_suite = SSLConnectionStatusToCipherSuite( |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 266 | ssl_info.connection_status); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 267 | EXPECT_NE(0U, cipher_suite); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 270 | void CheckFullRequestHeaders(const HttpRequestHeaders& headers, |
| 271 | const GURL& host_url) { |
| 272 | std::string sent_value; |
| 273 | |
| 274 | EXPECT_TRUE(headers.GetHeader("Host", &sent_value)); |
| 275 | EXPECT_EQ(GetHostAndOptionalPort(host_url), sent_value); |
| 276 | |
| 277 | EXPECT_TRUE(headers.GetHeader("Connection", &sent_value)); |
| 278 | EXPECT_EQ("keep-alive", sent_value); |
| 279 | } |
| 280 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 281 | bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) { |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 282 | size_t size = a.size(); |
| 283 | |
| 284 | if (size != b.size()) |
| 285 | return false; |
| 286 | |
| 287 | for (size_t i = 0; i < size; ++i) { |
| 288 | if (!a[i].Equals(b[i])) |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | return true; |
| 293 | } |
[email protected] | e3a8545 | 2013-11-14 01:46:17 | [diff] [blame] | 294 | #endif // !defined(OS_IOS) |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 295 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 296 | // A network delegate that allows the user to choose a subset of request stages |
| 297 | // to block in. When blocking, the delegate can do one of the following: |
| 298 | // * synchronously return a pre-specified error code, or |
| 299 | // * asynchronously return that value via an automatically called callback, |
| 300 | // or |
| 301 | // * block and wait for the user to do a callback. |
| 302 | // Additionally, the user may also specify a redirect URL -- then each request |
| 303 | // with the current URL different from the redirect target will be redirected |
| 304 | // to that target, in the on-before-URL-request stage, independent of whether |
| 305 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 306 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 307 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 308 | // Stages in which the delegate can block. |
| 309 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 310 | NOT_BLOCKED = 0, |
| 311 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 312 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 313 | ON_HEADERS_RECEIVED = 1 << 2, |
| 314 | ON_AUTH_REQUIRED = 1 << 3 |
| 315 | }; |
| 316 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 317 | // Behavior during blocked stages. During other stages, just |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 318 | // returns OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 319 | enum BlockMode { |
| 320 | SYNCHRONOUS, // No callback, returns specified return values. |
| 321 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 322 | // specified return codes. |
| 323 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 324 | // |auth_retval_| are ignored. In every blocking stage the |
| 325 | // message loop is quit. |
| 326 | }; |
| 327 | |
| 328 | // Creates a delegate which does not block at all. |
| 329 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 330 | |
| 331 | // For users to trigger a callback returning |response|. |
| 332 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 333 | // Only call if |block_mode_| == USER_CALLBACK. |
| 334 | void DoCallback(int response); |
| 335 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 336 | |
| 337 | // Setters. |
| 338 | void set_retval(int retval) { |
| 339 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 340 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 341 | ASSERT_NE(OK, retval); |
| 342 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 345 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 346 | // |auth_credentials_| will be passed with the response. |
| 347 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 348 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 349 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 350 | auth_retval_ = auth_retval; |
| 351 | } |
| 352 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 353 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 354 | } |
| 355 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 356 | void set_redirect_url(const GURL& url) { |
| 357 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 360 | void set_block_on(int block_on) { |
| 361 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 362 | } |
| 363 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 364 | // Allows the user to check in which state did we block. |
| 365 | Stage stage_blocked_for_callback() const { |
| 366 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 367 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 371 | void RunCallback(int response, const CompletionCallback& callback); |
| 372 | void RunAuthCallback(AuthRequiredResponse response, |
| 373 | const AuthCallback& callback); |
| 374 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 375 | // TestNetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 376 | int OnBeforeURLRequest(URLRequest* request, |
| 377 | const CompletionCallback& callback, |
| 378 | GURL* new_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 379 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 380 | int OnBeforeSendHeaders(URLRequest* request, |
| 381 | const CompletionCallback& callback, |
| 382 | HttpRequestHeaders* headers) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 383 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 384 | int OnHeadersReceived( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 385 | URLRequest* request, |
| 386 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 387 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 388 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 389 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 390 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 391 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 392 | URLRequest* request, |
| 393 | const AuthChallengeInfo& auth_info, |
| 394 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 395 | AuthCredentials* credentials) override; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 396 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 397 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 398 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 399 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 400 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 401 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 402 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 403 | |
| 404 | // Configuration parameters, can be adjusted by public methods: |
| 405 | const BlockMode block_mode_; |
| 406 | |
| 407 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 408 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 409 | int retval_; // To be returned in non-auth stages. |
| 410 | AuthRequiredResponse auth_retval_; |
| 411 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 412 | GURL redirect_url_; // Used if non-empty during OnBeforeURLRequest. |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 413 | int block_on_; // Bit mask: in which stages to block. |
| 414 | |
| 415 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 416 | // callback. |
| 417 | AuthCredentials auth_credentials_; |
| 418 | AuthCredentials* target_auth_credentials_; |
| 419 | |
| 420 | // Internal variables, not set by not the user: |
| 421 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 422 | // USER_CALLBACK). |
| 423 | Stage stage_blocked_for_callback_; |
| 424 | |
| 425 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 426 | CompletionCallback callback_; |
| 427 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 428 | |
| 429 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 430 | |
| 431 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 432 | }; |
| 433 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 434 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 435 | : block_mode_(block_mode), |
| 436 | retval_(OK), |
| 437 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 438 | block_on_(0), |
| 439 | target_auth_credentials_(NULL), |
| 440 | stage_blocked_for_callback_(NOT_BLOCKED), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 441 | weak_factory_(this) { |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 445 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 446 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 447 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 448 | CompletionCallback callback = callback_; |
| 449 | Reset(); |
| 450 | RunCallback(response, callback); |
| 451 | } |
| 452 | |
| 453 | void BlockingNetworkDelegate::DoAuthCallback( |
| 454 | NetworkDelegate::AuthRequiredResponse response) { |
| 455 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 456 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 457 | AuthCallback auth_callback = auth_callback_; |
| 458 | Reset(); |
| 459 | RunAuthCallback(response, auth_callback); |
| 460 | } |
| 461 | |
| 462 | void BlockingNetworkDelegate::RunCallback(int response, |
| 463 | const CompletionCallback& callback) { |
| 464 | callback.Run(response); |
| 465 | } |
| 466 | |
| 467 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 468 | const AuthCallback& callback) { |
| 469 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 470 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 471 | *target_auth_credentials_ = auth_credentials_; |
| 472 | } |
| 473 | callback.Run(response); |
| 474 | } |
| 475 | |
| 476 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 477 | URLRequest* request, |
| 478 | const CompletionCallback& callback, |
| 479 | GURL* new_url) { |
| 480 | if (redirect_url_ == request->url()) |
| 481 | return OK; // We've already seen this request and redirected elsewhere. |
| 482 | |
| 483 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 484 | |
| 485 | if (!redirect_url_.is_empty()) |
| 486 | *new_url = redirect_url_; |
| 487 | |
| 488 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 489 | } |
| 490 | |
| 491 | int BlockingNetworkDelegate::OnBeforeSendHeaders( |
| 492 | URLRequest* request, |
| 493 | const CompletionCallback& callback, |
| 494 | HttpRequestHeaders* headers) { |
| 495 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 496 | |
| 497 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 498 | } |
| 499 | |
| 500 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 501 | URLRequest* request, |
| 502 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 503 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 504 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
| 505 | GURL* allowed_unsafe_redirect_url) { |
| 506 | TestNetworkDelegate::OnHeadersReceived(request, |
| 507 | callback, |
| 508 | original_response_headers, |
| 509 | override_response_headers, |
| 510 | allowed_unsafe_redirect_url); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 511 | |
| 512 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 513 | } |
| 514 | |
| 515 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 516 | URLRequest* request, |
| 517 | const AuthChallengeInfo& auth_info, |
| 518 | const AuthCallback& callback, |
| 519 | AuthCredentials* credentials) { |
| 520 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 521 | credentials); |
| 522 | // Check that the user has provided callback for the previous blocked stage. |
| 523 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 524 | |
| 525 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 526 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 527 | } |
| 528 | |
| 529 | target_auth_credentials_ = credentials; |
| 530 | |
| 531 | switch (block_mode_) { |
| 532 | case SYNCHRONOUS: |
| 533 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 534 | *target_auth_credentials_ = auth_credentials_; |
| 535 | return auth_retval_; |
| 536 | |
| 537 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 538 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 539 | FROM_HERE, |
| 540 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 541 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 542 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 543 | |
| 544 | case USER_CALLBACK: |
| 545 | auth_callback_ = callback; |
| 546 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 547 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 548 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 549 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 550 | } |
| 551 | NOTREACHED(); |
| 552 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 553 | } |
| 554 | |
| 555 | void BlockingNetworkDelegate::Reset() { |
| 556 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 557 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 558 | callback_.Reset(); |
| 559 | auth_callback_.Reset(); |
| 560 | } |
| 561 | |
| 562 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 563 | BlockingNetworkDelegate::Stage stage, |
| 564 | const CompletionCallback& callback) { |
| 565 | // Check that the user has provided callback for the previous blocked stage. |
| 566 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 567 | |
| 568 | if ((block_on_ & stage) == 0) { |
| 569 | return OK; |
| 570 | } |
| 571 | |
| 572 | switch (block_mode_) { |
| 573 | case SYNCHRONOUS: |
| 574 | EXPECT_NE(OK, retval_); |
| 575 | return retval_; |
| 576 | |
| 577 | case AUTO_CALLBACK: |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 578 | base::MessageLoop::current()->PostTask( |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 579 | FROM_HERE, |
| 580 | base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 581 | weak_factory_.GetWeakPtr(), retval_, callback)); |
| 582 | return ERR_IO_PENDING; |
| 583 | |
| 584 | case USER_CALLBACK: |
| 585 | callback_ = callback; |
| 586 | stage_blocked_for_callback_ = stage; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 587 | base::MessageLoop::current()->PostTask(FROM_HERE, |
| 588 | base::MessageLoop::QuitClosure()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 589 | return ERR_IO_PENDING; |
| 590 | } |
| 591 | NOTREACHED(); |
| 592 | return 0; |
| 593 | } |
| 594 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 595 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 596 | public: |
| 597 | // Does not own |delegate|. |
| 598 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 599 | NetworkDelegate* delegate) |
| 600 | : TestURLRequestContext(true) { |
| 601 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 602 | set_network_delegate(delegate); |
| 603 | Init(); |
| 604 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 605 | ~TestURLRequestContextWithProxy() override {} |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 606 | }; |
| 607 | |
| 608 | } // namespace |
| 609 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 610 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 611 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 612 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 613 | URLRequestTest() : default_context_(true) { |
| 614 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 615 | default_context_.set_net_log(&net_log_); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 616 | job_factory_impl_ = new URLRequestJobFactoryImpl(); |
| 617 | job_factory_.reset(job_factory_impl_); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 618 | } |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 619 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 620 | ~URLRequestTest() override { |
[email protected] | e4034ad | 2013-09-20 08:36:18 | [diff] [blame] | 621 | // URLRequestJobs may post clean-up tasks on destruction. |
| 622 | base::RunLoop().RunUntilIdle(); |
| 623 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 624 | |
dcheng | 2339883c | 2014-12-23 00:23:05 | [diff] [blame] | 625 | void SetUp() override { |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 626 | SetUpFactory(); |
| 627 | default_context_.set_job_factory(job_factory_.get()); |
| 628 | default_context_.Init(); |
| 629 | PlatformTest::SetUp(); |
| 630 | } |
| 631 | |
| 632 | virtual void SetUpFactory() { |
| 633 | job_factory_impl_->SetProtocolHandler("data", new DataProtocolHandler); |
| 634 | #if !defined(DISABLE_FILE_SUPPORT) |
| 635 | job_factory_impl_->SetProtocolHandler( |
| 636 | "file", new FileProtocolHandler(base::MessageLoopProxy::current())); |
| 637 | #endif |
| 638 | } |
| 639 | |
| 640 | TestNetworkDelegate* default_network_delegate() { |
| 641 | return &default_network_delegate_; |
| 642 | } |
| 643 | |
| 644 | const TestURLRequestContext& default_context() const { |
| 645 | return default_context_; |
| 646 | } |
| 647 | |
| 648 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 649 | // Adds the TestJobInterceptor to the default context. |
| 650 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 651 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 652 | job_factory_impl_->SetProtocolHandler("http", NULL); |
| 653 | job_factory_impl_->SetProtocolHandler("http", protocol_handler_); |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 654 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 655 | } |
| 656 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 657 | protected: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 658 | TestNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 659 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 660 | URLRequestJobFactoryImpl* job_factory_impl_; |
| 661 | scoped_ptr<URLRequestJobFactory> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 662 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 663 | }; |
| 664 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 665 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 666 | TestDelegate d; |
| 667 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 668 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 669 | GURL("about:blank"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 670 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 671 | r->Start(); |
| 672 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 673 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 674 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 675 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 676 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 677 | EXPECT_FALSE(d.received_data_before_response()); |
| 678 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 679 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 680 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 681 | |
| 682 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 683 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 688 | TestDelegate d; |
| 689 | { |
| 690 | // Use our nice little Chrome logo. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 691 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 692 | GURL( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 693 | "data:image/png;base64," |
| 694 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUB" |
| 695 | "BjG3w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQ" |
| 696 | "xIEVxitD5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI" |
| 697 | "5MpCVdW0gO7tvNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKz" |
| 698 | "Mx1+fg9bAgK6zHq9cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7Ck" |
| 699 | "IMDxQpF7r/MWq12UctI1dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0" |
| 700 | "WYUqt57yWf2McHTObYPbVD+ZwbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2H" |
| 701 | "QQcm43XwmtoYM6vVKleh0uoWvnzW3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nk" |
| 702 | "DFHF9ZS+uYVjRUasMeHUmyLYtcklTvzWGFZnNOXczThvpKIzjcahSqIzkvDLayDq6" |
| 703 | "D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTbxXX1T5xsV9tm9r4TQwHLiZw/pdDZJ" |
| 704 | "ea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5MrVvMzNaI3+ERHfrFzPKQukrQ" |
| 705 | "GI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gVMd6q5c8GdosynKmSeRuGz" |
| 706 | "pjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq/uxxZKzNiZFGD0wRC" |
| 707 | "3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7FUx1qLnV7MGF40" |
| 708 | "smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufBJbsysjjW4" |
| 709 | "kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJMnlm2O" |
| 710 | "34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfmm" |
| 711 | "oRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV" |
| 712 | "5EnhORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
| 713 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 714 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 715 | r->Start(); |
| 716 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 717 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 718 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 719 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 720 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 721 | EXPECT_FALSE(d.received_data_before_response()); |
| 722 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 723 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 724 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 725 | |
| 726 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 727 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 731 | #if !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 732 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 733 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 734 | PathService::Get(base::FILE_EXE, &app_path); |
| 735 | GURL app_url = FilePathToFileURL(app_path); |
| 736 | |
| 737 | TestDelegate d; |
| 738 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 739 | scoped_ptr<URLRequest> r( |
| 740 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 741 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 742 | r->Start(); |
| 743 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 744 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 745 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 746 | |
| 747 | int64 file_size = -1; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 748 | EXPECT_TRUE(base::GetFileSize(app_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 749 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 750 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 751 | EXPECT_EQ(1, d.response_started_count()); |
| 752 | EXPECT_FALSE(d.received_data_before_response()); |
| 753 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 754 | EXPECT_EQ("", r->GetSocketAddress().host()); |
| 755 | EXPECT_EQ(0, r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 756 | |
| 757 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 758 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 762 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 763 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 764 | PathService::Get(base::FILE_EXE, &app_path); |
| 765 | GURL app_url = FilePathToFileURL(app_path); |
| 766 | |
| 767 | TestDelegate d; |
| 768 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 769 | scoped_ptr<URLRequest> r( |
| 770 | default_context_.CreateRequest(app_url, DEFAULT_PRIORITY, &d)); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 771 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 772 | r->Start(); |
| 773 | EXPECT_TRUE(r->is_pending()); |
| 774 | r->Cancel(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 775 | } |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 776 | // Async cancellation should be safe even when URLRequest has been already |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 777 | // destroyed. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 778 | base::RunLoop().RunUntilIdle(); |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 779 | } |
| 780 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 781 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 782 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 783 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 784 | FillBuffer(buffer.get(), buffer_size); |
| 785 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 786 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 787 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 788 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 789 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 790 | |
| 791 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 792 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 793 | |
| 794 | const size_t first_byte_position = 500; |
| 795 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 796 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 797 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 798 | buffer.get() + last_byte_position + 1); |
| 799 | |
| 800 | TestDelegate d; |
| 801 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 802 | scoped_ptr<URLRequest> r( |
| 803 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 804 | |
| 805 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 806 | headers.SetHeader( |
| 807 | HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 808 | HttpByteRange::Bounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 809 | first_byte_position, last_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 810 | r->SetExtraRequestHeaders(headers); |
| 811 | r->Start(); |
| 812 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 813 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 814 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 815 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 816 | EXPECT_EQ(1, d.response_started_count()); |
| 817 | EXPECT_FALSE(d.received_data_before_response()); |
| 818 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 819 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 820 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 821 | } |
| 822 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 823 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 827 | const size_t buffer_size = 4000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 828 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 829 | FillBuffer(buffer.get(), buffer_size); |
| 830 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 831 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 832 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 833 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 834 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 835 | |
| 836 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 837 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 838 | |
| 839 | const size_t first_byte_position = 500; |
| 840 | const size_t last_byte_position = buffer_size - 1; |
| 841 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 842 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 843 | buffer.get() + last_byte_position + 1); |
| 844 | |
| 845 | TestDelegate d; |
| 846 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 847 | scoped_ptr<URLRequest> r( |
| 848 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 849 | |
| 850 | HttpRequestHeaders headers; |
| 851 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 852 | HttpByteRange::RightUnbounded( |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 853 | first_byte_position).GetHeaderValue()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 854 | r->SetExtraRequestHeaders(headers); |
| 855 | r->Start(); |
| 856 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 857 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 858 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 859 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 860 | EXPECT_EQ(1, d.response_started_count()); |
| 861 | EXPECT_FALSE(d.received_data_before_response()); |
| 862 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 863 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 864 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 865 | } |
| 866 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 867 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 871 | const size_t buffer_size = 400000; |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 872 | scoped_ptr<char[]> buffer(new char[buffer_size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 873 | FillBuffer(buffer.get(), buffer_size); |
| 874 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 875 | base::FilePath temp_path; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 876 | EXPECT_TRUE(base::CreateTemporaryFile(&temp_path)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 877 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 878 | EXPECT_TRUE(base::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 879 | |
| 880 | int64 file_size; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 881 | EXPECT_TRUE(base::GetFileSize(temp_path, &file_size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 882 | |
| 883 | TestDelegate d; |
| 884 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 885 | scoped_ptr<URLRequest> r( |
| 886 | default_context_.CreateRequest(temp_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 887 | |
| 888 | HttpRequestHeaders headers; |
[email protected] | b7572ea | 2013-11-26 20:16:38 | [diff] [blame] | 889 | headers.SetHeader(HttpRequestHeaders::kRange, "bytes=0-0,10-200,200-300"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 890 | r->SetExtraRequestHeaders(headers); |
| 891 | r->Start(); |
| 892 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 893 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 894 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 895 | EXPECT_TRUE(d.request_failed()); |
| 896 | } |
| 897 | |
[email protected] | dd3aa79 | 2013-07-16 19:10:23 | [diff] [blame] | 898 | EXPECT_TRUE(base::DeleteFile(temp_path, false)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 899 | } |
| 900 | |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 901 | TEST_F(URLRequestTest, AllowFileURLs) { |
| 902 | base::ScopedTempDir temp_dir; |
| 903 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 904 | base::FilePath test_file; |
[email protected] | 03d9afc0 | 2013-12-03 17:55:52 | [diff] [blame] | 905 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir.path(), &test_file)); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 906 | std::string test_data("monkey"); |
[email protected] | e5c2a22e | 2014-03-06 20:42:30 | [diff] [blame] | 907 | base::WriteFile(test_file, test_data.data(), test_data.size()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 908 | GURL test_file_url = FilePathToFileURL(test_file); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 909 | |
| 910 | { |
| 911 | TestDelegate d; |
| 912 | TestNetworkDelegate network_delegate; |
| 913 | network_delegate.set_can_access_files(true); |
| 914 | default_context_.set_network_delegate(&network_delegate); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 915 | scoped_ptr<URLRequest> r( |
| 916 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 917 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 918 | base::RunLoop().Run(); |
| 919 | EXPECT_FALSE(d.request_failed()); |
| 920 | EXPECT_EQ(test_data, d.data_received()); |
| 921 | } |
| 922 | |
| 923 | { |
| 924 | TestDelegate d; |
| 925 | TestNetworkDelegate network_delegate; |
| 926 | network_delegate.set_can_access_files(false); |
| 927 | default_context_.set_network_delegate(&network_delegate); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 928 | scoped_ptr<URLRequest> r( |
| 929 | default_context_.CreateRequest(test_file_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 930 | r->Start(); |
[email protected] | 3ca8b36 | 2013-11-11 22:18:07 | [diff] [blame] | 931 | base::RunLoop().Run(); |
| 932 | EXPECT_TRUE(d.request_failed()); |
| 933 | EXPECT_EQ("", d.data_received()); |
| 934 | } |
| 935 | } |
| 936 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 937 | |
| 938 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 939 | // Put in mock resource provider. |
| 940 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 941 | |
| 942 | TestDelegate d; |
| 943 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 944 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 945 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 946 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 947 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 948 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 949 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 950 | FilePathToFileURL(file_path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 951 | req->Start(); |
| 952 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 953 | |
| 954 | d.set_cancel_in_received_data_pending(true); |
| 955 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 956 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | // Take out mock resource provider. |
| 960 | NetModule::SetResourceProvider(NULL); |
| 961 | } |
| 962 | |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 963 | TEST_F(URLRequestTest, FileDirOutputSanity) { |
| 964 | // Verify the general sanity of the the output of the file: |
| 965 | // directory lister by checking for the output of a known existing |
| 966 | // file. |
| 967 | const char sentinel_name[] = "filedir-sentinel"; |
| 968 | |
| 969 | base::FilePath path; |
| 970 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 971 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 972 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 973 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 974 | |
| 975 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 976 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 977 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 978 | req->Start(); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 979 | base::RunLoop().Run(); |
| 980 | |
| 981 | // Generate entry for the sentinel file. |
| 982 | base::FilePath sentinel_path = path.AppendASCII(sentinel_name); |
[email protected] | 54124ed0 | 2014-01-07 10:06:58 | [diff] [blame] | 983 | base::File::Info info; |
[email protected] | 9eae4e6 | 2013-12-04 20:56:49 | [diff] [blame] | 984 | EXPECT_TRUE(base::GetFileInfo(sentinel_path, &info)); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 985 | EXPECT_GT(info.size, 0); |
| 986 | std::string sentinel_output = GetDirectoryListingEntry( |
| 987 | base::string16(sentinel_name, sentinel_name + strlen(sentinel_name)), |
| 988 | std::string(sentinel_name), |
| 989 | false /* is_dir */, |
| 990 | info.size, |
| 991 | info.last_modified); |
| 992 | |
| 993 | ASSERT_LT(0, d.bytes_received()); |
| 994 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 995 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 5f958140 | 2013-10-30 13:08:32 | [diff] [blame] | 996 | // Check for the entry generated for the "sentinel" file. |
| 997 | const std::string& data = d.data_received(); |
| 998 | ASSERT_NE(data.find(sentinel_output), std::string::npos); |
| 999 | } |
| 1000 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1001 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 1002 | // There is an implicit redirect when loading a file path that matches a |
| 1003 | // directory and does not end with a slash. Ensure that following such |
| 1004 | // redirects does not crash. See http://crbug.com/18686. |
| 1005 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1006 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1007 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 1008 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 1009 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 1010 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 1011 | |
| 1012 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1013 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1014 | FilePathToFileURL(path), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1015 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1016 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1017 | |
| 1018 | ASSERT_EQ(1, d.received_redirect_count()); |
| 1019 | ASSERT_LT(0, d.bytes_received()); |
| 1020 | ASSERT_FALSE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1021 | ASSERT_TRUE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | #if defined(OS_WIN) |
| 1025 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 1026 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 1027 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1028 | scoped_ptr<URLRequest> req( |
| 1029 | default_context_.CreateRequest(GURL("file:///"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1030 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1031 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1032 | |
| 1033 | ASSERT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1034 | ASSERT_FALSE(req->status().is_success()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1035 | } |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1036 | #endif // defined(OS_WIN) |
| 1037 | |
| 1038 | #endif // !defined(DISABLE_FILE_SUPPORT) |
| 1039 | |
| 1040 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1041 | TestDelegate d; |
| 1042 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1043 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1044 | GURL("invalid url"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1045 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1046 | r->Start(); |
| 1047 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1048 | |
| 1049 | base::RunLoop().Run(); |
| 1050 | EXPECT_TRUE(d.request_failed()); |
| 1051 | } |
| 1052 | } |
| 1053 | |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1054 | TEST_F(URLRequestTest, InvalidReferrerTest) { |
| 1055 | TestURLRequestContext context; |
| 1056 | TestNetworkDelegate network_delegate; |
| 1057 | network_delegate.set_cancel_request_with_policy_violating_referrer(true); |
| 1058 | context.set_network_delegate(&network_delegate); |
| 1059 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1060 | scoped_ptr<URLRequest> req( |
| 1061 | context.CreateRequest(GURL("http://localhost/"), DEFAULT_PRIORITY, &d)); |
jochen | 0e3b3a6 | 2014-09-16 18:31:23 | [diff] [blame] | 1062 | req->SetReferrer("https://somewhere.com/"); |
| 1063 | |
| 1064 | req->Start(); |
| 1065 | base::RunLoop().Run(); |
| 1066 | EXPECT_TRUE(d.request_failed()); |
| 1067 | } |
| 1068 | |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1069 | #if defined(OS_WIN) |
| 1070 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1071 | base::FilePath app_path; |
| 1072 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1073 | app_path = app_path.AppendASCII("net"); |
| 1074 | app_path = app_path.AppendASCII("data"); |
| 1075 | app_path = app_path.AppendASCII("url_request_unittest"); |
| 1076 | app_path = app_path.AppendASCII("with-headers.html"); |
| 1077 | |
| 1078 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1079 | |
| 1080 | base::win::ScopedCOMInitializer com_initializer; |
| 1081 | |
| 1082 | // Temporarily create a shortcut for test |
| 1083 | { |
| 1084 | base::win::ScopedComPtr<IShellLink> shell; |
| 1085 | ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 1086 | CLSCTX_INPROC_SERVER))); |
| 1087 | base::win::ScopedComPtr<IPersistFile> persist; |
| 1088 | ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 1089 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1090 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1091 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1092 | } |
| 1093 | |
| 1094 | TestDelegate d; |
| 1095 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1096 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1097 | FilePathToFileURL(base::FilePath(lnk_path)), DEFAULT_PRIORITY, &d)); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1098 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1099 | r->Start(); |
| 1100 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1101 | |
| 1102 | base::RunLoop().Run(); |
| 1103 | |
| 1104 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 1105 | GetFileAttributesEx(app_path.value().c_str(), |
| 1106 | GetFileExInfoStandard, &data); |
| 1107 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1108 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1109 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 1110 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 1111 | scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
| 1112 | DWORD read_size; |
| 1113 | BOOL result; |
| 1114 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1115 | &read_size, NULL); |
| 1116 | std::string content(buffer.get(), read_size); |
| 1117 | CloseHandle(file); |
| 1118 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1119 | EXPECT_TRUE(!r->is_pending()); |
[email protected] | 5ecf7cb28 | 2014-05-11 01:49:55 | [diff] [blame] | 1120 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1121 | EXPECT_EQ(content, d.data_received()); |
| 1122 | } |
| 1123 | |
| 1124 | // Clean the shortcut |
| 1125 | DeleteFile(lnk_path.c_str()); |
| 1126 | } |
| 1127 | #endif // defined(OS_WIN) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1128 | |
| 1129 | // Custom URLRequestJobs for use with interceptor tests |
| 1130 | class RestartTestJob : public URLRequestTestJob { |
| 1131 | public: |
| 1132 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1133 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1134 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1135 | void StartAsync() override { this->NotifyRestartRequired(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1136 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1137 | ~RestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1138 | }; |
| 1139 | |
| 1140 | class CancelTestJob : public URLRequestTestJob { |
| 1141 | public: |
| 1142 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 1143 | : URLRequestTestJob(request, network_delegate, true) {} |
| 1144 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1145 | void StartAsync() override { request_->Cancel(); } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1146 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1147 | ~CancelTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1148 | }; |
| 1149 | |
| 1150 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 1151 | public: |
| 1152 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 1153 | NetworkDelegate* network_delegate) |
| 1154 | : URLRequestTestJob(request, network_delegate, true) { |
| 1155 | } |
| 1156 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1157 | void StartAsync() override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1158 | request_->Cancel(); |
| 1159 | this->NotifyRestartRequired(); |
| 1160 | } |
| 1161 | private: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1162 | ~CancelThenRestartTestJob() override {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1163 | }; |
| 1164 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1165 | // An Interceptor for use with interceptor tests. |
| 1166 | class MockURLRequestInterceptor : public URLRequestInterceptor { |
| 1167 | public: |
| 1168 | // Static getters for canned response header and data strings. |
| 1169 | static std::string ok_data() { |
| 1170 | return URLRequestTestJob::test_data_1(); |
| 1171 | } |
| 1172 | |
| 1173 | static std::string ok_headers() { |
| 1174 | return URLRequestTestJob::test_headers(); |
| 1175 | } |
| 1176 | |
| 1177 | static std::string redirect_data() { |
| 1178 | return std::string(); |
| 1179 | } |
| 1180 | |
| 1181 | static std::string redirect_headers() { |
| 1182 | return URLRequestTestJob::test_redirect_headers(); |
| 1183 | } |
| 1184 | |
| 1185 | static std::string error_data() { |
| 1186 | return std::string("ohhh nooooo mr. bill!"); |
| 1187 | } |
| 1188 | |
| 1189 | static std::string error_headers() { |
| 1190 | return URLRequestTestJob::test_error_headers(); |
| 1191 | } |
| 1192 | |
| 1193 | MockURLRequestInterceptor() |
| 1194 | : intercept_main_request_(false), restart_main_request_(false), |
| 1195 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 1196 | simulate_main_network_error_(false), |
| 1197 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 1198 | intercept_final_response_(false), cancel_final_request_(false), |
| 1199 | use_url_request_http_job_(false), |
| 1200 | did_intercept_main_(false), did_restart_main_(false), |
| 1201 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 1202 | did_simulate_error_main_(false), |
| 1203 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 1204 | did_intercept_final_(false), did_cancel_final_(false) { |
| 1205 | } |
| 1206 | |
| 1207 | ~MockURLRequestInterceptor() override { |
| 1208 | } |
| 1209 | |
| 1210 | // URLRequestInterceptor implementation: |
| 1211 | URLRequestJob* MaybeInterceptRequest( |
| 1212 | URLRequest* request, |
| 1213 | NetworkDelegate* network_delegate) const override { |
| 1214 | if (restart_main_request_) { |
| 1215 | restart_main_request_ = false; |
| 1216 | did_restart_main_ = true; |
| 1217 | return new RestartTestJob(request, network_delegate); |
| 1218 | } |
| 1219 | if (cancel_main_request_) { |
| 1220 | cancel_main_request_ = false; |
| 1221 | did_cancel_main_ = true; |
| 1222 | return new CancelTestJob(request, network_delegate); |
| 1223 | } |
| 1224 | if (cancel_then_restart_main_request_) { |
| 1225 | cancel_then_restart_main_request_ = false; |
| 1226 | did_cancel_then_restart_main_ = true; |
| 1227 | return new CancelThenRestartTestJob(request, network_delegate); |
| 1228 | } |
| 1229 | if (simulate_main_network_error_) { |
| 1230 | simulate_main_network_error_ = false; |
| 1231 | did_simulate_error_main_ = true; |
| 1232 | if (use_url_request_http_job_) { |
| 1233 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1234 | } |
| 1235 | // This job will result in error since the requested URL is not one of the |
| 1236 | // URLs supported by these tests. |
| 1237 | return new URLRequestTestJob(request, network_delegate, true); |
| 1238 | } |
| 1239 | if (!intercept_main_request_) |
| 1240 | return nullptr; |
| 1241 | intercept_main_request_ = false; |
| 1242 | did_intercept_main_ = true; |
| 1243 | URLRequestTestJob* job = new URLRequestTestJob(request, |
| 1244 | network_delegate, |
| 1245 | main_headers_, |
| 1246 | main_data_, |
| 1247 | true); |
| 1248 | job->set_load_timing_info(main_request_load_timing_info_); |
| 1249 | return job; |
| 1250 | } |
| 1251 | |
| 1252 | URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
| 1253 | NetworkDelegate* network_delegate, |
| 1254 | const GURL& location) const override { |
| 1255 | if (cancel_redirect_request_) { |
| 1256 | cancel_redirect_request_ = false; |
| 1257 | did_cancel_redirect_ = true; |
| 1258 | return new CancelTestJob(request, network_delegate); |
| 1259 | } |
| 1260 | if (!intercept_redirect_) |
| 1261 | return nullptr; |
| 1262 | intercept_redirect_ = false; |
| 1263 | did_intercept_redirect_ = true; |
| 1264 | if (use_url_request_http_job_) { |
| 1265 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1266 | } |
| 1267 | return new URLRequestTestJob(request, |
| 1268 | network_delegate, |
| 1269 | redirect_headers_, |
| 1270 | redirect_data_, |
| 1271 | true); |
| 1272 | } |
| 1273 | |
| 1274 | URLRequestJob* MaybeInterceptResponse( |
| 1275 | URLRequest* request, |
| 1276 | NetworkDelegate* network_delegate) const override { |
| 1277 | if (cancel_final_request_) { |
| 1278 | cancel_final_request_ = false; |
| 1279 | did_cancel_final_ = true; |
| 1280 | return new CancelTestJob(request, network_delegate); |
| 1281 | } |
| 1282 | if (!intercept_final_response_) |
| 1283 | return nullptr; |
| 1284 | intercept_final_response_ = false; |
| 1285 | did_intercept_final_ = true; |
| 1286 | if (use_url_request_http_job_) { |
| 1287 | return URLRequestHttpJob::Factory(request, network_delegate, "http"); |
| 1288 | } |
| 1289 | return new URLRequestTestJob(request, |
| 1290 | network_delegate, |
| 1291 | final_headers_, |
| 1292 | final_data_, |
| 1293 | true); |
| 1294 | } |
| 1295 | |
| 1296 | void set_intercept_main_request(bool intercept_main_request) { |
| 1297 | intercept_main_request_ = intercept_main_request; |
| 1298 | } |
| 1299 | |
| 1300 | void set_main_headers(const std::string& main_headers) { |
| 1301 | main_headers_ = main_headers; |
| 1302 | } |
| 1303 | |
| 1304 | void set_main_data(const std::string& main_data) { |
| 1305 | main_data_ = main_data; |
| 1306 | } |
| 1307 | |
| 1308 | void set_main_request_load_timing_info( |
| 1309 | const LoadTimingInfo& main_request_load_timing_info) { |
| 1310 | main_request_load_timing_info_ = main_request_load_timing_info; |
| 1311 | } |
| 1312 | |
| 1313 | void set_restart_main_request(bool restart_main_request) { |
| 1314 | restart_main_request_ = restart_main_request; |
| 1315 | } |
| 1316 | |
| 1317 | void set_cancel_main_request(bool cancel_main_request) { |
| 1318 | cancel_main_request_ = cancel_main_request; |
| 1319 | } |
| 1320 | |
| 1321 | void set_cancel_then_restart_main_request( |
| 1322 | bool cancel_then_restart_main_request) { |
| 1323 | cancel_then_restart_main_request_ = cancel_then_restart_main_request; |
| 1324 | } |
| 1325 | |
| 1326 | void set_simulate_main_network_error(bool simulate_main_network_error) { |
| 1327 | simulate_main_network_error_ = simulate_main_network_error; |
| 1328 | } |
| 1329 | |
| 1330 | void set_intercept_redirect(bool intercept_redirect) { |
| 1331 | intercept_redirect_ = intercept_redirect; |
| 1332 | } |
| 1333 | |
| 1334 | void set_redirect_headers(const std::string& redirect_headers) { |
| 1335 | redirect_headers_ = redirect_headers; |
| 1336 | } |
| 1337 | |
| 1338 | void set_redirect_data(const std::string& redirect_data) { |
| 1339 | redirect_data_ = redirect_data; |
| 1340 | } |
| 1341 | |
| 1342 | void set_cancel_redirect_request(bool cancel_redirect_request) { |
| 1343 | cancel_redirect_request_ = cancel_redirect_request; |
| 1344 | } |
| 1345 | |
| 1346 | void set_intercept_final_response(bool intercept_final_response) { |
| 1347 | intercept_final_response_ = intercept_final_response; |
| 1348 | } |
| 1349 | |
| 1350 | void set_final_headers(const std::string& final_headers) { |
| 1351 | final_headers_ = final_headers; |
| 1352 | } |
| 1353 | |
| 1354 | void set_final_data(const std::string& final_data) { |
| 1355 | final_data_ = final_data; |
| 1356 | } |
| 1357 | |
| 1358 | void set_cancel_final_request(bool cancel_final_request) { |
| 1359 | cancel_final_request_ = cancel_final_request; |
| 1360 | } |
| 1361 | |
| 1362 | void set_use_url_request_http_job(bool use_url_request_http_job) { |
| 1363 | use_url_request_http_job_ = use_url_request_http_job; |
| 1364 | } |
| 1365 | |
| 1366 | bool did_intercept_main() const { |
| 1367 | return did_intercept_main_; |
| 1368 | } |
| 1369 | |
| 1370 | bool did_restart_main() const { |
| 1371 | return did_restart_main_; |
| 1372 | } |
| 1373 | |
| 1374 | bool did_cancel_main() const { |
| 1375 | return did_cancel_main_; |
| 1376 | } |
| 1377 | |
| 1378 | bool did_cancel_then_restart_main() const { |
| 1379 | return did_cancel_then_restart_main_; |
| 1380 | } |
| 1381 | |
| 1382 | bool did_simulate_error_main() const { |
| 1383 | return did_simulate_error_main_; |
| 1384 | } |
| 1385 | |
| 1386 | bool did_intercept_redirect() const { |
| 1387 | return did_intercept_redirect_; |
| 1388 | } |
| 1389 | |
| 1390 | bool did_cancel_redirect() const { |
| 1391 | return did_cancel_redirect_; |
| 1392 | } |
| 1393 | |
| 1394 | bool did_intercept_final() const { |
| 1395 | return did_intercept_final_; |
| 1396 | } |
| 1397 | |
| 1398 | bool did_cancel_final() const { |
| 1399 | return did_cancel_final_; |
| 1400 | } |
| 1401 | |
| 1402 | private: |
| 1403 | // Indicate whether to intercept the main request, and if so specify the |
| 1404 | // response to return and the LoadTimingInfo to use. |
| 1405 | mutable bool intercept_main_request_; |
| 1406 | mutable std::string main_headers_; |
| 1407 | mutable std::string main_data_; |
| 1408 | mutable LoadTimingInfo main_request_load_timing_info_; |
| 1409 | |
| 1410 | // These indicate actions that can be taken within MaybeInterceptRequest. |
| 1411 | mutable bool restart_main_request_; |
| 1412 | mutable bool cancel_main_request_; |
| 1413 | mutable bool cancel_then_restart_main_request_; |
| 1414 | mutable bool simulate_main_network_error_; |
| 1415 | |
| 1416 | // Indicate whether to intercept redirects, and if so specify the response to |
| 1417 | // return. |
| 1418 | mutable bool intercept_redirect_; |
| 1419 | mutable std::string redirect_headers_; |
| 1420 | mutable std::string redirect_data_; |
| 1421 | |
| 1422 | // Cancel the request within MaybeInterceptRedirect. |
| 1423 | mutable bool cancel_redirect_request_; |
| 1424 | |
| 1425 | // Indicate whether to intercept the final response, and if so specify the |
| 1426 | // response to return. |
| 1427 | mutable bool intercept_final_response_; |
| 1428 | mutable std::string final_headers_; |
| 1429 | mutable std::string final_data_; |
| 1430 | |
| 1431 | // Cancel the final request within MaybeInterceptResponse. |
| 1432 | mutable bool cancel_final_request_; |
| 1433 | |
| 1434 | // Instruct the interceptor to use a real URLRequestHTTPJob. |
| 1435 | mutable bool use_url_request_http_job_; |
| 1436 | |
| 1437 | // These indicate if the interceptor did something or not. |
| 1438 | mutable bool did_intercept_main_; |
| 1439 | mutable bool did_restart_main_; |
| 1440 | mutable bool did_cancel_main_; |
| 1441 | mutable bool did_cancel_then_restart_main_; |
| 1442 | mutable bool did_simulate_error_main_; |
| 1443 | mutable bool did_intercept_redirect_; |
| 1444 | mutable bool did_cancel_redirect_; |
| 1445 | mutable bool did_intercept_final_; |
| 1446 | mutable bool did_cancel_final_; |
| 1447 | }; |
| 1448 | |
| 1449 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
| 1450 | class URLRequestInterceptorTest : public URLRequestTest { |
| 1451 | public: |
| 1452 | URLRequestInterceptorTest() : URLRequestTest(), interceptor_(NULL) { |
| 1453 | } |
| 1454 | |
| 1455 | ~URLRequestInterceptorTest() override { |
| 1456 | // URLRequestJobs may post clean-up tasks on destruction. |
| 1457 | base::RunLoop().RunUntilIdle(); |
| 1458 | } |
| 1459 | |
| 1460 | void SetUpFactory() override { |
| 1461 | interceptor_ = new MockURLRequestInterceptor(); |
| 1462 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
| 1463 | job_factory_.Pass(), make_scoped_ptr(interceptor_))); |
| 1464 | } |
| 1465 | |
| 1466 | MockURLRequestInterceptor* interceptor() const { |
| 1467 | return interceptor_; |
| 1468 | } |
| 1469 | |
| 1470 | private: |
| 1471 | MockURLRequestInterceptor* interceptor_; |
| 1472 | }; |
| 1473 | |
| 1474 | TEST_F(URLRequestInterceptorTest, Intercept) { |
| 1475 | // Intercept the main request and respond with a simple response. |
| 1476 | interceptor()->set_intercept_main_request(true); |
| 1477 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1478 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1479 | TestDelegate d; |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1480 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1481 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1482 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1483 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1484 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
| 1485 | req->SetUserData(nullptr, user_data0); |
| 1486 | req->SetUserData(&user_data1, user_data1); |
| 1487 | req->SetUserData(&user_data2, user_data2); |
| 1488 | req->set_method("GET"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 1489 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 1490 | base::RunLoop().Run(); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1491 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1492 | // Make sure we can retrieve our specific user data. |
| 1493 | EXPECT_EQ(user_data0, req->GetUserData(nullptr)); |
| 1494 | EXPECT_EQ(user_data1, req->GetUserData(&user_data1)); |
| 1495 | EXPECT_EQ(user_data2, req->GetUserData(&user_data2)); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1496 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1497 | // Check that we got one good response. |
| 1498 | EXPECT_TRUE(req->status().is_success()); |
| 1499 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1500 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1501 | EXPECT_EQ(1, d.response_started_count()); |
| 1502 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1503 | } |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1504 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1505 | TEST_F(URLRequestInterceptorTest, InterceptRedirect) { |
| 1506 | // Intercept the main request and respond with a redirect. |
| 1507 | interceptor()->set_intercept_main_request(true); |
| 1508 | interceptor()->set_main_headers( |
| 1509 | MockURLRequestInterceptor::redirect_headers()); |
| 1510 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1511 | |
| 1512 | // Intercept that redirect and respond with a final OK response. |
| 1513 | interceptor()->set_intercept_redirect(true); |
| 1514 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 1515 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 1516 | |
| 1517 | TestDelegate d; |
| 1518 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1519 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1520 | req->set_method("GET"); |
| 1521 | req->Start(); |
| 1522 | base::RunLoop().Run(); |
| 1523 | |
| 1524 | // Check that the interceptor got called as expected. |
| 1525 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1526 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 1527 | |
| 1528 | // Check that we got one good response. |
| 1529 | EXPECT_TRUE(req->status().is_success()); |
| 1530 | if (req->status().is_success()) |
| 1531 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1532 | |
| 1533 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1534 | EXPECT_EQ(1, d.response_started_count()); |
| 1535 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1536 | } |
| 1537 | |
| 1538 | TEST_F(URLRequestInterceptorTest, InterceptServerError) { |
| 1539 | // Intercept the main request to generate a server error response. |
| 1540 | interceptor()->set_intercept_main_request(true); |
| 1541 | interceptor()->set_main_headers(MockURLRequestInterceptor::error_headers()); |
| 1542 | interceptor()->set_main_data(MockURLRequestInterceptor::error_data()); |
| 1543 | |
| 1544 | // Intercept that error and respond with an OK response. |
| 1545 | interceptor()->set_intercept_final_response(true); |
| 1546 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1547 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1548 | |
| 1549 | TestDelegate d; |
| 1550 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1551 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1552 | req->set_method("GET"); |
| 1553 | req->Start(); |
| 1554 | base::RunLoop().Run(); |
| 1555 | |
| 1556 | // Check that the interceptor got called as expected. |
| 1557 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1558 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1559 | |
| 1560 | // Check that we got one good response. |
| 1561 | EXPECT_TRUE(req->status().is_success()); |
| 1562 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1563 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1564 | EXPECT_EQ(1, d.response_started_count()); |
| 1565 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1566 | } |
| 1567 | |
| 1568 | TEST_F(URLRequestInterceptorTest, InterceptNetworkError) { |
| 1569 | // Intercept the main request to simulate a network error. |
| 1570 | interceptor()->set_simulate_main_network_error(true); |
| 1571 | |
| 1572 | // Intercept that error and respond with an OK response. |
| 1573 | interceptor()->set_intercept_final_response(true); |
| 1574 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1575 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1576 | |
| 1577 | TestDelegate d; |
| 1578 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1579 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1580 | req->set_method("GET"); |
| 1581 | req->Start(); |
| 1582 | base::RunLoop().Run(); |
| 1583 | |
| 1584 | // Check that the interceptor got called as expected. |
| 1585 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1586 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 1587 | |
| 1588 | // Check that we received one good response. |
| 1589 | EXPECT_TRUE(req->status().is_success()); |
| 1590 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1591 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1592 | EXPECT_EQ(1, d.response_started_count()); |
| 1593 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1594 | } |
| 1595 | |
| 1596 | TEST_F(URLRequestInterceptorTest, InterceptRestartRequired) { |
| 1597 | // Restart the main request. |
| 1598 | interceptor()->set_restart_main_request(true); |
| 1599 | |
| 1600 | // then intercept the new main request and respond with an OK response |
| 1601 | interceptor()->set_intercept_main_request(true); |
| 1602 | interceptor()->set_main_headers(MockURLRequestInterceptor::ok_headers()); |
| 1603 | interceptor()->set_main_data(MockURLRequestInterceptor::ok_data()); |
| 1604 | |
| 1605 | TestDelegate d; |
| 1606 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1607 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1608 | req->set_method("GET"); |
| 1609 | req->Start(); |
| 1610 | base::RunLoop().Run(); |
| 1611 | |
| 1612 | // Check that the interceptor got called as expected. |
| 1613 | EXPECT_TRUE(interceptor()->did_restart_main()); |
| 1614 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1615 | |
| 1616 | // Check that we received one good response. |
| 1617 | EXPECT_TRUE(req->status().is_success()); |
| 1618 | if (req->status().is_success()) |
| 1619 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 1620 | |
| 1621 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 1622 | EXPECT_EQ(1, d.response_started_count()); |
| 1623 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1624 | } |
| 1625 | |
| 1626 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelMain) { |
| 1627 | // Intercept the main request and cancel from within the restarted job. |
| 1628 | interceptor()->set_cancel_main_request(true); |
| 1629 | |
| 1630 | // Set up to intercept the final response and override it with an OK response. |
| 1631 | interceptor()->set_intercept_final_response(true); |
| 1632 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1633 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1634 | |
| 1635 | TestDelegate d; |
| 1636 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1637 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1638 | req->set_method("GET"); |
| 1639 | req->Start(); |
| 1640 | base::RunLoop().Run(); |
| 1641 | |
| 1642 | // Check that the interceptor got called as expected. |
| 1643 | EXPECT_TRUE(interceptor()->did_cancel_main()); |
| 1644 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1645 | |
| 1646 | // Check that we see a canceled request. |
| 1647 | EXPECT_FALSE(req->status().is_success()); |
| 1648 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1649 | } |
| 1650 | |
| 1651 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelRedirect) { |
| 1652 | // Intercept the main request and respond with a redirect. |
| 1653 | interceptor()->set_intercept_main_request(true); |
| 1654 | interceptor()->set_main_headers( |
| 1655 | MockURLRequestInterceptor::redirect_headers()); |
| 1656 | interceptor()->set_main_data(MockURLRequestInterceptor::redirect_data()); |
| 1657 | |
| 1658 | // Intercept the redirect and cancel from within that job. |
| 1659 | interceptor()->set_cancel_redirect_request(true); |
| 1660 | |
| 1661 | // Set up to intercept the final response and override it with an OK response. |
| 1662 | interceptor()->set_intercept_final_response(true); |
| 1663 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1664 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 1665 | |
| 1666 | TestDelegate d; |
| 1667 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1668 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1669 | req->set_method("GET"); |
| 1670 | req->Start(); |
| 1671 | base::RunLoop().Run(); |
| 1672 | |
| 1673 | // Check that the interceptor got called as expected. |
| 1674 | EXPECT_TRUE(interceptor()->did_intercept_main()); |
| 1675 | EXPECT_TRUE(interceptor()->did_cancel_redirect()); |
| 1676 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1677 | |
| 1678 | // Check that we see a canceled request. |
| 1679 | EXPECT_FALSE(req->status().is_success()); |
| 1680 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1681 | } |
| 1682 | |
| 1683 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelFinal) { |
| 1684 | // Intercept the main request to simulate a network error. |
| 1685 | interceptor()->set_simulate_main_network_error(true); |
| 1686 | |
| 1687 | // Set up to intercept final the response and cancel from within that job. |
| 1688 | interceptor()->set_cancel_final_request(true); |
| 1689 | |
| 1690 | TestDelegate d; |
| 1691 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1692 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1693 | req->set_method("GET"); |
| 1694 | req->Start(); |
| 1695 | base::RunLoop().Run(); |
| 1696 | |
| 1697 | // Check that the interceptor got called as expected. |
| 1698 | EXPECT_TRUE(interceptor()->did_simulate_error_main()); |
| 1699 | EXPECT_TRUE(interceptor()->did_cancel_final()); |
| 1700 | |
| 1701 | // Check that we see a canceled request. |
| 1702 | EXPECT_FALSE(req->status().is_success()); |
| 1703 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
| 1704 | } |
| 1705 | |
| 1706 | TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { |
| 1707 | // Intercept the main request and cancel then restart from within that job. |
| 1708 | interceptor()->set_cancel_then_restart_main_request(true); |
| 1709 | |
| 1710 | // Set up to intercept the final response and override it with an OK response. |
| 1711 | interceptor()->set_intercept_final_response(true); |
bengr | b50d631e | 2014-11-17 22:50:50 | [diff] [blame] | 1712 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 1713 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1714 | |
| 1715 | TestDelegate d; |
| 1716 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1717 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1718 | req->set_method("GET"); |
| 1719 | req->Start(); |
| 1720 | base::RunLoop().Run(); |
| 1721 | |
| 1722 | // Check that the interceptor got called as expected. |
| 1723 | EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); |
| 1724 | EXPECT_FALSE(interceptor()->did_intercept_final()); |
| 1725 | |
| 1726 | // Check that we see a canceled request. |
| 1727 | EXPECT_FALSE(req->status().is_success()); |
| 1728 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | // "Normal" LoadTimingInfo as returned by a job. Everything is in order, not |
| 1732 | // reused. |connect_time_flags| is used to indicate if there should be dns |
| 1733 | // or SSL times, and |used_proxy| is used for proxy times. |
| 1734 | LoadTimingInfo NormalLoadTimingInfo(base::TimeTicks now, |
| 1735 | int connect_time_flags, |
| 1736 | bool used_proxy) { |
| 1737 | LoadTimingInfo load_timing; |
| 1738 | load_timing.socket_log_id = 1; |
| 1739 | |
| 1740 | if (used_proxy) { |
| 1741 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1742 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1743 | } |
| 1744 | |
| 1745 | LoadTimingInfo::ConnectTiming& connect_timing = load_timing.connect_timing; |
| 1746 | if (connect_time_flags & CONNECT_TIMING_HAS_DNS_TIMES) { |
| 1747 | connect_timing.dns_start = now + base::TimeDelta::FromDays(3); |
| 1748 | connect_timing.dns_end = now + base::TimeDelta::FromDays(4); |
| 1749 | } |
| 1750 | connect_timing.connect_start = now + base::TimeDelta::FromDays(5); |
| 1751 | if (connect_time_flags & CONNECT_TIMING_HAS_SSL_TIMES) { |
| 1752 | connect_timing.ssl_start = now + base::TimeDelta::FromDays(6); |
| 1753 | connect_timing.ssl_end = now + base::TimeDelta::FromDays(7); |
| 1754 | } |
| 1755 | connect_timing.connect_end = now + base::TimeDelta::FromDays(8); |
| 1756 | |
| 1757 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1758 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1759 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1760 | return load_timing; |
| 1761 | } |
| 1762 | |
| 1763 | // Same as above, but in the case of a reused socket. |
| 1764 | LoadTimingInfo NormalLoadTimingInfoReused(base::TimeTicks now, |
| 1765 | bool used_proxy) { |
| 1766 | LoadTimingInfo load_timing; |
| 1767 | load_timing.socket_log_id = 1; |
| 1768 | load_timing.socket_reused = true; |
| 1769 | |
| 1770 | if (used_proxy) { |
| 1771 | load_timing.proxy_resolve_start = now + base::TimeDelta::FromDays(1); |
| 1772 | load_timing.proxy_resolve_end = now + base::TimeDelta::FromDays(2); |
| 1773 | } |
| 1774 | |
| 1775 | load_timing.send_start = now + base::TimeDelta::FromDays(9); |
| 1776 | load_timing.send_end = now + base::TimeDelta::FromDays(10); |
| 1777 | load_timing.receive_headers_end = now + base::TimeDelta::FromDays(11); |
| 1778 | return load_timing; |
| 1779 | } |
| 1780 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1781 | LoadTimingInfo RunURLRequestInterceptorLoadTimingTest( |
| 1782 | const LoadTimingInfo& job_load_timing, |
| 1783 | const URLRequestContext& context, |
| 1784 | MockURLRequestInterceptor* interceptor) { |
| 1785 | interceptor->set_intercept_main_request(true); |
| 1786 | interceptor->set_main_request_load_timing_info(job_load_timing); |
| 1787 | TestDelegate d; |
| 1788 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 1789 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1790 | req->Start(); |
| 1791 | base::RunLoop().Run(); |
| 1792 | |
| 1793 | LoadTimingInfo resulting_load_timing; |
| 1794 | req->GetLoadTimingInfo(&resulting_load_timing); |
| 1795 | |
| 1796 | // None of these should be modified by the URLRequest. |
| 1797 | EXPECT_EQ(job_load_timing.socket_reused, resulting_load_timing.socket_reused); |
| 1798 | EXPECT_EQ(job_load_timing.socket_log_id, resulting_load_timing.socket_log_id); |
| 1799 | EXPECT_EQ(job_load_timing.send_start, resulting_load_timing.send_start); |
| 1800 | EXPECT_EQ(job_load_timing.send_end, resulting_load_timing.send_end); |
| 1801 | EXPECT_EQ(job_load_timing.receive_headers_end, |
| 1802 | resulting_load_timing.receive_headers_end); |
| 1803 | |
| 1804 | return resulting_load_timing; |
| 1805 | } |
| 1806 | |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1807 | // Basic test that the intercept + load timing tests work. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1808 | TEST_F(URLRequestInterceptorTest, InterceptLoadTiming) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1809 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1810 | LoadTimingInfo job_load_timing = |
| 1811 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, false); |
| 1812 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1813 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1814 | RunURLRequestInterceptorLoadTimingTest( |
| 1815 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1816 | |
| 1817 | // Nothing should have been changed by the URLRequest. |
| 1818 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1819 | load_timing_result.proxy_resolve_start); |
| 1820 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1821 | load_timing_result.proxy_resolve_end); |
| 1822 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1823 | load_timing_result.connect_timing.dns_start); |
| 1824 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1825 | load_timing_result.connect_timing.dns_end); |
| 1826 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1827 | load_timing_result.connect_timing.connect_start); |
| 1828 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1829 | load_timing_result.connect_timing.connect_end); |
| 1830 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1831 | load_timing_result.connect_timing.ssl_start); |
| 1832 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1833 | load_timing_result.connect_timing.ssl_end); |
| 1834 | |
| 1835 | // Redundant sanity check. |
| 1836 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_DNS_TIMES); |
| 1837 | } |
| 1838 | |
| 1839 | // Another basic test, with proxy and SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1840 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1841 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1842 | LoadTimingInfo job_load_timing = |
| 1843 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, true); |
| 1844 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1845 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1846 | RunURLRequestInterceptorLoadTimingTest( |
| 1847 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1848 | |
| 1849 | // Nothing should have been changed by the URLRequest. |
| 1850 | EXPECT_EQ(job_load_timing.proxy_resolve_start, |
| 1851 | load_timing_result.proxy_resolve_start); |
| 1852 | EXPECT_EQ(job_load_timing.proxy_resolve_end, |
| 1853 | load_timing_result.proxy_resolve_end); |
| 1854 | EXPECT_EQ(job_load_timing.connect_timing.dns_start, |
| 1855 | load_timing_result.connect_timing.dns_start); |
| 1856 | EXPECT_EQ(job_load_timing.connect_timing.dns_end, |
| 1857 | load_timing_result.connect_timing.dns_end); |
| 1858 | EXPECT_EQ(job_load_timing.connect_timing.connect_start, |
| 1859 | load_timing_result.connect_timing.connect_start); |
| 1860 | EXPECT_EQ(job_load_timing.connect_timing.connect_end, |
| 1861 | load_timing_result.connect_timing.connect_end); |
| 1862 | EXPECT_EQ(job_load_timing.connect_timing.ssl_start, |
| 1863 | load_timing_result.connect_timing.ssl_start); |
| 1864 | EXPECT_EQ(job_load_timing.connect_timing.ssl_end, |
| 1865 | load_timing_result.connect_timing.ssl_end); |
| 1866 | |
| 1867 | // Redundant sanity check. |
| 1868 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1869 | CONNECT_TIMING_HAS_SSL_TIMES); |
| 1870 | } |
| 1871 | |
| 1872 | // Make sure that URLRequest correctly adjusts proxy times when they're before |
| 1873 | // |request_start|, due to already having a connected socket. This happens in |
[email protected] | cf4cae3 | 2014-05-27 00:39:10 | [diff] [blame] | 1874 | // the case of reusing a SPDY session. The connected socket is not considered |
| 1875 | // reused in this test (May be a preconnect). |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1876 | // |
| 1877 | // To mix things up from the test above, assumes DNS times but no SSL times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1878 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyProxyResolution) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1879 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1880 | LoadTimingInfo job_load_timing = |
| 1881 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_DNS_TIMES, true); |
| 1882 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(6); |
| 1883 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(5); |
| 1884 | job_load_timing.connect_timing.dns_start = now - base::TimeDelta::FromDays(4); |
| 1885 | job_load_timing.connect_timing.dns_end = now - base::TimeDelta::FromDays(3); |
| 1886 | job_load_timing.connect_timing.connect_start = |
| 1887 | now - base::TimeDelta::FromDays(2); |
| 1888 | job_load_timing.connect_timing.connect_end = |
| 1889 | now - base::TimeDelta::FromDays(1); |
| 1890 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1891 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1892 | RunURLRequestInterceptorLoadTimingTest( |
| 1893 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1894 | |
| 1895 | // Proxy times, connect times, and DNS times should all be replaced with |
| 1896 | // request_start. |
| 1897 | EXPECT_EQ(load_timing_result.request_start, |
| 1898 | load_timing_result.proxy_resolve_start); |
| 1899 | EXPECT_EQ(load_timing_result.request_start, |
| 1900 | load_timing_result.proxy_resolve_end); |
| 1901 | EXPECT_EQ(load_timing_result.request_start, |
| 1902 | load_timing_result.connect_timing.dns_start); |
| 1903 | EXPECT_EQ(load_timing_result.request_start, |
| 1904 | load_timing_result.connect_timing.dns_end); |
| 1905 | EXPECT_EQ(load_timing_result.request_start, |
| 1906 | load_timing_result.connect_timing.connect_start); |
| 1907 | EXPECT_EQ(load_timing_result.request_start, |
| 1908 | load_timing_result.connect_timing.connect_end); |
| 1909 | |
| 1910 | // Other times should have been left null. |
| 1911 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1912 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 1913 | } |
| 1914 | |
| 1915 | // Same as above, but in the reused case. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1916 | TEST_F(URLRequestInterceptorTest, |
| 1917 | InterceptLoadTimingEarlyProxyResolutionReused) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1918 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1919 | LoadTimingInfo job_load_timing = NormalLoadTimingInfoReused(now, true); |
| 1920 | job_load_timing.proxy_resolve_start = now - base::TimeDelta::FromDays(4); |
| 1921 | job_load_timing.proxy_resolve_end = now - base::TimeDelta::FromDays(3); |
| 1922 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1923 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1924 | RunURLRequestInterceptorLoadTimingTest( |
| 1925 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1926 | |
| 1927 | // Proxy times and connect times should all be replaced with request_start. |
| 1928 | EXPECT_EQ(load_timing_result.request_start, |
| 1929 | load_timing_result.proxy_resolve_start); |
| 1930 | EXPECT_EQ(load_timing_result.request_start, |
| 1931 | load_timing_result.proxy_resolve_end); |
| 1932 | |
| 1933 | // Other times should have been left null. |
| 1934 | TestLoadTimingReusedWithProxy(load_timing_result); |
| 1935 | } |
| 1936 | |
| 1937 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1938 | // |request_start|, due to reusing a connected socket. The connected socket is |
| 1939 | // not considered reused in this test (May be a preconnect). |
| 1940 | // |
| 1941 | // To mix things up, the request has SSL times, but no DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1942 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnect) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1943 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1944 | LoadTimingInfo job_load_timing = |
| 1945 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_SSL_TIMES, false); |
| 1946 | job_load_timing.connect_timing.connect_start = |
| 1947 | now - base::TimeDelta::FromDays(1); |
| 1948 | job_load_timing.connect_timing.ssl_start = now - base::TimeDelta::FromDays(2); |
| 1949 | job_load_timing.connect_timing.ssl_end = now - base::TimeDelta::FromDays(3); |
| 1950 | job_load_timing.connect_timing.connect_end = |
| 1951 | now - base::TimeDelta::FromDays(4); |
| 1952 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1953 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1954 | RunURLRequestInterceptorLoadTimingTest( |
| 1955 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1956 | |
| 1957 | // Connect times, and SSL times should be replaced with request_start. |
| 1958 | EXPECT_EQ(load_timing_result.request_start, |
| 1959 | load_timing_result.connect_timing.connect_start); |
| 1960 | EXPECT_EQ(load_timing_result.request_start, |
| 1961 | load_timing_result.connect_timing.ssl_start); |
| 1962 | EXPECT_EQ(load_timing_result.request_start, |
| 1963 | load_timing_result.connect_timing.ssl_end); |
| 1964 | EXPECT_EQ(load_timing_result.request_start, |
| 1965 | load_timing_result.connect_timing.connect_end); |
| 1966 | |
| 1967 | // Other times should have been left null. |
| 1968 | TestLoadTimingNotReused(load_timing_result, CONNECT_TIMING_HAS_SSL_TIMES); |
| 1969 | } |
| 1970 | |
| 1971 | // Make sure that URLRequest correctly adjusts connect times when they're before |
| 1972 | // |request_start|, due to reusing a connected socket in the case that there |
| 1973 | // are also proxy times. The connected socket is not considered reused in this |
| 1974 | // test (May be a preconnect). |
| 1975 | // |
| 1976 | // In this test, there are no SSL or DNS times. |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1977 | TEST_F(URLRequestInterceptorTest, InterceptLoadTimingEarlyConnectWithProxy) { |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1978 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1979 | LoadTimingInfo job_load_timing = |
| 1980 | NormalLoadTimingInfo(now, CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY, true); |
| 1981 | job_load_timing.connect_timing.connect_start = |
| 1982 | now - base::TimeDelta::FromDays(1); |
| 1983 | job_load_timing.connect_timing.connect_end = |
| 1984 | now - base::TimeDelta::FromDays(2); |
| 1985 | |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 1986 | LoadTimingInfo load_timing_result = |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 1987 | RunURLRequestInterceptorLoadTimingTest( |
| 1988 | job_load_timing, default_context(), interceptor()); |
[email protected] | 2bba325 | 2013-04-08 19:50:59 | [diff] [blame] | 1989 | |
| 1990 | // Connect times should be replaced with proxy_resolve_end. |
| 1991 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1992 | load_timing_result.connect_timing.connect_start); |
| 1993 | EXPECT_EQ(load_timing_result.proxy_resolve_end, |
| 1994 | load_timing_result.connect_timing.connect_end); |
| 1995 | |
| 1996 | // Other times should have been left null. |
| 1997 | TestLoadTimingNotReusedWithProxy(load_timing_result, |
| 1998 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 1999 | } |
| 2000 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2001 | // Check that two different URL requests have different identifiers. |
| 2002 | TEST_F(URLRequestTest, Identifiers) { |
| 2003 | TestDelegate d; |
| 2004 | TestURLRequestContext context; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2005 | scoped_ptr<URLRequest> req( |
| 2006 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
| 2007 | scoped_ptr<URLRequest> other_req( |
| 2008 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2009 | |
mmenke | 19378d2 | 2014-09-09 04:12:59 | [diff] [blame] | 2010 | ASSERT_NE(req->identifier(), other_req->identifier()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | // Check that a failure to connect to the proxy is reported to the network |
| 2014 | // delegate. |
| 2015 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
| 2016 | MockHostResolver host_resolver; |
| 2017 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 2018 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2019 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2020 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 2021 | |
| 2022 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2023 | scoped_ptr<URLRequest> req( |
| 2024 | context.CreateRequest(GURL("http://example.com"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2025 | req->set_method("GET"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2026 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2027 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2028 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2029 | |
| 2030 | // Check we see a failed request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2031 | EXPECT_FALSE(req->status().is_success()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 2032 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2033 | EXPECT_TRUE(req->proxy_server().IsEmpty()); |
| 2034 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 2035 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2036 | |
| 2037 | EXPECT_EQ(1, network_delegate.error_count()); |
| 2038 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
| 2039 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2040 | } |
| 2041 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2042 | // Make sure that NetworkDelegate::NotifyCompleted is called if |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2043 | // content is empty. |
| 2044 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 2045 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2046 | scoped_ptr<URLRequest> req( |
| 2047 | default_context_.CreateRequest(GURL("data:,"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2048 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2049 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2050 | EXPECT_EQ("", d.data_received()); |
| 2051 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 2052 | } |
| 2053 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2054 | // Make sure that SetPriority actually sets the URLRequest's priority |
| 2055 | // correctly, both before and after start. |
| 2056 | TEST_F(URLRequestTest, SetPriorityBasic) { |
| 2057 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2058 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2059 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2060 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2061 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2062 | req->SetPriority(LOW); |
| 2063 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2064 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2065 | req->Start(); |
| 2066 | EXPECT_EQ(LOW, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2067 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2068 | req->SetPriority(MEDIUM); |
| 2069 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | // Make sure that URLRequest calls SetPriority on a job before calling |
| 2073 | // Start on it. |
| 2074 | TEST_F(URLRequestTest, SetJobPriorityBeforeJobStart) { |
| 2075 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2076 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2077 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2078 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2079 | |
| 2080 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2081 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2082 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2083 | EXPECT_EQ(DEFAULT_PRIORITY, job->priority()); |
| 2084 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2085 | req->SetPriority(LOW); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2086 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2087 | req->Start(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2088 | EXPECT_EQ(LOW, job->priority()); |
| 2089 | } |
| 2090 | |
| 2091 | // Make sure that URLRequest passes on its priority updates to its |
| 2092 | // job. |
| 2093 | TEST_F(URLRequestTest, SetJobPriority) { |
| 2094 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2095 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2096 | GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2097 | |
| 2098 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2099 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2100 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2101 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2102 | req->SetPriority(LOW); |
| 2103 | req->Start(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2104 | EXPECT_EQ(LOW, job->priority()); |
| 2105 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2106 | req->SetPriority(MEDIUM); |
| 2107 | EXPECT_EQ(MEDIUM, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 2108 | EXPECT_EQ(MEDIUM, job->priority()); |
| 2109 | } |
| 2110 | |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2111 | // Setting the IGNORE_LIMITS load flag should be okay if the priority |
| 2112 | // is MAXIMUM_PRIORITY. |
| 2113 | TEST_F(URLRequestTest, PriorityIgnoreLimits) { |
| 2114 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2115 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2116 | GURL("http://test_intercept/foo"), MAXIMUM_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2117 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2118 | |
| 2119 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2120 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2121 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
| 2122 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2123 | req->SetLoadFlags(LOAD_IGNORE_LIMITS); |
| 2124 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2125 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2126 | req->SetPriority(MAXIMUM_PRIORITY); |
| 2127 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2128 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2129 | req->Start(); |
| 2130 | EXPECT_EQ(MAXIMUM_PRIORITY, req->priority()); |
[email protected] | bb1c466 | 2013-11-14 00:00:07 | [diff] [blame] | 2131 | EXPECT_EQ(MAXIMUM_PRIORITY, job->priority()); |
| 2132 | } |
| 2133 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2134 | // TODO(droger): Support SpawnedTestServer on iOS (see http://crbug.com/148666). |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2135 | #if !defined(OS_IOS) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2136 | // A subclass of SpawnedTestServer that uses a statically-configured hostname. |
| 2137 | // This is to work around mysterious failures in chrome_frame_net_tests. See: |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2138 | // http://crbug.com/114369 |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 2139 | // TODO(erikwright): remove or update as needed; see http://crbug.com/334634. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2140 | class LocalHttpTestServer : public SpawnedTestServer { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2141 | public: |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 2142 | explicit LocalHttpTestServer(const base::FilePath& document_root) |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2143 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 2144 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 2145 | document_root) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2146 | LocalHttpTestServer() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 2147 | : SpawnedTestServer(SpawnedTestServer::TYPE_HTTP, |
| 2148 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 2149 | base::FilePath()) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2150 | }; |
| 2151 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2152 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2153 | LocalHttpTestServer test_server; |
| 2154 | ASSERT_TRUE(test_server.Start()); |
| 2155 | |
| 2156 | TestURLRequestContext context; |
| 2157 | scoped_refptr<DelayedCookieMonster> delayed_cm = |
| 2158 | new DelayedCookieMonster(); |
| 2159 | scoped_refptr<CookieStore> cookie_store = delayed_cm; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2160 | context.set_cookie_store(delayed_cm.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2161 | |
| 2162 | // Set up a cookie. |
| 2163 | { |
| 2164 | TestNetworkDelegate network_delegate; |
| 2165 | context.set_network_delegate(&network_delegate); |
| 2166 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2167 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 2168 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2169 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2170 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2171 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2172 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2173 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2174 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2175 | } |
| 2176 | |
| 2177 | // Verify that the cookie is set. |
| 2178 | { |
| 2179 | TestNetworkDelegate network_delegate; |
| 2180 | context.set_network_delegate(&network_delegate); |
| 2181 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2182 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2183 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2184 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2185 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2186 | |
| 2187 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2188 | != std::string::npos); |
| 2189 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2190 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2191 | } |
| 2192 | } |
| 2193 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2194 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2195 | LocalHttpTestServer test_server; |
| 2196 | ASSERT_TRUE(test_server.Start()); |
| 2197 | |
| 2198 | // Set up a cookie. |
| 2199 | { |
| 2200 | TestNetworkDelegate network_delegate; |
| 2201 | default_context_.set_network_delegate(&network_delegate); |
| 2202 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2203 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2204 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2205 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2206 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2207 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2208 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2209 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2210 | } |
| 2211 | |
| 2212 | // Verify that the cookie is set. |
| 2213 | { |
| 2214 | TestNetworkDelegate network_delegate; |
| 2215 | default_context_.set_network_delegate(&network_delegate); |
| 2216 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2217 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2218 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2219 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2220 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2221 | |
| 2222 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2223 | != std::string::npos); |
| 2224 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2225 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2226 | } |
| 2227 | |
| 2228 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2229 | { |
| 2230 | TestNetworkDelegate network_delegate; |
| 2231 | default_context_.set_network_delegate(&network_delegate); |
| 2232 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2233 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2234 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2235 | req->SetLoadFlags(LOAD_DO_NOT_SEND_COOKIES); |
| 2236 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2237 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2238 | |
| 2239 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2240 | == std::string::npos); |
| 2241 | |
| 2242 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 2243 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2244 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 2249 | LocalHttpTestServer test_server; |
| 2250 | ASSERT_TRUE(test_server.Start()); |
| 2251 | |
| 2252 | // Set up a cookie. |
| 2253 | { |
| 2254 | TestNetworkDelegate network_delegate; |
| 2255 | default_context_.set_network_delegate(&network_delegate); |
| 2256 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2257 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2258 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2259 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2260 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2261 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2262 | |
| 2263 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2264 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2265 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2266 | } |
| 2267 | |
| 2268 | // Try to set-up another cookie and update the previous cookie. |
| 2269 | { |
| 2270 | TestNetworkDelegate network_delegate; |
| 2271 | default_context_.set_network_delegate(&network_delegate); |
| 2272 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2273 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2274 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2275 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2276 | req->SetLoadFlags(LOAD_DO_NOT_SAVE_COOKIES); |
| 2277 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2278 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2279 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2280 | |
| 2281 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 2282 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2283 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2284 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2285 | } |
| 2286 | |
| 2287 | // Verify the cookies weren't saved or updated. |
| 2288 | { |
| 2289 | TestNetworkDelegate network_delegate; |
| 2290 | default_context_.set_network_delegate(&network_delegate); |
| 2291 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2292 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2293 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2294 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2295 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2296 | |
| 2297 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2298 | == std::string::npos); |
| 2299 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2300 | != std::string::npos); |
| 2301 | |
| 2302 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2303 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2304 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 2309 | LocalHttpTestServer test_server; |
| 2310 | ASSERT_TRUE(test_server.Start()); |
| 2311 | |
| 2312 | // Set up a cookie. |
| 2313 | { |
| 2314 | TestNetworkDelegate network_delegate; |
| 2315 | default_context_.set_network_delegate(&network_delegate); |
| 2316 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2317 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2318 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2319 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2320 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2321 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2322 | |
| 2323 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2324 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2325 | } |
| 2326 | |
| 2327 | // Verify that the cookie is set. |
| 2328 | { |
| 2329 | TestNetworkDelegate network_delegate; |
| 2330 | default_context_.set_network_delegate(&network_delegate); |
| 2331 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2332 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2333 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2334 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2335 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2336 | |
| 2337 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2338 | != std::string::npos); |
| 2339 | |
| 2340 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2341 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2342 | } |
| 2343 | |
| 2344 | // Verify that the cookie isn't sent. |
| 2345 | { |
| 2346 | TestNetworkDelegate network_delegate; |
| 2347 | default_context_.set_network_delegate(&network_delegate); |
| 2348 | TestDelegate d; |
| 2349 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2350 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2351 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2352 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2353 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2354 | |
| 2355 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2356 | == std::string::npos); |
| 2357 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2358 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2359 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 2364 | LocalHttpTestServer test_server; |
| 2365 | ASSERT_TRUE(test_server.Start()); |
| 2366 | |
| 2367 | // Set up a cookie. |
| 2368 | { |
| 2369 | TestNetworkDelegate network_delegate; |
| 2370 | default_context_.set_network_delegate(&network_delegate); |
| 2371 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2372 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2373 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2374 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2375 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2376 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2377 | |
| 2378 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2379 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2380 | } |
| 2381 | |
| 2382 | // Try to set-up another cookie and update the previous cookie. |
| 2383 | { |
| 2384 | TestNetworkDelegate network_delegate; |
| 2385 | default_context_.set_network_delegate(&network_delegate); |
| 2386 | TestDelegate d; |
| 2387 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2388 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2389 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2390 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2391 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2392 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2393 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2394 | |
| 2395 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2396 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2397 | } |
| 2398 | |
| 2399 | // Verify the cookies weren't saved or updated. |
| 2400 | { |
| 2401 | TestNetworkDelegate network_delegate; |
| 2402 | default_context_.set_network_delegate(&network_delegate); |
| 2403 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2404 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2405 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2406 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2407 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2408 | |
| 2409 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2410 | == std::string::npos); |
| 2411 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2412 | != std::string::npos); |
| 2413 | |
| 2414 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2415 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 2420 | LocalHttpTestServer test_server; |
| 2421 | ASSERT_TRUE(test_server.Start()); |
| 2422 | |
| 2423 | // Set up an empty 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( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2429 | test_server.GetURL("set-cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2430 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2431 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2432 | |
| 2433 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2434 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2435 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 2440 | LocalHttpTestServer test_server; |
| 2441 | ASSERT_TRUE(test_server.Start()); |
| 2442 | |
| 2443 | // Set up a cookie. |
| 2444 | { |
| 2445 | TestNetworkDelegate network_delegate; |
| 2446 | default_context_.set_network_delegate(&network_delegate); |
| 2447 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2448 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2449 | test_server.GetURL("set-cookie?CookieToNotSend=1"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2450 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2451 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2452 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2453 | |
| 2454 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2455 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2456 | } |
| 2457 | |
| 2458 | // Verify that the cookie is set. |
| 2459 | { |
| 2460 | TestNetworkDelegate network_delegate; |
| 2461 | default_context_.set_network_delegate(&network_delegate); |
| 2462 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2463 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2464 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2465 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2466 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2467 | |
| 2468 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2469 | != std::string::npos); |
| 2470 | |
| 2471 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2472 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2473 | } |
| 2474 | |
| 2475 | // Verify that the cookie isn't sent. |
| 2476 | { |
| 2477 | TestNetworkDelegate network_delegate; |
| 2478 | default_context_.set_network_delegate(&network_delegate); |
| 2479 | TestDelegate d; |
| 2480 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2481 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2482 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2483 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2484 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2485 | |
| 2486 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2487 | == std::string::npos); |
| 2488 | |
[email protected] | 22e045f | 2013-09-20 03:54:03 | [diff] [blame] | 2489 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2490 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2491 | } |
| 2492 | } |
| 2493 | |
| 2494 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 2495 | LocalHttpTestServer test_server; |
| 2496 | ASSERT_TRUE(test_server.Start()); |
| 2497 | |
| 2498 | // Set up a cookie. |
| 2499 | { |
| 2500 | TestNetworkDelegate network_delegate; |
| 2501 | default_context_.set_network_delegate(&network_delegate); |
| 2502 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2503 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2504 | test_server.GetURL("set-cookie?CookieToNotUpdate=2"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2505 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2506 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2507 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2508 | |
| 2509 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2510 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2511 | } |
| 2512 | |
| 2513 | // Try to set-up another cookie and update the previous cookie. |
| 2514 | { |
| 2515 | TestNetworkDelegate network_delegate; |
| 2516 | default_context_.set_network_delegate(&network_delegate); |
| 2517 | TestDelegate d; |
| 2518 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2519 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2520 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2521 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2522 | req->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2523 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2524 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2525 | |
| 2526 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2527 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 2528 | } |
| 2529 | |
| 2530 | // Verify the cookies weren't saved or updated. |
| 2531 | { |
| 2532 | TestNetworkDelegate network_delegate; |
| 2533 | default_context_.set_network_delegate(&network_delegate); |
| 2534 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2535 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2536 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2537 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2538 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2539 | |
| 2540 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2541 | == std::string::npos); |
| 2542 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2543 | != std::string::npos); |
| 2544 | |
| 2545 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2546 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2547 | } |
| 2548 | } |
| 2549 | |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2550 | TEST_F(URLRequestTest, FirstPartyOnlyCookiesEnabled) { |
| 2551 | LocalHttpTestServer test_server; |
| 2552 | ASSERT_TRUE(test_server.Start()); |
| 2553 | |
| 2554 | // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where |
| 2555 | // LocalHttpTestServer points). |
| 2556 | { |
| 2557 | TestNetworkDelegate network_delegate; |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2558 | network_delegate.set_first_party_only_cookies_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2559 | default_context_.set_network_delegate(&network_delegate); |
| 2560 | |
| 2561 | TestDelegate d; |
| 2562 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2563 | test_server.GetURL( |
| 2564 | "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2565 | DEFAULT_PRIORITY, &d)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2566 | req->Start(); |
| 2567 | base::RunLoop().Run(); |
| 2568 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2569 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2570 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2571 | } |
| 2572 | |
| 2573 | // Verify that the cookie is sent for first-party requests. |
| 2574 | { |
| 2575 | TestNetworkDelegate network_delegate; |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2576 | network_delegate.set_first_party_only_cookies_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2577 | default_context_.set_network_delegate(&network_delegate); |
| 2578 | TestDelegate d; |
| 2579 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2580 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | 92df3fd | 2015-04-06 19:57:47 | [diff] [blame] | 2581 | req->set_first_party_for_cookies(test_server.GetURL("")); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2582 | req->Start(); |
| 2583 | base::RunLoop().Run(); |
| 2584 | |
| 2585 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
| 2586 | std::string::npos); |
| 2587 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2588 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2589 | } |
| 2590 | |
| 2591 | // Verify that the cookie is not-sent for non-first-party requests. |
| 2592 | { |
| 2593 | TestNetworkDelegate network_delegate; |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2594 | network_delegate.set_first_party_only_cookies_enabled(true); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2595 | default_context_.set_network_delegate(&network_delegate); |
| 2596 | TestDelegate d; |
| 2597 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2598 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
mkwst | 3f3daac | 2015-02-26 20:15:26 | [diff] [blame] | 2599 | req->set_first_party_for_cookies(GURL("http://third-party.test/")); |
| 2600 | req->Start(); |
| 2601 | base::RunLoop().Run(); |
| 2602 | |
| 2603 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") == |
| 2604 | std::string::npos); |
| 2605 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2606 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2607 | } |
| 2608 | } |
| 2609 | |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2610 | TEST_F(URLRequestTest, FirstPartyOnlyCookiesDisabled) { |
| 2611 | LocalHttpTestServer test_server; |
| 2612 | ASSERT_TRUE(test_server.Start()); |
| 2613 | |
| 2614 | // Set up a 'First-Party-Only' cookie (on '127.0.0.1', as that's where |
| 2615 | // LocalHttpTestServer points). |
| 2616 | { |
| 2617 | TestNetworkDelegate network_delegate; |
| 2618 | network_delegate.set_first_party_only_cookies_enabled(false); |
| 2619 | default_context_.set_network_delegate(&network_delegate); |
| 2620 | |
| 2621 | TestDelegate d; |
| 2622 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2623 | test_server.GetURL( |
| 2624 | "set-cookie?FirstPartyCookieToSet=1;First-Party-Only"), |
| 2625 | DEFAULT_PRIORITY, &d)); |
| 2626 | req->Start(); |
| 2627 | base::RunLoop().Run(); |
| 2628 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2629 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2630 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 2631 | } |
| 2632 | |
| 2633 | // Verify that the cookie is sent for first-party requests. |
| 2634 | { |
| 2635 | TestNetworkDelegate network_delegate; |
| 2636 | network_delegate.set_first_party_only_cookies_enabled(false); |
| 2637 | default_context_.set_network_delegate(&network_delegate); |
| 2638 | TestDelegate d; |
| 2639 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2640 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
estark | 92df3fd | 2015-04-06 19:57:47 | [diff] [blame] | 2641 | req->set_first_party_for_cookies(test_server.GetURL("")); |
mkwst | 0513c9d | 2015-04-01 05:53:15 | [diff] [blame] | 2642 | req->Start(); |
| 2643 | base::RunLoop().Run(); |
| 2644 | |
| 2645 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
| 2646 | std::string::npos); |
| 2647 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2648 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2649 | } |
| 2650 | |
| 2651 | // Verify that the cookie is also sent for non-first-party requests. |
| 2652 | { |
| 2653 | TestNetworkDelegate network_delegate; |
| 2654 | network_delegate.set_first_party_only_cookies_enabled(false); |
| 2655 | default_context_.set_network_delegate(&network_delegate); |
| 2656 | TestDelegate d; |
| 2657 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 2658 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
| 2659 | req->set_first_party_for_cookies(GURL("http://third-party.test/")); |
| 2660 | req->Start(); |
| 2661 | base::RunLoop().Run(); |
| 2662 | |
| 2663 | EXPECT_TRUE(d.data_received().find("FirstPartyCookieToSet=1") != |
| 2664 | std::string::npos); |
| 2665 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2666 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2667 | } |
| 2668 | } |
| 2669 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2670 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 2671 | // value for the |fixed_date| argument given to the constructor. |
| 2672 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 2673 | public: |
| 2674 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 2675 | : fixed_date_(fixed_date) {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 2676 | ~FixedDateNetworkDelegate() override {} |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2677 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2678 | // NetworkDelegate implementation |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 2679 | int OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2680 | URLRequest* request, |
| 2681 | const CompletionCallback& callback, |
| 2682 | const HttpResponseHeaders* original_response_headers, |
| 2683 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 2684 | GURL* allowed_unsafe_redirect_url) override; |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2685 | |
| 2686 | private: |
| 2687 | std::string fixed_date_; |
| 2688 | |
| 2689 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 2690 | }; |
| 2691 | |
| 2692 | int FixedDateNetworkDelegate::OnHeadersReceived( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2693 | URLRequest* request, |
| 2694 | const CompletionCallback& callback, |
| 2695 | const HttpResponseHeaders* original_response_headers, |
| 2696 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2697 | GURL* allowed_unsafe_redirect_url) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 2698 | HttpResponseHeaders* new_response_headers = |
| 2699 | new HttpResponseHeaders(original_response_headers->raw_headers()); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2700 | |
| 2701 | new_response_headers->RemoveHeader("Date"); |
| 2702 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 2703 | |
| 2704 | *override_response_headers = new_response_headers; |
| 2705 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 2706 | callback, |
| 2707 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 2708 | override_response_headers, |
| 2709 | allowed_unsafe_redirect_url); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | // Test that cookie expiration times are adjusted for server/client clock |
| 2713 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 2714 | // headers by defaulting to GMT. (crbug.com/135131) |
| 2715 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 2716 | LocalHttpTestServer test_server; |
| 2717 | ASSERT_TRUE(test_server.Start()); |
| 2718 | |
| 2719 | // Set up an expired cookie. |
| 2720 | { |
| 2721 | TestNetworkDelegate network_delegate; |
| 2722 | default_context_.set_network_delegate(&network_delegate); |
| 2723 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2724 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2725 | test_server.GetURL( |
| 2726 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2727 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2728 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2729 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2730 | } |
| 2731 | // Verify that the cookie is not set. |
| 2732 | { |
| 2733 | TestNetworkDelegate network_delegate; |
| 2734 | default_context_.set_network_delegate(&network_delegate); |
| 2735 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2736 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2737 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2738 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2739 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2740 | |
| 2741 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 2742 | } |
| 2743 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 2744 | { |
| 2745 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 2746 | default_context_.set_network_delegate(&network_delegate); |
| 2747 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2748 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 2749 | test_server.GetURL( |
| 2750 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2751 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2752 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2753 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2754 | } |
| 2755 | // Verify that the cookie is set. |
| 2756 | { |
| 2757 | TestNetworkDelegate network_delegate; |
| 2758 | default_context_.set_network_delegate(&network_delegate); |
| 2759 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2760 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2761 | test_server.GetURL("echoheader?Cookie"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2762 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2763 | base::RunLoop().Run(); |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 2764 | |
| 2765 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2770 | // Check that it is impossible to change the referrer in the extra headers of |
| 2771 | // an URLRequest. |
| 2772 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 2773 | LocalHttpTestServer test_server; |
| 2774 | ASSERT_TRUE(test_server.Start()); |
| 2775 | |
| 2776 | // If extra headers contain referer and the request contains a referer, |
| 2777 | // only the latter shall be respected. |
| 2778 | { |
| 2779 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2780 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2781 | test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2782 | req->SetReferrer("http://foo.com/"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2783 | |
| 2784 | HttpRequestHeaders headers; |
| 2785 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2786 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2787 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2788 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2789 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2790 | |
| 2791 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 2792 | } |
| 2793 | |
| 2794 | // If extra headers contain a referer but the request does not, no referer |
| 2795 | // shall be sent in the header. |
| 2796 | { |
| 2797 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2798 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2799 | test_server.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2800 | |
| 2801 | HttpRequestHeaders headers; |
| 2802 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2803 | req->SetExtraRequestHeaders(headers); |
| 2804 | req->SetLoadFlags(LOAD_VALIDATE_CACHE); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2805 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2806 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2807 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 2808 | |
| 2809 | EXPECT_EQ("None", d.data_received()); |
| 2810 | } |
| 2811 | } |
| 2812 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2813 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2814 | public: |
| 2815 | URLRequestTestHTTP() |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 2816 | : test_server_(base::FilePath(FILE_PATH_LITERAL( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2817 | "net/data/url_request_unittest"))) { |
| 2818 | } |
| 2819 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2820 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2821 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 2822 | // |request_method| is the method to use for the initial request. |
| 2823 | // |redirect_method| is the method that is expected to be used for the second |
| 2824 | // request, after redirection. |
| 2825 | // If |include_data| is true, data is uploaded with the request. The |
| 2826 | // response body is expected to match it exactly, if and only if |
| 2827 | // |request_method| == |redirect_method|. |
| 2828 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 2829 | const std::string& request_method, |
| 2830 | const std::string& redirect_method, |
| 2831 | bool include_data) { |
| 2832 | static const char kData[] = "hello world"; |
| 2833 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2834 | scoped_ptr<URLRequest> req( |
| 2835 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2836 | req->set_method(request_method); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2837 | if (include_data) { |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 2838 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2839 | HttpRequestHeaders headers; |
| 2840 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 2841 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2842 | req->SetExtraRequestHeaders(headers); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2843 | } |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2844 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2845 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2846 | EXPECT_EQ(redirect_method, req->method()); |
| 2847 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 2848 | EXPECT_EQ(OK, req->status().error()); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 2849 | if (include_data) { |
| 2850 | if (request_method == redirect_method) { |
| 2851 | EXPECT_EQ(kData, d.data_received()); |
| 2852 | } else { |
| 2853 | EXPECT_NE(kData, d.data_received()); |
| 2854 | } |
| 2855 | } |
| 2856 | if (HasFailure()) |
| 2857 | LOG(WARNING) << "Request method was: " << request_method; |
| 2858 | } |
| 2859 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 2860 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 2861 | // |request_method| is the method to use for the initial request. |
| 2862 | // |redirect_method| is the method that is expected to be used for the second |
| 2863 | // request, after redirection. |
| 2864 | // |origin_value| is the expected value for the Origin header after |
| 2865 | // redirection. If empty, expects that there will be no Origin header. |
| 2866 | void HTTPRedirectOriginHeaderTest(const GURL& redirect_url, |
| 2867 | const std::string& request_method, |
| 2868 | const std::string& redirect_method, |
| 2869 | const std::string& origin_value) { |
| 2870 | TestDelegate d; |
| 2871 | scoped_ptr<URLRequest> req( |
| 2872 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
| 2873 | req->set_method(request_method); |
| 2874 | req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin, |
| 2875 | redirect_url.GetOrigin().spec(), false); |
| 2876 | req->Start(); |
| 2877 | |
| 2878 | base::RunLoop().Run(); |
| 2879 | |
| 2880 | EXPECT_EQ(redirect_method, req->method()); |
| 2881 | // Note that there is no check for request success here because, for |
| 2882 | // purposes of testing, the request very well may fail. For example, if the |
| 2883 | // test redirects to an HTTPS server from an HTTP origin, thus it is cross |
| 2884 | // origin, there is not an HTTPS server in this unit test framework, so the |
| 2885 | // request would fail. However, that's fine, as long as the request headers |
| 2886 | // are in order and pass the checks below. |
| 2887 | if (origin_value.empty()) { |
| 2888 | EXPECT_FALSE( |
| 2889 | req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin)); |
| 2890 | } else { |
| 2891 | std::string origin_header; |
| 2892 | EXPECT_TRUE(req->extra_request_headers().GetHeader( |
| 2893 | HttpRequestHeaders::kOrigin, &origin_header)); |
| 2894 | EXPECT_EQ(origin_value, origin_header); |
| 2895 | } |
| 2896 | } |
| 2897 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2898 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2899 | const int kMsgSize = 20000; // multiple of 10 |
| 2900 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 2901 | char* uploadBytes = new char[kMsgSize+1]; |
| 2902 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2903 | char marker = 'a'; |
| 2904 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 2905 | memcpy(ptr, "----------", 10); |
| 2906 | ptr += 10; |
| 2907 | if (idx % 100 == 0) { |
| 2908 | ptr--; |
| 2909 | *ptr++ = marker; |
| 2910 | if (++marker > 'z') |
| 2911 | marker = 'a'; |
| 2912 | } |
| 2913 | } |
| 2914 | uploadBytes[kMsgSize] = '\0'; |
| 2915 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2916 | for (int i = 0; i < kIterations; ++i) { |
| 2917 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2918 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2919 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2920 | r->set_method(method.c_str()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2921 | |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 2922 | r->set_upload(CreateSimpleUploadData(uploadBytes)); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2923 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2924 | r->Start(); |
| 2925 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2926 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2927 | base::RunLoop().Run(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2928 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2929 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2930 | << "request failed: " << r->status().status() |
| 2931 | << ", os error: " << r->status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2932 | |
| 2933 | EXPECT_FALSE(d.received_data_before_response()); |
| 2934 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2935 | } |
| 2936 | delete[] uploadBytes; |
| 2937 | } |
| 2938 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2939 | void AddChunksToUpload(URLRequest* r) { |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 2940 | r->AppendChunkToUpload("a", 1, false); |
| 2941 | r->AppendChunkToUpload("bcd", 3, false); |
| 2942 | r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); |
| 2943 | r->AppendChunkToUpload("\r\n\r\n", 4, false); |
| 2944 | r->AppendChunkToUpload("0", 1, false); |
| 2945 | r->AppendChunkToUpload("2323", 4, true); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2946 | } |
| 2947 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2948 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2949 | // This should match the chunks sent by AddChunksToUpload(). |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2950 | const std::string expected_data = |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2951 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 2952 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2953 | ASSERT_EQ(1, d->response_started_count()) |
| 2954 | << "request failed: " << r->status().status() |
| 2955 | << ", os error: " << r->status().error(); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2956 | |
| 2957 | EXPECT_FALSE(d->received_data_before_response()); |
| 2958 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2959 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 2960 | EXPECT_EQ(expected_data, d->data_received()); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2961 | } |
| 2962 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2963 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2964 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2965 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 2966 | test_server_.GetURL("set-many-cookies?" + |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 2967 | base::IntToString(num_cookies)), |
| 2968 | DEFAULT_PRIORITY, &d)); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2969 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2970 | r->Start(); |
| 2971 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2972 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 2973 | base::RunLoop().Run(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2974 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2975 | bool is_success = r->status().is_success(); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2976 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2977 | if (!is_success) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 2978 | EXPECT_TRUE(r->status().error() == ERR_RESPONSE_HEADERS_TOO_BIG); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2979 | // The test server appears to be unable to handle subsequent requests |
| 2980 | // after this error is triggered. Force it to restart. |
| 2981 | EXPECT_TRUE(test_server_.Stop()); |
| 2982 | EXPECT_TRUE(test_server_.Start()); |
| 2983 | } |
| 2984 | |
| 2985 | return is_success; |
| 2986 | } |
| 2987 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 2988 | LocalHttpTestServer* test_server() { |
| 2989 | return &test_server_; |
| 2990 | } |
| 2991 | |
| 2992 | protected: |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2993 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2994 | }; |
| 2995 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2996 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 2997 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 2998 | // The HTTPTestServer will return a 302 response, which we should not |
| 2999 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 3000 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3001 | ASSERT_TRUE(test_server_.Start()); |
| 3002 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3003 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3004 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3005 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3006 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3007 | TestDelegate d; |
| 3008 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3009 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3010 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3011 | r->Start(); |
| 3012 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3013 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3014 | base::RunLoop().Run(); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3015 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3016 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3017 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3018 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3019 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3020 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 3021 | // We should not have followed the redirect. |
| 3022 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3023 | } |
| 3024 | } |
| 3025 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3026 | // This is the same as the previous test, but checks that the network delegate |
| 3027 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 3028 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3029 | ASSERT_TRUE(test_server_.Start()); |
| 3030 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3031 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3032 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3033 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3034 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3035 | TestDelegate d; |
| 3036 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3037 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3038 | GURL("https://www.redirect.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3039 | r->Start(); |
| 3040 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3041 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3042 | base::RunLoop().Run(); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3043 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3044 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3045 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3046 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3047 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3048 | EXPECT_EQ(1, d.response_started_count()); |
| 3049 | // We should not have followed the redirect. |
| 3050 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3051 | |
| 3052 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 3053 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3054 | } |
| 3055 | } |
| 3056 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3057 | // Tests that we can block and asynchronously return OK in various stages. |
| 3058 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 3059 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 3060 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3061 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3062 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 3063 | }; |
| 3064 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 3065 | |
| 3066 | ASSERT_TRUE(test_server_.Start()); |
| 3067 | |
| 3068 | TestDelegate d; |
| 3069 | BlockingNetworkDelegate network_delegate( |
| 3070 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3071 | network_delegate.set_block_on( |
| 3072 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 3073 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 3074 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3075 | |
| 3076 | TestURLRequestContext context(true); |
| 3077 | context.set_network_delegate(&network_delegate); |
| 3078 | context.Init(); |
| 3079 | |
| 3080 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3081 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3082 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3083 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3084 | r->Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3085 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3086 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3087 | EXPECT_EQ(blocking_stages[i], |
| 3088 | network_delegate.stage_blocked_for_callback()); |
| 3089 | network_delegate.DoCallback(OK); |
| 3090 | } |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3091 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3092 | EXPECT_EQ(200, r->GetResponseCode()); |
| 3093 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3094 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3095 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3096 | } |
| 3097 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3098 | } |
| 3099 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3100 | // Tests that the network delegate can block and cancel a request. |
| 3101 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
| 3102 | ASSERT_TRUE(test_server_.Start()); |
| 3103 | |
| 3104 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3105 | BlockingNetworkDelegate network_delegate( |
| 3106 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3107 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 3108 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3109 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3110 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3111 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3112 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3113 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3114 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3115 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3116 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3117 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3118 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3119 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3120 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3121 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3122 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3123 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r->status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3124 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3125 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3126 | } |
| 3127 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3128 | } |
| 3129 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3130 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 3131 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 3132 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 3133 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 3134 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 3135 | BlockingNetworkDelegate::Stage stage, |
| 3136 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3137 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3138 | BlockingNetworkDelegate network_delegate(block_mode); |
| 3139 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 3140 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3141 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3142 | TestURLRequestContext context(true); |
| 3143 | context.set_network_delegate(&network_delegate); |
| 3144 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3145 | |
| 3146 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3147 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3148 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3149 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3150 | base::RunLoop().Run(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3151 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3152 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3153 | // The proxy server is not set before cancellation. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3154 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3155 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r->status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 3156 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3157 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3158 | } |
| 3159 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3160 | } |
| 3161 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3162 | // The following 3 tests check that the network delegate can cancel a request |
| 3163 | // synchronously in various stages of the request. |
| 3164 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
| 3165 | ASSERT_TRUE(test_server_.Start()); |
| 3166 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3167 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3168 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
| 3172 | ASSERT_TRUE(test_server_.Start()); |
| 3173 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3174 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3175 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
| 3179 | ASSERT_TRUE(test_server_.Start()); |
| 3180 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 3181 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3182 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | // The following 3 tests check that the network delegate can cancel a request |
| 3186 | // asynchronously in various stages of the request. |
| 3187 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
| 3188 | ASSERT_TRUE(test_server_.Start()); |
| 3189 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3190 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3191 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
| 3195 | ASSERT_TRUE(test_server_.Start()); |
| 3196 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3197 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3198 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
| 3202 | ASSERT_TRUE(test_server_.Start()); |
| 3203 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 3204 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 3205 | test_server_.GetURL(std::string())); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 3206 | } |
| 3207 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3208 | // Tests that the network delegate can block and redirect a request to a new |
| 3209 | // URL. |
| 3210 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
| 3211 | ASSERT_TRUE(test_server_.Start()); |
| 3212 | |
| 3213 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3214 | BlockingNetworkDelegate network_delegate( |
| 3215 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3216 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3217 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 3218 | network_delegate.set_redirect_url(redirect_url); |
| 3219 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3220 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3221 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3222 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3223 | { |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3224 | GURL original_url(test_server_.GetURL("empty.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3225 | scoped_ptr<URLRequest> r( |
| 3226 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3227 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3228 | // Quit after hitting the redirect, so can check the headers. |
| 3229 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3230 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3231 | base::RunLoop().Run(); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3232 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3233 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3234 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3235 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3236 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3237 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3238 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3239 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3240 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3241 | |
| 3242 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3243 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3244 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3245 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3246 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3247 | EXPECT_EQ( |
| 3248 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 3249 | EXPECT_TRUE( |
| 3250 | network_delegate.last_observed_proxy().Equals( |
| 3251 | test_server_.host_port_pair())); |
| 3252 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3253 | EXPECT_EQ(0, r->status().error()); |
| 3254 | EXPECT_EQ(redirect_url, r->url()); |
| 3255 | EXPECT_EQ(original_url, r->original_url()); |
| 3256 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 3257 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3258 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3259 | } |
| 3260 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3261 | } |
| 3262 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3263 | // Tests that the network delegate can block and redirect a request to a new |
| 3264 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 3265 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
| 3266 | ASSERT_TRUE(test_server_.Start()); |
| 3267 | |
| 3268 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3269 | BlockingNetworkDelegate network_delegate( |
| 3270 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3271 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 3272 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3273 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3274 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3275 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3276 | |
| 3277 | { |
| 3278 | GURL original_url(test_server_.GetURL("empty.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3279 | scoped_ptr<URLRequest> r( |
| 3280 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3281 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3282 | // Quit after hitting the redirect, so can check the headers. |
| 3283 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3284 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3285 | base::RunLoop().Run(); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3286 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3287 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3288 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3289 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3290 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3291 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3292 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3293 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3294 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3295 | |
| 3296 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3297 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3298 | base::RunLoop().Run(); |
| 3299 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3300 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3301 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3302 | EXPECT_EQ( |
| 3303 | 1, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 3304 | EXPECT_TRUE( |
| 3305 | network_delegate.last_observed_proxy().Equals( |
| 3306 | test_server_.host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3307 | EXPECT_EQ(0, r->status().error()); |
| 3308 | EXPECT_EQ(redirect_url, r->url()); |
| 3309 | EXPECT_EQ(original_url, r->original_url()); |
| 3310 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 3311 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3312 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3313 | } |
| 3314 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3315 | } |
| 3316 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3317 | // Tests that redirects caused by the network delegate preserve POST data. |
| 3318 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 3319 | ASSERT_TRUE(test_server_.Start()); |
| 3320 | |
| 3321 | const char kData[] = "hello world"; |
| 3322 | |
| 3323 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3324 | BlockingNetworkDelegate network_delegate( |
| 3325 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3326 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3327 | GURL redirect_url(test_server_.GetURL("echo")); |
| 3328 | network_delegate.set_redirect_url(redirect_url); |
| 3329 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3330 | TestURLRequestContext context(true); |
| 3331 | context.set_network_delegate(&network_delegate); |
| 3332 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3333 | |
| 3334 | { |
| 3335 | GURL original_url(test_server_.GetURL("empty.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3336 | scoped_ptr<URLRequest> r( |
| 3337 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3338 | r->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 3339 | r->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3340 | HttpRequestHeaders headers; |
| 3341 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3342 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3343 | r->SetExtraRequestHeaders(headers); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3344 | |
| 3345 | // Quit after hitting the redirect, so can check the headers. |
| 3346 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3347 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3348 | base::RunLoop().Run(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3349 | |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3350 | // Check headers from URLRequestJob. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3351 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3352 | EXPECT_EQ(307, r->GetResponseCode()); |
| 3353 | EXPECT_EQ(307, r->response_headers()->response_code()); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3354 | std::string location; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3355 | ASSERT_TRUE(r->response_headers()->EnumerateHeader(NULL, "Location", |
| 3356 | &location)); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3357 | EXPECT_EQ(redirect_url, GURL(location)); |
| 3358 | |
| 3359 | // Let the request finish. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3360 | r->FollowDeferredRedirect(); |
[email protected] | 6be6fa9 | 2014-08-06 23:44:56 | [diff] [blame] | 3361 | base::RunLoop().Run(); |
| 3362 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3363 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3364 | EXPECT_EQ(0, r->status().error()); |
| 3365 | EXPECT_EQ(redirect_url, r->url()); |
| 3366 | EXPECT_EQ(original_url, r->original_url()); |
| 3367 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3368 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3369 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3370 | EXPECT_EQ("POST", r->method()); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3371 | EXPECT_EQ(kData, d.data_received()); |
| 3372 | } |
| 3373 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3374 | } |
| 3375 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3376 | // Tests that the network delegate can block and redirect a request to a new |
| 3377 | // URL during OnHeadersReceived. |
| 3378 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestOnHeadersReceived) { |
| 3379 | ASSERT_TRUE(test_server_.Start()); |
| 3380 | |
| 3381 | TestDelegate d; |
| 3382 | BlockingNetworkDelegate network_delegate( |
| 3383 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3384 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 3385 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 3386 | network_delegate.set_redirect_on_headers_received_url(redirect_url); |
| 3387 | |
| 3388 | TestURLRequestContextWithProxy context( |
| 3389 | test_server_.host_port_pair().ToString(), &network_delegate); |
| 3390 | |
| 3391 | { |
| 3392 | GURL original_url(test_server_.GetURL("empty.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3393 | scoped_ptr<URLRequest> r( |
| 3394 | context.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3395 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3396 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3397 | base::RunLoop().Run(); |
| 3398 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3399 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3400 | EXPECT_TRUE(r->proxy_server().Equals(test_server_.host_port_pair())); |
[email protected] | 597a1ab | 2014-06-26 08:12:27 | [diff] [blame] | 3401 | EXPECT_EQ( |
| 3402 | 2, network_delegate.observed_before_proxy_headers_sent_callbacks()); |
| 3403 | EXPECT_TRUE( |
| 3404 | network_delegate.last_observed_proxy().Equals( |
| 3405 | test_server_.host_port_pair())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3406 | |
| 3407 | EXPECT_EQ(OK, r->status().error()); |
| 3408 | EXPECT_EQ(redirect_url, r->url()); |
| 3409 | EXPECT_EQ(original_url, r->original_url()); |
| 3410 | EXPECT_EQ(2U, r->url_chain().size()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 3411 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 3412 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3413 | } |
| 3414 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3415 | } |
| 3416 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3417 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3418 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 3419 | // handle the challenge, and is passing the buck along to the |
| 3420 | // URLRequest::Delegate. |
| 3421 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 3422 | ASSERT_TRUE(test_server_.Start()); |
| 3423 | |
| 3424 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3425 | BlockingNetworkDelegate network_delegate( |
| 3426 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3427 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3428 | TestURLRequestContext context(true); |
| 3429 | context.set_network_delegate(&network_delegate); |
| 3430 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3431 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3432 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3433 | |
| 3434 | { |
| 3435 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3436 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3437 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3438 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3439 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3440 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3441 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3442 | EXPECT_EQ(0, r->status().error()); |
| 3443 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3444 | EXPECT_TRUE(d.auth_required_called()); |
| 3445 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3446 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3447 | } |
| 3448 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3449 | } |
| 3450 | |
| 3451 | TEST_F(URLRequestTestHTTP, |
| 3452 | NetworkDelegateOnAuthRequiredSyncNoAction_GetFullRequestHeaders) { |
| 3453 | ASSERT_TRUE(test_server_.Start()); |
| 3454 | |
| 3455 | TestDelegate d; |
| 3456 | BlockingNetworkDelegate network_delegate( |
| 3457 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3458 | |
| 3459 | TestURLRequestContext context(true); |
| 3460 | context.set_network_delegate(&network_delegate); |
| 3461 | context.Init(); |
| 3462 | |
| 3463 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3464 | |
| 3465 | { |
| 3466 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3467 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3468 | r->Start(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3469 | |
| 3470 | { |
| 3471 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3472 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3473 | EXPECT_FALSE(headers.HasHeader("Authorization")); |
| 3474 | } |
| 3475 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3476 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3477 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3478 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3479 | EXPECT_EQ(0, r->status().error()); |
| 3480 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3481 | EXPECT_TRUE(d.auth_required_called()); |
| 3482 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3483 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3484 | } |
| 3485 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3486 | } |
| 3487 | |
| 3488 | // Tests that the network delegate can synchronously complete OnAuthRequired |
[email protected] | 1e110eae | 2013-05-10 22:02:40 | [diff] [blame] | 3489 | // by setting credentials. |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3490 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 3491 | ASSERT_TRUE(test_server_.Start()); |
| 3492 | |
| 3493 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3494 | BlockingNetworkDelegate network_delegate( |
| 3495 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3496 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3497 | network_delegate.set_auth_retval( |
| 3498 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3499 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3500 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3501 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3502 | TestURLRequestContext context(true); |
| 3503 | context.set_network_delegate(&network_delegate); |
| 3504 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3505 | |
| 3506 | { |
| 3507 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3508 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3509 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3510 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3511 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3512 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3513 | EXPECT_EQ(0, r->status().error()); |
| 3514 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3515 | EXPECT_FALSE(d.auth_required_called()); |
| 3516 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3517 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3518 | } |
| 3519 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3520 | } |
| 3521 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3522 | // Same as above, but also tests that GetFullRequestHeaders returns the proper |
| 3523 | // headers (for the first or second request) when called at the proper times. |
| 3524 | TEST_F(URLRequestTestHTTP, |
| 3525 | NetworkDelegateOnAuthRequiredSyncSetAuth_GetFullRequestHeaders) { |
| 3526 | ASSERT_TRUE(test_server_.Start()); |
| 3527 | |
| 3528 | TestDelegate d; |
| 3529 | BlockingNetworkDelegate network_delegate( |
| 3530 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3531 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
| 3532 | network_delegate.set_auth_retval( |
| 3533 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3534 | |
| 3535 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
| 3536 | |
| 3537 | TestURLRequestContext context(true); |
| 3538 | context.set_network_delegate(&network_delegate); |
| 3539 | context.Init(); |
| 3540 | |
| 3541 | { |
| 3542 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3543 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3544 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3545 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3546 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3547 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3548 | EXPECT_EQ(0, r->status().error()); |
| 3549 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3550 | EXPECT_FALSE(d.auth_required_called()); |
| 3551 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3552 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3553 | |
| 3554 | { |
| 3555 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3556 | EXPECT_TRUE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3557 | EXPECT_TRUE(headers.HasHeader("Authorization")); |
| 3558 | } |
| 3559 | } |
| 3560 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3561 | } |
| 3562 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3563 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 3564 | // by cancelling authentication. |
| 3565 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 3566 | ASSERT_TRUE(test_server_.Start()); |
| 3567 | |
| 3568 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3569 | BlockingNetworkDelegate network_delegate( |
| 3570 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 3571 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3572 | network_delegate.set_auth_retval( |
| 3573 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3574 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3575 | TestURLRequestContext context(true); |
| 3576 | context.set_network_delegate(&network_delegate); |
| 3577 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3578 | |
| 3579 | { |
| 3580 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3581 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3582 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3583 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3584 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3585 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3586 | EXPECT_EQ(OK, r->status().error()); |
| 3587 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3588 | EXPECT_FALSE(d.auth_required_called()); |
| 3589 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3590 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3591 | } |
| 3592 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3593 | } |
| 3594 | |
| 3595 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3596 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 3597 | // to handle the challenge, and is passing the buck along to the |
| 3598 | // URLRequest::Delegate. |
| 3599 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 3600 | ASSERT_TRUE(test_server_.Start()); |
| 3601 | |
| 3602 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3603 | BlockingNetworkDelegate network_delegate( |
| 3604 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3605 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3606 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3607 | TestURLRequestContext context(true); |
| 3608 | context.set_network_delegate(&network_delegate); |
| 3609 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3610 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3611 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3612 | |
| 3613 | { |
| 3614 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3615 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3616 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3617 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3618 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3619 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3620 | EXPECT_EQ(0, r->status().error()); |
| 3621 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3622 | EXPECT_TRUE(d.auth_required_called()); |
| 3623 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3624 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3625 | } |
| 3626 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3627 | } |
| 3628 | |
| 3629 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3630 | // by setting credentials. |
| 3631 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 3632 | ASSERT_TRUE(test_server_.Start()); |
| 3633 | |
| 3634 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3635 | BlockingNetworkDelegate network_delegate( |
| 3636 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3637 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3638 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3639 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 3640 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3641 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3642 | network_delegate.set_auth_credentials(auth_credentials); |
| 3643 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3644 | TestURLRequestContext context(true); |
| 3645 | context.set_network_delegate(&network_delegate); |
| 3646 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3647 | |
| 3648 | { |
| 3649 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3650 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3651 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3652 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3653 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3654 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3655 | EXPECT_EQ(0, r->status().error()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3656 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3657 | EXPECT_EQ(200, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3658 | EXPECT_FALSE(d.auth_required_called()); |
| 3659 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3660 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3661 | } |
| 3662 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3663 | } |
| 3664 | |
| 3665 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 3666 | // by cancelling authentication. |
| 3667 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 3668 | ASSERT_TRUE(test_server_.Start()); |
| 3669 | |
| 3670 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3671 | BlockingNetworkDelegate network_delegate( |
| 3672 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 3673 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3674 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3675 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 3676 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3677 | TestURLRequestContext context(true); |
| 3678 | context.set_network_delegate(&network_delegate); |
| 3679 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3680 | |
| 3681 | { |
| 3682 | GURL url(test_server_.GetURL("auth-basic")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3683 | scoped_ptr<URLRequest> r(context.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3684 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3685 | base::RunLoop().Run(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3686 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3687 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 3688 | EXPECT_EQ(OK, r->status().error()); |
| 3689 | EXPECT_EQ(401, r->GetResponseCode()); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 3690 | EXPECT_FALSE(d.auth_required_called()); |
| 3691 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3692 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3693 | } |
| 3694 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3695 | } |
| 3696 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3697 | // Tests that we can handle when a network request was canceled while we were |
| 3698 | // waiting for the network delegate. |
| 3699 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 3700 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 3701 | ASSERT_TRUE(test_server_.Start()); |
| 3702 | |
| 3703 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3704 | BlockingNetworkDelegate network_delegate( |
| 3705 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3706 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3707 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3708 | TestURLRequestContext context(true); |
| 3709 | context.set_network_delegate(&network_delegate); |
| 3710 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3711 | |
| 3712 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3713 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3714 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3715 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3716 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3717 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3718 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 3719 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3720 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3721 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3722 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3723 | // Ensure that network delegate is notified. |
| 3724 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3725 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3726 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3727 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3728 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3729 | } |
| 3730 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3731 | } |
| 3732 | |
| 3733 | // Tests that we can handle when a network request was canceled while we were |
| 3734 | // waiting for the network delegate. |
| 3735 | // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 3736 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
| 3737 | ASSERT_TRUE(test_server_.Start()); |
| 3738 | |
| 3739 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3740 | BlockingNetworkDelegate network_delegate( |
| 3741 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3742 | network_delegate.set_block_on( |
| 3743 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3744 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3745 | TestURLRequestContext context(true); |
| 3746 | context.set_network_delegate(&network_delegate); |
| 3747 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3748 | |
| 3749 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3750 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3751 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3752 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3753 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3754 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3755 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 3756 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3757 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3758 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3759 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3760 | // Ensure that network delegate is notified. |
| 3761 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3762 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3763 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3764 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3765 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3766 | } |
| 3767 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3768 | } |
| 3769 | |
| 3770 | // Tests that we can handle when a network request was canceled while we were |
| 3771 | // waiting for the network delegate. |
| 3772 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 3773 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 3774 | ASSERT_TRUE(test_server_.Start()); |
| 3775 | |
| 3776 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3777 | BlockingNetworkDelegate network_delegate( |
| 3778 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3779 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3780 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3781 | TestURLRequestContext context(true); |
| 3782 | context.set_network_delegate(&network_delegate); |
| 3783 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3784 | |
| 3785 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3786 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3787 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3788 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3789 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3790 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3791 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 3792 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3793 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3794 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3795 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3796 | // Ensure that network delegate is notified. |
| 3797 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3798 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3799 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3800 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3801 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3802 | } |
| 3803 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3804 | } |
| 3805 | |
| 3806 | // Tests that we can handle when a network request was canceled while we were |
| 3807 | // waiting for the network delegate. |
| 3808 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 3809 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3810 | ASSERT_TRUE(test_server_.Start()); |
| 3811 | |
| 3812 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3813 | BlockingNetworkDelegate network_delegate( |
| 3814 | BlockingNetworkDelegate::USER_CALLBACK); |
| 3815 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3816 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 3817 | TestURLRequestContext context(true); |
| 3818 | context.set_network_delegate(&network_delegate); |
| 3819 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3820 | |
| 3821 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3822 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3823 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3824 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3825 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3826 | base::RunLoop().Run(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 3827 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 3828 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3829 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 3830 | // Cancel before callback. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3831 | r->Cancel(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3832 | // Ensure that network delegate is notified. |
| 3833 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3834 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
| 3835 | EXPECT_EQ(ERR_ABORTED, r->status().error()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 3836 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 3837 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 3838 | } |
| 3839 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 3840 | } |
| 3841 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3842 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 3843 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 3844 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3845 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3846 | ASSERT_TRUE(test_server_.Start()); |
| 3847 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3848 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 3849 | TestURLRequestContextWithProxy context( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 3850 | test_server_.host_port_pair().ToString(), &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3851 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3852 | TestDelegate d; |
| 3853 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3854 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3855 | GURL("https://www.server-auth.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3856 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3857 | r->Start(); |
| 3858 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3859 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3860 | base::RunLoop().Run(); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3861 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3862 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
[email protected] | d8fc472 | 2014-06-13 13:17:15 | [diff] [blame] | 3863 | // The proxy server is not set before failure. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3864 | EXPECT_TRUE(r->proxy_server().IsEmpty()); |
| 3865 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r->status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 3866 | } |
| 3867 | } |
| 3868 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3869 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3870 | ASSERT_TRUE(test_server_.Start()); |
| 3871 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3872 | TestDelegate d; |
| 3873 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3874 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3875 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3876 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3877 | r->Start(); |
| 3878 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3879 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3880 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3881 | |
| 3882 | EXPECT_EQ(1, d.response_started_count()); |
| 3883 | EXPECT_FALSE(d.received_data_before_response()); |
| 3884 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3885 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3886 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3887 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3888 | r->GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 3889 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 3890 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3891 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3892 | } |
| 3893 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3894 | // This test has the server send a large number of cookies to the client. |
| 3895 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 3896 | // search is used to estimate that maximum number of cookies that are accepted |
| 3897 | // by the browser. Beyond the maximum number, the request will fail with |
| 3898 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
[email protected] | 69dd6fe | 2013-02-23 23:15:30 | [diff] [blame] | 3899 | #if defined(OS_WIN) |
| 3900 | // http://crbug.com/177916 |
| 3901 | #define MAYBE_GetTest_ManyCookies DISABLED_GetTest_ManyCookies |
| 3902 | #else |
| 3903 | #define MAYBE_GetTest_ManyCookies GetTest_ManyCookies |
| 3904 | #endif // defined(OS_WIN) |
| 3905 | TEST_F(URLRequestTestHTTP, MAYBE_GetTest_ManyCookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3906 | ASSERT_TRUE(test_server_.Start()); |
| 3907 | |
| 3908 | int lower_bound = 0; |
| 3909 | int upper_bound = 1; |
| 3910 | |
| 3911 | // Double the number of cookies until the response header limits are |
| 3912 | // exceeded. |
| 3913 | while (DoManyCookiesRequest(upper_bound)) { |
| 3914 | lower_bound = upper_bound; |
| 3915 | upper_bound *= 2; |
| 3916 | ASSERT_LT(upper_bound, 1000000); |
| 3917 | } |
| 3918 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 3919 | int tolerance = static_cast<int>(upper_bound * 0.005); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 3920 | if (tolerance < 2) |
| 3921 | tolerance = 2; |
| 3922 | |
| 3923 | // Perform a binary search to find the highest possible number of cookies, |
| 3924 | // within the desired tolerance. |
| 3925 | while (upper_bound - lower_bound >= tolerance) { |
| 3926 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 3927 | |
| 3928 | if (DoManyCookiesRequest(num_cookies)) |
| 3929 | lower_bound = num_cookies; |
| 3930 | else |
| 3931 | upper_bound = num_cookies; |
| 3932 | } |
| 3933 | // Success: the test did not crash. |
| 3934 | } |
| 3935 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 3936 | TEST_F(URLRequestTestHTTP, GetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3937 | ASSERT_TRUE(test_server_.Start()); |
| 3938 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3939 | TestDelegate d; |
| 3940 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3941 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3942 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3943 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3944 | r->Start(); |
| 3945 | EXPECT_TRUE(r->is_pending()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3946 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3947 | base::RunLoop().Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3948 | |
| 3949 | EXPECT_EQ(1, d.response_started_count()); |
| 3950 | EXPECT_FALSE(d.received_data_before_response()); |
| 3951 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3952 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3953 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3954 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3955 | r->GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 3956 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 3957 | } |
| 3958 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3959 | TEST_F(URLRequestTestHTTP, GetTest_GetFullRequestHeaders) { |
| 3960 | ASSERT_TRUE(test_server_.Start()); |
| 3961 | |
| 3962 | TestDelegate d; |
| 3963 | { |
| 3964 | GURL test_url(test_server_.GetURL(std::string())); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3965 | scoped_ptr<URLRequest> r( |
| 3966 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3967 | |
| 3968 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3969 | EXPECT_FALSE(r->GetFullRequestHeaders(&headers)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3970 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3971 | r->Start(); |
| 3972 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3973 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 3974 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3975 | |
| 3976 | EXPECT_EQ(1, d.response_started_count()); |
| 3977 | EXPECT_FALSE(d.received_data_before_response()); |
| 3978 | EXPECT_NE(0, d.bytes_received()); |
| 3979 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3980 | r->GetSocketAddress().host()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3981 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3982 | r->GetSocketAddress().port()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 3983 | |
| 3984 | EXPECT_TRUE(d.have_full_request_headers()); |
| 3985 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 3986 | } |
| 3987 | } |
| 3988 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3989 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
| 3990 | ASSERT_TRUE(test_server_.Start()); |
| 3991 | |
| 3992 | TestDelegate d; |
| 3993 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3994 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 3995 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3996 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 3997 | r->Start(); |
| 3998 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3999 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4000 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4001 | |
| 4002 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4003 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4004 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4005 | |
| 4006 | EXPECT_EQ(1, d.response_started_count()); |
| 4007 | EXPECT_FALSE(d.received_data_before_response()); |
| 4008 | EXPECT_NE(0, d.bytes_received()); |
| 4009 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4010 | r->GetSocketAddress().host()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4011 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4012 | r->GetSocketAddress().port()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4013 | } |
| 4014 | } |
| 4015 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4016 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
| 4017 | ASSERT_TRUE(test_server_.Start()); |
| 4018 | |
| 4019 | // Parameter that specifies the Content-Length field in the response: |
| 4020 | // C - Compressed length. |
| 4021 | // U - Uncompressed length. |
| 4022 | // L - Large length (larger than both C & U). |
| 4023 | // M - Medium length (between C & U). |
| 4024 | // S - Small length (smaller than both C & U). |
| 4025 | const char test_parameters[] = "CULMS"; |
| 4026 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 4027 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 4028 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4029 | // S has too little data, but we seem to accept it. |
| 4030 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 4031 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4032 | |
| 4033 | for (int i = 0; i < num_tests ; i++) { |
| 4034 | TestDelegate d; |
| 4035 | { |
| 4036 | std::string test_file = |
| 4037 | base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c", |
| 4038 | test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4039 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4040 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4041 | TestURLRequestContext context(true); |
| 4042 | context.set_network_delegate(&network_delegate); |
| 4043 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4044 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4045 | scoped_ptr<URLRequest> r(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4046 | test_server_.GetURL(test_file), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4047 | r->Start(); |
| 4048 | EXPECT_TRUE(r->is_pending()); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4049 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4050 | base::RunLoop().Run(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4051 | |
| 4052 | EXPECT_EQ(1, d.response_started_count()); |
| 4053 | EXPECT_FALSE(d.received_data_before_response()); |
| 4054 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4055 | << " status = " << r->status().status() |
| 4056 | << " error = " << r->status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4057 | if (test_expect_success[i]) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4058 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4059 | << " Parameter = \"" << test_file << "\""; |
| 4060 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4061 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 4062 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r->status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 4063 | << " Parameter = \"" << test_file << "\""; |
| 4064 | } |
| 4065 | } |
| 4066 | } |
| 4067 | } |
| 4068 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4069 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
| 4070 | ASSERT_TRUE(test_server_.Start()); |
| 4071 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4072 | GURL destination_url = test_server_.GetURL(std::string()); |
| 4073 | GURL original_url = |
| 4074 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4075 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4076 | scoped_ptr<URLRequest> req( |
| 4077 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4078 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4079 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4080 | |
| 4081 | EXPECT_EQ(1, d.response_started_count()); |
| 4082 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4083 | EXPECT_EQ(destination_url, req->url()); |
| 4084 | EXPECT_EQ(original_url, req->original_url()); |
| 4085 | ASSERT_EQ(2U, req->url_chain().size()); |
| 4086 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4087 | EXPECT_EQ(destination_url, req->url_chain()[1]); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4088 | |
| 4089 | LoadTimingInfo load_timing_info_before_redirect; |
| 4090 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 4091 | &load_timing_info_before_redirect)); |
| 4092 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 4093 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 4094 | |
| 4095 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4096 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 4097 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 4098 | |
| 4099 | // Check that a new socket was used on redirect, since the server does not |
| 4100 | // supposed keep-alive sockets, and that the times before the redirect are |
| 4101 | // before the ones recorded for the second request. |
| 4102 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 4103 | load_timing_info.socket_log_id); |
| 4104 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 4105 | load_timing_info.connect_timing.connect_start); |
| 4106 | } |
| 4107 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4108 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
| 4109 | ASSERT_TRUE(test_server_.Start()); |
| 4110 | |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 4111 | GURL destination_url = test_server_.GetURL(std::string()); |
| 4112 | GURL middle_redirect_url = |
| 4113 | test_server_.GetURL("server-redirect?" + destination_url.spec()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4114 | GURL original_url = test_server_.GetURL( |
| 4115 | "server-redirect?" + middle_redirect_url.spec()); |
| 4116 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4117 | scoped_ptr<URLRequest> req( |
| 4118 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4119 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4120 | base::RunLoop().Run(); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4121 | |
| 4122 | EXPECT_EQ(1, d.response_started_count()); |
| 4123 | EXPECT_EQ(2, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4124 | EXPECT_EQ(destination_url, req->url()); |
| 4125 | EXPECT_EQ(original_url, req->original_url()); |
| 4126 | ASSERT_EQ(3U, req->url_chain().size()); |
| 4127 | EXPECT_EQ(original_url, req->url_chain()[0]); |
| 4128 | EXPECT_EQ(middle_redirect_url, req->url_chain()[1]); |
| 4129 | EXPECT_EQ(destination_url, req->url_chain()[2]); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 4130 | } |
| 4131 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4132 | // First and second pieces of information logged by delegates to URLRequests. |
| 4133 | const char kFirstDelegateInfo[] = "Wonderful delegate"; |
| 4134 | const char kSecondDelegateInfo[] = "Exciting delegate"; |
| 4135 | |
| 4136 | // Logs delegate information to a URLRequest. The first string is logged |
| 4137 | // synchronously on Start(), using DELEGATE_INFO_DEBUG_ONLY. The second is |
| 4138 | // logged asynchronously, using DELEGATE_INFO_DISPLAY_TO_USER. Then |
| 4139 | // another asynchronous call is used to clear the delegate information |
| 4140 | // before calling a callback. The object then deletes itself. |
| 4141 | class AsyncDelegateLogger : public base::RefCounted<AsyncDelegateLogger> { |
| 4142 | public: |
| 4143 | typedef base::Callback<void()> Callback; |
| 4144 | |
| 4145 | // Each time delegate information is added to the URLRequest, the resulting |
| 4146 | // load state is checked. The expected load state after each request is |
| 4147 | // passed in as an argument. |
| 4148 | static void Run(URLRequest* url_request, |
| 4149 | LoadState expected_first_load_state, |
| 4150 | LoadState expected_second_load_state, |
| 4151 | LoadState expected_third_load_state, |
| 4152 | const Callback& callback) { |
| 4153 | AsyncDelegateLogger* logger = new AsyncDelegateLogger( |
| 4154 | url_request, |
| 4155 | expected_first_load_state, |
| 4156 | expected_second_load_state, |
| 4157 | expected_third_load_state, |
| 4158 | callback); |
| 4159 | logger->Start(); |
| 4160 | } |
| 4161 | |
| 4162 | // Checks that the log entries, starting with log_position, contain the |
| 4163 | // DELEGATE_INFO NetLog events that an AsyncDelegateLogger should have |
| 4164 | // recorded. Returns the index of entry after the expected number of |
| 4165 | // events this logged, or entries.size() if there aren't enough entries. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4166 | static size_t CheckDelegateInfo(const TestNetLog::CapturedEntryList& entries, |
| 4167 | size_t log_position) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4168 | // There should be 4 DELEGATE_INFO events: Two begins and two ends. |
| 4169 | if (log_position + 3 >= entries.size()) { |
| 4170 | ADD_FAILURE() << "Not enough log entries"; |
| 4171 | return entries.size(); |
| 4172 | } |
| 4173 | std::string delegate_info; |
| 4174 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4175 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4176 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4177 | &delegate_info)); |
| 4178 | EXPECT_EQ(kFirstDelegateInfo, delegate_info); |
| 4179 | |
| 4180 | ++log_position; |
| 4181 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4182 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4183 | |
| 4184 | ++log_position; |
| 4185 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4186 | EXPECT_EQ(NetLog::PHASE_BEGIN, entries[log_position].phase); |
| 4187 | EXPECT_TRUE(entries[log_position].GetStringValue("delegate_info", |
| 4188 | &delegate_info)); |
| 4189 | EXPECT_EQ(kSecondDelegateInfo, delegate_info); |
| 4190 | |
| 4191 | ++log_position; |
| 4192 | EXPECT_EQ(NetLog::TYPE_DELEGATE_INFO, entries[log_position].type); |
| 4193 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4194 | |
| 4195 | return log_position + 1; |
| 4196 | } |
| 4197 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4198 | // Find delegate request begin and end messages for OnBeforeNetworkStart. |
| 4199 | // Returns the position of the end message. |
| 4200 | static size_t ExpectBeforeNetworkEvents( |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4201 | const TestNetLog::CapturedEntryList& entries, |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4202 | size_t log_position) { |
| 4203 | log_position = |
| 4204 | ExpectLogContainsSomewhereAfter(entries, |
| 4205 | log_position, |
| 4206 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4207 | NetLog::PHASE_BEGIN); |
| 4208 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4209 | entries[log_position + 1].type); |
| 4210 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position + 1].phase); |
| 4211 | return log_position + 1; |
| 4212 | } |
| 4213 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4214 | private: |
| 4215 | friend class base::RefCounted<AsyncDelegateLogger>; |
| 4216 | |
| 4217 | AsyncDelegateLogger(URLRequest* url_request, |
| 4218 | LoadState expected_first_load_state, |
| 4219 | LoadState expected_second_load_state, |
| 4220 | LoadState expected_third_load_state, |
| 4221 | const Callback& callback) |
| 4222 | : url_request_(url_request), |
| 4223 | expected_first_load_state_(expected_first_load_state), |
| 4224 | expected_second_load_state_(expected_second_load_state), |
| 4225 | expected_third_load_state_(expected_third_load_state), |
| 4226 | callback_(callback) { |
| 4227 | } |
| 4228 | |
| 4229 | ~AsyncDelegateLogger() {} |
| 4230 | |
| 4231 | void Start() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4232 | url_request_->LogBlockedBy(kFirstDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4233 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4234 | EXPECT_EQ(expected_first_load_state_, load_state.state); |
| 4235 | EXPECT_NE(ASCIIToUTF16(kFirstDelegateInfo), load_state.param); |
| 4236 | base::MessageLoop::current()->PostTask( |
| 4237 | FROM_HERE, |
| 4238 | base::Bind(&AsyncDelegateLogger::LogSecondDelegate, this)); |
| 4239 | } |
| 4240 | |
| 4241 | void LogSecondDelegate() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4242 | url_request_->LogAndReportBlockedBy(kSecondDelegateInfo); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4243 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4244 | EXPECT_EQ(expected_second_load_state_, load_state.state); |
| 4245 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) { |
| 4246 | EXPECT_EQ(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4247 | } else { |
| 4248 | EXPECT_NE(ASCIIToUTF16(kSecondDelegateInfo), load_state.param); |
| 4249 | } |
| 4250 | base::MessageLoop::current()->PostTask( |
| 4251 | FROM_HERE, |
| 4252 | base::Bind(&AsyncDelegateLogger::LogComplete, this)); |
| 4253 | } |
| 4254 | |
| 4255 | void LogComplete() { |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 4256 | url_request_->LogUnblocked(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4257 | LoadStateWithParam load_state = url_request_->GetLoadState(); |
| 4258 | EXPECT_EQ(expected_third_load_state_, load_state.state); |
| 4259 | if (expected_second_load_state_ == LOAD_STATE_WAITING_FOR_DELEGATE) |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4260 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4261 | callback_.Run(); |
| 4262 | } |
| 4263 | |
| 4264 | URLRequest* url_request_; |
| 4265 | const int expected_first_load_state_; |
| 4266 | const int expected_second_load_state_; |
| 4267 | const int expected_third_load_state_; |
| 4268 | const Callback callback_; |
| 4269 | |
| 4270 | DISALLOW_COPY_AND_ASSIGN(AsyncDelegateLogger); |
| 4271 | }; |
| 4272 | |
| 4273 | // NetworkDelegate that logs delegate information before a request is started, |
| 4274 | // before headers are sent, when headers are read, and when auth information |
| 4275 | // is requested. Uses AsyncDelegateLogger. |
| 4276 | class AsyncLoggingNetworkDelegate : public TestNetworkDelegate { |
| 4277 | public: |
| 4278 | AsyncLoggingNetworkDelegate() {} |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4279 | ~AsyncLoggingNetworkDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4280 | |
| 4281 | // NetworkDelegate implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4282 | int OnBeforeURLRequest(URLRequest* request, |
| 4283 | const CompletionCallback& callback, |
| 4284 | GURL* new_url) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4285 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 4286 | return RunCallbackAsynchronously(request, callback); |
| 4287 | } |
| 4288 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4289 | int OnBeforeSendHeaders(URLRequest* request, |
| 4290 | const CompletionCallback& callback, |
| 4291 | HttpRequestHeaders* headers) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4292 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 4293 | return RunCallbackAsynchronously(request, callback); |
| 4294 | } |
| 4295 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4296 | int OnHeadersReceived( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4297 | URLRequest* request, |
| 4298 | const CompletionCallback& callback, |
| 4299 | const HttpResponseHeaders* original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4300 | scoped_refptr<HttpResponseHeaders>* override_response_headers, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4301 | GURL* allowed_unsafe_redirect_url) override { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4302 | TestNetworkDelegate::OnHeadersReceived(request, |
| 4303 | callback, |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4304 | original_response_headers, |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 4305 | override_response_headers, |
| 4306 | allowed_unsafe_redirect_url); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4307 | return RunCallbackAsynchronously(request, callback); |
| 4308 | } |
| 4309 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4310 | NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4311 | URLRequest* request, |
| 4312 | const AuthChallengeInfo& auth_info, |
| 4313 | const AuthCallback& callback, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 4314 | AuthCredentials* credentials) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4315 | AsyncDelegateLogger::Run( |
| 4316 | request, |
| 4317 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4318 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4319 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4320 | base::Bind(&AsyncLoggingNetworkDelegate::SetAuthAndResume, |
| 4321 | callback, credentials)); |
| 4322 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 4323 | } |
| 4324 | |
| 4325 | private: |
| 4326 | static int RunCallbackAsynchronously( |
| 4327 | URLRequest* request, |
| 4328 | const CompletionCallback& callback) { |
| 4329 | AsyncDelegateLogger::Run( |
| 4330 | request, |
| 4331 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4332 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4333 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4334 | base::Bind(callback, OK)); |
| 4335 | return ERR_IO_PENDING; |
| 4336 | } |
| 4337 | |
| 4338 | static void SetAuthAndResume(const AuthCallback& callback, |
| 4339 | AuthCredentials* credentials) { |
| 4340 | *credentials = AuthCredentials(kUser, kSecret); |
| 4341 | callback.Run(NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 4342 | } |
| 4343 | |
| 4344 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingNetworkDelegate); |
| 4345 | }; |
| 4346 | |
| 4347 | // URLRequest::Delegate that logs delegate information when the headers |
| 4348 | // are received, when each read completes, and during redirects. Uses |
| 4349 | // AsyncDelegateLogger. Can optionally cancel a request in any phase. |
| 4350 | // |
| 4351 | // Inherits from TestDelegate to reuse the TestDelegate code to handle |
| 4352 | // advancing to the next step in most cases, as well as cancellation. |
| 4353 | class AsyncLoggingUrlRequestDelegate : public TestDelegate { |
| 4354 | public: |
| 4355 | enum CancelStage { |
| 4356 | NO_CANCEL = 0, |
| 4357 | CANCEL_ON_RECEIVED_REDIRECT, |
| 4358 | CANCEL_ON_RESPONSE_STARTED, |
| 4359 | CANCEL_ON_READ_COMPLETED |
| 4360 | }; |
| 4361 | |
| 4362 | explicit AsyncLoggingUrlRequestDelegate(CancelStage cancel_stage) |
| 4363 | : cancel_stage_(cancel_stage) { |
| 4364 | if (cancel_stage == CANCEL_ON_RECEIVED_REDIRECT) |
| 4365 | set_cancel_in_received_redirect(true); |
| 4366 | else if (cancel_stage == CANCEL_ON_RESPONSE_STARTED) |
| 4367 | set_cancel_in_response_started(true); |
| 4368 | else if (cancel_stage == CANCEL_ON_READ_COMPLETED) |
| 4369 | set_cancel_in_received_data(true); |
| 4370 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4371 | ~AsyncLoggingUrlRequestDelegate() override {} |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4372 | |
| 4373 | // URLRequest::Delegate implementation: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4374 | void OnReceivedRedirect(URLRequest* request, |
| 4375 | const RedirectInfo& redirect_info, |
| 4376 | bool* defer_redirect) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4377 | *defer_redirect = true; |
| 4378 | AsyncDelegateLogger::Run( |
| 4379 | request, |
| 4380 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4381 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4382 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4383 | base::Bind( |
| 4384 | &AsyncLoggingUrlRequestDelegate::OnReceivedRedirectLoggingComplete, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4385 | base::Unretained(this), request, redirect_info)); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4386 | } |
| 4387 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4388 | void OnResponseStarted(URLRequest* request) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4389 | AsyncDelegateLogger::Run( |
| 4390 | request, |
| 4391 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4392 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4393 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4394 | base::Bind( |
| 4395 | &AsyncLoggingUrlRequestDelegate::OnResponseStartedLoggingComplete, |
| 4396 | base::Unretained(this), request)); |
| 4397 | } |
| 4398 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4399 | void OnReadCompleted(URLRequest* request, int bytes_read) override { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4400 | AsyncDelegateLogger::Run( |
| 4401 | request, |
| 4402 | LOAD_STATE_IDLE, |
| 4403 | LOAD_STATE_IDLE, |
| 4404 | LOAD_STATE_IDLE, |
| 4405 | base::Bind( |
| 4406 | &AsyncLoggingUrlRequestDelegate::AfterReadCompletedLoggingComplete, |
| 4407 | base::Unretained(this), request, bytes_read)); |
| 4408 | } |
| 4409 | |
| 4410 | private: |
| 4411 | void OnReceivedRedirectLoggingComplete(URLRequest* request, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4412 | const RedirectInfo& redirect_info) { |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4413 | bool defer_redirect = false; |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4414 | TestDelegate::OnReceivedRedirect(request, redirect_info, &defer_redirect); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4415 | // FollowDeferredRedirect should not be called after cancellation. |
| 4416 | if (cancel_stage_ == CANCEL_ON_RECEIVED_REDIRECT) |
| 4417 | return; |
| 4418 | if (!defer_redirect) |
| 4419 | request->FollowDeferredRedirect(); |
| 4420 | } |
| 4421 | |
| 4422 | void OnResponseStartedLoggingComplete(URLRequest* request) { |
| 4423 | // The parent class continues the request. |
| 4424 | TestDelegate::OnResponseStarted(request); |
| 4425 | } |
| 4426 | |
| 4427 | void AfterReadCompletedLoggingComplete(URLRequest* request, int bytes_read) { |
| 4428 | // The parent class continues the request. |
| 4429 | TestDelegate::OnReadCompleted(request, bytes_read); |
| 4430 | } |
| 4431 | |
| 4432 | const CancelStage cancel_stage_; |
| 4433 | |
| 4434 | DISALLOW_COPY_AND_ASSIGN(AsyncLoggingUrlRequestDelegate); |
| 4435 | }; |
| 4436 | |
| 4437 | // Tests handling of delegate info before a request starts. |
| 4438 | TEST_F(URLRequestTestHTTP, DelegateInfoBeforeStart) { |
| 4439 | ASSERT_TRUE(test_server_.Start()); |
| 4440 | |
| 4441 | TestDelegate request_delegate; |
| 4442 | TestURLRequestContext context(true); |
| 4443 | context.set_network_delegate(NULL); |
| 4444 | context.set_net_log(&net_log_); |
| 4445 | context.Init(); |
| 4446 | |
| 4447 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4448 | scoped_ptr<URLRequest> r( |
| 4449 | context.CreateRequest(test_server_.GetURL("empty.html"), |
| 4450 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4451 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4452 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4453 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4454 | |
| 4455 | AsyncDelegateLogger::Run( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4456 | r.get(), |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4457 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4458 | LOAD_STATE_WAITING_FOR_DELEGATE, |
| 4459 | LOAD_STATE_IDLE, |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4460 | base::Bind(&URLRequest::Start, base::Unretained(r.get()))); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4461 | |
| 4462 | base::RunLoop().Run(); |
| 4463 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4464 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4465 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4466 | } |
| 4467 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4468 | TestNetLog::CapturedEntryList entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4469 | net_log_.GetEntries(&entries); |
| 4470 | size_t log_position = ExpectLogContainsSomewhereAfter( |
| 4471 | entries, |
| 4472 | 0, |
| 4473 | NetLog::TYPE_DELEGATE_INFO, |
| 4474 | NetLog::PHASE_BEGIN); |
| 4475 | |
| 4476 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, log_position); |
| 4477 | |
| 4478 | // Nothing else should add any delegate info to the request. |
| 4479 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4480 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4481 | } |
| 4482 | |
| 4483 | // Tests handling of delegate info from a network delegate. |
| 4484 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfo) { |
| 4485 | ASSERT_TRUE(test_server_.Start()); |
| 4486 | |
| 4487 | TestDelegate request_delegate; |
| 4488 | AsyncLoggingNetworkDelegate network_delegate; |
| 4489 | TestURLRequestContext context(true); |
| 4490 | context.set_network_delegate(&network_delegate); |
| 4491 | context.set_net_log(&net_log_); |
| 4492 | context.Init(); |
| 4493 | |
| 4494 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4495 | scoped_ptr<URLRequest> r( |
| 4496 | context.CreateRequest(test_server_.GetURL("simple.html"), |
| 4497 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4498 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4499 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4500 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4501 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4502 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4503 | base::RunLoop().Run(); |
| 4504 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4505 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4506 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4507 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4508 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4509 | } |
| 4510 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4511 | |
| 4512 | size_t log_position = 0; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4513 | TestNetLog::CapturedEntryList entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4514 | net_log_.GetEntries(&entries); |
| 4515 | for (size_t i = 0; i < 3; ++i) { |
| 4516 | log_position = ExpectLogContainsSomewhereAfter( |
| 4517 | entries, |
| 4518 | log_position + 1, |
| 4519 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4520 | NetLog::PHASE_BEGIN); |
| 4521 | |
| 4522 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4523 | log_position + 1); |
| 4524 | |
| 4525 | ASSERT_LT(log_position, entries.size()); |
| 4526 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4527 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4528 | |
| 4529 | if (i == 1) { |
| 4530 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4531 | entries, log_position + 1); |
| 4532 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4536 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4537 | } |
| 4538 | |
| 4539 | // Tests handling of delegate info from a network delegate in the case of an |
| 4540 | // HTTP redirect. |
| 4541 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoRedirect) { |
| 4542 | ASSERT_TRUE(test_server_.Start()); |
| 4543 | |
| 4544 | TestDelegate request_delegate; |
| 4545 | AsyncLoggingNetworkDelegate network_delegate; |
| 4546 | TestURLRequestContext context(true); |
| 4547 | context.set_network_delegate(&network_delegate); |
| 4548 | context.set_net_log(&net_log_); |
| 4549 | context.Init(); |
| 4550 | |
| 4551 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4552 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4553 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4554 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4555 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4556 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4557 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4558 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4559 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4560 | base::RunLoop().Run(); |
| 4561 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4562 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4563 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4564 | EXPECT_EQ(2, network_delegate.created_requests()); |
| 4565 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4566 | } |
| 4567 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4568 | |
| 4569 | size_t log_position = 0; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4570 | TestNetLog::CapturedEntryList entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4571 | net_log_.GetEntries(&entries); |
| 4572 | // The NetworkDelegate logged information in OnBeforeURLRequest, |
| 4573 | // OnBeforeSendHeaders, and OnHeadersReceived. |
| 4574 | for (size_t i = 0; i < 3; ++i) { |
| 4575 | log_position = ExpectLogContainsSomewhereAfter( |
| 4576 | entries, |
| 4577 | log_position + 1, |
| 4578 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4579 | NetLog::PHASE_BEGIN); |
| 4580 | |
| 4581 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4582 | log_position + 1); |
| 4583 | |
| 4584 | ASSERT_LT(log_position, entries.size()); |
| 4585 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4586 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4587 | |
| 4588 | if (i == 1) { |
| 4589 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4590 | entries, log_position + 1); |
| 4591 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4592 | } |
| 4593 | |
| 4594 | // The URLRequest::Delegate then gets informed about the redirect. |
| 4595 | log_position = ExpectLogContainsSomewhereAfter( |
| 4596 | entries, |
| 4597 | log_position + 1, |
| 4598 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4599 | NetLog::PHASE_BEGIN); |
| 4600 | |
| 4601 | // The NetworkDelegate logged information in the same three events as before. |
| 4602 | for (size_t i = 0; i < 3; ++i) { |
| 4603 | log_position = ExpectLogContainsSomewhereAfter( |
| 4604 | entries, |
| 4605 | log_position + 1, |
| 4606 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4607 | NetLog::PHASE_BEGIN); |
| 4608 | |
| 4609 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4610 | log_position + 1); |
| 4611 | |
| 4612 | ASSERT_LT(log_position, entries.size()); |
| 4613 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4614 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4615 | } |
| 4616 | |
| 4617 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4618 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4619 | } |
| 4620 | |
| 4621 | // Tests handling of delegate info from a network delegate in the case of HTTP |
| 4622 | // AUTH. |
| 4623 | TEST_F(URLRequestTestHTTP, NetworkDelegateInfoAuth) { |
| 4624 | ASSERT_TRUE(test_server_.Start()); |
| 4625 | |
| 4626 | TestDelegate request_delegate; |
| 4627 | AsyncLoggingNetworkDelegate network_delegate; |
| 4628 | TestURLRequestContext context(true); |
| 4629 | context.set_network_delegate(&network_delegate); |
| 4630 | context.set_net_log(&net_log_); |
| 4631 | context.Init(); |
| 4632 | |
| 4633 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4634 | scoped_ptr<URLRequest> r( |
| 4635 | context.CreateRequest(test_server_.GetURL("auth-basic"), |
| 4636 | DEFAULT_PRIORITY, &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4637 | LoadStateWithParam load_state = r->GetLoadState(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4638 | EXPECT_EQ(LOAD_STATE_IDLE, load_state.state); |
[email protected] | 754bd20 | 2013-12-18 08:29:08 | [diff] [blame] | 4639 | EXPECT_EQ(base::string16(), load_state.param); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4640 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4641 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4642 | base::RunLoop().Run(); |
| 4643 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4644 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4645 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4646 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 4647 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 4648 | } |
| 4649 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 4650 | |
| 4651 | size_t log_position = 0; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4652 | TestNetLog::CapturedEntryList entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4653 | net_log_.GetEntries(&entries); |
| 4654 | // The NetworkDelegate should have logged information in OnBeforeURLRequest, |
| 4655 | // OnBeforeSendHeaders, OnHeadersReceived, OnAuthRequired, and then again in |
| 4656 | // OnBeforeURLRequest and OnBeforeSendHeaders. |
| 4657 | for (size_t i = 0; i < 6; ++i) { |
| 4658 | log_position = ExpectLogContainsSomewhereAfter( |
| 4659 | entries, |
| 4660 | log_position + 1, |
| 4661 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4662 | NetLog::PHASE_BEGIN); |
| 4663 | |
| 4664 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4665 | log_position + 1); |
| 4666 | |
| 4667 | ASSERT_LT(log_position, entries.size()); |
| 4668 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4669 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4670 | |
| 4671 | if (i == 1) { |
| 4672 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4673 | entries, log_position + 1); |
| 4674 | } |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4675 | } |
| 4676 | |
| 4677 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4678 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4679 | } |
| 4680 | |
| 4681 | // Tests handling of delegate info from a URLRequest::Delegate. |
| 4682 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfo) { |
| 4683 | ASSERT_TRUE(test_server_.Start()); |
| 4684 | |
| 4685 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4686 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4687 | TestURLRequestContext context(true); |
| 4688 | context.set_network_delegate(NULL); |
| 4689 | context.set_net_log(&net_log_); |
| 4690 | context.Init(); |
| 4691 | |
| 4692 | { |
| 4693 | // A chunked response with delays between chunks is used to make sure that |
| 4694 | // attempts by the URLRequest delegate to log information while reading the |
| 4695 | // body are ignored. Since they are ignored, this test is robust against |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4696 | // the possibility of multiple reads being combined in the unlikely event |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4697 | // that it occurs. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4698 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4699 | test_server_.GetURL("chunked?waitBetweenChunks=20"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4700 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4701 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4702 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4703 | base::RunLoop().Run(); |
| 4704 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4705 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4706 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4707 | } |
| 4708 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4709 | TestNetLog::CapturedEntryList entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4710 | net_log_.GetEntries(&entries); |
| 4711 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4712 | size_t log_position = 0; |
| 4713 | |
| 4714 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4715 | entries, log_position); |
| 4716 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4717 | // The delegate info should only have been logged on header complete. Other |
| 4718 | // times it should silently be ignored. |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4719 | log_position = |
| 4720 | ExpectLogContainsSomewhereAfter(entries, |
| 4721 | log_position + 1, |
| 4722 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4723 | NetLog::PHASE_BEGIN); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4724 | |
| 4725 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4726 | log_position + 1); |
| 4727 | |
| 4728 | ASSERT_LT(log_position, entries.size()); |
| 4729 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4730 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4731 | |
| 4732 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4733 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4734 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4735 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4736 | } |
| 4737 | |
| 4738 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4739 | // an HTTP redirect. |
| 4740 | TEST_F(URLRequestTestHTTP, URLRequestDelegateInfoOnRedirect) { |
| 4741 | ASSERT_TRUE(test_server_.Start()); |
| 4742 | |
| 4743 | AsyncLoggingUrlRequestDelegate request_delegate( |
| 4744 | AsyncLoggingUrlRequestDelegate::NO_CANCEL); |
| 4745 | TestURLRequestContext context(true); |
| 4746 | context.set_network_delegate(NULL); |
| 4747 | context.set_net_log(&net_log_); |
| 4748 | context.Init(); |
| 4749 | |
| 4750 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4751 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4752 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4753 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4754 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4755 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4756 | base::RunLoop().Run(); |
| 4757 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4758 | EXPECT_EQ(200, r->GetResponseCode()); |
| 4759 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4760 | } |
| 4761 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4762 | TestNetLog::CapturedEntryList entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4763 | net_log_.GetEntries(&entries); |
| 4764 | |
| 4765 | // Delegate info should only have been logged in OnReceivedRedirect and |
| 4766 | // OnResponseStarted. |
| 4767 | size_t log_position = 0; |
| 4768 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4769 | if (i == 0) { |
| 4770 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4771 | entries, log_position) + 1; |
| 4772 | } |
| 4773 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4774 | log_position = ExpectLogContainsSomewhereAfter( |
| 4775 | entries, |
| 4776 | log_position, |
| 4777 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4778 | NetLog::PHASE_BEGIN); |
| 4779 | |
| 4780 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4781 | log_position + 1); |
| 4782 | |
| 4783 | ASSERT_LT(log_position, entries.size()); |
| 4784 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4785 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4786 | } |
| 4787 | |
| 4788 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4789 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4790 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4791 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4792 | } |
| 4793 | |
| 4794 | // Tests handling of delegate info from a URLRequest::Delegate in the case of |
| 4795 | // an HTTP redirect, with cancellation at various points. |
| 4796 | TEST_F(URLRequestTestHTTP, URLRequestDelegateOnRedirectCancelled) { |
| 4797 | ASSERT_TRUE(test_server_.Start()); |
| 4798 | |
| 4799 | const AsyncLoggingUrlRequestDelegate::CancelStage kCancelStages[] = { |
| 4800 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RECEIVED_REDIRECT, |
| 4801 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_RESPONSE_STARTED, |
| 4802 | AsyncLoggingUrlRequestDelegate::CANCEL_ON_READ_COMPLETED, |
| 4803 | }; |
| 4804 | |
| 4805 | for (size_t test_case = 0; test_case < arraysize(kCancelStages); |
| 4806 | ++test_case) { |
| 4807 | AsyncLoggingUrlRequestDelegate request_delegate(kCancelStages[test_case]); |
| 4808 | TestURLRequestContext context(true); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4809 | TestNetLog net_log; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4810 | context.set_network_delegate(NULL); |
| 4811 | context.set_net_log(&net_log); |
| 4812 | context.Init(); |
| 4813 | |
| 4814 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4815 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 4816 | test_server_.GetURL("server-redirect?simple.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4817 | &request_delegate)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4818 | LoadStateWithParam load_state = r->GetLoadState(); |
| 4819 | r->Start(); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4820 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4821 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4822 | } |
| 4823 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 4824 | TestNetLog::CapturedEntryList entries; |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4825 | net_log.GetEntries(&entries); |
| 4826 | |
| 4827 | // Delegate info is always logged in both OnReceivedRedirect and |
| 4828 | // OnResponseStarted. In the CANCEL_ON_RECEIVED_REDIRECT, the |
| 4829 | // OnResponseStarted delegate call is after cancellation, but logging is |
| 4830 | // still currently supported in that call. |
| 4831 | size_t log_position = 0; |
| 4832 | for (int i = 0; i < 2; ++i) { |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 4833 | if (i == 0) { |
| 4834 | log_position = AsyncDelegateLogger::ExpectBeforeNetworkEvents( |
| 4835 | entries, log_position) + 1; |
| 4836 | } |
| 4837 | |
[email protected] | abe1c4a | 2013-10-25 19:28:51 | [diff] [blame] | 4838 | log_position = ExpectLogContainsSomewhereAfter( |
| 4839 | entries, |
| 4840 | log_position, |
| 4841 | NetLog::TYPE_URL_REQUEST_DELEGATE, |
| 4842 | NetLog::PHASE_BEGIN); |
| 4843 | |
| 4844 | log_position = AsyncDelegateLogger::CheckDelegateInfo(entries, |
| 4845 | log_position + 1); |
| 4846 | |
| 4847 | ASSERT_LT(log_position, entries.size()); |
| 4848 | EXPECT_EQ(NetLog::TYPE_URL_REQUEST_DELEGATE, entries[log_position].type); |
| 4849 | EXPECT_EQ(NetLog::PHASE_END, entries[log_position].phase); |
| 4850 | } |
| 4851 | |
| 4852 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4853 | entries, log_position + 1, NetLog::TYPE_DELEGATE_INFO)); |
| 4854 | EXPECT_FALSE(LogContainsEntryWithTypeAfter( |
| 4855 | entries, log_position + 1, NetLog::TYPE_URL_REQUEST_DELEGATE)); |
| 4856 | } |
| 4857 | } |
| 4858 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4859 | namespace { |
| 4860 | |
| 4861 | const char kExtraHeader[] = "Allow-Snafu"; |
| 4862 | const char kExtraValue[] = "fubar"; |
| 4863 | |
| 4864 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4865 | void OnReceivedRedirect(URLRequest* request, |
| 4866 | const RedirectInfo& redirect_info, |
| 4867 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4868 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4869 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 4870 | } |
| 4871 | }; |
| 4872 | |
| 4873 | } // namespace |
| 4874 | |
| 4875 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
| 4876 | ASSERT_TRUE(test_server_.Start()); |
| 4877 | |
| 4878 | GURL destination_url = test_server_.GetURL( |
| 4879 | "echoheader?" + std::string(kExtraHeader)); |
| 4880 | GURL original_url = test_server_.GetURL( |
| 4881 | "server-redirect?" + destination_url.spec()); |
| 4882 | RedirectWithAdditionalHeadersDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4883 | scoped_ptr<URLRequest> req( |
| 4884 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4885 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4886 | base::RunLoop().Run(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4887 | |
| 4888 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4889 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4890 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 4891 | EXPECT_EQ(kExtraValue, value); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4892 | EXPECT_FALSE(req->is_pending()); |
| 4893 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 4894 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 4895 | } |
| 4896 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4897 | namespace { |
| 4898 | |
| 4899 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 4900 | |
| 4901 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 4902 | void OnReceivedRedirect(URLRequest* request, |
| 4903 | const RedirectInfo& redirect_info, |
| 4904 | bool* defer_redirect) override { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 4905 | TestDelegate::OnReceivedRedirect(request, redirect_info, defer_redirect); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4906 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 4907 | } |
| 4908 | }; |
| 4909 | |
| 4910 | } // namespace |
| 4911 | |
| 4912 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
| 4913 | ASSERT_TRUE(test_server_.Start()); |
| 4914 | |
| 4915 | GURL destination_url = test_server_.GetURL( |
| 4916 | "echoheader?" + std::string(kExtraHeaderToRemove)); |
| 4917 | GURL original_url = test_server_.GetURL( |
| 4918 | "server-redirect?" + destination_url.spec()); |
| 4919 | RedirectWithHeaderRemovalDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4920 | scoped_ptr<URLRequest> req( |
| 4921 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4922 | req->SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 4923 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4924 | base::RunLoop().Run(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4925 | |
| 4926 | std::string value; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4927 | const HttpRequestHeaders& headers = req->extra_request_headers(); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4928 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4929 | EXPECT_FALSE(req->is_pending()); |
| 4930 | EXPECT_FALSE(req->is_redirecting()); |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 4931 | EXPECT_EQ("None", d.data_received()); |
| 4932 | } |
| 4933 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4934 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 4935 | TestDelegate d; |
| 4936 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4937 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4938 | GURL("http://www.google.com/"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4939 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4940 | r->Start(); |
| 4941 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4942 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4943 | r->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4944 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4945 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4946 | |
| 4947 | // We expect to receive OnResponseStarted even though the request has been |
| 4948 | // cancelled. |
| 4949 | EXPECT_EQ(1, d.response_started_count()); |
| 4950 | EXPECT_EQ(0, d.bytes_received()); |
| 4951 | EXPECT_FALSE(d.received_data_before_response()); |
| 4952 | } |
| 4953 | } |
| 4954 | |
| 4955 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
| 4956 | ASSERT_TRUE(test_server_.Start()); |
| 4957 | |
| 4958 | TestDelegate d; |
| 4959 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4960 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4961 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4962 | |
| 4963 | d.set_cancel_in_response_started(true); |
| 4964 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4965 | r->Start(); |
| 4966 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4967 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4968 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4969 | |
| 4970 | EXPECT_EQ(1, d.response_started_count()); |
| 4971 | EXPECT_EQ(0, d.bytes_received()); |
| 4972 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4973 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4974 | } |
| 4975 | } |
| 4976 | |
| 4977 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
| 4978 | ASSERT_TRUE(test_server_.Start()); |
| 4979 | |
| 4980 | TestDelegate d; |
| 4981 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4982 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 4983 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4984 | |
| 4985 | d.set_cancel_in_received_data(true); |
| 4986 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4987 | r->Start(); |
| 4988 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4989 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 4990 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4991 | |
| 4992 | EXPECT_EQ(1, d.response_started_count()); |
| 4993 | // There is no guarantee about how much data was received |
| 4994 | // before the cancel was issued. It could have been 0 bytes, |
| 4995 | // or it could have been all the bytes. |
| 4996 | // EXPECT_EQ(0, d.bytes_received()); |
| 4997 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 4998 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
| 5003 | ASSERT_TRUE(test_server_.Start()); |
| 5004 | |
| 5005 | TestDelegate d; |
| 5006 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5007 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5008 | test_server_.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5009 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5010 | r->Start(); |
| 5011 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5012 | |
| 5013 | // The request will be implicitly canceled when it is destroyed. The |
| 5014 | // test delegate must not post a quit message when this happens because |
| 5015 | // this test doesn't actually have a message loop. The quit message would |
| 5016 | // get put on this thread's message queue and the next test would exit |
| 5017 | // early, causing problems. |
| 5018 | d.set_quit_on_complete(false); |
| 5019 | } |
| 5020 | // expect things to just cleanup properly. |
| 5021 | |
| 5022 | // we won't actually get a received reponse here because we've never run the |
| 5023 | // message loop |
| 5024 | EXPECT_FALSE(d.received_data_before_response()); |
| 5025 | EXPECT_EQ(0, d.bytes_received()); |
| 5026 | } |
| 5027 | |
| 5028 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
| 5029 | ASSERT_TRUE(test_server_.Start()); |
| 5030 | |
| 5031 | // populate cache |
| 5032 | { |
| 5033 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5034 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5035 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5036 | r->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5037 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5038 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5039 | } |
| 5040 | |
| 5041 | // cancel read from cache (see bug 990242) |
| 5042 | { |
| 5043 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5044 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5045 | test_server_.GetURL("cachetime"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5046 | r->Start(); |
| 5047 | r->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5048 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5049 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5050 | EXPECT_EQ(URLRequestStatus::CANCELED, r->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5051 | EXPECT_EQ(1, d.response_started_count()); |
| 5052 | EXPECT_EQ(0, d.bytes_received()); |
| 5053 | EXPECT_FALSE(d.received_data_before_response()); |
| 5054 | } |
| 5055 | } |
| 5056 | |
| 5057 | TEST_F(URLRequestTestHTTP, PostTest) { |
| 5058 | ASSERT_TRUE(test_server_.Start()); |
| 5059 | HTTPUploadDataOperationTest("POST"); |
| 5060 | } |
| 5061 | |
| 5062 | TEST_F(URLRequestTestHTTP, PutTest) { |
| 5063 | ASSERT_TRUE(test_server_.Start()); |
| 5064 | HTTPUploadDataOperationTest("PUT"); |
| 5065 | } |
| 5066 | |
| 5067 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
| 5068 | ASSERT_TRUE(test_server_.Start()); |
| 5069 | |
| 5070 | TestDelegate d; |
| 5071 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5072 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5073 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5074 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5075 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5076 | r->Start(); |
| 5077 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5078 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5079 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5080 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5081 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5082 | << "request failed: " << r->status().status() |
| 5083 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5084 | |
| 5085 | EXPECT_FALSE(d.received_data_before_response()); |
| 5086 | EXPECT_TRUE(d.data_received().empty()); |
| 5087 | } |
| 5088 | } |
| 5089 | |
| 5090 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
| 5091 | ASSERT_TRUE(test_server_.Start()); |
| 5092 | |
| 5093 | TestDelegate d; |
| 5094 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5095 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5096 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5097 | r->set_method("POST"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5098 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5099 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5100 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 5101 | base::SetCurrentDirectory(dir); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5102 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 5103 | ScopedVector<UploadElementReader> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5104 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5105 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5106 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 5107 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 5108 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 5109 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 5110 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
[email protected] | cadac62 | 2013-06-11 16:46:36 | [diff] [blame] | 5111 | element_readers.push_back( |
| 5112 | new UploadFileElementReader(base::MessageLoopProxy::current().get(), |
| 5113 | path, |
| 5114 | 0, |
| 5115 | kuint64max, |
| 5116 | base::Time())); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 5117 | r->set_upload(make_scoped_ptr<UploadDataStream>( |
| 5118 | new ElementsUploadDataStream(element_readers.Pass(), 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5119 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5120 | r->Start(); |
| 5121 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5122 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5123 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5124 | |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 5125 | int64 size64 = 0; |
| 5126 | ASSERT_EQ(true, base::GetFileSize(path, &size64)); |
| 5127 | ASSERT_LE(size64, std::numeric_limits<int>::max()); |
| 5128 | int size = static_cast<int>(size64); |
[email protected] | 4356f0f | 2013-04-07 00:58:17 | [diff] [blame] | 5129 | scoped_ptr<char[]> buf(new char[size]); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5130 | |
[email protected] | 7600d0b | 2013-12-08 21:43:30 | [diff] [blame] | 5131 | ASSERT_EQ(size, base::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5132 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5133 | ASSERT_EQ(1, d.response_started_count()) |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5134 | << "request failed: " << r->status().status() |
| 5135 | << ", error: " << r->status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5136 | |
| 5137 | EXPECT_FALSE(d.received_data_before_response()); |
| 5138 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5139 | EXPECT_EQ(size, d.bytes_received()); |
| 5140 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5141 | } |
| 5142 | } |
| 5143 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5144 | TEST_F(URLRequestTestHTTP, PostUnreadableFileTest) { |
| 5145 | ASSERT_TRUE(test_server_.Start()); |
| 5146 | |
| 5147 | TestDelegate d; |
| 5148 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5149 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5150 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5151 | r->set_method("POST"); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5152 | |
| 5153 | ScopedVector<UploadElementReader> element_readers; |
| 5154 | |
| 5155 | element_readers.push_back(new UploadFileElementReader( |
| 5156 | base::MessageLoopProxy::current().get(), |
| 5157 | base::FilePath(FILE_PATH_LITERAL( |
| 5158 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
| 5159 | 0, |
| 5160 | kuint64max, |
| 5161 | base::Time())); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 5162 | r->set_upload(make_scoped_ptr<UploadDataStream>( |
| 5163 | new ElementsUploadDataStream(element_readers.Pass(), 0))); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5164 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5165 | r->Start(); |
| 5166 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5167 | |
| 5168 | base::RunLoop().Run(); |
| 5169 | |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5170 | EXPECT_TRUE(d.request_failed()); |
| 5171 | EXPECT_FALSE(d.received_data_before_response()); |
| 5172 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5173 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 5174 | EXPECT_EQ(ERR_FILE_NOT_FOUND, r->status().error()); |
[email protected] | 999dd8c | 2013-11-12 06:45:54 | [diff] [blame] | 5175 | } |
| 5176 | } |
| 5177 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5178 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
| 5179 | ASSERT_TRUE(test_server_.Start()); |
| 5180 | |
| 5181 | TestDelegate d; |
| 5182 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5183 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5184 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5185 | r->EnableChunkedUpload(); |
| 5186 | r->set_method("POST"); |
| 5187 | AddChunksToUpload(r.get()); |
| 5188 | r->Start(); |
| 5189 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5190 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5191 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5192 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5193 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5194 | } |
| 5195 | } |
| 5196 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5197 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
| 5198 | ASSERT_TRUE(test_server_.Start()); |
| 5199 | |
| 5200 | TestDelegate d; |
| 5201 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5202 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5203 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5204 | r->EnableChunkedUpload(); |
| 5205 | r->set_method("POST"); |
| 5206 | r->Start(); |
| 5207 | EXPECT_TRUE(r->is_pending()); |
| 5208 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5209 | base::RunLoop().Run(); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5210 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5211 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 5212 | } |
| 5213 | } |
| 5214 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5215 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
| 5216 | ASSERT_TRUE(test_server_.Start()); |
| 5217 | |
| 5218 | TestDelegate d; |
| 5219 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5220 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5221 | test_server_.GetURL("echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5222 | r->EnableChunkedUpload(); |
| 5223 | r->set_method("POST"); |
| 5224 | r->Start(); |
| 5225 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5226 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5227 | base::RunLoop().RunUntilIdle(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5228 | AddChunksToUpload(r.get()); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5229 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5230 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5231 | VerifyReceivedDataMatchesChunks(r.get(), &d); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
| 5236 | ASSERT_TRUE(test_server_.Start()); |
| 5237 | |
| 5238 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5239 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5240 | test_server_.GetURL("files/with-headers.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5241 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5242 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5243 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5244 | const HttpResponseHeaders* headers = req->response_headers(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5245 | |
| 5246 | // Simple sanity check that response_info() accesses the same data. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5247 | EXPECT_EQ(headers, req->response_info().headers.get()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5248 | |
| 5249 | std::string header; |
| 5250 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 5251 | EXPECT_EQ("private", header); |
| 5252 | |
| 5253 | header.clear(); |
| 5254 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 5255 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 5256 | |
| 5257 | // The response has two "X-Multiple-Entries" headers. |
| 5258 | // This verfies our output has them concatenated together. |
| 5259 | header.clear(); |
| 5260 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 5261 | EXPECT_EQ("a, b", header); |
| 5262 | } |
| 5263 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5264 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5265 | SpawnedTestServer::SSLOptions ssl_options( |
| 5266 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 5267 | SpawnedTestServer https_test_server( |
| 5268 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5269 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5270 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5271 | ASSERT_TRUE(https_test_server.Start()); |
| 5272 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5273 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5274 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5275 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5276 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5277 | https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY, |
| 5278 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5279 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5280 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5281 | |
| 5282 | TransportSecurityState* security_state = |
| 5283 | default_context_.transport_security_state(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5284 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5285 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5286 | &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5287 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5288 | domain_state.sts.upgrade_mode); |
| 5289 | EXPECT_TRUE(domain_state.sts.include_subdomains); |
| 5290 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5291 | #if defined(OS_ANDROID) |
| 5292 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5293 | #else |
| 5294 | EXPECT_FALSE(domain_state.HasPublicKeyPins()); |
| 5295 | #endif |
| 5296 | } |
| 5297 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5298 | TEST_F(URLRequestTestHTTP, STSNotProcessedOnIP) { |
| 5299 | SpawnedTestServer https_test_server( |
| 5300 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(), |
| 5301 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5302 | ASSERT_TRUE(https_test_server.Start()); |
| 5303 | // Make sure this test fails if the test server is changed to not |
| 5304 | // listen on an IP by default. |
| 5305 | ASSERT_TRUE(https_test_server.GetURL("").HostIsIPAddress()); |
| 5306 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5307 | |
| 5308 | TestDelegate d; |
| 5309 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 5310 | https_test_server.GetURL("files/hsts-headers.html"), DEFAULT_PRIORITY, |
| 5311 | &d)); |
| 5312 | request->Start(); |
| 5313 | base::RunLoop().Run(); |
| 5314 | |
| 5315 | TransportSecurityState* security_state = |
| 5316 | default_context_.transport_security_state(); |
| 5317 | TransportSecurityState::DomainState domain_state; |
| 5318 | EXPECT_FALSE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5319 | &domain_state)); |
| 5320 | } |
| 5321 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5322 | // Android's CertVerifyProc does not (yet) handle pins. Therefore, it will |
| 5323 | // reject HPKP headers, and a test setting only HPKP headers will fail (no |
| 5324 | // DomainState present because header rejected). |
| 5325 | #if defined(OS_ANDROID) |
| 5326 | #define MAYBE_ProcessPKP DISABLED_ProcessPKP |
| 5327 | #else |
| 5328 | #define MAYBE_ProcessPKP ProcessPKP |
| 5329 | #endif |
| 5330 | |
| 5331 | // Tests that enabling HPKP on a domain does not affect the HSTS |
| 5332 | // validity/expiration. |
| 5333 | TEST_F(URLRequestTestHTTP, MAYBE_ProcessPKP) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5334 | SpawnedTestServer::SSLOptions ssl_options( |
| 5335 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5336 | SpawnedTestServer https_test_server( |
| 5337 | SpawnedTestServer::TYPE_HTTPS, |
| 5338 | ssl_options, |
| 5339 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5340 | ASSERT_TRUE(https_test_server.Start()); |
| 5341 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5342 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5343 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5344 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5345 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5346 | https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY, |
| 5347 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5348 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5349 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5350 | |
| 5351 | TransportSecurityState* security_state = |
| 5352 | default_context_.transport_security_state(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5353 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5354 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5355 | &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5356 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_DEFAULT, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5357 | domain_state.sts.upgrade_mode); |
| 5358 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
| 5359 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5360 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5361 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5362 | } |
| 5363 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5364 | TEST_F(URLRequestTestHTTP, PKPNotProcessedOnIP) { |
| 5365 | SpawnedTestServer https_test_server( |
| 5366 | SpawnedTestServer::TYPE_HTTPS, SpawnedTestServer::SSLOptions(), |
| 5367 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5368 | ASSERT_TRUE(https_test_server.Start()); |
| 5369 | // Make sure this test fails if the test server is changed to not |
| 5370 | // listen on an IP by default. |
| 5371 | ASSERT_TRUE(https_test_server.GetURL("").HostIsIPAddress()); |
| 5372 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5373 | |
| 5374 | TestDelegate d; |
| 5375 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
| 5376 | https_test_server.GetURL("files/hpkp-headers.html"), DEFAULT_PRIORITY, |
| 5377 | &d)); |
| 5378 | request->Start(); |
| 5379 | base::RunLoop().Run(); |
| 5380 | |
| 5381 | TransportSecurityState* security_state = |
| 5382 | default_context_.transport_security_state(); |
| 5383 | TransportSecurityState::DomainState domain_state; |
| 5384 | EXPECT_FALSE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5385 | &domain_state)); |
| 5386 | } |
| 5387 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5388 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5389 | SpawnedTestServer::SSLOptions ssl_options( |
| 5390 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 5391 | SpawnedTestServer https_test_server( |
| 5392 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5393 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5394 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5395 | ASSERT_TRUE(https_test_server.Start()); |
| 5396 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5397 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5398 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5399 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5400 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5401 | https_test_server.GetURL("files/hsts-multiple-headers.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5402 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5403 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5404 | base::RunLoop().Run(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5405 | |
| 5406 | // We should have set parameters from the first header, not the second. |
| 5407 | TransportSecurityState* security_state = |
| 5408 | default_context_.transport_security_state(); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5409 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5410 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5411 | &domain_state)); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5412 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5413 | domain_state.sts.upgrade_mode); |
| 5414 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
| 5415 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 5416 | } |
| 5417 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5418 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5419 | SpawnedTestServer::SSLOptions ssl_options( |
| 5420 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 5421 | SpawnedTestServer https_test_server( |
| 5422 | SpawnedTestServer::TYPE_HTTPS, |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5423 | ssl_options, |
| 5424 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5425 | ASSERT_TRUE(https_test_server.Start()); |
| 5426 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5427 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5428 | |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5429 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5430 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5431 | https_test_server.GetURL("files/hsts-and-hpkp-headers.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5432 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5433 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5434 | base::RunLoop().Run(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5435 | |
| 5436 | // We should have set parameters from the first header, not the second. |
| 5437 | TransportSecurityState* security_state = |
| 5438 | default_context_.transport_security_state(); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5439 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5440 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5441 | &domain_state)); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5442 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5443 | domain_state.sts.upgrade_mode); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5444 | #if defined(OS_ANDROID) |
| 5445 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5446 | #else |
| 5447 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5448 | #endif |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5449 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5450 | |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5451 | // Even though there is an HSTS header asserting includeSubdomains, it is |
| 5452 | // the *second* such header, and we MUST process only the first. |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5453 | EXPECT_FALSE(domain_state.sts.include_subdomains); |
[email protected] | a165f09 | 2013-06-12 16:10:05 | [diff] [blame] | 5454 | // includeSubdomains does not occur in the test HPKP header. |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5455 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 9f972ec | 2013-04-10 20:24:36 | [diff] [blame] | 5456 | } |
| 5457 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5458 | // Tests that when multiple HPKP headers are present, asserting different |
| 5459 | // policies, that only the first such policy is processed. |
| 5460 | TEST_F(URLRequestTestHTTP, ProcessSTSAndPKP2) { |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5461 | SpawnedTestServer::SSLOptions ssl_options( |
| 5462 | SpawnedTestServer::SSLOptions::CERT_COMMON_NAME_IS_DOMAIN); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5463 | SpawnedTestServer https_test_server( |
| 5464 | SpawnedTestServer::TYPE_HTTPS, |
| 5465 | ssl_options, |
| 5466 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
| 5467 | ASSERT_TRUE(https_test_server.Start()); |
| 5468 | |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5469 | std::string test_server_hostname = https_test_server.GetURL("").host(); |
| 5470 | |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5471 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5472 | scoped_ptr<URLRequest> request(default_context_.CreateRequest( |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5473 | https_test_server.GetURL("files/hsts-and-hpkp-headers2.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5474 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5475 | request->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5476 | base::RunLoop().Run(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5477 | |
| 5478 | TransportSecurityState* security_state = |
| 5479 | default_context_.transport_security_state(); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5480 | TransportSecurityState::DomainState domain_state; |
estark | a5da7670 | 2015-04-09 04:00:16 | [diff] [blame] | 5481 | EXPECT_TRUE(security_state->GetDynamicDomainState(test_server_hostname, |
| 5482 | &domain_state)); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5483 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5484 | domain_state.sts.upgrade_mode); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5485 | #if defined(OS_ANDROID) |
| 5486 | // Android's CertVerifyProc does not (yet) handle pins. |
| 5487 | #else |
| 5488 | EXPECT_TRUE(domain_state.HasPublicKeyPins()); |
| 5489 | #endif |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5490 | EXPECT_NE(domain_state.sts.expiry, domain_state.pkp.expiry); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5491 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 5492 | EXPECT_TRUE(domain_state.sts.include_subdomains); |
| 5493 | EXPECT_FALSE(domain_state.pkp.include_subdomains); |
[email protected] | 37fd55fb | 2013-06-29 13:13:27 | [diff] [blame] | 5494 | } |
| 5495 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5496 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
| 5497 | ASSERT_TRUE(test_server_.Start()); |
| 5498 | |
| 5499 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5500 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5501 | test_server_.GetURL("files/content-type-normalization.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5502 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5503 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5504 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5505 | |
| 5506 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5507 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5508 | EXPECT_EQ("text/html", mime_type); |
| 5509 | |
| 5510 | std::string charset; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5511 | req->GetCharset(&charset); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5512 | EXPECT_EQ("utf-8", charset); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5513 | req->Cancel(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5514 | } |
| 5515 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5516 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictDataRedirects) { |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5517 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5518 | GURL data_url("data:,foo"); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5519 | DataProtocolHandler data_protocol_handler; |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5520 | EXPECT_FALSE(data_protocol_handler.IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5521 | |
| 5522 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 5523 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(data_url)); |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 5524 | } |
| 5525 | |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5526 | #if !defined(DISABLE_FILE_SUPPORT) |
| 5527 | TEST_F(URLRequestTestHTTP, ProtocolHandlerAndFactoryRestrictFileRedirects) { |
| 5528 | // Test URLRequestJobFactory::ProtocolHandler::IsSafeRedirectTarget(). |
| 5529 | GURL file_url("file:///foo.txt"); |
| 5530 | FileProtocolHandler file_protocol_handler(base::MessageLoopProxy::current()); |
| 5531 | EXPECT_FALSE(file_protocol_handler.IsSafeRedirectTarget(file_url)); |
| 5532 | |
| 5533 | // Test URLRequestJobFactoryImpl::IsSafeRedirectTarget(). |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 5534 | EXPECT_FALSE(job_factory_->IsSafeRedirectTarget(file_url)); |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5535 | } |
| 5536 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5537 | TEST_F(URLRequestTestHTTP, RestrictFileRedirects) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5538 | ASSERT_TRUE(test_server_.Start()); |
| 5539 | |
| 5540 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5541 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5542 | test_server_.GetURL("files/redirect-to-file.html"), DEFAULT_PRIORITY, |
| 5543 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5544 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5545 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5546 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5547 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5548 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5549 | } |
[email protected] | 02494ec | 2014-05-07 15:05:29 | [diff] [blame] | 5550 | #endif // !defined(DISABLE_FILE_SUPPORT) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5551 | |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5552 | TEST_F(URLRequestTestHTTP, RestrictDataRedirects) { |
| 5553 | ASSERT_TRUE(test_server_.Start()); |
| 5554 | |
| 5555 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5556 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5557 | test_server_.GetURL("files/redirect-to-data.html"), DEFAULT_PRIORITY, |
| 5558 | &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5559 | req->Start(); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5560 | base::MessageLoop::current()->Run(); |
| 5561 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5562 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5563 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req->status().error()); |
[email protected] | 588614c2 | 2013-08-16 00:09:02 | [diff] [blame] | 5564 | } |
| 5565 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5566 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
| 5567 | ASSERT_TRUE(test_server_.Start()); |
| 5568 | |
| 5569 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5570 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
| 5571 | test_server_.GetURL("files/redirect-to-invalid-url.html"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5572 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5573 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5574 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5575 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5576 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 5577 | EXPECT_EQ(ERR_INVALID_URL, req->status().error()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5578 | } |
| 5579 | |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5580 | // Make sure redirects are cached, despite not reading their bodies. |
| 5581 | TEST_F(URLRequestTestHTTP, CacheRedirect) { |
| 5582 | ASSERT_TRUE(test_server_.Start()); |
| 5583 | GURL redirect_url = |
| 5584 | test_server_.GetURL("files/redirect302-to-echo-cacheable"); |
| 5585 | |
| 5586 | { |
| 5587 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5588 | scoped_ptr<URLRequest> req( |
| 5589 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5590 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5591 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5592 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5593 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5594 | EXPECT_EQ(test_server_.GetURL("echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5595 | } |
| 5596 | |
| 5597 | { |
| 5598 | TestDelegate d; |
| 5599 | d.set_quit_on_redirect(true); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5600 | scoped_ptr<URLRequest> req( |
| 5601 | default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5602 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5603 | base::RunLoop().Run(); |
| 5604 | |
| 5605 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5606 | EXPECT_EQ(0, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5607 | EXPECT_TRUE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5608 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5609 | req->FollowDeferredRedirect(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5610 | base::RunLoop().Run(); |
| 5611 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5612 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5613 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 5614 | EXPECT_EQ(test_server_.GetURL("echo"), req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5615 | } |
| 5616 | } |
| 5617 | |
| 5618 | // Make sure a request isn't cached when a NetworkDelegate forces a redirect |
| 5619 | // when the headers are read, since the body won't have been read. |
| 5620 | TEST_F(URLRequestTestHTTP, NoCacheOnNetworkDelegateRedirect) { |
| 5621 | ASSERT_TRUE(test_server_.Start()); |
| 5622 | // URL that is normally cached. |
| 5623 | GURL initial_url = test_server_.GetURL("cachetime"); |
| 5624 | |
| 5625 | { |
| 5626 | // Set up the TestNetworkDelegate tp force a redirect. |
| 5627 | GURL redirect_to_url = test_server_.GetURL("echo"); |
| 5628 | default_network_delegate_.set_redirect_on_headers_received_url( |
| 5629 | redirect_to_url); |
| 5630 | |
| 5631 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5632 | scoped_ptr<URLRequest> req( |
| 5633 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5634 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5635 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5636 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5637 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5638 | EXPECT_EQ(redirect_to_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5639 | } |
| 5640 | |
| 5641 | { |
| 5642 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5643 | scoped_ptr<URLRequest> req( |
| 5644 | default_context_.CreateRequest(initial_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5645 | req->Start(); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5646 | base::RunLoop().Run(); |
| 5647 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5648 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
| 5649 | EXPECT_FALSE(req->was_cached()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5650 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5651 | EXPECT_EQ(initial_url, req->url()); |
[email protected] | e50efea | 2014-03-24 18:41:00 | [diff] [blame] | 5652 | } |
| 5653 | } |
| 5654 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5655 | // Tests that redirection to an unsafe URL is allowed when it has been marked as |
| 5656 | // safe. |
| 5657 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToWhitelistedUnsafeURL) { |
| 5658 | ASSERT_TRUE(test_server_.Start()); |
| 5659 | |
| 5660 | GURL unsafe_url("data:text/html,this-is-considered-an-unsafe-url"); |
| 5661 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5662 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5663 | |
| 5664 | TestDelegate d; |
| 5665 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5666 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5667 | test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5668 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5669 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5670 | base::RunLoop().Run(); |
| 5671 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5672 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5673 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5674 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5675 | EXPECT_EQ(OK, r->status().error()); |
| 5676 | EXPECT_EQ(unsafe_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5677 | EXPECT_EQ("this-is-considered-an-unsafe-url", d.data_received()); |
| 5678 | } |
| 5679 | } |
| 5680 | |
| 5681 | // Tests that a redirect to a different unsafe URL is blocked, even after adding |
| 5682 | // some other URL to the whitelist. |
| 5683 | TEST_F(URLRequestTestHTTP, UnsafeRedirectToDifferentUnsafeURL) { |
| 5684 | ASSERT_TRUE(test_server_.Start()); |
| 5685 | |
| 5686 | GURL unsafe_url("data:text/html,something"); |
| 5687 | GURL different_unsafe_url("data:text/html,something-else"); |
| 5688 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5689 | default_network_delegate_.set_allowed_unsafe_redirect_url( |
| 5690 | different_unsafe_url); |
| 5691 | |
| 5692 | TestDelegate d; |
| 5693 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5694 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5695 | test_server_.GetURL("whatever"), DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5696 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5697 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5698 | base::RunLoop().Run(); |
| 5699 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5700 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 5701 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, r->status().error()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5702 | } |
| 5703 | } |
| 5704 | |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5705 | // Redirects from an URL with fragment to an unsafe URL with fragment should |
| 5706 | // be allowed, and the reference fragment of the target URL should be preserved. |
| 5707 | TEST_F(URLRequestTestHTTP, UnsafeRedirectWithDifferentReferenceFragment) { |
| 5708 | ASSERT_TRUE(test_server_.Start()); |
| 5709 | |
| 5710 | GURL original_url(test_server_.GetURL("original#fragment1")); |
| 5711 | GURL unsafe_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 5712 | GURL expected_url("data:,url-marked-safe-and-used-in-redirect#fragment2"); |
| 5713 | |
| 5714 | default_network_delegate_.set_redirect_on_headers_received_url(unsafe_url); |
| 5715 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5716 | |
| 5717 | TestDelegate d; |
| 5718 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5719 | scoped_ptr<URLRequest> r( |
| 5720 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5721 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5722 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5723 | base::RunLoop().Run(); |
| 5724 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5725 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5726 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5727 | EXPECT_EQ(OK, r->status().error()); |
| 5728 | EXPECT_EQ(original_url, r->original_url()); |
| 5729 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5730 | } |
| 5731 | } |
| 5732 | |
| 5733 | // When a delegate has specified a safe redirect URL, but it does not match the |
| 5734 | // redirect target, then do not prevent the reference fragment from being added. |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5735 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragmentAndUnrelatedUnsafeUrl) { |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5736 | ASSERT_TRUE(test_server_.Start()); |
| 5737 | |
| 5738 | GURL original_url(test_server_.GetURL("original#expected-fragment")); |
| 5739 | GURL unsafe_url("data:text/html,this-url-does-not-match-redirect-url"); |
| 5740 | GURL redirect_url(test_server_.GetURL("target")); |
| 5741 | GURL expected_redirect_url(test_server_.GetURL("target#expected-fragment")); |
| 5742 | |
| 5743 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 5744 | default_network_delegate_.set_allowed_unsafe_redirect_url(unsafe_url); |
| 5745 | |
| 5746 | TestDelegate d; |
| 5747 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5748 | scoped_ptr<URLRequest> r( |
| 5749 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5750 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5751 | r->Start(); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5752 | base::RunLoop().Run(); |
| 5753 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5754 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5755 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5756 | EXPECT_EQ(OK, r->status().error()); |
| 5757 | EXPECT_EQ(original_url, r->original_url()); |
| 5758 | EXPECT_EQ(expected_redirect_url, r->url()); |
[email protected] | 5f71413 | 2014-03-26 10:41:16 | [diff] [blame] | 5759 | } |
| 5760 | } |
| 5761 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5762 | // When a delegate has specified a safe redirect URL, assume that the redirect |
| 5763 | // URL should not be changed. In particular, the reference fragment should not |
| 5764 | // be modified. |
| 5765 | TEST_F(URLRequestTestHTTP, RedirectWithReferenceFragment) { |
| 5766 | ASSERT_TRUE(test_server_.Start()); |
| 5767 | |
| 5768 | GURL original_url(test_server_.GetURL("original#should-not-be-appended")); |
| 5769 | GURL redirect_url("data:text/html,expect-no-reference-fragment"); |
| 5770 | |
| 5771 | default_network_delegate_.set_redirect_on_headers_received_url(redirect_url); |
| 5772 | default_network_delegate_.set_allowed_unsafe_redirect_url(redirect_url); |
| 5773 | |
| 5774 | TestDelegate d; |
| 5775 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5776 | scoped_ptr<URLRequest> r( |
| 5777 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5778 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5779 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5780 | base::RunLoop().Run(); |
| 5781 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5782 | EXPECT_EQ(2U, r->url_chain().size()); |
| 5783 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5784 | EXPECT_EQ(OK, r->status().error()); |
| 5785 | EXPECT_EQ(original_url, r->original_url()); |
| 5786 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5787 | } |
| 5788 | } |
| 5789 | |
| 5790 | // When a URLRequestRedirectJob is created, the redirection must be followed and |
| 5791 | // the reference fragment of the target URL must not be modified. |
| 5792 | TEST_F(URLRequestTestHTTP, RedirectJobWithReferenceFragment) { |
| 5793 | ASSERT_TRUE(test_server_.Start()); |
| 5794 | |
| 5795 | GURL original_url(test_server_.GetURL("original#should-not-be-appended")); |
| 5796 | GURL redirect_url(test_server_.GetURL("echo")); |
| 5797 | |
| 5798 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5799 | scoped_ptr<URLRequest> r( |
| 5800 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5801 | |
| 5802 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5803 | r.get(), &default_network_delegate_, redirect_url, |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5804 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
| 5805 | AddTestInterceptor()->set_main_intercept_job(job); |
| 5806 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5807 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5808 | base::RunLoop().Run(); |
| 5809 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5810 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 5811 | EXPECT_EQ(OK, r->status().error()); |
| 5812 | EXPECT_EQ(original_url, r->original_url()); |
| 5813 | EXPECT_EQ(redirect_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 5814 | } |
| 5815 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5816 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
| 5817 | ASSERT_TRUE(test_server_.Start()); |
| 5818 | |
| 5819 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5820 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5821 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5822 | req->SetReferrer("http://user:[email protected]/"); |
| 5823 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5824 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5825 | |
| 5826 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 5827 | } |
| 5828 | |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5829 | TEST_F(URLRequestTestHTTP, NoFragmentInReferrer) { |
| 5830 | ASSERT_TRUE(test_server_.Start()); |
| 5831 | |
| 5832 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5833 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5834 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5835 | req->SetReferrer("http://foo.com/test#fragment"); |
| 5836 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5837 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5838 | |
| 5839 | EXPECT_EQ(std::string("http://foo.com/test"), d.data_received()); |
| 5840 | } |
| 5841 | |
| 5842 | TEST_F(URLRequestTestHTTP, EmptyReferrerAfterValidReferrer) { |
| 5843 | ASSERT_TRUE(test_server_.Start()); |
| 5844 | |
| 5845 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5846 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5847 | test_server_.GetURL("echoheader?Referer"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5848 | req->SetReferrer("http://foo.com/test#fragment"); |
| 5849 | req->SetReferrer(""); |
| 5850 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5851 | base::RunLoop().Run(); |
[email protected] | 99ecf6e | 2013-04-10 22:46:13 | [diff] [blame] | 5852 | |
| 5853 | EXPECT_EQ(std::string("None"), d.data_received()); |
| 5854 | } |
| 5855 | |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5856 | // Defer network start and then resume, checking that the request was a success |
| 5857 | // and bytes were received. |
| 5858 | TEST_F(URLRequestTestHTTP, DeferredBeforeNetworkStart) { |
| 5859 | ASSERT_TRUE(test_server_.Start()); |
| 5860 | |
| 5861 | TestDelegate d; |
| 5862 | { |
| 5863 | d.set_quit_on_network_start(true); |
| 5864 | GURL test_url(test_server_.GetURL("echo")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5865 | scoped_ptr<URLRequest> req( |
| 5866 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5867 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5868 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5869 | base::RunLoop().Run(); |
| 5870 | |
| 5871 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5872 | EXPECT_EQ(0, d.response_started_count()); |
| 5873 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5874 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5875 | base::RunLoop().Run(); |
| 5876 | |
| 5877 | EXPECT_EQ(1, d.response_started_count()); |
| 5878 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5879 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5880 | } |
| 5881 | } |
| 5882 | |
| 5883 | // Check that OnBeforeNetworkStart is only called once even if there is a |
| 5884 | // redirect. |
| 5885 | TEST_F(URLRequestTestHTTP, BeforeNetworkStartCalledOnce) { |
| 5886 | ASSERT_TRUE(test_server_.Start()); |
| 5887 | |
| 5888 | TestDelegate d; |
| 5889 | { |
| 5890 | d.set_quit_on_redirect(true); |
| 5891 | d.set_quit_on_network_start(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5892 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5893 | test_server_.GetURL("server-redirect?echo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5894 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5895 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5896 | base::RunLoop().Run(); |
| 5897 | |
| 5898 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5899 | EXPECT_EQ(0, d.response_started_count()); |
| 5900 | EXPECT_EQ(0, d.received_redirect_count()); |
| 5901 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5902 | req->ResumeNetworkStart(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5903 | base::RunLoop().Run(); |
| 5904 | |
| 5905 | EXPECT_EQ(1, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5906 | req->FollowDeferredRedirect(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5907 | base::RunLoop().Run(); |
| 5908 | |
| 5909 | // Check that the redirect's new network transaction does not get propagated |
| 5910 | // to a second OnBeforeNetworkStart() notification. |
| 5911 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5912 | |
| 5913 | EXPECT_EQ(1, d.response_started_count()); |
| 5914 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5915 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5916 | } |
| 5917 | } |
| 5918 | |
| 5919 | // Cancel the request after learning that the request would use the network. |
| 5920 | TEST_F(URLRequestTestHTTP, CancelOnBeforeNetworkStart) { |
| 5921 | ASSERT_TRUE(test_server_.Start()); |
| 5922 | |
| 5923 | TestDelegate d; |
| 5924 | { |
| 5925 | d.set_quit_on_network_start(true); |
| 5926 | GURL test_url(test_server_.GetURL("echo")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5927 | scoped_ptr<URLRequest> req( |
| 5928 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5929 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5930 | req->Start(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5931 | base::RunLoop().Run(); |
| 5932 | |
| 5933 | EXPECT_EQ(1, d.received_before_network_start_count()); |
| 5934 | EXPECT_EQ(0, d.response_started_count()); |
| 5935 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5936 | req->Cancel(); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5937 | base::RunLoop().Run(); |
| 5938 | |
| 5939 | EXPECT_EQ(1, d.response_started_count()); |
| 5940 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5941 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 1826a40 | 2014-01-08 15:40:48 | [diff] [blame] | 5942 | } |
| 5943 | } |
| 5944 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5945 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
| 5946 | ASSERT_TRUE(test_server_.Start()); |
| 5947 | |
| 5948 | TestDelegate d; |
| 5949 | { |
| 5950 | d.set_cancel_in_received_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5951 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5952 | test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5953 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5954 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5955 | |
| 5956 | EXPECT_EQ(1, d.response_started_count()); |
| 5957 | EXPECT_EQ(0, d.bytes_received()); |
| 5958 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5959 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5960 | } |
| 5961 | } |
| 5962 | |
| 5963 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
| 5964 | ASSERT_TRUE(test_server_.Start()); |
| 5965 | |
| 5966 | TestDelegate d; |
| 5967 | { |
| 5968 | d.set_quit_on_redirect(true); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5969 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 5970 | scoped_ptr<URLRequest> req( |
| 5971 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5972 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5973 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5974 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5975 | |
| 5976 | EXPECT_EQ(1, d.received_redirect_count()); |
| 5977 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5978 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 5979 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5980 | |
| 5981 | EXPECT_EQ(1, d.response_started_count()); |
| 5982 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 5983 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5984 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5985 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5986 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 5987 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 5988 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 5989 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 5990 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 5991 | |
| 5992 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 5993 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5994 | EXPECT_EQ(contents, d.data_received()); |
| 5995 | } |
| 5996 | } |
| 5997 | |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 5998 | TEST_F(URLRequestTestHTTP, DeferredRedirect_GetFullRequestHeaders) { |
| 5999 | ASSERT_TRUE(test_server_.Start()); |
| 6000 | |
| 6001 | TestDelegate d; |
| 6002 | { |
| 6003 | d.set_quit_on_redirect(true); |
| 6004 | GURL test_url(test_server_.GetURL("files/redirect-test.html")); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6005 | scoped_ptr<URLRequest> req( |
| 6006 | default_context_.CreateRequest(test_url, DEFAULT_PRIORITY, &d)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6007 | |
| 6008 | EXPECT_FALSE(d.have_full_request_headers()); |
| 6009 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6010 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6011 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6012 | |
| 6013 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6014 | EXPECT_TRUE(d.have_full_request_headers()); |
| 6015 | CheckFullRequestHeaders(d.full_request_headers(), test_url); |
| 6016 | d.ClearFullRequestHeaders(); |
| 6017 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6018 | req->FollowDeferredRedirect(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6019 | base::RunLoop().Run(); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6020 | |
| 6021 | GURL target_url(test_server_.GetURL("files/with-headers.html")); |
| 6022 | EXPECT_EQ(1, d.response_started_count()); |
| 6023 | EXPECT_TRUE(d.have_full_request_headers()); |
| 6024 | CheckFullRequestHeaders(d.full_request_headers(), target_url); |
| 6025 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6026 | EXPECT_EQ(URLRequestStatus::SUCCESS, req->status().status()); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6027 | |
| 6028 | base::FilePath path; |
| 6029 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 6030 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 6031 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 6032 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 6033 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 6034 | |
| 6035 | std::string contents; |
[email protected] | 82f84b9 | 2013-08-30 18:23:50 | [diff] [blame] | 6036 | EXPECT_TRUE(base::ReadFileToString(path, &contents)); |
[email protected] | 79e1fd6 | 2013-06-20 06:50:04 | [diff] [blame] | 6037 | EXPECT_EQ(contents, d.data_received()); |
| 6038 | } |
| 6039 | } |
| 6040 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6041 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
| 6042 | ASSERT_TRUE(test_server_.Start()); |
| 6043 | |
| 6044 | TestDelegate d; |
| 6045 | { |
| 6046 | d.set_quit_on_redirect(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6047 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6048 | test_server_.GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6049 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6050 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6051 | |
| 6052 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6053 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6054 | req->Cancel(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6055 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6056 | |
| 6057 | EXPECT_EQ(1, d.response_started_count()); |
| 6058 | EXPECT_EQ(0, d.bytes_received()); |
| 6059 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6060 | EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6061 | } |
| 6062 | } |
| 6063 | |
| 6064 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
| 6065 | ASSERT_TRUE(test_server_.Start()); |
| 6066 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6067 | // Populate the cache. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6068 | { |
| 6069 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6070 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6071 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6072 | HttpRequestHeaders headers; |
| 6073 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6074 | req->SetExtraRequestHeaders(headers); |
| 6075 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6076 | base::RunLoop().Run(); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6077 | |
| 6078 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6079 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6080 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6081 | } |
| 6082 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6083 | // Expect a cache hit. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6084 | { |
| 6085 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6086 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6087 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6088 | HttpRequestHeaders headers; |
| 6089 | headers.SetHeader("foo", "1"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6090 | req->SetExtraRequestHeaders(headers); |
| 6091 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6092 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6093 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6094 | EXPECT_TRUE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6095 | |
| 6096 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6097 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6098 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6099 | } |
| 6100 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6101 | // Expect a cache miss. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6102 | { |
| 6103 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6104 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6105 | test_server_.GetURL("echoheadercache?foo"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6106 | HttpRequestHeaders headers; |
| 6107 | headers.SetHeader("foo", "2"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6108 | req->SetExtraRequestHeaders(headers); |
| 6109 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6110 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6111 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6112 | EXPECT_FALSE(req->was_cached()); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6113 | |
| 6114 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6115 | req->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6116 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6117 | } |
| 6118 | } |
| 6119 | |
| 6120 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
| 6121 | ASSERT_TRUE(test_server_.Start()); |
| 6122 | |
| 6123 | // populate the cache |
| 6124 | { |
| 6125 | TestDelegate d; |
| 6126 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6127 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6128 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6129 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6130 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6131 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6132 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6133 | |
| 6134 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6135 | } |
| 6136 | |
| 6137 | // repeat request with end-to-end validation. since auth-basic results in a |
| 6138 | // cachable page, we expect this test to result in a 304. in which case, the |
| 6139 | // response should be fetched from the cache. |
| 6140 | { |
| 6141 | TestDelegate d; |
| 6142 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6143 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6144 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6145 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6146 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 6147 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6148 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6149 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6150 | |
| 6151 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6152 | |
| 6153 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6154 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6155 | } |
| 6156 | } |
| 6157 | |
| 6158 | // Check that Set-Cookie headers in 401 responses are respected. |
| 6159 | // http://crbug.com/6450 |
| 6160 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
| 6161 | ASSERT_TRUE(test_server_.Start()); |
| 6162 | |
| 6163 | GURL url_requiring_auth = |
| 6164 | test_server_.GetURL("auth-basic?set-cookie-if-challenged"); |
| 6165 | |
| 6166 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 6167 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 6168 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6169 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6170 | TestURLRequestContext context(true); |
| 6171 | context.set_network_delegate(&network_delegate); |
| 6172 | context.Init(); |
| 6173 | |
| 6174 | TestDelegate d; |
| 6175 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6176 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6177 | scoped_ptr<URLRequest> r( |
| 6178 | context.CreateRequest(url_requiring_auth, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6179 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6180 | |
[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 | |
| 6183 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6184 | |
| 6185 | // Make sure we sent the cookie in the restarted transaction. |
| 6186 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 6187 | != std::string::npos); |
| 6188 | } |
| 6189 | |
| 6190 | // Same test as above, except this time the restart is initiated earlier |
| 6191 | // (without user intervention since identity is embedded in the URL). |
| 6192 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6193 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6194 | TestURLRequestContext context(true); |
| 6195 | context.set_network_delegate(&network_delegate); |
| 6196 | context.Init(); |
| 6197 | |
| 6198 | TestDelegate d; |
| 6199 | |
| 6200 | GURL::Replacements replacements; |
mgiuca | 77752c3 | 2015-02-05 07:31:18 | [diff] [blame] | 6201 | replacements.SetUsernameStr("user2"); |
| 6202 | replacements.SetPasswordStr("secret"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6203 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 6204 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6205 | scoped_ptr<URLRequest> r( |
| 6206 | context.CreateRequest(url_with_identity, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6207 | r->Start(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6208 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6209 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6210 | |
| 6211 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 6212 | |
| 6213 | // Make sure we sent the cookie in the restarted transaction. |
| 6214 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 6215 | != std::string::npos); |
| 6216 | } |
| 6217 | } |
| 6218 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6219 | // Tests that load timing works as expected with auth and the cache. |
| 6220 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
| 6221 | ASSERT_TRUE(test_server_.Start()); |
| 6222 | |
| 6223 | // populate the cache |
| 6224 | { |
| 6225 | TestDelegate d; |
| 6226 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6227 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6228 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6229 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6230 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6231 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6232 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6233 | |
| 6234 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6235 | |
| 6236 | LoadTimingInfo load_timing_info_before_auth; |
| 6237 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 6238 | &load_timing_info_before_auth)); |
| 6239 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 6240 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 6241 | |
| 6242 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6243 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6244 | // The test server does not support keep alive sockets, so the second |
| 6245 | // request with auth should use a new socket. |
| 6246 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 6247 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 6248 | load_timing_info.socket_log_id); |
| 6249 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 6250 | load_timing_info.connect_timing.connect_start); |
| 6251 | } |
| 6252 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6253 | // Repeat request with end-to-end validation. Since auth-basic results in a |
| 6254 | // 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] | 6255 | // response should be fetched from the cache. |
| 6256 | { |
| 6257 | TestDelegate d; |
| 6258 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 6259 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6260 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6261 | test_server_.GetURL("auth-basic"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6262 | r->SetLoadFlags(LOAD_VALIDATE_CACHE); |
| 6263 | r->Start(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6264 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6265 | base::RunLoop().Run(); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6266 | |
| 6267 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 6268 | |
| 6269 | // Should be the same cached document. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6270 | EXPECT_TRUE(r->was_cached()); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6271 | |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6272 | // Since there was a request that went over the wire, the load timing |
| 6273 | // information should include connection times. |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6274 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6275 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 6276 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 6277 | } |
| 6278 | } |
| 6279 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6280 | // In this test, we do a POST which the server will 302 redirect. |
| 6281 | // The subsequent transaction should use GET, and should not send the |
| 6282 | // Content-Type header. |
| 6283 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 6284 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
| 6285 | ASSERT_TRUE(test_server_.Start()); |
| 6286 | |
| 6287 | const char kData[] = "hello world"; |
| 6288 | |
| 6289 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6290 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6291 | test_server_.GetURL("files/redirect-to-echoall"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6292 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6293 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6294 | |
| 6295 | // Set headers (some of which are specific to the POST). |
| 6296 | HttpRequestHeaders headers; |
| 6297 | headers.AddHeadersFromString( |
| 6298 | "Content-Type: multipart/form-data; " |
| 6299 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 6300 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 6301 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 6302 | "Accept-Language: en-US,en\r\n" |
| 6303 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 6304 | "Content-Length: 11\r\n" |
| 6305 | "Origin: http://localhost:1337/"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6306 | req->SetExtraRequestHeaders(headers); |
| 6307 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6308 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6309 | |
| 6310 | std::string mime_type; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6311 | req->GetMimeType(&mime_type); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6312 | EXPECT_EQ("text/html", mime_type); |
| 6313 | |
| 6314 | const std::string& data = d.data_received(); |
| 6315 | |
| 6316 | // Check that the post-specific headers were stripped: |
| 6317 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 6318 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 6319 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 6320 | |
| 6321 | // These extra request headers should not have been stripped. |
| 6322 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 6323 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 6324 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 6325 | } |
| 6326 | |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6327 | // The following tests check that we handle mutating the request for HTTP |
| 6328 | // redirects as expected. |
| 6329 | // See https://crbug.com/56373, https://crbug.com/102130, and |
| 6330 | // https://crbug.com/465517. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6331 | |
| 6332 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
| 6333 | ASSERT_TRUE(test_server_.Start()); |
| 6334 | |
| 6335 | const GURL url = test_server_.GetURL("files/redirect301-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6336 | const GURL https_redirect_url = |
| 6337 | test_server_.GetURL("files/redirect301-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6338 | |
| 6339 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 6340 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6341 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6342 | |
| 6343 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6344 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6345 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 6346 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 6347 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6348 | } |
| 6349 | |
| 6350 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
| 6351 | ASSERT_TRUE(test_server_.Start()); |
| 6352 | |
| 6353 | const GURL url = test_server_.GetURL("files/redirect302-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6354 | const GURL https_redirect_url = |
| 6355 | test_server_.GetURL("files/redirect302-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6356 | |
| 6357 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 6358 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6359 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6360 | |
| 6361 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6362 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6363 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 6364 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 6365 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
| 6369 | ASSERT_TRUE(test_server_.Start()); |
| 6370 | |
| 6371 | const GURL url = test_server_.GetURL("files/redirect303-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6372 | const GURL https_redirect_url = |
| 6373 | test_server_.GetURL("files/redirect303-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6374 | |
| 6375 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 6376 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 6377 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6378 | |
| 6379 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6380 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6381 | HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string()); |
| 6382 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET", |
| 6383 | std::string()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6384 | } |
| 6385 | |
| 6386 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
| 6387 | ASSERT_TRUE(test_server_.Start()); |
| 6388 | |
| 6389 | const GURL url = test_server_.GetURL("files/redirect307-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6390 | const GURL https_redirect_url = |
| 6391 | test_server_.GetURL("files/redirect307-to-https"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6392 | |
| 6393 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 6394 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6395 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6396 | |
| 6397 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6398 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6399 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 6400 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6401 | } |
| 6402 | |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6403 | TEST_F(URLRequestTestHTTP, Redirect308Tests) { |
| 6404 | ASSERT_TRUE(test_server_.Start()); |
| 6405 | |
| 6406 | const GURL url = test_server_.GetURL("files/redirect308-to-echo"); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6407 | const GURL https_redirect_url = |
| 6408 | test_server_.GetURL("files/redirect308-to-https"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6409 | |
| 6410 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 6411 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 6412 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
jww | 5fe460ff | 2015-03-28 00:22:51 | [diff] [blame] | 6413 | |
| 6414 | HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec()); |
| 6415 | HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null"); |
| 6416 | HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec()); |
| 6417 | HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null"); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6418 | } |
| 6419 | |
| 6420 | // Make sure that 308 responses without bodies are not treated as redirects. |
| 6421 | // Certain legacy apis that pre-date the response code expect this behavior |
| 6422 | // (Like Google Drive). |
| 6423 | TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { |
| 6424 | ASSERT_TRUE(test_server_.Start()); |
| 6425 | |
| 6426 | TestDelegate d; |
| 6427 | const GURL url = test_server_.GetURL("files/308-without-location-header"); |
| 6428 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6429 | scoped_ptr<URLRequest> request( |
| 6430 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6431 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6432 | request->Start(); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6433 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6434 | EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
| 6435 | EXPECT_EQ(OK, request->status().error()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6436 | EXPECT_EQ(0, d.received_redirect_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6437 | EXPECT_EQ(308, request->response_headers()->response_code()); |
[email protected] | 0a17aab3 | 2014-04-24 03:32:37 | [diff] [blame] | 6438 | EXPECT_EQ("This is not a redirect.", d.data_received()); |
| 6439 | } |
| 6440 | |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6441 | TEST_F(URLRequestTestHTTP, Redirect302PreserveReferenceFragment) { |
| 6442 | ASSERT_TRUE(test_server_.Start()); |
| 6443 | |
| 6444 | GURL original_url(test_server_.GetURL("files/redirect302-to-echo#fragment")); |
| 6445 | GURL expected_url(test_server_.GetURL("echo#fragment")); |
| 6446 | |
| 6447 | TestDelegate d; |
| 6448 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6449 | scoped_ptr<URLRequest> r( |
| 6450 | default_context_.CreateRequest(original_url, DEFAULT_PRIORITY, &d)); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6451 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6452 | r->Start(); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6453 | base::RunLoop().Run(); |
| 6454 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6455 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6456 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6457 | EXPECT_EQ(OK, r->status().error()); |
| 6458 | EXPECT_EQ(original_url, r->original_url()); |
| 6459 | EXPECT_EQ(expected_url, r->url()); |
[email protected] | f878230e | 2014-04-03 15:36:14 | [diff] [blame] | 6460 | } |
| 6461 | } |
| 6462 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6463 | TEST_F(URLRequestTestHTTP, RedirectPreserveFirstPartyURL) { |
| 6464 | ASSERT_TRUE(test_server_.Start()); |
| 6465 | |
| 6466 | GURL url(test_server_.GetURL("files/redirect302-to-echo")); |
| 6467 | GURL first_party_url("http://example.com"); |
| 6468 | |
| 6469 | TestDelegate d; |
| 6470 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6471 | scoped_ptr<URLRequest> r( |
| 6472 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6473 | r->set_first_party_for_cookies(first_party_url); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6474 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6475 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6476 | base::RunLoop().Run(); |
| 6477 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6478 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6479 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6480 | EXPECT_EQ(OK, r->status().error()); |
| 6481 | EXPECT_EQ(first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6482 | } |
| 6483 | } |
| 6484 | |
| 6485 | TEST_F(URLRequestTestHTTP, RedirectUpdateFirstPartyURL) { |
| 6486 | ASSERT_TRUE(test_server_.Start()); |
| 6487 | |
| 6488 | GURL url(test_server_.GetURL("files/redirect302-to-echo")); |
| 6489 | GURL original_first_party_url("http://example.com"); |
| 6490 | GURL expected_first_party_url(test_server_.GetURL("echo")); |
| 6491 | |
| 6492 | TestDelegate d; |
| 6493 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6494 | scoped_ptr<URLRequest> r( |
| 6495 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6496 | r->set_first_party_for_cookies(original_first_party_url); |
| 6497 | r->set_first_party_url_policy( |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6498 | URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 6499 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6500 | r->Start(); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6501 | base::RunLoop().Run(); |
| 6502 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6503 | EXPECT_EQ(2U, r->url_chain().size()); |
| 6504 | EXPECT_EQ(URLRequestStatus::SUCCESS, r->status().status()); |
| 6505 | EXPECT_EQ(OK, r->status().error()); |
| 6506 | EXPECT_EQ(expected_first_party_url, r->first_party_for_cookies()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 6507 | } |
| 6508 | } |
| 6509 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6510 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
| 6511 | ASSERT_TRUE(test_server_.Start()); |
| 6512 | |
| 6513 | const char kData[] = "hello world"; |
| 6514 | |
| 6515 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6516 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6517 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6518 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6519 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6520 | HttpRequestHeaders headers; |
| 6521 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 6522 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6523 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6524 | |
| 6525 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6526 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6527 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6528 | AddTestInterceptor()->set_main_intercept_job(job); |
| 6529 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6530 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6531 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6532 | EXPECT_EQ("GET", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6533 | } |
| 6534 | |
| 6535 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 6536 | ASSERT_TRUE(test_server_.Start()); |
| 6537 | |
| 6538 | const char kData[] = "hello world"; |
| 6539 | |
| 6540 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6541 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6542 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6543 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 6544 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6545 | HttpRequestHeaders headers; |
| 6546 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 6547 | base::UintToString(arraysize(kData) - 1)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6548 | req->SetExtraRequestHeaders(headers); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6549 | |
| 6550 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6551 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6552 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, |
| 6553 | "Very Good Reason"); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6554 | AddTestInterceptor()->set_main_intercept_job(job); |
| 6555 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6556 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6557 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6558 | EXPECT_EQ("POST", req->method()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6559 | EXPECT_EQ(kData, d.data_received()); |
| 6560 | } |
| 6561 | |
| 6562 | // Check that default A-L header is sent. |
| 6563 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
| 6564 | ASSERT_TRUE(test_server_.Start()); |
| 6565 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 6566 | StaticHttpUserAgentSettings settings("en", std::string()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6567 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6568 | TestURLRequestContext context(true); |
| 6569 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6570 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6571 | context.Init(); |
| 6572 | |
| 6573 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6574 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6575 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6576 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6577 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6578 | EXPECT_EQ("en", d.data_received()); |
| 6579 | } |
| 6580 | |
| 6581 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 6582 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
| 6583 | ASSERT_TRUE(test_server_.Start()); |
| 6584 | |
[email protected] | 8790210c | 2013-12-02 05:29:53 | [diff] [blame] | 6585 | std::string empty_string; // Avoid most vexing parse on line below. |
| 6586 | StaticHttpUserAgentSettings settings(empty_string, empty_string); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6587 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6588 | TestURLRequestContext context(true); |
| 6589 | context.set_network_delegate(&network_delegate); |
| 6590 | context.Init(); |
| 6591 | // We override the language after initialization because empty entries |
| 6592 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6593 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6594 | |
| 6595 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6596 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6597 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6598 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6599 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6600 | EXPECT_EQ("None", d.data_received()); |
| 6601 | } |
| 6602 | |
| 6603 | // Check that if request overrides the A-L header, the default is not appended. |
| 6604 | // See http://crbug.com/20894 |
| 6605 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
| 6606 | ASSERT_TRUE(test_server_.Start()); |
| 6607 | |
| 6608 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6609 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6610 | test_server_.GetURL("echoheader?Accept-Language"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6611 | HttpRequestHeaders headers; |
| 6612 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6613 | req->SetExtraRequestHeaders(headers); |
| 6614 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6615 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6616 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 6617 | } |
| 6618 | |
| 6619 | // Check that default A-E header is sent. |
| 6620 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
| 6621 | ASSERT_TRUE(test_server_.Start()); |
| 6622 | |
| 6623 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6624 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6625 | test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6626 | HttpRequestHeaders headers; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6627 | req->SetExtraRequestHeaders(headers); |
| 6628 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6629 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6630 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 6631 | } |
| 6632 | |
| 6633 | // Check that if request overrides the A-E header, the default is not appended. |
| 6634 | // See http://crbug.com/47381 |
| 6635 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
| 6636 | ASSERT_TRUE(test_server_.Start()); |
| 6637 | |
| 6638 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6639 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6640 | test_server_.GetURL("echoheader?Accept-Encoding"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6641 | HttpRequestHeaders headers; |
| 6642 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6643 | req->SetExtraRequestHeaders(headers); |
| 6644 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6645 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6646 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 6647 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 6648 | } |
| 6649 | |
[email protected] | 84f0543 | 2013-03-15 01:00:12 | [diff] [blame] | 6650 | // Check that setting the A-C header sends the proper header. |
| 6651 | TEST_F(URLRequestTestHTTP, SetAcceptCharset) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6652 | ASSERT_TRUE(test_server_.Start()); |
| 6653 | |
| 6654 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6655 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6656 | test_server_.GetURL("echoheader?Accept-Charset"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6657 | HttpRequestHeaders headers; |
| 6658 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6659 | req->SetExtraRequestHeaders(headers); |
| 6660 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6661 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6662 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 6663 | } |
| 6664 | |
| 6665 | // Check that default User-Agent header is sent. |
| 6666 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
| 6667 | ASSERT_TRUE(test_server_.Start()); |
| 6668 | |
| 6669 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6670 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6671 | test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6672 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6673 | base::RunLoop().Run(); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6674 | EXPECT_EQ(default_context_.http_user_agent_settings()->GetUserAgent(), |
[email protected] | 051a4b3 | 2014-01-09 04:02:37 | [diff] [blame] | 6675 | d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6676 | } |
| 6677 | |
| 6678 | // Check that if request overrides the User-Agent header, |
| 6679 | // the default is not appended. |
| 6680 | TEST_F(URLRequestTestHTTP, OverrideUserAgent) { |
| 6681 | ASSERT_TRUE(test_server_.Start()); |
| 6682 | |
| 6683 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6684 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6685 | test_server_.GetURL("echoheader?User-Agent"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6686 | HttpRequestHeaders headers; |
| 6687 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6688 | req->SetExtraRequestHeaders(headers); |
| 6689 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6690 | base::RunLoop().Run(); |
[email protected] | cd6f252 | 2014-01-16 18:27:35 | [diff] [blame] | 6691 | EXPECT_EQ(std::string("Lynx (textmode)"), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 6692 | } |
| 6693 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6694 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 6695 | // User-Agent header to be sent but does not send the Accept-Language and |
| 6696 | // Accept-Charset headers. |
| 6697 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
| 6698 | ASSERT_TRUE(test_server_.Start()); |
| 6699 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 6700 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6701 | TestURLRequestContext context(true); |
| 6702 | context.set_network_delegate(&network_delegate); |
| 6703 | context.Init(); |
| 6704 | // We override the HttpUserAgentSettings after initialization because empty |
| 6705 | // entries get overridden by Init(). |
| 6706 | context.set_http_user_agent_settings(NULL); |
| 6707 | |
| 6708 | struct { |
| 6709 | const char* request; |
| 6710 | const char* expected_response; |
| 6711 | } tests[] = { { "echoheader?Accept-Language", "None" }, |
| 6712 | { "echoheader?Accept-Charset", "None" }, |
| 6713 | { "echoheader?User-Agent", "" } }; |
| 6714 | |
viettrungluu | e4a8b88 | 2014-10-16 06:17:38 | [diff] [blame] | 6715 | for (size_t i = 0; i < arraysize(tests); i++) { |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6716 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6717 | scoped_ptr<URLRequest> req(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6718 | test_server_.GetURL(tests[i].request), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6719 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6720 | base::RunLoop().Run(); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 6721 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 6722 | << " Request = \"" << tests[i].request << "\""; |
| 6723 | } |
| 6724 | } |
| 6725 | |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6726 | // Make sure that URLRequest passes on its priority updates to |
| 6727 | // newly-created jobs after the first one. |
| 6728 | TEST_F(URLRequestTestHTTP, SetSubsequentJobPriority) { |
| 6729 | ASSERT_TRUE(test_server_.Start()); |
| 6730 | |
| 6731 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6732 | scoped_ptr<URLRequest> req(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6733 | test_server_.GetURL("empty.html"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6734 | EXPECT_EQ(DEFAULT_PRIORITY, req->priority()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6735 | |
| 6736 | scoped_refptr<URLRequestRedirectJob> redirect_job = |
| 6737 | new URLRequestRedirectJob( |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6738 | req.get(), &default_network_delegate_, test_server_.GetURL("echo"), |
[email protected] | 7983c4a | 2014-03-12 01:47:09 | [diff] [blame] | 6739 | URLRequestRedirectJob::REDIRECT_302_FOUND, "Very Good Reason"); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6740 | AddTestInterceptor()->set_main_intercept_job(redirect_job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6741 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6742 | req->SetPriority(LOW); |
| 6743 | req->Start(); |
| 6744 | EXPECT_TRUE(req->is_pending()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6745 | |
| 6746 | scoped_refptr<URLRequestTestJob> job = |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6747 | new URLRequestTestJob(req.get(), &default_network_delegate_); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 6748 | AddTestInterceptor()->set_main_intercept_job(job.get()); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6749 | |
| 6750 | // Should trigger |job| to be started. |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 6751 | base::RunLoop().Run(); |
[email protected] | 5033ab8 | 2013-03-22 20:17:46 | [diff] [blame] | 6752 | EXPECT_EQ(LOW, job->priority()); |
| 6753 | } |
| 6754 | |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6755 | // Check that creating a network request while entering/exiting suspend mode |
| 6756 | // fails as it should. This is the only case where an HttpTransactionFactory |
| 6757 | // does not return an HttpTransaction. |
| 6758 | TEST_F(URLRequestTestHTTP, NetworkSuspendTest) { |
| 6759 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6760 | HttpNetworkSession::Params params; |
| 6761 | params.host_resolver = default_context_.host_resolver(); |
| 6762 | params.cert_verifier = default_context_.cert_verifier(); |
| 6763 | params.transport_security_state = default_context_.transport_security_state(); |
| 6764 | params.proxy_service = default_context_.proxy_service(); |
| 6765 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6766 | params.http_auth_handler_factory = |
| 6767 | default_context_.http_auth_handler_factory(); |
| 6768 | params.network_delegate = &default_network_delegate_; |
| 6769 | params.http_server_properties = default_context_.http_server_properties(); |
| 6770 | scoped_ptr<HttpNetworkLayer> network_layer( |
| 6771 | new HttpNetworkLayer(new HttpNetworkSession(params))); |
| 6772 | network_layer->OnSuspend(); |
| 6773 | |
| 6774 | HttpCache http_cache(network_layer.release(), default_context_.net_log(), |
| 6775 | HttpCache::DefaultBackend::InMemory(0)); |
| 6776 | |
| 6777 | TestURLRequestContext context(true); |
| 6778 | context.set_http_transaction_factory(&http_cache); |
| 6779 | context.Init(); |
| 6780 | |
| 6781 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6782 | scoped_ptr<URLRequest> req( |
| 6783 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6784 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6785 | base::RunLoop().Run(); |
| 6786 | |
| 6787 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6788 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6789 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6790 | } |
| 6791 | |
| 6792 | // Check that creating a network request while entering/exiting suspend mode |
| 6793 | // fails as it should in the case there is no cache. This is the only case |
| 6794 | // where an HttpTransactionFactory does not return an HttpTransaction. |
| 6795 | TEST_F(URLRequestTestHTTP, NetworkSuspendTestNoCache) { |
| 6796 | // Create a new HttpNetworkLayer that thinks it's suspended. |
| 6797 | HttpNetworkSession::Params params; |
| 6798 | params.host_resolver = default_context_.host_resolver(); |
| 6799 | params.cert_verifier = default_context_.cert_verifier(); |
| 6800 | params.transport_security_state = default_context_.transport_security_state(); |
| 6801 | params.proxy_service = default_context_.proxy_service(); |
| 6802 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 6803 | params.http_auth_handler_factory = |
| 6804 | default_context_.http_auth_handler_factory(); |
| 6805 | params.network_delegate = &default_network_delegate_; |
| 6806 | params.http_server_properties = default_context_.http_server_properties(); |
| 6807 | HttpNetworkLayer network_layer(new HttpNetworkSession(params)); |
| 6808 | network_layer.OnSuspend(); |
| 6809 | |
| 6810 | TestURLRequestContext context(true); |
| 6811 | context.set_http_transaction_factory(&network_layer); |
| 6812 | context.Init(); |
| 6813 | |
| 6814 | TestDelegate d; |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6815 | scoped_ptr<URLRequest> req( |
| 6816 | context.CreateRequest(GURL("http://127.0.0.1/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6817 | req->Start(); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6818 | base::RunLoop().Run(); |
| 6819 | |
| 6820 | EXPECT_TRUE(d.request_failed()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 6821 | EXPECT_EQ(URLRequestStatus::FAILED, req->status().status()); |
| 6822 | EXPECT_EQ(ERR_NETWORK_IO_SUSPENDED, req->status().error()); |
[email protected] | 80abdad | 2014-03-15 00:20:54 | [diff] [blame] | 6823 | } |
| 6824 | |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6825 | class URLRequestInterceptorTestHTTP : public URLRequestTestHTTP { |
| 6826 | public: |
| 6827 | // TODO(bengr): Merge this with the URLRequestInterceptorHTTPTest fixture, |
| 6828 | // ideally remove the dependency on URLRequestTestJob, and maybe move these |
| 6829 | // tests into the factory tests. |
| 6830 | URLRequestInterceptorTestHTTP() : URLRequestTestHTTP(), interceptor_(NULL) { |
| 6831 | } |
| 6832 | |
| 6833 | void SetUpFactory() override { |
| 6834 | interceptor_ = new MockURLRequestInterceptor(); |
| 6835 | job_factory_.reset(new URLRequestInterceptingJobFactory( |
| 6836 | job_factory_.Pass(), make_scoped_ptr(interceptor_))); |
| 6837 | } |
| 6838 | |
| 6839 | MockURLRequestInterceptor* interceptor() const { |
| 6840 | return interceptor_; |
| 6841 | } |
| 6842 | |
| 6843 | private: |
| 6844 | MockURLRequestInterceptor* interceptor_; |
| 6845 | }; |
| 6846 | |
| 6847 | TEST_F(URLRequestInterceptorTestHTTP, |
| 6848 | NetworkDelegateNotificationOnRedirectIntercept) { |
| 6849 | interceptor()->set_intercept_redirect(true); |
| 6850 | interceptor()->set_redirect_headers(MockURLRequestInterceptor::ok_headers()); |
| 6851 | interceptor()->set_redirect_data(MockURLRequestInterceptor::ok_data()); |
| 6852 | |
| 6853 | ASSERT_TRUE(test_server()->Start()); |
| 6854 | |
| 6855 | TestDelegate d; |
| 6856 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6857 | test_server()->GetURL("files/redirect-test.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6858 | req->Start(); |
| 6859 | base::RunLoop().Run(); |
| 6860 | |
| 6861 | EXPECT_TRUE(interceptor()->did_intercept_redirect()); |
| 6862 | // Check we got one good response |
| 6863 | EXPECT_TRUE(req->status().is_success()); |
| 6864 | if (req->status().is_success()) |
| 6865 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 6866 | |
| 6867 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 6868 | EXPECT_EQ(1, d.response_started_count()); |
| 6869 | EXPECT_EQ(0, d.received_redirect_count()); |
| 6870 | |
| 6871 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 6872 | EXPECT_EQ(1, default_network_delegate()->before_send_headers_count()); |
| 6873 | EXPECT_EQ(1, default_network_delegate()->headers_received_count()); |
| 6874 | } |
| 6875 | |
| 6876 | TEST_F(URLRequestInterceptorTestHTTP, |
| 6877 | NetworkDelegateNotificationOnErrorIntercept) { |
| 6878 | // Intercept that error and respond with an OK response. |
| 6879 | interceptor()->set_intercept_final_response(true); |
| 6880 | interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers()); |
| 6881 | interceptor()->set_final_data(MockURLRequestInterceptor::ok_data()); |
| 6882 | default_network_delegate()->set_can_be_intercepted_on_error(true); |
| 6883 | |
| 6884 | ASSERT_TRUE(test_server()->Start()); |
| 6885 | |
| 6886 | TestDelegate d; |
| 6887 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
| 6888 | test_server()->GetURL("files/two-content-lengths.html"), DEFAULT_PRIORITY, |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6889 | &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6890 | req->set_method("GET"); |
| 6891 | req->Start(); |
| 6892 | base::RunLoop().Run(); |
| 6893 | |
| 6894 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 6895 | |
| 6896 | // Check we received one good response. |
| 6897 | EXPECT_TRUE(req->status().is_success()); |
| 6898 | if (req->status().is_success()) |
| 6899 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 6900 | EXPECT_EQ(MockURLRequestInterceptor::ok_data(), d.data_received()); |
| 6901 | EXPECT_EQ(1, d.response_started_count()); |
| 6902 | EXPECT_EQ(0, d.received_redirect_count()); |
| 6903 | |
| 6904 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 6905 | EXPECT_EQ(1, default_network_delegate()->before_send_headers_count()); |
| 6906 | EXPECT_EQ(0, default_network_delegate()->headers_received_count()); |
| 6907 | } |
| 6908 | |
| 6909 | TEST_F(URLRequestInterceptorTestHTTP, |
| 6910 | NetworkDelegateNotificationOnResponseIntercept) { |
| 6911 | // Intercept that error and respond with an OK response. |
| 6912 | interceptor()->set_intercept_final_response(true); |
| 6913 | |
| 6914 | // Intercept with a real URLRequestHttpJob. |
| 6915 | interceptor()->set_use_url_request_http_job(true); |
| 6916 | |
| 6917 | ASSERT_TRUE(test_server()->Start()); |
| 6918 | |
| 6919 | TestDelegate d; |
| 6920 | scoped_ptr<URLRequest> req(default_context().CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6921 | test_server()->GetURL("files/simple.html"), DEFAULT_PRIORITY, &d)); |
bengr | 1bf8e94 | 2014-11-07 01:36:50 | [diff] [blame] | 6922 | req->set_method("GET"); |
| 6923 | req->Start(); |
| 6924 | base::RunLoop().Run(); |
| 6925 | |
| 6926 | EXPECT_TRUE(interceptor()->did_intercept_final()); |
| 6927 | |
| 6928 | // Check we received one good response. |
| 6929 | EXPECT_TRUE(req->status().is_success()); |
| 6930 | if (req->status().is_success()) |
| 6931 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 6932 | EXPECT_EQ("hello", d.data_received()); |
| 6933 | EXPECT_EQ(1, d.response_started_count()); |
| 6934 | EXPECT_EQ(0, d.received_redirect_count()); |
| 6935 | |
| 6936 | EXPECT_EQ(1, default_network_delegate()->created_requests()); |
| 6937 | EXPECT_EQ(2, default_network_delegate()->before_send_headers_count()); |
| 6938 | EXPECT_EQ(2, default_network_delegate()->headers_received_count()); |
| 6939 | } |
| 6940 | |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 6941 | class URLRequestTestReferrerPolicy : public URLRequestTest { |
| 6942 | public: |
| 6943 | URLRequestTestReferrerPolicy() {} |
| 6944 | |
| 6945 | void InstantiateSameOriginServers(SpawnedTestServer::Type origin_type) { |
| 6946 | origin_server_.reset(new SpawnedTestServer( |
| 6947 | origin_type, SpawnedTestServer::kLocalhost, |
| 6948 | origin_type == SpawnedTestServer::TYPE_HTTPS |
| 6949 | ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl")) |
| 6950 | : base::FilePath( |
| 6951 | FILE_PATH_LITERAL("net/data/url_request_unittest")))); |
| 6952 | ASSERT_TRUE(origin_server_->Start()); |
| 6953 | } |
| 6954 | |
| 6955 | void InstantiateCrossOriginServers(SpawnedTestServer::Type origin_type, |
| 6956 | SpawnedTestServer::Type destination_type) { |
| 6957 | origin_server_.reset(new SpawnedTestServer( |
| 6958 | origin_type, SpawnedTestServer::kLocalhost, |
| 6959 | origin_type == SpawnedTestServer::TYPE_HTTPS |
| 6960 | ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl")) |
| 6961 | : base::FilePath( |
| 6962 | FILE_PATH_LITERAL("net/data/url_request_unittest")))); |
| 6963 | ASSERT_TRUE(origin_server_->Start()); |
| 6964 | |
| 6965 | destination_server_.reset(new SpawnedTestServer( |
| 6966 | destination_type, SpawnedTestServer::kLocalhost, |
| 6967 | destination_type == SpawnedTestServer::TYPE_HTTPS |
| 6968 | ? base::FilePath(FILE_PATH_LITERAL("net/data/ssl")) |
| 6969 | : base::FilePath( |
| 6970 | FILE_PATH_LITERAL("net/data/url_request_unittest")))); |
| 6971 | ASSERT_TRUE(destination_server_->Start()); |
| 6972 | } |
| 6973 | |
| 6974 | void VerifyReferrerAfterRedirect(URLRequest::ReferrerPolicy policy, |
| 6975 | const GURL& referrer, |
| 6976 | const GURL& expected) { |
| 6977 | // Create and execute the request: we'll only have a |destination_server_| |
| 6978 | // if the origins are meant to be distinct. Otherwise, we'll use the |
| 6979 | // |origin_server_| for both endpoints. |
| 6980 | GURL destination_url = |
| 6981 | destination_server_ ? destination_server_->GetURL("echoheader?Referer") |
| 6982 | : origin_server_->GetURL("echoheader?Referer"); |
| 6983 | GURL origin_url = |
| 6984 | origin_server_->GetURL("server-redirect?" + destination_url.spec()); |
| 6985 | |
| 6986 | TestDelegate d; |
| 6987 | scoped_ptr<URLRequest> req( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 6988 | default_context_.CreateRequest(origin_url, DEFAULT_PRIORITY, &d)); |
mkwst | 0c5eab87 | 2014-11-21 14:18:54 | [diff] [blame] | 6989 | req->set_referrer_policy(policy); |
| 6990 | req->SetReferrer(referrer.spec()); |
| 6991 | req->Start(); |
| 6992 | base::RunLoop().Run(); |
| 6993 | |
| 6994 | EXPECT_EQ(1, d.response_started_count()); |
| 6995 | EXPECT_EQ(1, d.received_redirect_count()); |
| 6996 | EXPECT_EQ(destination_url, req->url()); |
| 6997 | EXPECT_TRUE(req->status().is_success()); |
| 6998 | EXPECT_EQ(200, req->response_headers()->response_code()); |
| 6999 | |
| 7000 | EXPECT_EQ(expected.spec(), req->referrer()); |
| 7001 | if (expected.is_empty()) |
| 7002 | EXPECT_EQ("None", d.data_received()); |
| 7003 | else |
| 7004 | EXPECT_EQ(expected.spec(), d.data_received()); |
| 7005 | } |
| 7006 | |
| 7007 | SpawnedTestServer* origin_server() const { return origin_server_.get(); } |
| 7008 | |
| 7009 | private: |
| 7010 | scoped_ptr<SpawnedTestServer> origin_server_; |
| 7011 | scoped_ptr<SpawnedTestServer> destination_server_; |
| 7012 | }; |
| 7013 | |
| 7014 | TEST_F(URLRequestTestReferrerPolicy, HTTPToSameOriginHTTP) { |
| 7015 | InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTP); |
| 7016 | |
| 7017 | VerifyReferrerAfterRedirect( |
| 7018 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7019 | origin_server()->GetURL("path/to/file.html"), |
| 7020 | origin_server()->GetURL("path/to/file.html")); |
| 7021 | |
| 7022 | VerifyReferrerAfterRedirect( |
| 7023 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7024 | origin_server()->GetURL("path/to/file.html"), |
| 7025 | origin_server()->GetURL("path/to/file.html")); |
| 7026 | |
| 7027 | VerifyReferrerAfterRedirect( |
| 7028 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7029 | origin_server()->GetURL("path/to/file.html"), |
| 7030 | origin_server()->GetURL("path/to/file.html")); |
| 7031 | |
| 7032 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7033 | origin_server()->GetURL("path/to/file.html"), |
| 7034 | origin_server()->GetURL("path/to/file.html")); |
| 7035 | } |
| 7036 | |
| 7037 | TEST_F(URLRequestTestReferrerPolicy, HTTPToCrossOriginHTTP) { |
| 7038 | InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP, |
| 7039 | SpawnedTestServer::TYPE_HTTP); |
| 7040 | |
| 7041 | VerifyReferrerAfterRedirect( |
| 7042 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7043 | origin_server()->GetURL("path/to/file.html"), |
| 7044 | origin_server()->GetURL("path/to/file.html")); |
| 7045 | |
| 7046 | VerifyReferrerAfterRedirect( |
| 7047 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7048 | origin_server()->GetURL("path/to/file.html"), |
| 7049 | origin_server()->GetURL(std::string())); |
| 7050 | |
| 7051 | VerifyReferrerAfterRedirect( |
| 7052 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7053 | origin_server()->GetURL("path/to/file.html"), |
| 7054 | origin_server()->GetURL(std::string())); |
| 7055 | |
| 7056 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7057 | origin_server()->GetURL("path/to/file.html"), |
| 7058 | origin_server()->GetURL("path/to/file.html")); |
| 7059 | } |
| 7060 | |
| 7061 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToSameOriginHTTPS) { |
| 7062 | InstantiateSameOriginServers(SpawnedTestServer::TYPE_HTTPS); |
| 7063 | |
| 7064 | VerifyReferrerAfterRedirect( |
| 7065 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7066 | origin_server()->GetURL("path/to/file.html"), |
| 7067 | origin_server()->GetURL("path/to/file.html")); |
| 7068 | |
| 7069 | VerifyReferrerAfterRedirect( |
| 7070 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7071 | origin_server()->GetURL("path/to/file.html"), |
| 7072 | origin_server()->GetURL("path/to/file.html")); |
| 7073 | |
| 7074 | VerifyReferrerAfterRedirect( |
| 7075 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7076 | origin_server()->GetURL("path/to/file.html"), |
| 7077 | origin_server()->GetURL("path/to/file.html")); |
| 7078 | |
| 7079 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7080 | origin_server()->GetURL("path/to/file.html"), |
| 7081 | origin_server()->GetURL("path/to/file.html")); |
| 7082 | } |
| 7083 | |
| 7084 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToCrossOriginHTTPS) { |
| 7085 | InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS, |
| 7086 | SpawnedTestServer::TYPE_HTTPS); |
| 7087 | |
| 7088 | VerifyReferrerAfterRedirect( |
| 7089 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7090 | origin_server()->GetURL("path/to/file.html"), |
| 7091 | origin_server()->GetURL("path/to/file.html")); |
| 7092 | |
| 7093 | VerifyReferrerAfterRedirect( |
| 7094 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7095 | origin_server()->GetURL("path/to/file.html"), |
| 7096 | origin_server()->GetURL(std::string())); |
| 7097 | |
| 7098 | VerifyReferrerAfterRedirect( |
| 7099 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7100 | origin_server()->GetURL("path/to/file.html"), |
| 7101 | origin_server()->GetURL(std::string())); |
| 7102 | |
| 7103 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7104 | origin_server()->GetURL("path/to/file.html"), |
| 7105 | origin_server()->GetURL("path/to/file.html")); |
| 7106 | } |
| 7107 | |
| 7108 | TEST_F(URLRequestTestReferrerPolicy, HTTPToHTTPS) { |
| 7109 | InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTP, |
| 7110 | SpawnedTestServer::TYPE_HTTPS); |
| 7111 | |
| 7112 | VerifyReferrerAfterRedirect( |
| 7113 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7114 | origin_server()->GetURL("path/to/file.html"), |
| 7115 | origin_server()->GetURL("path/to/file.html")); |
| 7116 | |
| 7117 | VerifyReferrerAfterRedirect( |
| 7118 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7119 | origin_server()->GetURL("path/to/file.html"), |
| 7120 | origin_server()->GetURL(std::string())); |
| 7121 | |
| 7122 | VerifyReferrerAfterRedirect( |
| 7123 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7124 | origin_server()->GetURL("path/to/file.html"), |
| 7125 | origin_server()->GetURL(std::string())); |
| 7126 | |
| 7127 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7128 | origin_server()->GetURL("path/to/file.html"), |
| 7129 | origin_server()->GetURL("path/to/file.html")); |
| 7130 | } |
| 7131 | |
| 7132 | TEST_F(URLRequestTestReferrerPolicy, HTTPSToHTTP) { |
| 7133 | InstantiateCrossOriginServers(SpawnedTestServer::TYPE_HTTPS, |
| 7134 | SpawnedTestServer::TYPE_HTTP); |
| 7135 | |
| 7136 | VerifyReferrerAfterRedirect( |
| 7137 | URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 7138 | origin_server()->GetURL("path/to/file.html"), GURL()); |
| 7139 | |
| 7140 | VerifyReferrerAfterRedirect( |
| 7141 | URLRequest::REDUCE_REFERRER_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN, |
| 7142 | origin_server()->GetURL("path/to/file.html"), GURL()); |
| 7143 | |
| 7144 | VerifyReferrerAfterRedirect( |
| 7145 | URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN, |
| 7146 | origin_server()->GetURL("path/to/file.html"), |
| 7147 | origin_server()->GetURL(std::string())); |
| 7148 | |
| 7149 | VerifyReferrerAfterRedirect(URLRequest::NEVER_CLEAR_REFERRER, |
| 7150 | origin_server()->GetURL("path/to/file.html"), |
| 7151 | origin_server()->GetURL("path/to/file.html")); |
| 7152 | } |
| 7153 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 7154 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7155 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7156 | HTTPSRequestTest() : default_context_(true) { |
| 7157 | default_context_.set_network_delegate(&default_network_delegate_); |
| 7158 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7159 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 7160 | ~HTTPSRequestTest() override {} |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 7161 | |
| 7162 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7163 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 7164 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7165 | }; |
| 7166 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame] | 7167 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7168 | SpawnedTestServer test_server( |
| 7169 | SpawnedTestServer::TYPE_HTTPS, |
| 7170 | SpawnedTestServer::kLocalhost, |
| 7171 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7172 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7173 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7174 | TestDelegate d; |
| 7175 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7176 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7177 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7178 | r->Start(); |
| 7179 | EXPECT_TRUE(r->is_pending()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7180 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7181 | base::RunLoop().Run(); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7182 | |
| 7183 | EXPECT_EQ(1, d.response_started_count()); |
| 7184 | EXPECT_FALSE(d.received_data_before_response()); |
| 7185 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7186 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7187 | EXPECT_EQ(test_server.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7188 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 7189 | EXPECT_EQ(test_server.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7190 | r->GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7191 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 7192 | } |
| 7193 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 7194 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7195 | SpawnedTestServer::SSLOptions ssl_options( |
| 7196 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 7197 | SpawnedTestServer test_server( |
| 7198 | SpawnedTestServer::TYPE_HTTPS, |
| 7199 | ssl_options, |
| 7200 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7201 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7202 | |
| 7203 | bool err_allowed = true; |
| 7204 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 7205 | TestDelegate d; |
| 7206 | { |
| 7207 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7208 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7209 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7210 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7211 | r->Start(); |
| 7212 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7213 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7214 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7215 | |
| 7216 | EXPECT_EQ(1, d.response_started_count()); |
| 7217 | EXPECT_FALSE(d.received_data_before_response()); |
| 7218 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7219 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7220 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7221 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7222 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7223 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7224 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7225 | } |
| 7226 | } |
| 7227 | } |
| 7228 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 7229 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7230 | SpawnedTestServer::SSLOptions ssl_options( |
| 7231 | SpawnedTestServer::SSLOptions::CERT_EXPIRED); |
| 7232 | SpawnedTestServer test_server( |
| 7233 | SpawnedTestServer::TYPE_HTTPS, |
| 7234 | ssl_options, |
| 7235 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 7236 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7237 | |
| 7238 | // Iterate from false to true, just so that we do the opposite of the |
| 7239 | // previous test in order to increase test coverage. |
| 7240 | bool err_allowed = false; |
| 7241 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 7242 | TestDelegate d; |
| 7243 | { |
| 7244 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7245 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7246 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7247 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7248 | r->Start(); |
| 7249 | EXPECT_TRUE(r->is_pending()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7250 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7251 | base::RunLoop().Run(); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7252 | |
| 7253 | EXPECT_EQ(1, d.response_started_count()); |
| 7254 | EXPECT_FALSE(d.received_data_before_response()); |
| 7255 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7256 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7257 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7258 | CheckSSLInfo(r->ssl_info()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7259 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7260 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 7261 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 7262 | } |
| 7263 | } |
| 7264 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 7265 | |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 7266 | // Tests that servers which require a deprecated cipher suite still work. |
| 7267 | TEST_F(HTTPSRequestTest, CipherFallbackTest) { |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 7268 | TestNetLog net_log; |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 7269 | default_context_.set_net_log(&net_log); |
| 7270 | |
| 7271 | SpawnedTestServer::SSLOptions ssl_options; |
| 7272 | ssl_options.bulk_ciphers = SpawnedTestServer::SSLOptions::BULK_CIPHER_RC4; |
| 7273 | SpawnedTestServer test_server( |
| 7274 | SpawnedTestServer::TYPE_HTTPS, ssl_options, |
| 7275 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7276 | ASSERT_TRUE(test_server.Start()); |
| 7277 | |
| 7278 | TestDelegate d; |
| 7279 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 7280 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
| 7281 | r->Start(); |
| 7282 | EXPECT_TRUE(r->is_pending()); |
| 7283 | |
| 7284 | base::RunLoop().Run(); |
| 7285 | |
| 7286 | EXPECT_EQ(1, d.response_started_count()); |
| 7287 | EXPECT_FALSE(d.received_data_before_response()); |
| 7288 | EXPECT_NE(0, d.bytes_received()); |
| 7289 | CheckSSLInfo(r->ssl_info()); |
| 7290 | EXPECT_EQ(test_server.host_port_pair().host(), r->GetSocketAddress().host()); |
| 7291 | EXPECT_EQ(test_server.host_port_pair().port(), r->GetSocketAddress().port()); |
| 7292 | |
| 7293 | // No version downgrade should have been necessary. |
| 7294 | EXPECT_FALSE(r->ssl_info().connection_status & |
| 7295 | SSL_CONNECTION_VERSION_FALLBACK); |
| 7296 | int expected_version = SSL_CONNECTION_VERSION_TLS1_2; |
| 7297 | if (SSLClientSocket::GetMaxSupportedSSLVersion() < |
| 7298 | SSL_PROTOCOL_VERSION_TLS1_2) { |
| 7299 | expected_version = SSL_CONNECTION_VERSION_TLS1_1; |
| 7300 | } |
| 7301 | EXPECT_EQ(expected_version, |
| 7302 | SSLConnectionStatusToVersion(r->ssl_info().connection_status)); |
| 7303 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 7304 | TestNetLog::CapturedEntryList entries; |
davidben | a4c9d06 | 2015-04-03 22:34:25 | [diff] [blame] | 7305 | net_log.GetEntries(&entries); |
| 7306 | ExpectLogContainsSomewhere(entries, 0, NetLog::TYPE_SSL_CIPHER_FALLBACK, |
| 7307 | NetLog::PHASE_NONE); |
| 7308 | } |
| 7309 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7310 | // This tests that a load of www.google.com with a certificate error sets |
| 7311 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 7312 | // the interstitial to be fatal. |
| 7313 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7314 | SpawnedTestServer::SSLOptions ssl_options( |
| 7315 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 7316 | SpawnedTestServer test_server( |
| 7317 | SpawnedTestServer::TYPE_HTTPS, |
| 7318 | ssl_options, |
| 7319 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7320 | ASSERT_TRUE(test_server.Start()); |
| 7321 | |
| 7322 | // We require that the URL be www.google.com in order to pick up the |
| 7323 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 7324 | // 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] | 7325 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7326 | |
| 7327 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7328 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7329 | TestURLRequestContext context(true); |
| 7330 | context.set_network_delegate(&network_delegate); |
| 7331 | context.set_host_resolver(&host_resolver); |
| 7332 | TransportSecurityState transport_security_state; |
| 7333 | context.set_transport_security_state(&transport_security_state); |
| 7334 | context.Init(); |
| 7335 | |
| 7336 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7337 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 7338 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7339 | test_server.host_port_pair().port())), |
| 7340 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7341 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7342 | r->Start(); |
| 7343 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7344 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7345 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7346 | |
| 7347 | EXPECT_EQ(1, d.response_started_count()); |
| 7348 | EXPECT_FALSE(d.received_data_before_response()); |
| 7349 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7350 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 7351 | } |
| 7352 | |
| 7353 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 7354 | // TransportSecurityState. |
| 7355 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 7356 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 7357 | // matter. It just has to be any error. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7358 | SpawnedTestServer::SSLOptions ssl_options( |
| 7359 | SpawnedTestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 7360 | SpawnedTestServer test_server( |
| 7361 | SpawnedTestServer::TYPE_HTTPS, |
| 7362 | ssl_options, |
| 7363 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7364 | ASSERT_TRUE(test_server.Start()); |
| 7365 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7366 | // We require that the URL be www.google.com in order to pick up the static |
| 7367 | // and dynamic STS and PKP entries in the TransportSecurityState. This means |
| 7368 | // that we have to use a MockHostResolver in order to direct www.google.com to |
| 7369 | // the testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7370 | |
| 7371 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 7372 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7373 | TestURLRequestContext context(true); |
| 7374 | context.set_network_delegate(&network_delegate); |
| 7375 | context.set_host_resolver(&host_resolver); |
| 7376 | TransportSecurityState transport_security_state; |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7377 | |
| 7378 | TransportSecurityState::DomainState static_domain_state; |
| 7379 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Adam Langley | 5cbb7d7a | 2014-09-25 23:14:12 | [diff] [blame] | 7380 | "www.google.com", &static_domain_state)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7381 | context.set_transport_security_state(&transport_security_state); |
| 7382 | context.Init(); |
| 7383 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7384 | TransportSecurityState::DomainState dynamic_domain_state; |
| 7385 | EXPECT_FALSE(transport_security_state.GetDynamicDomainState( |
| 7386 | "www.google.com", &dynamic_domain_state)); |
| 7387 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7388 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7389 | scoped_ptr<URLRequest> r(context.CreateRequest( |
| 7390 | GURL(base::StringPrintf("https://www.google.com:%d", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7391 | test_server.host_port_pair().port())), |
| 7392 | DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7393 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7394 | r->Start(); |
| 7395 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7396 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7397 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7398 | |
| 7399 | EXPECT_EQ(1, d.response_started_count()); |
| 7400 | EXPECT_FALSE(d.received_data_before_response()); |
| 7401 | EXPECT_TRUE(d.have_certificate_errors()); |
| 7402 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 7403 | |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7404 | // Get a fresh copy of the states, and check that they haven't changed. |
| 7405 | TransportSecurityState::DomainState new_static_domain_state; |
| 7406 | EXPECT_TRUE(transport_security_state.GetStaticDomainState( |
Adam Langley | 5cbb7d7a | 2014-09-25 23:14:12 | [diff] [blame] | 7407 | "www.google.com", &new_static_domain_state)); |
[email protected] | 9e6968d | 2014-05-07 21:46:26 | [diff] [blame] | 7408 | TransportSecurityState::DomainState new_dynamic_domain_state; |
| 7409 | EXPECT_FALSE(transport_security_state.GetDynamicDomainState( |
| 7410 | "www.google.com", &new_dynamic_domain_state)); |
| 7411 | |
| 7412 | EXPECT_EQ(new_static_domain_state.sts.upgrade_mode, |
| 7413 | static_domain_state.sts.upgrade_mode); |
| 7414 | EXPECT_EQ(new_static_domain_state.sts.include_subdomains, |
| 7415 | static_domain_state.sts.include_subdomains); |
| 7416 | EXPECT_EQ(new_static_domain_state.pkp.include_subdomains, |
| 7417 | static_domain_state.pkp.include_subdomains); |
| 7418 | EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.spki_hashes, |
| 7419 | static_domain_state.pkp.spki_hashes)); |
| 7420 | EXPECT_TRUE(FingerprintsEqual(new_static_domain_state.pkp.bad_spki_hashes, |
| 7421 | static_domain_state.pkp.bad_spki_hashes)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7422 | } |
| 7423 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7424 | // Make sure HSTS preserves a POST request's method and body. |
| 7425 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 7426 | static const char kData[] = "hello world"; |
| 7427 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7428 | SpawnedTestServer::SSLOptions ssl_options( |
| 7429 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7430 | SpawnedTestServer test_server( |
| 7431 | SpawnedTestServer::TYPE_HTTPS, |
| 7432 | ssl_options, |
| 7433 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7434 | ASSERT_TRUE(test_server.Start()); |
| 7435 | |
| 7436 | |
| 7437 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 7438 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7439 | // the SpawnedTestServer. By default, MockHostResolver maps all hosts |
| 7440 | // to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7441 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7442 | |
| 7443 | // Force https for www.somewhere.com. |
| 7444 | TransportSecurityState transport_security_state; |
[email protected] | 474f079e | 2013-03-02 19:11:20 | [diff] [blame] | 7445 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1000); |
| 7446 | bool include_subdomains = false; |
| 7447 | transport_security_state.AddHSTS("www.somewhere.com", expiry, |
| 7448 | include_subdomains); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7449 | |
| 7450 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 7451 | |
| 7452 | TestURLRequestContext context(true); |
| 7453 | context.set_host_resolver(&host_resolver); |
| 7454 | context.set_transport_security_state(&transport_security_state); |
| 7455 | context.set_network_delegate(&network_delegate); |
| 7456 | context.Init(); |
| 7457 | |
| 7458 | TestDelegate d; |
| 7459 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 7460 | // cause a certificate error. Ignore the error. |
| 7461 | d.set_allow_certificate_errors(true); |
| 7462 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7463 | scoped_ptr<URLRequest> req(context.CreateRequest( |
| 7464 | GURL(base::StringPrintf("http://www.somewhere.com:%d/echo", |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7465 | test_server.host_port_pair().port())), |
| 7466 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7467 | req->set_method("POST"); |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 7468 | req->set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7469 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7470 | req->Start(); |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7471 | base::RunLoop().Run(); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7472 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7473 | EXPECT_EQ("https", req->url().scheme()); |
| 7474 | EXPECT_EQ("POST", req->method()); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7475 | EXPECT_EQ(kData, d.data_received()); |
[email protected] | 07d9bfd | 2013-06-27 14:16:40 | [diff] [blame] | 7476 | |
| 7477 | LoadTimingInfo load_timing_info; |
| 7478 | network_delegate.GetLoadTimingInfoBeforeRedirect(&load_timing_info); |
| 7479 | // LoadTimingInfo of HSTS redirects is similar to that of network cache hits |
| 7480 | TestLoadTimingCacheHitNoNetwork(load_timing_info); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 7481 | } |
| 7482 | |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 7483 | // Make sure that the CORS headers are added to cross-origin HSTS redirects. |
| 7484 | TEST_F(HTTPSRequestTest, HSTSCrossOriginAddHeaders) { |
| 7485 | static const char kOriginHeaderValue[] = "http://www.example.com"; |
| 7486 | |
| 7487 | SpawnedTestServer::SSLOptions ssl_options( |
| 7488 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7489 | SpawnedTestServer test_server( |
| 7490 | SpawnedTestServer::TYPE_HTTPS, |
| 7491 | ssl_options, |
| 7492 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7493 | ASSERT_TRUE(test_server.Start()); |
| 7494 | |
| 7495 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 7496 | // address, so a MockHostResolver is needed to redirect example.net to the |
| 7497 | // SpawnedTestServer. MockHostResolver maps all hosts to 127.0.0.1 by default. |
| 7498 | MockHostResolver host_resolver; |
| 7499 | |
| 7500 | TransportSecurityState transport_security_state; |
| 7501 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 7502 | bool include_subdomains = false; |
| 7503 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 7504 | |
| 7505 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 7506 | |
| 7507 | MockCertVerifier cert_verifier; |
| 7508 | cert_verifier.set_default_result(OK); |
| 7509 | |
| 7510 | TestURLRequestContext context(true); |
| 7511 | context.set_host_resolver(&host_resolver); |
| 7512 | context.set_transport_security_state(&transport_security_state); |
| 7513 | context.set_network_delegate(&network_delegate); |
| 7514 | context.set_cert_verifier(&cert_verifier); |
| 7515 | context.Init(); |
| 7516 | |
| 7517 | GURL hsts_http_url(base::StringPrintf("http://example.net:%d/somehstssite", |
| 7518 | test_server.host_port_pair().port())); |
| 7519 | url::Replacements<char> replacements; |
| 7520 | const char kNewScheme[] = "https"; |
| 7521 | replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); |
| 7522 | GURL hsts_https_url = hsts_http_url.ReplaceComponents(replacements); |
| 7523 | |
| 7524 | TestDelegate d; |
| 7525 | // Quit on redirect to allow response header inspection upon redirect. |
| 7526 | d.set_quit_on_redirect(true); |
| 7527 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7528 | scoped_ptr<URLRequest> req( |
| 7529 | context.CreateRequest(hsts_http_url, DEFAULT_PRIORITY, &d)); |
rob | 4e0be1f | 2014-09-11 23:40:22 | [diff] [blame] | 7530 | // Set Origin header to simulate a cross-origin request. |
| 7531 | HttpRequestHeaders request_headers; |
| 7532 | request_headers.SetHeader("Origin", kOriginHeaderValue); |
| 7533 | req->SetExtraRequestHeaders(request_headers); |
| 7534 | |
| 7535 | req->Start(); |
| 7536 | base::RunLoop().Run(); |
| 7537 | |
| 7538 | EXPECT_EQ(1, d.received_redirect_count()); |
| 7539 | |
| 7540 | const HttpResponseHeaders* headers = req->response_headers(); |
| 7541 | std::string redirect_location; |
| 7542 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); |
| 7543 | EXPECT_EQ(hsts_https_url.spec(), redirect_location); |
| 7544 | |
| 7545 | std::string received_cors_header; |
| 7546 | EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", |
| 7547 | &received_cors_header)); |
| 7548 | EXPECT_EQ(kOriginHeaderValue, received_cors_header); |
| 7549 | } |
| 7550 | |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 7551 | // This just tests the behaviour of GetHSTSRedirect(). End-to-end tests of HSTS |
| 7552 | // are performed in net/websockets/websocket_end_to_end_test.cc. |
| 7553 | TEST(WebSocketURLRequestTest, HSTSApplied) { |
| 7554 | TestNetworkDelegate network_delegate; |
| 7555 | TransportSecurityState transport_security_state; |
| 7556 | base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); |
| 7557 | bool include_subdomains = false; |
| 7558 | transport_security_state.AddHSTS("example.net", expiry, include_subdomains); |
| 7559 | TestURLRequestContext context(true); |
| 7560 | context.set_transport_security_state(&transport_security_state); |
| 7561 | context.set_network_delegate(&network_delegate); |
| 7562 | context.Init(); |
| 7563 | GURL ws_url("ws://example.net/echo"); |
| 7564 | TestDelegate delegate; |
| 7565 | scoped_ptr<URLRequest> request( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7566 | context.CreateRequest(ws_url, DEFAULT_PRIORITY, &delegate)); |
Adam Rice | cb76ac6 | 2015-02-20 05:33:25 | [diff] [blame] | 7567 | EXPECT_TRUE(request->GetHSTSRedirect(&ws_url)); |
| 7568 | EXPECT_TRUE(ws_url.SchemeIs("wss")); |
| 7569 | } |
| 7570 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7571 | namespace { |
| 7572 | |
| 7573 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 7574 | public: |
| 7575 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 7576 | } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 7577 | void OnCertificateRequested(URLRequest* request, |
| 7578 | SSLCertRequestInfo* cert_request_info) override { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7579 | on_certificate_requested_count_++; |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 7580 | base::MessageLoop::current()->Quit(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7581 | } |
| 7582 | int on_certificate_requested_count() { |
| 7583 | return on_certificate_requested_count_; |
| 7584 | } |
| 7585 | private: |
| 7586 | int on_certificate_requested_count_; |
| 7587 | }; |
| 7588 | |
| 7589 | } // namespace |
| 7590 | |
| 7591 | // TODO(davidben): Test the rest of the code. Specifically, |
| 7592 | // - Filtering which certificates to select. |
| 7593 | // - Sending a certificate back. |
| 7594 | // - Getting a certificate request in an SSL renegotiation sending the |
| 7595 | // HTTP request. |
| 7596 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7597 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7598 | ssl_options.request_client_certificate = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7599 | SpawnedTestServer test_server( |
| 7600 | SpawnedTestServer::TYPE_HTTPS, |
| 7601 | ssl_options, |
| 7602 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7603 | ASSERT_TRUE(test_server.Start()); |
| 7604 | |
| 7605 | SSLClientAuthTestDelegate d; |
| 7606 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7607 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7608 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7609 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7610 | r->Start(); |
| 7611 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7612 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7613 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7614 | |
| 7615 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 7616 | EXPECT_FALSE(d.received_data_before_response()); |
| 7617 | EXPECT_EQ(0, d.bytes_received()); |
| 7618 | |
| 7619 | // Send no certificate. |
| 7620 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 7621 | // all platforms so we can test sending a cert as well. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7622 | r->ContinueWithCertificate(NULL); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7623 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7624 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7625 | |
| 7626 | EXPECT_EQ(1, d.response_started_count()); |
| 7627 | EXPECT_FALSE(d.received_data_before_response()); |
| 7628 | EXPECT_NE(0, d.bytes_received()); |
| 7629 | } |
| 7630 | } |
| 7631 | |
| 7632 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 7633 | // Test that we attempt a session resume when making two connections to the |
| 7634 | // same host. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7635 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7636 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7637 | SpawnedTestServer test_server( |
| 7638 | SpawnedTestServer::TYPE_HTTPS, |
| 7639 | ssl_options, |
| 7640 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7641 | ASSERT_TRUE(test_server.Start()); |
| 7642 | |
| 7643 | SSLClientSocket::ClearSessionCache(); |
| 7644 | |
| 7645 | { |
| 7646 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7647 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7648 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7649 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7650 | r->Start(); |
| 7651 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7652 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7653 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7654 | |
| 7655 | EXPECT_EQ(1, d.response_started_count()); |
| 7656 | } |
| 7657 | |
| 7658 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 7659 | CloseAllConnections(); |
| 7660 | |
| 7661 | { |
| 7662 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7663 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7664 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7665 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7666 | r->Start(); |
| 7667 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7668 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7669 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7670 | |
| 7671 | // The response will look like; |
| 7672 | // insert abc |
| 7673 | // lookup abc |
| 7674 | // insert xyz |
| 7675 | // |
| 7676 | // With a newline at the end which makes the split think that there are |
| 7677 | // four lines. |
| 7678 | |
| 7679 | EXPECT_EQ(1, d.response_started_count()); |
| 7680 | std::vector<std::string> lines; |
| 7681 | base::SplitString(d.data_received(), '\n', &lines); |
| 7682 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 7683 | |
| 7684 | std::string session_id; |
| 7685 | |
| 7686 | for (size_t i = 0; i < 2; i++) { |
| 7687 | std::vector<std::string> parts; |
| 7688 | base::SplitString(lines[i], '\t', &parts); |
| 7689 | ASSERT_EQ(2u, parts.size()); |
| 7690 | if (i == 0) { |
| 7691 | EXPECT_EQ("insert", parts[0]); |
| 7692 | session_id = parts[1]; |
| 7693 | } else { |
| 7694 | EXPECT_EQ("lookup", parts[0]); |
| 7695 | EXPECT_EQ(session_id, parts[1]); |
| 7696 | } |
| 7697 | } |
| 7698 | } |
| 7699 | } |
| 7700 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7701 | // AssertTwoDistinctSessionsInserted checks that |session_info|, which must be |
| 7702 | // the result of fetching "ssl-session-cache" from the test server, indicates |
| 7703 | // that exactly two different sessions were inserted, with no lookups etc. |
| 7704 | static void AssertTwoDistinctSessionsInserted(const string& session_info) { |
| 7705 | std::vector<std::string> lines; |
| 7706 | base::SplitString(session_info, '\n', &lines); |
| 7707 | ASSERT_EQ(3u, lines.size()) << session_info; |
| 7708 | |
| 7709 | std::string session_id; |
| 7710 | for (size_t i = 0; i < 2; i++) { |
| 7711 | std::vector<std::string> parts; |
| 7712 | base::SplitString(lines[i], '\t', &parts); |
| 7713 | ASSERT_EQ(2u, parts.size()); |
| 7714 | EXPECT_EQ("insert", parts[0]); |
| 7715 | if (i == 0) { |
| 7716 | session_id = parts[1]; |
| 7717 | } else { |
| 7718 | EXPECT_NE(session_id, parts[1]); |
| 7719 | } |
| 7720 | } |
| 7721 | } |
| 7722 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7723 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 7724 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 7725 | // differs. |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7726 | SpawnedTestServer::SSLOptions ssl_options; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7727 | ssl_options.record_resume = true; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 7728 | SpawnedTestServer test_server( |
| 7729 | SpawnedTestServer::TYPE_HTTPS, |
| 7730 | ssl_options, |
| 7731 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7732 | ASSERT_TRUE(test_server.Start()); |
| 7733 | |
| 7734 | SSLClientSocket::ClearSessionCache(); |
| 7735 | |
| 7736 | { |
| 7737 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7738 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7739 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7740 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7741 | r->Start(); |
| 7742 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7743 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7744 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7745 | |
| 7746 | EXPECT_EQ(1, d.response_started_count()); |
| 7747 | } |
| 7748 | |
| 7749 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 7750 | HttpNetworkSession::Params params; |
| 7751 | params.host_resolver = default_context_.host_resolver(); |
| 7752 | params.cert_verifier = default_context_.cert_verifier(); |
[email protected] | b1c988b | 2013-06-13 06:48:11 | [diff] [blame] | 7753 | params.transport_security_state = default_context_.transport_security_state(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7754 | params.proxy_service = default_context_.proxy_service(); |
| 7755 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 7756 | params.http_auth_handler_factory = |
| 7757 | default_context_.http_auth_handler_factory(); |
[email protected] | cc05edc | 2013-03-08 18:04:41 | [diff] [blame] | 7758 | params.network_delegate = &default_network_delegate_; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7759 | params.http_server_properties = default_context_.http_server_properties(); |
| 7760 | params.ssl_session_cache_shard = "alternate"; |
| 7761 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 7762 | scoped_ptr<HttpCache> cache(new HttpCache( |
| 7763 | new HttpNetworkSession(params), |
| 7764 | HttpCache::DefaultBackend::InMemory(0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7765 | |
| 7766 | default_context_.set_http_transaction_factory(cache.get()); |
| 7767 | |
| 7768 | { |
| 7769 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7770 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7771 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7772 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 7773 | r->Start(); |
| 7774 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7775 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 7776 | base::RunLoop().Run(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7777 | |
| 7778 | // The response will look like; |
| 7779 | // insert abc |
| 7780 | // insert xyz |
| 7781 | // |
| 7782 | // With a newline at the end which makes the split think that there are |
| 7783 | // three lines. |
| 7784 | |
| 7785 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7786 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 7787 | } |
| 7788 | } |
| 7789 | |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 7790 | #if defined(OS_WIN) |
| 7791 | |
| 7792 | namespace { |
| 7793 | |
| 7794 | bool IsECDSACipherSuite(uint16_t cipher_suite) { |
| 7795 | const char* key_exchange; |
| 7796 | const char* cipher; |
| 7797 | const char* mac; |
| 7798 | bool is_aead; |
| 7799 | SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead, cipher_suite); |
| 7800 | return std::string(key_exchange).find("ECDSA") != std::string::npos; |
| 7801 | } |
| 7802 | |
| 7803 | } // namespace |
| 7804 | |
| 7805 | // Test that ECDSA is disabled on Windows XP, where ECDSA certificates cannot be |
| 7806 | // verified. |
davidben | 9399c95 | 2014-10-07 04:09:11 | [diff] [blame] | 7807 | TEST_F(HTTPSRequestTest, DisableECDSAOnXP) { |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 7808 | if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
| 7809 | LOG(INFO) << "Skipping test on this version."; |
| 7810 | return; |
| 7811 | } |
| 7812 | |
| 7813 | SpawnedTestServer test_server( |
| 7814 | SpawnedTestServer::TYPE_HTTPS, |
| 7815 | SpawnedTestServer::kLocalhost, |
| 7816 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7817 | ASSERT_TRUE(test_server.Start()); |
| 7818 | |
| 7819 | TestDelegate d; |
| 7820 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7821 | test_server.GetURL("client-cipher-list"), DEFAULT_PRIORITY, &d)); |
davidben | 8ecc307 | 2014-09-03 23:19:09 | [diff] [blame] | 7822 | r->Start(); |
| 7823 | EXPECT_TRUE(r->is_pending()); |
| 7824 | |
| 7825 | base::RunLoop().Run(); |
| 7826 | |
| 7827 | EXPECT_EQ(1, d.response_started_count()); |
| 7828 | std::vector<std::string> lines; |
| 7829 | base::SplitString(d.data_received(), '\n', &lines); |
| 7830 | |
| 7831 | for (size_t i = 0; i < lines.size(); i++) { |
| 7832 | int cipher_suite; |
| 7833 | ASSERT_TRUE(base::StringToInt(lines[i], &cipher_suite)); |
| 7834 | EXPECT_FALSE(IsECDSACipherSuite(cipher_suite)) |
| 7835 | << "ClientHello advertised " << cipher_suite; |
| 7836 | } |
| 7837 | } |
| 7838 | |
| 7839 | #endif // OS_WIN |
| 7840 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7841 | class TestSSLConfigService : public SSLConfigService { |
| 7842 | public: |
| 7843 | TestSSLConfigService(bool ev_enabled, |
| 7844 | bool online_rev_checking, |
| 7845 | bool rev_checking_required_local_anchors) |
| 7846 | : ev_enabled_(ev_enabled), |
| 7847 | online_rev_checking_(online_rev_checking), |
| 7848 | rev_checking_required_local_anchors_( |
| 7849 | rev_checking_required_local_anchors), |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 7850 | min_version_(kDefaultSSLVersionMin), |
| 7851 | fallback_min_version_(kDefaultSSLVersionFallbackMin) {} |
| 7852 | |
| 7853 | void set_min_version(uint16 version) { |
| 7854 | min_version_ = version; |
| 7855 | } |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7856 | |
| 7857 | void set_fallback_min_version(uint16 version) { |
| 7858 | fallback_min_version_ = version; |
| 7859 | } |
| 7860 | |
| 7861 | // SSLConfigService: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 7862 | void GetSSLConfig(SSLConfig* config) override { |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7863 | *config = SSLConfig(); |
| 7864 | config->rev_checking_enabled = online_rev_checking_; |
| 7865 | config->verify_ev_cert = ev_enabled_; |
| 7866 | config->rev_checking_required_local_anchors = |
| 7867 | rev_checking_required_local_anchors_; |
| 7868 | if (fallback_min_version_) { |
| 7869 | config->version_fallback_min = fallback_min_version_; |
| 7870 | } |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 7871 | if (min_version_) { |
| 7872 | config->version_min = min_version_; |
| 7873 | } |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7874 | } |
| 7875 | |
| 7876 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 7877 | ~TestSSLConfigService() override {} |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7878 | |
| 7879 | private: |
| 7880 | const bool ev_enabled_; |
| 7881 | const bool online_rev_checking_; |
| 7882 | const bool rev_checking_required_local_anchors_; |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 7883 | uint16 min_version_; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7884 | uint16 fallback_min_version_; |
| 7885 | }; |
| 7886 | |
| 7887 | class FallbackTestURLRequestContext : public TestURLRequestContext { |
| 7888 | public: |
| 7889 | explicit FallbackTestURLRequestContext(bool delay_initialization) |
| 7890 | : TestURLRequestContext(delay_initialization) {} |
| 7891 | |
| 7892 | void set_fallback_min_version(uint16 version) { |
| 7893 | TestSSLConfigService *ssl_config_service = |
| 7894 | new TestSSLConfigService(true /* check for EV */, |
| 7895 | false /* online revocation checking */, |
| 7896 | false /* require rev. checking for local |
| 7897 | anchors */); |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 7898 | ssl_config_service->set_min_version(SSL_PROTOCOL_VERSION_SSL3); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7899 | ssl_config_service->set_fallback_min_version(version); |
| 7900 | set_ssl_config_service(ssl_config_service); |
| 7901 | } |
| 7902 | }; |
| 7903 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7904 | class HTTPSFallbackTest : public testing::Test { |
| 7905 | public: |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7906 | HTTPSFallbackTest() : context_(true) {} |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 7907 | ~HTTPSFallbackTest() override {} |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7908 | |
| 7909 | protected: |
| 7910 | void DoFallbackTest(const SpawnedTestServer::SSLOptions& ssl_options) { |
| 7911 | DCHECK(!request_); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7912 | context_.Init(); |
| 7913 | delegate_.set_allow_certificate_errors(true); |
| 7914 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7915 | SpawnedTestServer test_server( |
| 7916 | SpawnedTestServer::TYPE_HTTPS, |
| 7917 | ssl_options, |
| 7918 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 7919 | ASSERT_TRUE(test_server.Start()); |
| 7920 | |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 7921 | request_ = context_.CreateRequest(test_server.GetURL(std::string()), |
| 7922 | DEFAULT_PRIORITY, &delegate_); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7923 | request_->Start(); |
| 7924 | |
| 7925 | base::RunLoop().Run(); |
| 7926 | } |
| 7927 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7928 | void set_fallback_min_version(uint16 version) { |
| 7929 | context_.set_fallback_min_version(version); |
| 7930 | } |
| 7931 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7932 | void ExpectConnection(int version) { |
| 7933 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 7934 | EXPECT_NE(0, delegate_.bytes_received()); |
| 7935 | EXPECT_EQ(version, SSLConnectionStatusToVersion( |
| 7936 | request_->ssl_info().connection_status)); |
| 7937 | EXPECT_TRUE(request_->ssl_info().connection_status & |
| 7938 | SSL_CONNECTION_VERSION_FALLBACK); |
| 7939 | } |
| 7940 | |
| 7941 | void ExpectFailure(int error) { |
| 7942 | EXPECT_EQ(1, delegate_.response_started_count()); |
| 7943 | EXPECT_FALSE(request_->status().is_success()); |
| 7944 | EXPECT_EQ(URLRequestStatus::FAILED, request_->status().status()); |
| 7945 | EXPECT_EQ(error, request_->status().error()); |
| 7946 | } |
| 7947 | |
| 7948 | private: |
| 7949 | TestDelegate delegate_; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 7950 | FallbackTestURLRequestContext context_; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7951 | scoped_ptr<URLRequest> request_; |
| 7952 | }; |
| 7953 | |
| 7954 | // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more |
| 7955 | // than necessary. |
| 7956 | TEST_F(HTTPSFallbackTest, TLSv1Fallback) { |
| 7957 | SpawnedTestServer::SSLOptions ssl_options( |
| 7958 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7959 | ssl_options.tls_intolerant = |
| 7960 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 7961 | |
| 7962 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7963 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
| 7964 | } |
| 7965 | |
| 7966 | // This test is disabled on Android because the remote test server doesn't cause |
| 7967 | // a TCP reset. |
| 7968 | #if !defined(OS_ANDROID) |
| 7969 | // Tests fallback to TLS 1.0 on connection reset. |
| 7970 | TEST_F(HTTPSFallbackTest, TLSv1FallbackReset) { |
| 7971 | SpawnedTestServer::SSLOptions ssl_options( |
| 7972 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 7973 | ssl_options.tls_intolerant = |
| 7974 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 7975 | ssl_options.tls_intolerance_type = |
| 7976 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 7977 | |
| 7978 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7979 | ExpectConnection(SSL_CONNECTION_VERSION_TLS1); |
| 7980 | } |
| 7981 | #endif // !OS_ANDROID |
| 7982 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 7983 | // Tests that we don't fallback on handshake failure with servers that implement |
| 7984 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7985 | TEST_F(HTTPSFallbackTest, FallbackSCSV) { |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7986 | SpawnedTestServer::SSLOptions ssl_options( |
| 7987 | SpawnedTestServer::SSLOptions::CERT_OK); |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 7988 | // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7989 | // a version fallback. |
| 7990 | ssl_options.tls_intolerant = |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 7991 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 7992 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 7993 | // connections are rejected. |
| 7994 | ssl_options.fallback_scsv_enabled = true; |
| 7995 | |
| 7996 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 7997 | |
| 7998 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH is how the server simulates version |
| 7999 | // intolerance. If the fallback SCSV is processed when the original error |
| 8000 | // that caused the fallback should be returned, which should be |
| 8001 | // ERR_SSL_VERSION_OR_CIPHER_MISMATCH. |
| 8002 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
| 8003 | } |
| 8004 | |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8005 | // Tests that we don't fallback on connection closed with servers that implement |
| 8006 | // TLS_FALLBACK_SCSV. Also ensure that the original error code is reported. |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8007 | TEST_F(HTTPSFallbackTest, FallbackSCSVClosed) { |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8008 | SpawnedTestServer::SSLOptions ssl_options( |
| 8009 | SpawnedTestServer::SSLOptions::CERT_OK); |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8010 | // Configure HTTPS server to be intolerant of TLS >= 1.1 in order to trigger |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8011 | // a version fallback. |
| 8012 | ssl_options.tls_intolerant = |
davidben | c8dc58c | 2015-01-06 21:08:43 | [diff] [blame] | 8013 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
[email protected] | 1283330 | 2014-07-02 01:57:31 | [diff] [blame] | 8014 | ssl_options.tls_intolerance_type = |
| 8015 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 8016 | // Have the server process TLS_FALLBACK_SCSV so that version fallback |
| 8017 | // connections are rejected. |
| 8018 | ssl_options.fallback_scsv_enabled = true; |
| 8019 | |
| 8020 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8021 | |
| 8022 | // The original error should be replayed on rejected fallback. |
| 8023 | ExpectFailure(ERR_CONNECTION_CLOSED); |
| 8024 | } |
| 8025 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8026 | // Tests that the SSLv3 fallback doesn't happen by default. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8027 | TEST_F(HTTPSFallbackTest, SSLv3Fallback) { |
| 8028 | SpawnedTestServer::SSLOptions ssl_options( |
| 8029 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8030 | ssl_options.tls_intolerant = |
| 8031 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 8032 | |
| 8033 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 8034 | ExpectFailure(ERR_SSL_VERSION_OR_CIPHER_MISMATCH); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8035 | } |
| 8036 | |
| 8037 | // Tests that the SSLv3 fallback works when explicitly enabled. |
| 8038 | TEST_F(HTTPSFallbackTest, SSLv3FallbackEnabled) { |
| 8039 | SpawnedTestServer::SSLOptions ssl_options( |
| 8040 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8041 | ssl_options.tls_intolerant = |
| 8042 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 8043 | set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3); |
| 8044 | |
| 8045 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8046 | ExpectConnection(SSL_CONNECTION_VERSION_SSL3); |
| 8047 | } |
| 8048 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8049 | // Tests that the SSLv3 fallback triggers on closed connections when explicitly |
| 8050 | // enabled. |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8051 | TEST_F(HTTPSFallbackTest, SSLv3FallbackClosed) { |
| 8052 | SpawnedTestServer::SSLOptions ssl_options( |
| 8053 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8054 | ssl_options.tls_intolerant = |
| 8055 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 8056 | ssl_options.tls_intolerance_type = |
| 8057 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8058 | set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3); |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8059 | |
| 8060 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8061 | ExpectConnection(SSL_CONNECTION_VERSION_SSL3); |
| 8062 | } |
| 8063 | |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8064 | // Test that SSLv3 fallback probe connections don't cause sessions to be cached. |
| 8065 | TEST_F(HTTPSRequestTest, SSLv3FallbackNoCache) { |
| 8066 | SpawnedTestServer::SSLOptions ssl_options( |
| 8067 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8068 | ssl_options.tls_intolerant = |
| 8069 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 8070 | ssl_options.tls_intolerance_type = |
| 8071 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_CLOSE; |
| 8072 | ssl_options.record_resume = true; |
| 8073 | |
| 8074 | SpawnedTestServer test_server( |
| 8075 | SpawnedTestServer::TYPE_HTTPS, |
| 8076 | ssl_options, |
| 8077 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8078 | ASSERT_TRUE(test_server.Start()); |
| 8079 | |
| 8080 | SSLClientSocket::ClearSessionCache(); |
| 8081 | |
| 8082 | // Make a connection that does a probe fallback to SSLv3 but fails because |
| 8083 | // SSLv3 fallback is disabled. We don't wish a session for this connection to |
| 8084 | // be inserted locally. |
| 8085 | { |
| 8086 | TestDelegate delegate; |
| 8087 | FallbackTestURLRequestContext context(true); |
| 8088 | |
Adam Langley | ac6f24b4 | 2014-10-31 20:24:02 | [diff] [blame] | 8089 | context.set_fallback_min_version(SSL_PROTOCOL_VERSION_TLS1); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8090 | context.Init(); |
| 8091 | scoped_ptr<URLRequest> request(context.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8092 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &delegate)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8093 | request->Start(); |
| 8094 | |
| 8095 | base::RunLoop().Run(); |
| 8096 | |
| 8097 | EXPECT_EQ(1, delegate.response_started_count()); |
| 8098 | EXPECT_FALSE(request->status().is_success()); |
| 8099 | EXPECT_EQ(URLRequestStatus::FAILED, request->status().status()); |
| 8100 | EXPECT_EQ(ERR_SSL_FALLBACK_BEYOND_MINIMUM_VERSION, |
| 8101 | request->status().error()); |
| 8102 | } |
| 8103 | |
| 8104 | // Now allow SSLv3 connections and request the session cache log. |
| 8105 | { |
| 8106 | TestDelegate delegate; |
| 8107 | FallbackTestURLRequestContext context(true); |
| 8108 | context.set_fallback_min_version(SSL_PROTOCOL_VERSION_SSL3); |
| 8109 | |
| 8110 | context.Init(); |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8111 | scoped_ptr<URLRequest> request(context.CreateRequest( |
| 8112 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &delegate)); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8113 | request->Start(); |
| 8114 | |
| 8115 | base::RunLoop().Run(); |
| 8116 | |
| 8117 | EXPECT_EQ(1, delegate.response_started_count()); |
| 8118 | EXPECT_NE(0, delegate.bytes_received()); |
| 8119 | EXPECT_EQ(SSL_CONNECTION_VERSION_SSL3, SSLConnectionStatusToVersion( |
| 8120 | request->ssl_info().connection_status)); |
| 8121 | EXPECT_TRUE(request->ssl_info().connection_status & |
| 8122 | SSL_CONNECTION_VERSION_FALLBACK); |
| 8123 | |
| 8124 | std::vector<std::string> lines; |
| 8125 | // If no sessions were cached then the server should have seen two sessions |
| 8126 | // inserted with no lookups. |
| 8127 | AssertTwoDistinctSessionsInserted(delegate.data_received()); |
| 8128 | } |
| 8129 | } |
| 8130 | |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8131 | // 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] | 8132 | // a TCP reset. |
| 8133 | #if !defined(OS_ANDROID) |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8134 | // Tests that a reset connection does not fallback down to SSL3. |
| 8135 | TEST_F(HTTPSFallbackTest, SSLv3NoFallbackReset) { |
| 8136 | SpawnedTestServer::SSLOptions ssl_options( |
| 8137 | SpawnedTestServer::SSLOptions::CERT_OK); |
| 8138 | ssl_options.tls_intolerant = |
| 8139 | SpawnedTestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 8140 | ssl_options.tls_intolerance_type = |
| 8141 | SpawnedTestServer::SSLOptions::TLS_INTOLERANCE_RESET; |
| 8142 | |
| 8143 | ASSERT_NO_FATAL_FAILURE(DoFallbackTest(ssl_options)); |
| 8144 | ExpectFailure(ERR_CONNECTION_RESET); |
| 8145 | } |
[email protected] | 5aea7918 | 2014-07-14 20:43:41 | [diff] [blame] | 8146 | #endif // !OS_ANDROID |
[email protected] | 48d2b7c5 | 2014-06-27 01:16:55 | [diff] [blame] | 8147 | |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8148 | class HTTPSSessionTest : public testing::Test { |
| 8149 | public: |
| 8150 | HTTPSSessionTest() : default_context_(true) { |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8151 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8152 | |
| 8153 | default_context_.set_network_delegate(&default_network_delegate_); |
| 8154 | default_context_.set_cert_verifier(&cert_verifier_); |
| 8155 | default_context_.Init(); |
| 8156 | } |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8157 | ~HTTPSSessionTest() override {} |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8158 | |
| 8159 | protected: |
| 8160 | MockCertVerifier cert_verifier_; |
| 8161 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
| 8162 | TestURLRequestContext default_context_; |
| 8163 | }; |
| 8164 | |
| 8165 | // Tests that session resumption is not attempted if an invalid certificate |
| 8166 | // is presented. |
| 8167 | TEST_F(HTTPSSessionTest, DontResumeSessionsForInvalidCertificates) { |
| 8168 | SpawnedTestServer::SSLOptions ssl_options; |
| 8169 | ssl_options.record_resume = true; |
| 8170 | SpawnedTestServer test_server( |
| 8171 | SpawnedTestServer::TYPE_HTTPS, |
| 8172 | ssl_options, |
| 8173 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 8174 | ASSERT_TRUE(test_server.Start()); |
| 8175 | |
| 8176 | SSLClientSocket::ClearSessionCache(); |
| 8177 | |
| 8178 | // Simulate the certificate being expired and attempt a connection. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8179 | cert_verifier_.set_default_result(ERR_CERT_DATE_INVALID); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8180 | { |
| 8181 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8182 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8183 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8184 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8185 | r->Start(); |
| 8186 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8187 | |
| 8188 | base::RunLoop().Run(); |
| 8189 | |
| 8190 | EXPECT_EQ(1, d.response_started_count()); |
| 8191 | } |
| 8192 | |
| 8193 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 8194 | CloseAllConnections(); |
| 8195 | |
| 8196 | // Now change the certificate to be acceptable (so that the response is |
| 8197 | // loaded), and ensure that no session id is presented to the peer. |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8198 | cert_verifier_.set_default_result(OK); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8199 | { |
| 8200 | TestDelegate d; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8201 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8202 | test_server.GetURL("ssl-session-cache"), DEFAULT_PRIORITY, &d)); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8203 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8204 | r->Start(); |
| 8205 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8206 | |
| 8207 | base::RunLoop().Run(); |
| 8208 | |
| 8209 | // The response will look like; |
| 8210 | // insert abc |
| 8211 | // insert xyz |
| 8212 | // |
| 8213 | // With a newline at the end which makes the split think that there are |
| 8214 | // three lines. |
| 8215 | // |
| 8216 | // If a session was presented (eg: a bug), then the response would look |
| 8217 | // like; |
| 8218 | // insert abc |
| 8219 | // lookup abc |
| 8220 | // insert xyz |
| 8221 | |
| 8222 | EXPECT_EQ(1, d.response_started_count()); |
Adam Langley | 32352ad | 2014-10-14 22:31:00 | [diff] [blame] | 8223 | AssertTwoDistinctSessionsInserted(d.data_received()); |
[email protected] | a8fed174 | 2013-12-27 02:14:24 | [diff] [blame] | 8224 | } |
| 8225 | } |
| 8226 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8227 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 8228 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 8229 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8230 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 8231 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 8232 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8233 | // This is the SHA256, SPKI hash of the "Testing CA" certificate used by the |
| 8234 | // testserver. |
| 8235 | static const SHA256HashValue kOCSPTestCertSPKI = { { |
| 8236 | 0xee, 0xe6, 0x51, 0x2d, 0x4c, 0xfa, 0xf7, 0x3e, |
| 8237 | 0x6c, 0xd8, 0xca, 0x67, 0xed, 0xb5, 0x5d, 0x49, |
| 8238 | 0x76, 0xe1, 0x52, 0xa7, 0x6e, 0x0e, 0xa0, 0x74, |
| 8239 | 0x09, 0x75, 0xe6, 0x23, 0x24, 0xbd, 0x1b, 0x28, |
| 8240 | } }; |
| 8241 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8242 | // This is the policy OID contained in the certificates that testserver |
| 8243 | // generates. |
| 8244 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 8245 | |
| 8246 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 8247 | public: |
| 8248 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8249 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8250 | ev_test_policy_( |
| 8251 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 8252 | kOCSPTestCertFingerprint, |
| 8253 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8254 | } |
| 8255 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8256 | void SetUp() override { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8257 | SetupContext(&context_); |
| 8258 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8259 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 8260 | scoped_refptr<X509Certificate> root_cert = |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8261 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
dcheng | c2e01e8 | 2014-08-27 00:24:42 | [diff] [blame] | 8262 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert.get()); |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 8263 | test_root_.reset(new ScopedTestRoot(root_cert.get())); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8264 | |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame^] | 8265 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8266 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8267 | EnsureNSSHttpIOInit(); |
| 8268 | #endif |
| 8269 | } |
| 8270 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8271 | void DoConnection(const SpawnedTestServer::SSLOptions& ssl_options, |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8272 | CertStatus* out_cert_status) { |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8273 | // We always overwrite out_cert_status. |
| 8274 | *out_cert_status = 0; |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8275 | SpawnedTestServer test_server( |
| 8276 | SpawnedTestServer::TYPE_HTTPS, |
| 8277 | ssl_options, |
| 8278 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8279 | ASSERT_TRUE(test_server.Start()); |
| 8280 | |
| 8281 | TestDelegate d; |
| 8282 | d.set_allow_certificate_errors(true); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8283 | scoped_ptr<URLRequest> r(context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8284 | test_server.GetURL(std::string()), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8285 | r->Start(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8286 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8287 | base::RunLoop().Run(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8288 | |
| 8289 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8290 | *out_cert_status = r->ssl_info().cert_status; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8291 | } |
| 8292 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 8293 | ~HTTPSOCSPTest() override { |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame^] | 8294 | #if defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8295 | ShutdownNSSHttpIO(); |
| 8296 | #endif |
| 8297 | } |
| 8298 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8299 | protected: |
| 8300 | // SetupContext configures the URLRequestContext that will be used for making |
| 8301 | // connetions to testserver. This can be overridden in test subclasses for |
| 8302 | // different behaviour. |
| 8303 | virtual void SetupContext(URLRequestContext* context) { |
| 8304 | context->set_ssl_config_service( |
| 8305 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8306 | true /* online revocation checking */, |
| 8307 | false /* require rev. checking for local |
| 8308 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8309 | } |
| 8310 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8311 | scoped_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 8312 | TestURLRequestContext context_; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8313 | scoped_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8314 | }; |
| 8315 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8316 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 8317 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8318 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 8319 | // have that ability on other platforms. |
| 8320 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 8321 | #else |
| 8322 | return 0; |
| 8323 | #endif |
| 8324 | } |
| 8325 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8326 | // SystemSupportsHardFailRevocationChecking returns true iff the current |
| 8327 | // operating system supports revocation checking and can distinguish between |
| 8328 | // situations where a given certificate lacks any revocation information (eg: |
| 8329 | // no CRLDistributionPoints and no OCSP Responder AuthorityInfoAccess) and when |
| 8330 | // revocation information cannot be obtained (eg: the CRL was unreachable). |
| 8331 | // If it does not, then tests which rely on 'hard fail' behaviour should be |
| 8332 | // skipped. |
| 8333 | static bool SystemSupportsHardFailRevocationChecking() { |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame^] | 8334 | #if defined(OS_WIN) || defined(USE_NSS_CERTS) || defined(OS_IOS) |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8335 | return true; |
| 8336 | #else |
| 8337 | return false; |
| 8338 | #endif |
| 8339 | } |
| 8340 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8341 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 8342 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 8343 | // several tests are effected because our testing EV certificate won't be |
| 8344 | // recognised as EV. |
| 8345 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 8346 | #if defined(USE_OPENSSL_CERTS) && !defined(OS_ANDROID) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 8347 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 8348 | return false; |
[email protected] | e1b2d73 | 2014-03-28 16:20:32 | [diff] [blame] | 8349 | #elif (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID) |
| 8350 | // On OS X and Android, we use the system to tell us whether a certificate is |
| 8351 | // EV or not and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 8352 | return false; |
| 8353 | #else |
| 8354 | return true; |
| 8355 | #endif |
| 8356 | } |
| 8357 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8358 | static bool SystemSupportsOCSP() { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8359 | #if defined(USE_OPENSSL_CERTS) |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 8360 | // http://crbug.com/117478 - OpenSSL does not support OCSP. |
| 8361 | return false; |
| 8362 | #elif defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8363 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 8364 | #elif defined(OS_ANDROID) |
| 8365 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 8366 | return false; |
| 8367 | #else |
| 8368 | return true; |
| 8369 | #endif |
| 8370 | } |
| 8371 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8372 | static bool SystemSupportsOCSPStapling() { |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame^] | 8373 | #if defined(USE_NSS_CERTS) |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8374 | return true; |
| 8375 | #elif defined(OS_WIN) |
| 8376 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 8377 | #else |
| 8378 | return false; |
| 8379 | #endif |
| 8380 | } |
| 8381 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8382 | TEST_F(HTTPSOCSPTest, Valid) { |
| 8383 | if (!SystemSupportsOCSP()) { |
| 8384 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8385 | return; |
| 8386 | } |
| 8387 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8388 | SpawnedTestServer::SSLOptions ssl_options( |
| 8389 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8390 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8391 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8392 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8393 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8394 | |
| 8395 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8396 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8397 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8398 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8399 | |
| 8400 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8401 | } |
| 8402 | |
| 8403 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 8404 | if (!SystemSupportsOCSP()) { |
| 8405 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8406 | return; |
| 8407 | } |
| 8408 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8409 | SpawnedTestServer::SSLOptions ssl_options( |
| 8410 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8411 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8412 | |
| 8413 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8414 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8415 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 8416 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8417 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 8418 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8419 | #endif |
| 8420 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8421 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8422 | } |
| 8423 | |
| 8424 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 8425 | if (!SystemSupportsOCSP()) { |
| 8426 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8427 | return; |
| 8428 | } |
| 8429 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8430 | SpawnedTestServer::SSLOptions ssl_options( |
| 8431 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8432 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8433 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8434 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8435 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8436 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8437 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8438 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8439 | |
| 8440 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 8441 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8442 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8443 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8444 | |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8445 | TEST_F(HTTPSOCSPTest, ValidStapled) { |
| 8446 | if (!SystemSupportsOCSPStapling()) { |
| 8447 | LOG(WARNING) |
| 8448 | << "Skipping test because system doesn't support OCSP stapling"; |
| 8449 | return; |
| 8450 | } |
| 8451 | |
| 8452 | SpawnedTestServer::SSLOptions ssl_options( |
| 8453 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8454 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 8455 | ssl_options.staple_ocsp_response = true; |
| 8456 | ssl_options.ocsp_server_unavailable = true; |
| 8457 | |
| 8458 | CertStatus cert_status; |
| 8459 | DoConnection(ssl_options, &cert_status); |
| 8460 | |
| 8461 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8462 | |
| 8463 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8464 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 8465 | |
| 8466 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8467 | } |
| 8468 | |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 8469 | // Disabled on NSS ports. See https://crbug.com/431716. |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame^] | 8470 | #if defined(USE_NSS_CERTS) |
davidben | 6c3191b | 2014-11-21 22:38:04 | [diff] [blame] | 8471 | #define MAYBE_RevokedStapled DISABLED_RevokedStapled |
| 8472 | #else |
| 8473 | #define MAYBE_RevokedStapled RevokedStapled |
| 8474 | #endif |
| 8475 | TEST_F(HTTPSOCSPTest, MAYBE_RevokedStapled) { |
davidben | d1fb2f1 | 2014-11-08 02:51:00 | [diff] [blame] | 8476 | if (!SystemSupportsOCSPStapling()) { |
| 8477 | LOG(WARNING) |
| 8478 | << "Skipping test because system doesn't support OCSP stapling"; |
| 8479 | return; |
| 8480 | } |
| 8481 | |
| 8482 | SpawnedTestServer::SSLOptions ssl_options( |
| 8483 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8484 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 8485 | ssl_options.staple_ocsp_response = true; |
| 8486 | ssl_options.ocsp_server_unavailable = true; |
| 8487 | |
| 8488 | CertStatus cert_status; |
| 8489 | DoConnection(ssl_options, &cert_status); |
| 8490 | |
| 8491 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8492 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8493 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8494 | } |
| 8495 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8496 | class HTTPSHardFailTest : public HTTPSOCSPTest { |
| 8497 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8498 | void SetupContext(URLRequestContext* context) override { |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8499 | context->set_ssl_config_service( |
| 8500 | new TestSSLConfigService(false /* check for EV */, |
| 8501 | false /* online revocation checking */, |
| 8502 | true /* require rev. checking for local |
| 8503 | anchors */)); |
| 8504 | } |
| 8505 | }; |
| 8506 | |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8507 | TEST_F(HTTPSHardFailTest, FailsOnOCSPInvalid) { |
| 8508 | if (!SystemSupportsOCSP()) { |
| 8509 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8510 | return; |
| 8511 | } |
| 8512 | |
| 8513 | if (!SystemSupportsHardFailRevocationChecking()) { |
| 8514 | LOG(WARNING) << "Skipping test because system doesn't support hard fail " |
| 8515 | << "revocation checking"; |
| 8516 | return; |
| 8517 | } |
| 8518 | |
| 8519 | SpawnedTestServer::SSLOptions ssl_options( |
| 8520 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8521 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 8522 | |
| 8523 | CertStatus cert_status; |
| 8524 | DoConnection(ssl_options, &cert_status); |
| 8525 | |
| 8526 | EXPECT_EQ(CERT_STATUS_REVOKED, |
| 8527 | cert_status & CERT_STATUS_REVOKED); |
| 8528 | |
| 8529 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 8530 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8531 | } |
| 8532 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8533 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 8534 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8535 | void SetupContext(URLRequestContext* context) override { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8536 | context->set_ssl_config_service( |
| 8537 | new TestSSLConfigService(true /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8538 | false /* online revocation checking */, |
| 8539 | false /* require rev. checking for local |
| 8540 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8541 | } |
| 8542 | }; |
| 8543 | |
| 8544 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 8545 | if (!SystemSupportsOCSP()) { |
| 8546 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8547 | return; |
| 8548 | } |
| 8549 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8550 | SpawnedTestServer::SSLOptions ssl_options( |
| 8551 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8552 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8553 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 8554 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8555 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8556 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8557 | |
| 8558 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 8559 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 8560 | |
| 8561 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8562 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8563 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8564 | } |
| 8565 | |
[email protected] | be0fff6 | 2013-08-29 23:37:48 | [diff] [blame] | 8566 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndRevokedOCSP) { |
| 8567 | if (!SystemSupportsOCSP()) { |
| 8568 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8569 | return; |
| 8570 | } |
| 8571 | |
| 8572 | SpawnedTestServer::SSLOptions ssl_options( |
| 8573 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8574 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
| 8575 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 8576 | |
| 8577 | CertStatus cert_status; |
| 8578 | DoConnection(ssl_options, &cert_status); |
| 8579 | |
| 8580 | // Currently only works for Windows. When using NSS or OS X, it's not |
| 8581 | // possible to determine whether the check failed because of actual |
| 8582 | // revocation or because there was an OCSP failure. |
| 8583 | #if defined(OS_WIN) |
| 8584 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8585 | #else |
| 8586 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8587 | #endif |
| 8588 | |
| 8589 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8590 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8591 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 8592 | } |
| 8593 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8594 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 8595 | if (!SystemSupportsOCSP()) { |
| 8596 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8597 | return; |
| 8598 | } |
| 8599 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8600 | SpawnedTestServer::SSLOptions ssl_options( |
| 8601 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8602 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8603 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 8604 | |
| 8605 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8606 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8607 | |
| 8608 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8609 | |
| 8610 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8611 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8612 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8613 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8614 | } |
| 8615 | |
| 8616 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 8617 | if (!SystemSupportsOCSP()) { |
| 8618 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8619 | return; |
| 8620 | } |
| 8621 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8622 | SpawnedTestServer::SSLOptions ssl_options( |
| 8623 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8624 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8625 | SSLConfigService::SetCRLSet( |
| 8626 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 8627 | |
| 8628 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8629 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8630 | |
| 8631 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 8632 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 8633 | |
| 8634 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8635 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8636 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8637 | } |
| 8638 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8639 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetCovered) { |
| 8640 | if (!SystemSupportsOCSP()) { |
| 8641 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8642 | return; |
| 8643 | } |
| 8644 | |
| 8645 | SpawnedTestServer::SSLOptions ssl_options( |
| 8646 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8647 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
| 8648 | SSLConfigService::SetCRLSet( |
| 8649 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 8650 | false, &kOCSPTestCertSPKI, ""))); |
| 8651 | |
| 8652 | CertStatus cert_status; |
| 8653 | DoConnection(ssl_options, &cert_status); |
| 8654 | |
| 8655 | // With a fresh CRLSet that covers the issuing certificate, we shouldn't do a |
| 8656 | // revocation check for EV. |
| 8657 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8658 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 8659 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 8660 | EXPECT_FALSE( |
| 8661 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 8662 | } |
| 8663 | |
| 8664 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSetNotCovered) { |
| 8665 | if (!SystemSupportsOCSP()) { |
| 8666 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8667 | return; |
| 8668 | } |
| 8669 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8670 | SpawnedTestServer::SSLOptions ssl_options( |
| 8671 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8672 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8673 | SSLConfigService::SetCRLSet( |
| 8674 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 8675 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8676 | CertStatus cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8677 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8678 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8679 | // Even with a fresh CRLSet, we should still do online revocation checks when |
| 8680 | // the certificate chain isn't covered by the CRLSet, which it isn't in this |
| 8681 | // test. |
| 8682 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 8683 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8684 | |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8685 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8686 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8687 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8688 | } |
| 8689 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8690 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 8691 | // Test that when EV verification is requested, but online revocation |
| 8692 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 8693 | // no revocation checking actually happens. |
| 8694 | if (!SystemSupportsOCSP()) { |
| 8695 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 8696 | return; |
| 8697 | } |
| 8698 | |
| 8699 | // Unmark the certificate's OID as EV, which should disable revocation |
| 8700 | // checking (as per the user preference) |
| 8701 | ev_test_policy_.reset(); |
| 8702 | |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8703 | SpawnedTestServer::SSLOptions ssl_options( |
| 8704 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8705 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8706 | SSLConfigService::SetCRLSet( |
| 8707 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 8708 | |
| 8709 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8710 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 8711 | |
| 8712 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8713 | |
| 8714 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8715 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8716 | } |
| 8717 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8718 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 8719 | protected: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 8720 | void SetupContext(URLRequestContext* context) override { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8721 | context->set_ssl_config_service( |
| 8722 | new TestSSLConfigService(false /* check for EV */, |
[email protected] | 3a86a71 | 2013-07-30 07:16:20 | [diff] [blame] | 8723 | false /* online revocation checking */, |
| 8724 | false /* require rev. checking for local |
| 8725 | anchors */)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8726 | } |
| 8727 | }; |
| 8728 | |
| 8729 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8730 | SpawnedTestServer::SSLOptions ssl_options( |
| 8731 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8732 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8733 | SSLConfigService::SetCRLSet( |
| 8734 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 8735 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 8736 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 8737 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 8738 | |
| 8739 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 8740 | // we don't fall back to online revocation checks. |
| 8741 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8742 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8743 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 8744 | } |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8745 | |
| 8746 | TEST_F(HTTPSCRLSetTest, CRLSetRevoked) { |
[email protected] | 591cffcd | 2014-08-18 20:02:30 | [diff] [blame] | 8747 | #if defined(OS_ANDROID) |
[email protected] | 51523f5 | 2013-07-31 21:57:28 | [diff] [blame] | 8748 | LOG(WARNING) << "Skipping test because system doesn't support CRLSets"; |
| 8749 | return; |
| 8750 | #endif |
| 8751 | |
| 8752 | SpawnedTestServer::SSLOptions ssl_options( |
| 8753 | SpawnedTestServer::SSLOptions::CERT_AUTO); |
| 8754 | ssl_options.ocsp_status = SpawnedTestServer::SSLOptions::OCSP_OK; |
| 8755 | ssl_options.cert_serial = 10; |
| 8756 | SSLConfigService::SetCRLSet( |
| 8757 | scoped_refptr<CRLSet>(CRLSet::ForTesting( |
| 8758 | false, &kOCSPTestCertSPKI, "\x0a"))); |
| 8759 | |
| 8760 | CertStatus cert_status = 0; |
| 8761 | DoConnection(ssl_options, &cert_status); |
| 8762 | |
| 8763 | // If the certificate is recorded as revoked in the CRLSet, that should be |
| 8764 | // reflected without online revocation checking. |
| 8765 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 8766 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 8767 | EXPECT_FALSE( |
| 8768 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
| 8769 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8770 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 8771 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 8772 | #if !defined(DISABLE_FTP_SUPPORT) |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 8773 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8774 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 8775 | URLRequestTestFTP() |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8776 | : test_server_(SpawnedTestServer::TYPE_FTP, SpawnedTestServer::kLocalhost, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8777 | base::FilePath()) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8778 | } |
| 8779 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 8780 | protected: |
[email protected] | ce7d0cbc | 2013-05-03 18:57:22 | [diff] [blame] | 8781 | SpawnedTestServer test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 8782 | }; |
| 8783 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8784 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 8785 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8786 | ASSERT_TRUE(test_server_.Start()); |
| 8787 | |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 8788 | URLRequestJobFactoryImpl job_factory; |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 8789 | FtpNetworkLayer ftp_transaction_factory(default_context_.host_resolver()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8790 | |
| 8791 | GURL url("ftp://127.0.0.1:7"); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8792 | job_factory.SetProtocolHandler( |
| 8793 | "ftp", |
[email protected] | e0f35c9 | 2013-05-08 16:04:34 | [diff] [blame] | 8794 | new FtpProtocolHandler(&ftp_transaction_factory)); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8795 | default_context_.set_job_factory(&job_factory); |
| 8796 | |
| 8797 | TestDelegate d; |
| 8798 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8799 | scoped_ptr<URLRequest> r( |
| 8800 | default_context_.CreateRequest(url, DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8801 | r->Start(); |
| 8802 | EXPECT_TRUE(r->is_pending()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8803 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8804 | base::RunLoop().Run(); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8805 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8806 | EXPECT_FALSE(r->is_pending()); |
| 8807 | EXPECT_EQ(URLRequestStatus::FAILED, r->status().status()); |
| 8808 | EXPECT_EQ(ERR_UNSAFE_PORT, r->status().error()); |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 8809 | } |
| 8810 | } |
| 8811 | |
[email protected] | 5accf733 | 2009-11-24 03:41:38 | [diff] [blame] | 8812 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8813 | TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8814 | ASSERT_TRUE(test_server_.Start()); |
| 8815 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8816 | TestDelegate d; |
| 8817 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8818 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8819 | test_server_.GetURL("/"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8820 | r->Start(); |
| 8821 | EXPECT_TRUE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8822 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8823 | base::RunLoop().Run(); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8824 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8825 | EXPECT_FALSE(r->is_pending()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8826 | EXPECT_EQ(1, d.response_started_count()); |
| 8827 | EXPECT_FALSE(d.received_data_before_response()); |
| 8828 | EXPECT_LT(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8829 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8830 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8831 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8832 | r->GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 8833 | } |
| 8834 | } |
| 8835 | |
[email protected] | 7df7001 | 2010-02-04 00:09:55 | [diff] [blame] | 8836 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8837 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8838 | ASSERT_TRUE(test_server_.Start()); |
| 8839 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8840 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8841 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 8842 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8843 | TestDelegate d; |
| 8844 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8845 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8846 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8847 | r->Start(); |
| 8848 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8849 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8850 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8851 | |
| 8852 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8853 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8854 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8855 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8856 | EXPECT_EQ(1, d.response_started_count()); |
| 8857 | EXPECT_FALSE(d.received_data_before_response()); |
| 8858 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8859 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8860 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8861 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8862 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8863 | } |
| 8864 | } |
| 8865 | |
[email protected] | e9ecbd1 | 2009-12-20 18:44:40 | [diff] [blame] | 8866 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8867 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8868 | ASSERT_TRUE(test_server_.Start()); |
| 8869 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8870 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8871 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 8872 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8873 | TestDelegate d; |
| 8874 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8875 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8876 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8877 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8878 | r->Start(); |
| 8879 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8880 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8881 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8882 | |
| 8883 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8884 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8885 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8886 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8887 | EXPECT_EQ(test_server_.host_port_pair().host(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8888 | r->GetSocketAddress().host()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 8889 | EXPECT_EQ(test_server_.host_port_pair().port(), |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8890 | r->GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8891 | EXPECT_EQ(1, d.response_started_count()); |
| 8892 | EXPECT_FALSE(d.received_data_before_response()); |
| 8893 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 8894 | |
| 8895 | LoadTimingInfo load_timing_info; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8896 | r->GetLoadTimingInfo(&load_timing_info); |
[email protected] | 3b23a22 | 2013-05-15 21:33:25 | [diff] [blame] | 8897 | TestLoadTimingNoHttpResponse(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8898 | } |
| 8899 | } |
| 8900 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 8901 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8902 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8903 | ASSERT_TRUE(test_server_.Start()); |
| 8904 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8905 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8906 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 8907 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8908 | TestDelegate d; |
| 8909 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8910 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8911 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 8912 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8913 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8914 | r->Start(); |
| 8915 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8916 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8917 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8918 | |
| 8919 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8920 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8921 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8922 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8923 | EXPECT_EQ(1, d.response_started_count()); |
| 8924 | EXPECT_FALSE(d.received_data_before_response()); |
| 8925 | EXPECT_EQ(d.bytes_received(), 0); |
| 8926 | } |
| 8927 | } |
| 8928 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 8929 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8930 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8931 | ASSERT_TRUE(test_server_.Start()); |
| 8932 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8933 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8934 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 8935 | app_path = app_path.AppendASCII("LICENSE"); |
| 8936 | TestDelegate d; |
| 8937 | // Set correct login credentials. The delegate will be asked for them when |
| 8938 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 8939 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8940 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8941 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 8942 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 8943 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8944 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8945 | r->Start(); |
| 8946 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8947 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8948 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8949 | |
| 8950 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8951 | base::GetFileSize(app_path, &file_size); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8952 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8953 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8954 | EXPECT_EQ(1, d.response_started_count()); |
| 8955 | EXPECT_FALSE(d.received_data_before_response()); |
| 8956 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 8957 | } |
| 8958 | } |
| 8959 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 8960 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8961 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8962 | ASSERT_TRUE(test_server_.Start()); |
| 8963 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8964 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8965 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 8966 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8967 | TestDelegate d; |
| 8968 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 8969 | scoped_ptr<URLRequest> r( |
| 8970 | default_context_.CreateRequest(test_server_.GetURLWithUserAndPassword( |
| 8971 | "/LICENSE", "wrong_user", "chrome"), |
| 8972 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8973 | r->Start(); |
| 8974 | EXPECT_TRUE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8975 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 8976 | base::RunLoop().Run(); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8977 | |
| 8978 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 8979 | base::GetFileSize(app_path, &file_size); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8980 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8981 | EXPECT_FALSE(r->is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 8982 | EXPECT_EQ(1, d.response_started_count()); |
| 8983 | EXPECT_FALSE(d.received_data_before_response()); |
| 8984 | EXPECT_EQ(d.bytes_received(), 0); |
| 8985 | } |
| 8986 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8987 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 8988 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 8989 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 8990 | ASSERT_TRUE(test_server_.Start()); |
| 8991 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 8992 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8993 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 8994 | app_path = app_path.AppendASCII("LICENSE"); |
| 8995 | TestDelegate d; |
| 8996 | // Set correct login credentials. The delegate will be asked for them when |
| 8997 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 8998 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 8999 | { |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9000 | scoped_ptr<URLRequest> r( |
| 9001 | default_context_.CreateRequest(test_server_.GetURLWithUserAndPassword( |
| 9002 | "/LICENSE", "wrong_user", "chrome"), |
| 9003 | DEFAULT_PRIORITY, &d)); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9004 | r->Start(); |
| 9005 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9006 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9007 | base::RunLoop().Run(); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9008 | |
| 9009 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 9010 | base::GetFileSize(app_path, &file_size); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9011 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9012 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 9013 | EXPECT_EQ(1, d.response_started_count()); |
| 9014 | EXPECT_FALSE(d.received_data_before_response()); |
| 9015 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 9016 | } |
| 9017 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9018 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 9019 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 9020 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9021 | ASSERT_TRUE(test_server_.Start()); |
| 9022 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 9023 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9024 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 9025 | app_path = app_path.AppendASCII("LICENSE"); |
| 9026 | |
| 9027 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 9028 | { |
| 9029 | // Pass correct login identity in the URL. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9030 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 9031 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9032 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9033 | r->Start(); |
| 9034 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9035 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9036 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9037 | |
| 9038 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 9039 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9040 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9041 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9042 | EXPECT_EQ(1, d->response_started_count()); |
| 9043 | EXPECT_FALSE(d->received_data_before_response()); |
| 9044 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 9045 | } |
| 9046 | |
| 9047 | d.reset(new TestDelegate); |
| 9048 | { |
| 9049 | // This request should use cached identity from previous request. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9050 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9051 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9052 | r->Start(); |
| 9053 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9054 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9055 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9056 | |
| 9057 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 9058 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9059 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9060 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9061 | EXPECT_EQ(1, d->response_started_count()); |
| 9062 | EXPECT_FALSE(d->received_data_before_response()); |
| 9063 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 9064 | } |
| 9065 | } |
| 9066 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 9067 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 9068 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 9069 | ASSERT_TRUE(test_server_.Start()); |
| 9070 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 9071 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9072 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 9073 | app_path = app_path.AppendASCII("LICENSE"); |
| 9074 | |
| 9075 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 9076 | // Set correct login credentials. The delegate will be asked for them when |
| 9077 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 9078 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9079 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9080 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
| 9081 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", |
| 9082 | "wrong_password"), |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9083 | DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9084 | r->Start(); |
| 9085 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9086 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9087 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9088 | |
| 9089 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 9090 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9091 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9092 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9093 | EXPECT_EQ(1, d->response_started_count()); |
| 9094 | EXPECT_FALSE(d->received_data_before_response()); |
| 9095 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 9096 | } |
| 9097 | |
| 9098 | // Use a new delegate without explicit credentials. The cached ones should be |
| 9099 | // used. |
| 9100 | d.reset(new TestDelegate); |
| 9101 | { |
| 9102 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 9103 | // ones. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9104 | scoped_ptr<URLRequest> r(default_context_.CreateRequest( |
davidben | 151423e | 2015-03-23 18:48:36 | [diff] [blame] | 9105 | test_server_.GetURL("/LICENSE"), DEFAULT_PRIORITY, d.get())); |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9106 | r->Start(); |
| 9107 | EXPECT_TRUE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9108 | |
[email protected] | 255620da | 2013-08-19 13:14:29 | [diff] [blame] | 9109 | base::RunLoop().Run(); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9110 | |
| 9111 | int64 file_size = 0; |
[email protected] | 5628570 | 2013-12-04 18:22:49 | [diff] [blame] | 9112 | base::GetFileSize(app_path, &file_size); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9113 | |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 9114 | EXPECT_FALSE(r->is_pending()); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 9115 | EXPECT_EQ(1, d->response_started_count()); |
| 9116 | EXPECT_FALSE(d->received_data_before_response()); |
| 9117 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 9118 | } |
| 9119 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 9120 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 9121 | |
| 9122 | } // namespace net |