[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 7 | #if defined(OS_WIN) |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 8 | #include <windows.h> |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 9 | #include <shlobj.h> |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 10 | #endif |
| 11 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | #include <string> |
| 14 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 15 | #include "base/basictypes.h" |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 16 | #include "base/bind.h" |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 18 | #include "base/file_util.h" |
[email protected] | 34b2b00 | 2009-11-20 06:53:28 | [diff] [blame] | 19 | #include "base/format_macros.h" |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 20 | #include "base/memory/weak_ptr.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 21 | #include "base/message_loop.h" |
| 22 | #include "base/path_service.h" |
| 23 | #include "base/process_util.h" |
[email protected] | 528c56d | 2010-07-30 19:28:44 | [diff] [blame] | 24 | #include "base/string_number_conversions.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 25 | #include "base/string_piece.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 26 | #include "base/string_split.h" |
[email protected] | ebbad60 | 2011-01-21 23:30:47 | [diff] [blame] | 27 | #include "base/string_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 28 | #include "base/stringprintf.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 29 | #include "base/utf_string_conversions.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 30 | #include "net/base/capturing_net_log.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 31 | #include "net/base/cert_test_util.h" |
| 32 | #include "net/base/ev_root_ca_metadata.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 33 | #include "net/base/load_flags.h" |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 34 | #include "net/base/load_timing_info.h" |
| 35 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 36 | #include "net/base/mock_host_resolver.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 37 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 38 | #include "net/base/net_log.h" |
| 39 | #include "net/base/net_log_unittest.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 40 | #include "net/base/net_module.h" |
| 41 | #include "net/base/net_util.h" |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 42 | #include "net/base/ssl_connection_status_flags.h" |
[email protected] | 42fdb45 | 2012-11-01 12:44:40 | [diff] [blame] | 43 | #include "net/base/test_data_directory.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 44 | #include "net/base/test_root_certs.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 45 | #include "net/base/upload_bytes_element_reader.h" |
| 46 | #include "net/base/upload_data_stream.h" |
| 47 | #include "net/base/upload_file_element_reader.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame] | 48 | #include "net/cookies/cookie_monster.h" |
| 49 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 50 | #include "net/disk_cache/disk_cache.h" |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 51 | #include "net/ftp/ftp_network_layer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 52 | #include "net/http/http_cache.h" |
| 53 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 54 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 55 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 56 | #include "net/http/http_response_headers.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 57 | #include "net/ocsp/nss_ocsp.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 58 | #include "net/proxy/proxy_service.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 59 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 1b9565c | 2010-07-21 01:19:31 | [diff] [blame] | 60 | #include "net/test/test_server.h" |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 61 | #include "net/url_request/ftp_protocol_handler.h" |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 62 | #include "net/url_request/static_http_user_agent_settings.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 63 | #include "net/url_request/url_request.h" |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 64 | #include "net/url_request/url_request_file_dir_job.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 65 | #include "net/url_request/url_request_http_job.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 66 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 67 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 68 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 69 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 70 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 71 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 72 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 73 | #if defined(OS_WIN) |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 74 | #include "base/win/scoped_com_initializer.h" |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 75 | #include "base/win/windows_version.h" |
| 76 | #endif |
| 77 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 78 | using base::Time; |
| 79 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 80 | namespace net { |
| 81 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 82 | namespace { |
| 83 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 84 | const string16 kChrome(ASCIIToUTF16("chrome")); |
| 85 | const string16 kSecret(ASCIIToUTF16("secret")); |
| 86 | const string16 kUser(ASCIIToUTF16("user")); |
| 87 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 88 | // Tests load timing information in the case a fresh connection was used. |
| 89 | // These tests use the TestServer, which doesn't support keep-alive sockets, |
| 90 | // so there are no tests here that reuse sockets. |
| 91 | void TestLoadTimingNotReused(const net::LoadTimingInfo& load_timing_info, |
| 92 | int connect_timing_flags) { |
| 93 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 94 | EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 95 | |
| 96 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 97 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 98 | |
| 99 | EXPECT_LE(load_timing_info.request_start, |
| 100 | load_timing_info.connect_timing.connect_start); |
| 101 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 102 | connect_timing_flags); |
| 103 | EXPECT_LE(load_timing_info.connect_timing.connect_end, |
| 104 | load_timing_info.send_start); |
| 105 | EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); |
| 106 | EXPECT_LE(load_timing_info.send_end, load_timing_info.receive_headers_end); |
| 107 | |
| 108 | // Not set by these tests. |
| 109 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 110 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 111 | } |
| 112 | |
| 113 | // Tests load timing in the case that there is no underlying connection. This |
| 114 | // can be used to test in the case of cached responses, errors, or non-HTTP |
| 115 | // requests. |
| 116 | void TestLoadTimingNoHttpConnection( |
| 117 | const net::LoadTimingInfo& load_timing_info) { |
| 118 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 119 | EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 120 | |
| 121 | // Only the request times should be non-null. |
| 122 | EXPECT_FALSE(load_timing_info.request_start_time.is_null()); |
| 123 | EXPECT_FALSE(load_timing_info.request_start.is_null()); |
| 124 | |
| 125 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 126 | |
| 127 | EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
| 128 | EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
| 129 | EXPECT_TRUE(load_timing_info.send_start.is_null()); |
| 130 | EXPECT_TRUE(load_timing_info.send_end.is_null()); |
| 131 | EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 132 | } |
| 133 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 134 | base::StringPiece TestNetResourceProvider(int key) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 135 | return "header"; |
| 136 | } |
| 137 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 138 | // Do a case-insensitive search through |haystack| for |needle|. |
| 139 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 140 | std::string::const_iterator it = |
| 141 | std::search(haystack.begin(), |
| 142 | haystack.end(), |
| 143 | needle, |
| 144 | needle + strlen(needle), |
[email protected] | 07f1cee | 2010-11-03 03:53:35 | [diff] [blame] | 145 | base::CaseInsensitiveCompare<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 146 | return it != haystack.end(); |
| 147 | } |
| 148 | |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 149 | void FillBuffer(char* buffer, size_t len) { |
| 150 | static bool called = false; |
| 151 | if (!called) { |
| 152 | called = true; |
| 153 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 154 | srand(seed); |
| 155 | } |
| 156 | |
| 157 | for (size_t i = 0; i < len; i++) { |
| 158 | buffer[i] = static_cast<char>(rand()); |
| 159 | if (!buffer[i]) |
| 160 | buffer[i] = 'g'; |
| 161 | } |
| 162 | } |
| 163 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 164 | UploadDataStream* CreateSimpleUploadData(const char* data) { |
| 165 | scoped_ptr<UploadElementReader> reader( |
| 166 | new UploadBytesElementReader(data, strlen(data))); |
| 167 | return UploadDataStream::CreateWithReader(reader.Pass(), 0); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 170 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 171 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 172 | // Allow ChromeFrame fake SSLInfo to get through. |
[email protected] | c679be2 | 2010-08-29 15:20:05 | [diff] [blame] | 173 | if (ssl_info.cert.get() && |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 174 | ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") { |
| 175 | // -1 means unknown. |
| 176 | EXPECT_EQ(ssl_info.security_bits, -1); |
[email protected] | c679be2 | 2010-08-29 15:20:05 | [diff] [blame] | 177 | return; |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 180 | // -1 means unknown. 0 means no encryption. |
| 181 | EXPECT_GT(ssl_info.security_bits, 0); |
| 182 | |
| 183 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 184 | int cipher_suite = SSLConnectionStatusToCipherSuite( |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 185 | ssl_info.connection_status); |
| 186 | EXPECT_NE(0, cipher_suite); |
| 187 | } |
| 188 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 189 | bool FingerprintsEqual(const HashValueVector& a, const HashValueVector& b) { |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 190 | size_t size = a.size(); |
| 191 | |
| 192 | if (size != b.size()) |
| 193 | return false; |
| 194 | |
| 195 | for (size_t i = 0; i < size; ++i) { |
| 196 | if (!a[i].Equals(b[i])) |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 203 | // A network delegate that allows the user to choose a subset of request stages |
| 204 | // to block in. When blocking, the delegate can do one of the following: |
| 205 | // * synchronously return a pre-specified error code, or |
| 206 | // * asynchronously return that value via an automatically called callback, |
| 207 | // or |
| 208 | // * block and wait for the user to do a callback. |
| 209 | // Additionally, the user may also specify a redirect URL -- then each request |
| 210 | // with the current URL different from the redirect target will be redirected |
| 211 | // to that target, in the on-before-URL-request stage, independent of whether |
| 212 | // the delegate blocks in ON_BEFORE_URL_REQUEST or not. |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 213 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 214 | public: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 215 | // Stages in which the delegate can block. |
| 216 | enum Stage { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 217 | NOT_BLOCKED = 0, |
| 218 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 219 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 220 | ON_HEADERS_RECEIVED = 1 << 2, |
| 221 | ON_AUTH_REQUIRED = 1 << 3 |
| 222 | }; |
| 223 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 224 | // Behavior during blocked stages. During other stages, just |
| 225 | // returns net::OK or NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION. |
| 226 | enum BlockMode { |
| 227 | SYNCHRONOUS, // No callback, returns specified return values. |
| 228 | AUTO_CALLBACK, // |this| posts a task to run the callback using the |
| 229 | // specified return codes. |
| 230 | USER_CALLBACK, // User takes care of doing a callback. |retval_| and |
| 231 | // |auth_retval_| are ignored. In every blocking stage the |
| 232 | // message loop is quit. |
| 233 | }; |
| 234 | |
| 235 | // Creates a delegate which does not block at all. |
| 236 | explicit BlockingNetworkDelegate(BlockMode block_mode); |
| 237 | |
| 238 | // For users to trigger a callback returning |response|. |
| 239 | // Side-effects: resets |stage_blocked_for_callback_| and stored callbacks. |
| 240 | // Only call if |block_mode_| == USER_CALLBACK. |
| 241 | void DoCallback(int response); |
| 242 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response); |
| 243 | |
| 244 | // Setters. |
| 245 | void set_retval(int retval) { |
| 246 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 247 | ASSERT_NE(ERR_IO_PENDING, retval); |
| 248 | ASSERT_NE(OK, retval); |
| 249 | retval_ = retval; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 250 | } |
| 251 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 252 | // If |auth_retval| == AUTH_REQUIRED_RESPONSE_SET_AUTH, then |
| 253 | // |auth_credentials_| will be passed with the response. |
| 254 | void set_auth_retval(AuthRequiredResponse auth_retval) { |
| 255 | ASSERT_NE(USER_CALLBACK, block_mode_); |
| 256 | ASSERT_NE(AUTH_REQUIRED_RESPONSE_IO_PENDING, auth_retval); |
| 257 | auth_retval_ = auth_retval; |
| 258 | } |
| 259 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 260 | auth_credentials_ = auth_credentials; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 263 | void set_redirect_url(const GURL& url) { |
| 264 | redirect_url_ = url; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 267 | void set_block_on(int block_on) { |
| 268 | block_on_ = block_on; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 269 | } |
| 270 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 271 | // Allows the user to check in which state did we block. |
| 272 | Stage stage_blocked_for_callback() const { |
| 273 | EXPECT_EQ(USER_CALLBACK, block_mode_); |
| 274 | return stage_blocked_for_callback_; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | private: |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 278 | void RunCallback(int response, const CompletionCallback& callback); |
| 279 | void RunAuthCallback(AuthRequiredResponse response, |
| 280 | const AuthCallback& callback); |
| 281 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 282 | // TestNetworkDelegate implementation. |
| 283 | virtual int OnBeforeURLRequest(URLRequest* request, |
| 284 | const CompletionCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 285 | GURL* new_url) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 286 | |
| 287 | virtual int OnBeforeSendHeaders(URLRequest* request, |
| 288 | const CompletionCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 289 | HttpRequestHeaders* headers) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 290 | |
| 291 | virtual int OnHeadersReceived( |
| 292 | URLRequest* request, |
| 293 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 294 | const HttpResponseHeaders* original_response_headers, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 295 | scoped_refptr<HttpResponseHeaders>* override_response_headers) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 296 | |
| 297 | virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 298 | URLRequest* request, |
| 299 | const AuthChallengeInfo& auth_info, |
| 300 | const AuthCallback& callback, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 301 | AuthCredentials* credentials) OVERRIDE; |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 302 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 303 | // Resets the callbacks and |stage_blocked_for_callback_|. |
| 304 | void Reset(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 305 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 306 | // Checks whether we should block in |stage|. If yes, returns an error code |
| 307 | // and optionally sets up callback based on |block_mode_|. If no, returns OK. |
| 308 | int MaybeBlockStage(Stage stage, const CompletionCallback& callback); |
| 309 | |
| 310 | // Configuration parameters, can be adjusted by public methods: |
| 311 | const BlockMode block_mode_; |
| 312 | |
| 313 | // Values returned on blocking stages when mode is SYNCHRONOUS or |
| 314 | // AUTO_CALLBACK. For USER_CALLBACK these are set automatically to IO_PENDING. |
| 315 | int retval_; // To be returned in non-auth stages. |
| 316 | AuthRequiredResponse auth_retval_; |
| 317 | |
| 318 | GURL redirect_url_; // Used if non-empty. |
| 319 | int block_on_; // Bit mask: in which stages to block. |
| 320 | |
| 321 | // |auth_credentials_| will be copied to |*target_auth_credential_| on |
| 322 | // callback. |
| 323 | AuthCredentials auth_credentials_; |
| 324 | AuthCredentials* target_auth_credentials_; |
| 325 | |
| 326 | // Internal variables, not set by not the user: |
| 327 | // Last blocked stage waiting for user callback (unused if |block_mode_| != |
| 328 | // USER_CALLBACK). |
| 329 | Stage stage_blocked_for_callback_; |
| 330 | |
| 331 | // Callback objects stored during blocking stages. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 332 | CompletionCallback callback_; |
| 333 | AuthCallback auth_callback_; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 334 | |
| 335 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
| 336 | |
| 337 | DISALLOW_COPY_AND_ASSIGN(BlockingNetworkDelegate); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 338 | }; |
| 339 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 340 | BlockingNetworkDelegate::BlockingNetworkDelegate(BlockMode block_mode) |
| 341 | : block_mode_(block_mode), |
| 342 | retval_(OK), |
| 343 | auth_retval_(AUTH_REQUIRED_RESPONSE_NO_ACTION), |
| 344 | block_on_(0), |
| 345 | target_auth_credentials_(NULL), |
| 346 | stage_blocked_for_callback_(NOT_BLOCKED), |
| 347 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 348 | } |
| 349 | |
| 350 | void BlockingNetworkDelegate::DoCallback(int response) { |
| 351 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 352 | ASSERT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 353 | ASSERT_NE(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 354 | CompletionCallback callback = callback_; |
| 355 | Reset(); |
| 356 | RunCallback(response, callback); |
| 357 | } |
| 358 | |
| 359 | void BlockingNetworkDelegate::DoAuthCallback( |
| 360 | NetworkDelegate::AuthRequiredResponse response) { |
| 361 | ASSERT_EQ(USER_CALLBACK, block_mode_); |
| 362 | ASSERT_EQ(ON_AUTH_REQUIRED, stage_blocked_for_callback_); |
| 363 | AuthCallback auth_callback = auth_callback_; |
| 364 | Reset(); |
| 365 | RunAuthCallback(response, auth_callback); |
| 366 | } |
| 367 | |
| 368 | void BlockingNetworkDelegate::RunCallback(int response, |
| 369 | const CompletionCallback& callback) { |
| 370 | callback.Run(response); |
| 371 | } |
| 372 | |
| 373 | void BlockingNetworkDelegate::RunAuthCallback(AuthRequiredResponse response, |
| 374 | const AuthCallback& callback) { |
| 375 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 376 | ASSERT_TRUE(target_auth_credentials_ != NULL); |
| 377 | *target_auth_credentials_ = auth_credentials_; |
| 378 | } |
| 379 | callback.Run(response); |
| 380 | } |
| 381 | |
| 382 | int BlockingNetworkDelegate::OnBeforeURLRequest( |
| 383 | URLRequest* request, |
| 384 | const CompletionCallback& callback, |
| 385 | GURL* new_url) { |
| 386 | if (redirect_url_ == request->url()) |
| 387 | return OK; // We've already seen this request and redirected elsewhere. |
| 388 | |
| 389 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 390 | |
| 391 | if (!redirect_url_.is_empty()) |
| 392 | *new_url = redirect_url_; |
| 393 | |
| 394 | return MaybeBlockStage(ON_BEFORE_URL_REQUEST, callback); |
| 395 | } |
| 396 | |
| 397 | int BlockingNetworkDelegate::OnBeforeSendHeaders( |
| 398 | URLRequest* request, |
| 399 | const CompletionCallback& callback, |
| 400 | HttpRequestHeaders* headers) { |
| 401 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 402 | |
| 403 | return MaybeBlockStage(ON_BEFORE_SEND_HEADERS, callback); |
| 404 | } |
| 405 | |
| 406 | int BlockingNetworkDelegate::OnHeadersReceived( |
| 407 | URLRequest* request, |
| 408 | const CompletionCallback& callback, |
[email protected] | 507af8f | 2012-10-20 00:42:32 | [diff] [blame] | 409 | const HttpResponseHeaders* original_response_headers, |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 410 | scoped_refptr<HttpResponseHeaders>* override_response_headers) { |
| 411 | TestNetworkDelegate::OnHeadersReceived( |
| 412 | request, callback, original_response_headers, |
| 413 | override_response_headers); |
| 414 | |
| 415 | return MaybeBlockStage(ON_HEADERS_RECEIVED, callback); |
| 416 | } |
| 417 | |
| 418 | NetworkDelegate::AuthRequiredResponse BlockingNetworkDelegate::OnAuthRequired( |
| 419 | URLRequest* request, |
| 420 | const AuthChallengeInfo& auth_info, |
| 421 | const AuthCallback& callback, |
| 422 | AuthCredentials* credentials) { |
| 423 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 424 | credentials); |
| 425 | // Check that the user has provided callback for the previous blocked stage. |
| 426 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 427 | |
| 428 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 429 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 430 | } |
| 431 | |
| 432 | target_auth_credentials_ = credentials; |
| 433 | |
| 434 | switch (block_mode_) { |
| 435 | case SYNCHRONOUS: |
| 436 | if (auth_retval_ == AUTH_REQUIRED_RESPONSE_SET_AUTH) |
| 437 | *target_auth_credentials_ = auth_credentials_; |
| 438 | return auth_retval_; |
| 439 | |
| 440 | case AUTO_CALLBACK: |
| 441 | MessageLoop::current()->PostTask( |
| 442 | FROM_HERE, |
| 443 | base::Bind(&BlockingNetworkDelegate::RunAuthCallback, |
| 444 | weak_factory_.GetWeakPtr(), auth_retval_, callback)); |
| 445 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 446 | |
| 447 | case USER_CALLBACK: |
| 448 | auth_callback_ = callback; |
| 449 | stage_blocked_for_callback_ = ON_AUTH_REQUIRED; |
| 450 | MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 451 | return AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 452 | } |
| 453 | NOTREACHED(); |
| 454 | return AUTH_REQUIRED_RESPONSE_NO_ACTION; // Dummy value. |
| 455 | } |
| 456 | |
| 457 | void BlockingNetworkDelegate::Reset() { |
| 458 | EXPECT_NE(NOT_BLOCKED, stage_blocked_for_callback_); |
| 459 | stage_blocked_for_callback_ = NOT_BLOCKED; |
| 460 | callback_.Reset(); |
| 461 | auth_callback_.Reset(); |
| 462 | } |
| 463 | |
| 464 | int BlockingNetworkDelegate::MaybeBlockStage( |
| 465 | BlockingNetworkDelegate::Stage stage, |
| 466 | const CompletionCallback& callback) { |
| 467 | // Check that the user has provided callback for the previous blocked stage. |
| 468 | EXPECT_EQ(NOT_BLOCKED, stage_blocked_for_callback_); |
| 469 | |
| 470 | if ((block_on_ & stage) == 0) { |
| 471 | return OK; |
| 472 | } |
| 473 | |
| 474 | switch (block_mode_) { |
| 475 | case SYNCHRONOUS: |
| 476 | EXPECT_NE(OK, retval_); |
| 477 | return retval_; |
| 478 | |
| 479 | case AUTO_CALLBACK: |
| 480 | MessageLoop::current()->PostTask( |
| 481 | FROM_HERE, |
| 482 | base::Bind(&BlockingNetworkDelegate::RunCallback, |
| 483 | weak_factory_.GetWeakPtr(), retval_, callback)); |
| 484 | return ERR_IO_PENDING; |
| 485 | |
| 486 | case USER_CALLBACK: |
| 487 | callback_ = callback; |
| 488 | stage_blocked_for_callback_ = stage; |
| 489 | MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 490 | return ERR_IO_PENDING; |
| 491 | } |
| 492 | NOTREACHED(); |
| 493 | return 0; |
| 494 | } |
| 495 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 496 | class TestURLRequestContextWithProxy : public TestURLRequestContext { |
| 497 | public: |
| 498 | // Does not own |delegate|. |
| 499 | TestURLRequestContextWithProxy(const std::string& proxy, |
| 500 | NetworkDelegate* delegate) |
| 501 | : TestURLRequestContext(true) { |
| 502 | context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
| 503 | set_network_delegate(delegate); |
| 504 | Init(); |
| 505 | } |
| 506 | virtual ~TestURLRequestContextWithProxy() {} |
| 507 | }; |
| 508 | |
| 509 | } // namespace |
| 510 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 511 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X. |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 512 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 513 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 514 | URLRequestTest() : default_context_(true) { |
| 515 | default_context_.set_network_delegate(&default_network_delegate_); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 516 | default_context_.set_net_log(&net_log_); |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 517 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 518 | } |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 519 | virtual ~URLRequestTest() {} |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 520 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 521 | // Adds the TestJobInterceptor to the default context. |
| 522 | TestJobInterceptor* AddTestInterceptor() { |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 523 | TestJobInterceptor* protocol_handler_ = new TestJobInterceptor(); |
| 524 | job_factory_.reset(new URLRequestJobFactoryImpl); |
| 525 | job_factory_->SetProtocolHandler("http", protocol_handler_); |
| 526 | default_context_.set_job_factory(job_factory_.get()); |
| 527 | return protocol_handler_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 528 | } |
| 529 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 530 | protected: |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 531 | CapturingNetLog net_log_; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 532 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | f53b480 | 2012-12-20 17:04:23 | [diff] [blame] | 533 | scoped_ptr<URLRequestJobFactoryImpl> job_factory_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 534 | TestURLRequestContext default_context_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 535 | }; |
| 536 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 537 | TEST_F(URLRequestTest, AboutBlankTest) { |
| 538 | TestDelegate d; |
| 539 | { |
| 540 | URLRequest r(GURL("about:blank"), &d, &default_context_); |
| 541 | |
| 542 | r.Start(); |
| 543 | EXPECT_TRUE(r.is_pending()); |
| 544 | |
| 545 | MessageLoop::current()->Run(); |
| 546 | |
| 547 | EXPECT_TRUE(!r.is_pending()); |
| 548 | EXPECT_FALSE(d.received_data_before_response()); |
| 549 | EXPECT_EQ(d.bytes_received(), 0); |
| 550 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 551 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 556 | TestDelegate d; |
| 557 | { |
| 558 | // Use our nice little Chrome logo. |
| 559 | URLRequest r(GURL( |
| 560 | "data:image/png;base64," |
| 561 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3" |
| 562 | "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD" |
| 563 | "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t" |
| 564 | "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9" |
| 565 | "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1" |
| 566 | "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z" |
| 567 | "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW" |
| 568 | "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW" |
| 569 | "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb" |
| 570 | "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5" |
| 571 | "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV" |
| 572 | "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq" |
| 573 | "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F" |
| 574 | "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB" |
| 575 | "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM" |
| 576 | "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm" |
| 577 | "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En" |
| 578 | "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
| 579 | &d, |
| 580 | &default_context_); |
| 581 | |
| 582 | r.Start(); |
| 583 | EXPECT_TRUE(r.is_pending()); |
| 584 | |
| 585 | MessageLoop::current()->Run(); |
| 586 | |
| 587 | EXPECT_TRUE(!r.is_pending()); |
| 588 | EXPECT_FALSE(d.received_data_before_response()); |
| 589 | EXPECT_EQ(d.bytes_received(), 911); |
| 590 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 591 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 596 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 597 | PathService::Get(base::FILE_EXE, &app_path); |
| 598 | GURL app_url = FilePathToFileURL(app_path); |
| 599 | |
| 600 | TestDelegate d; |
| 601 | { |
| 602 | URLRequest r(app_url, &d, &default_context_); |
| 603 | |
| 604 | r.Start(); |
| 605 | EXPECT_TRUE(r.is_pending()); |
| 606 | |
| 607 | MessageLoop::current()->Run(); |
| 608 | |
| 609 | int64 file_size = -1; |
| 610 | EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size)); |
| 611 | |
| 612 | EXPECT_TRUE(!r.is_pending()); |
| 613 | EXPECT_EQ(1, d.response_started_count()); |
| 614 | EXPECT_FALSE(d.received_data_before_response()); |
| 615 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 616 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 617 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
| 618 | } |
| 619 | } |
| 620 | |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 621 | TEST_F(URLRequestTest, FileTestCancel) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 622 | base::FilePath app_path; |
[email protected] | ba40bb76 | 2012-12-17 07:11:04 | [diff] [blame] | 623 | PathService::Get(base::FILE_EXE, &app_path); |
| 624 | GURL app_url = FilePathToFileURL(app_path); |
| 625 | |
| 626 | TestDelegate d; |
| 627 | { |
| 628 | URLRequest r(app_url, &d, &default_context_); |
| 629 | |
| 630 | r.Start(); |
| 631 | EXPECT_TRUE(r.is_pending()); |
| 632 | r.Cancel(); |
| 633 | } |
| 634 | // Async cancelation should be safe even when URLRequest has been already |
| 635 | // destroyed. |
| 636 | MessageLoop::current()->RunUntilIdle(); |
| 637 | } |
| 638 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 639 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 640 | const size_t buffer_size = 4000; |
| 641 | scoped_array<char> buffer(new char[buffer_size]); |
| 642 | FillBuffer(buffer.get(), buffer_size); |
| 643 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 644 | base::FilePath temp_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 645 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
| 646 | GURL temp_url = FilePathToFileURL(temp_path); |
| 647 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
| 648 | |
| 649 | int64 file_size; |
| 650 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 651 | |
| 652 | const size_t first_byte_position = 500; |
| 653 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 654 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 655 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 656 | buffer.get() + last_byte_position + 1); |
| 657 | |
| 658 | TestDelegate d; |
| 659 | { |
| 660 | URLRequest r(temp_url, &d, &default_context_); |
| 661 | |
| 662 | HttpRequestHeaders headers; |
| 663 | headers.SetHeader(HttpRequestHeaders::kRange, |
| 664 | base::StringPrintf( |
| 665 | "bytes=%" PRIuS "-%" PRIuS, |
| 666 | first_byte_position, last_byte_position)); |
| 667 | r.SetExtraRequestHeaders(headers); |
| 668 | r.Start(); |
| 669 | EXPECT_TRUE(r.is_pending()); |
| 670 | |
| 671 | MessageLoop::current()->Run(); |
| 672 | EXPECT_TRUE(!r.is_pending()); |
| 673 | EXPECT_EQ(1, d.response_started_count()); |
| 674 | EXPECT_FALSE(d.received_data_before_response()); |
| 675 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 676 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 677 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 678 | } |
| 679 | |
| 680 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
| 681 | } |
| 682 | |
| 683 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 684 | const size_t buffer_size = 4000; |
| 685 | scoped_array<char> buffer(new char[buffer_size]); |
| 686 | FillBuffer(buffer.get(), buffer_size); |
| 687 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 688 | base::FilePath temp_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 689 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
| 690 | GURL temp_url = FilePathToFileURL(temp_path); |
| 691 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
| 692 | |
| 693 | int64 file_size; |
| 694 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 695 | |
| 696 | const size_t first_byte_position = 500; |
| 697 | const size_t last_byte_position = buffer_size - 1; |
| 698 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 699 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 700 | buffer.get() + last_byte_position + 1); |
| 701 | |
| 702 | TestDelegate d; |
| 703 | { |
| 704 | URLRequest r(temp_url, &d, &default_context_); |
| 705 | |
| 706 | HttpRequestHeaders headers; |
| 707 | headers.SetHeader(HttpRequestHeaders::kRange, |
| 708 | base::StringPrintf("bytes=%" PRIuS "-", |
| 709 | first_byte_position)); |
| 710 | r.SetExtraRequestHeaders(headers); |
| 711 | r.Start(); |
| 712 | EXPECT_TRUE(r.is_pending()); |
| 713 | |
| 714 | MessageLoop::current()->Run(); |
| 715 | EXPECT_TRUE(!r.is_pending()); |
| 716 | EXPECT_EQ(1, d.response_started_count()); |
| 717 | EXPECT_FALSE(d.received_data_before_response()); |
| 718 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 719 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 720 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 721 | } |
| 722 | |
| 723 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
| 724 | } |
| 725 | |
| 726 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 727 | const size_t buffer_size = 400000; |
| 728 | scoped_array<char> buffer(new char[buffer_size]); |
| 729 | FillBuffer(buffer.get(), buffer_size); |
| 730 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 731 | base::FilePath temp_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 732 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
| 733 | GURL temp_url = FilePathToFileURL(temp_path); |
| 734 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
| 735 | |
| 736 | int64 file_size; |
| 737 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 738 | |
| 739 | TestDelegate d; |
| 740 | { |
| 741 | URLRequest r(temp_url, &d, &default_context_); |
| 742 | |
| 743 | HttpRequestHeaders headers; |
| 744 | headers.SetHeader(HttpRequestHeaders::kRange, |
| 745 | "bytes=0-0,10-200,200-300"); |
| 746 | r.SetExtraRequestHeaders(headers); |
| 747 | r.Start(); |
| 748 | EXPECT_TRUE(r.is_pending()); |
| 749 | |
| 750 | MessageLoop::current()->Run(); |
| 751 | EXPECT_TRUE(d.request_failed()); |
| 752 | } |
| 753 | |
| 754 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
| 755 | } |
| 756 | |
| 757 | TEST_F(URLRequestTest, InvalidUrlTest) { |
| 758 | TestDelegate d; |
| 759 | { |
| 760 | URLRequest r(GURL("invalid url"), &d, &default_context_); |
| 761 | |
| 762 | r.Start(); |
| 763 | EXPECT_TRUE(r.is_pending()); |
| 764 | |
| 765 | MessageLoop::current()->Run(); |
| 766 | EXPECT_TRUE(d.request_failed()); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | #if defined(OS_WIN) |
| 771 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 772 | base::FilePath app_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 773 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 774 | app_path = app_path.AppendASCII("net"); |
| 775 | app_path = app_path.AppendASCII("data"); |
| 776 | app_path = app_path.AppendASCII("url_request_unittest"); |
| 777 | app_path = app_path.AppendASCII("with-headers.html"); |
| 778 | |
| 779 | std::wstring lnk_path = app_path.value() + L".lnk"; |
| 780 | |
[email protected] | 451fd90 | 2012-10-03 17:14:48 | [diff] [blame] | 781 | base::win::ScopedCOMInitializer com_initializer; |
[email protected] | 00d60fa | 2012-10-01 18:20:17 | [diff] [blame] | 782 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 783 | // Temporarily create a shortcut for test |
[email protected] | 00d60fa | 2012-10-01 18:20:17 | [diff] [blame] | 784 | IShellLink* shell = NULL; |
| 785 | ASSERT_TRUE(SUCCEEDED(CoCreateInstance( |
| 786 | CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 787 | reinterpret_cast<LPVOID*>(&shell)))); |
| 788 | IPersistFile* persist = NULL; |
| 789 | ASSERT_TRUE(SUCCEEDED(shell->QueryInterface( |
| 790 | IID_IPersistFile, reinterpret_cast<LPVOID*>(&persist)))); |
| 791 | EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 792 | EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 793 | EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 794 | if (persist) |
| 795 | persist->Release(); |
| 796 | if (shell) |
| 797 | shell->Release(); |
| 798 | |
| 799 | TestDelegate d; |
| 800 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 801 | URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)), &d, |
| 802 | &default_context_); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 803 | |
| 804 | r.Start(); |
| 805 | EXPECT_TRUE(r.is_pending()); |
| 806 | |
| 807 | MessageLoop::current()->Run(); |
| 808 | |
| 809 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 810 | GetFileAttributesEx(app_path.value().c_str(), |
| 811 | GetFileExInfoStandard, &data); |
| 812 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 813 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 814 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 815 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 816 | scoped_array<char> buffer(new char[data.nFileSizeLow]); |
| 817 | DWORD read_size; |
| 818 | BOOL result; |
| 819 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 820 | &read_size, NULL); |
| 821 | std::string content(buffer.get(), read_size); |
| 822 | CloseHandle(file); |
| 823 | |
| 824 | EXPECT_TRUE(!r.is_pending()); |
| 825 | EXPECT_EQ(1, d.received_redirect_count()); |
| 826 | EXPECT_EQ(content, d.data_received()); |
| 827 | } |
| 828 | |
| 829 | // Clean the shortcut |
| 830 | DeleteFile(lnk_path.c_str()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 831 | } |
| 832 | #endif // defined(OS_WIN) |
| 833 | |
| 834 | TEST_F(URLRequestTest, FileDirCancelTest) { |
| 835 | // Put in mock resource provider. |
| 836 | NetModule::SetResourceProvider(TestNetResourceProvider); |
| 837 | |
| 838 | TestDelegate d; |
| 839 | { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 840 | base::FilePath file_path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 841 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 842 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 843 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
| 844 | |
| 845 | URLRequest req(FilePathToFileURL(file_path), &d, &default_context_); |
| 846 | req.Start(); |
| 847 | EXPECT_TRUE(req.is_pending()); |
| 848 | |
| 849 | d.set_cancel_in_received_data_pending(true); |
| 850 | |
| 851 | MessageLoop::current()->Run(); |
| 852 | } |
| 853 | |
| 854 | // Take out mock resource provider. |
| 855 | NetModule::SetResourceProvider(NULL); |
| 856 | } |
| 857 | |
| 858 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 859 | // There is an implicit redirect when loading a file path that matches a |
| 860 | // directory and does not end with a slash. Ensure that following such |
| 861 | // redirects does not crash. See http://crbug.com/18686. |
| 862 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 863 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 864 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 865 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 866 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 867 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 868 | |
| 869 | TestDelegate d; |
| 870 | URLRequest req(FilePathToFileURL(path), &d, &default_context_); |
| 871 | req.Start(); |
| 872 | MessageLoop::current()->Run(); |
| 873 | |
| 874 | ASSERT_EQ(1, d.received_redirect_count()); |
| 875 | ASSERT_LT(0, d.bytes_received()); |
| 876 | ASSERT_FALSE(d.request_failed()); |
| 877 | ASSERT_TRUE(req.status().is_success()); |
| 878 | } |
| 879 | |
| 880 | #if defined(OS_WIN) |
| 881 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 882 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 883 | TestDelegate d; |
| 884 | URLRequest req(GURL("file:///"), &d, &default_context_); |
| 885 | req.Start(); |
| 886 | MessageLoop::current()->Run(); |
| 887 | |
| 888 | ASSERT_EQ(1, d.received_redirect_count()); |
| 889 | ASSERT_FALSE(req.status().is_success()); |
| 890 | } |
| 891 | #endif |
| 892 | |
| 893 | // Custom URLRequestJobs for use with interceptor tests |
| 894 | class RestartTestJob : public URLRequestTestJob { |
| 895 | public: |
| 896 | RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 897 | : URLRequestTestJob(request, network_delegate, true) {} |
| 898 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 899 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 900 | this->NotifyRestartRequired(); |
| 901 | } |
| 902 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 903 | virtual ~RestartTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 904 | }; |
| 905 | |
| 906 | class CancelTestJob : public URLRequestTestJob { |
| 907 | public: |
| 908 | explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 909 | : URLRequestTestJob(request, network_delegate, true) {} |
| 910 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 911 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 912 | request_->Cancel(); |
| 913 | } |
| 914 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 915 | virtual ~CancelTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 916 | }; |
| 917 | |
| 918 | class CancelThenRestartTestJob : public URLRequestTestJob { |
| 919 | public: |
| 920 | explicit CancelThenRestartTestJob(URLRequest* request, |
| 921 | NetworkDelegate* network_delegate) |
| 922 | : URLRequestTestJob(request, network_delegate, true) { |
| 923 | } |
| 924 | protected: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 925 | virtual void StartAsync() OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 926 | request_->Cancel(); |
| 927 | this->NotifyRestartRequired(); |
| 928 | } |
| 929 | private: |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 930 | virtual ~CancelThenRestartTestJob() {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 931 | }; |
| 932 | |
| 933 | // An Interceptor for use with interceptor tests |
| 934 | class TestInterceptor : URLRequest::Interceptor { |
| 935 | public: |
| 936 | TestInterceptor() |
| 937 | : intercept_main_request_(false), restart_main_request_(false), |
| 938 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 939 | simulate_main_network_error_(false), |
| 940 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 941 | intercept_final_response_(false), cancel_final_request_(false), |
| 942 | did_intercept_main_(false), did_restart_main_(false), |
| 943 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 944 | did_simulate_error_main_(false), |
| 945 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 946 | did_intercept_final_(false), did_cancel_final_(false) { |
| 947 | URLRequest::Deprecated::RegisterRequestInterceptor(this); |
| 948 | } |
| 949 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 950 | virtual ~TestInterceptor() { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 951 | URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
| 952 | } |
| 953 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 954 | virtual URLRequestJob* MaybeIntercept( |
| 955 | URLRequest* request, |
| 956 | NetworkDelegate* network_delegate) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 957 | if (restart_main_request_) { |
| 958 | restart_main_request_ = false; |
| 959 | did_restart_main_ = true; |
| 960 | return new RestartTestJob(request, network_delegate); |
| 961 | } |
| 962 | if (cancel_main_request_) { |
| 963 | cancel_main_request_ = false; |
| 964 | did_cancel_main_ = true; |
| 965 | return new CancelTestJob(request, network_delegate); |
| 966 | } |
| 967 | if (cancel_then_restart_main_request_) { |
| 968 | cancel_then_restart_main_request_ = false; |
| 969 | did_cancel_then_restart_main_ = true; |
| 970 | return new CancelThenRestartTestJob(request, network_delegate); |
| 971 | } |
| 972 | if (simulate_main_network_error_) { |
| 973 | simulate_main_network_error_ = false; |
| 974 | did_simulate_error_main_ = true; |
| 975 | // will error since the requeted url is not one of its canned urls |
| 976 | return new URLRequestTestJob(request, network_delegate, true); |
| 977 | } |
| 978 | if (!intercept_main_request_) |
| 979 | return NULL; |
| 980 | intercept_main_request_ = false; |
| 981 | did_intercept_main_ = true; |
| 982 | return new URLRequestTestJob(request, |
| 983 | network_delegate, |
| 984 | main_headers_, |
| 985 | main_data_, |
| 986 | true); |
| 987 | } |
| 988 | |
| 989 | virtual URLRequestJob* MaybeInterceptRedirect( |
| 990 | URLRequest* request, |
| 991 | NetworkDelegate* network_delegate, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 992 | const GURL& location) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 993 | if (cancel_redirect_request_) { |
| 994 | cancel_redirect_request_ = false; |
| 995 | did_cancel_redirect_ = true; |
| 996 | return new CancelTestJob(request, network_delegate); |
| 997 | } |
| 998 | if (!intercept_redirect_) |
| 999 | return NULL; |
| 1000 | intercept_redirect_ = false; |
| 1001 | did_intercept_redirect_ = true; |
| 1002 | return new URLRequestTestJob(request, |
| 1003 | network_delegate, |
| 1004 | redirect_headers_, |
| 1005 | redirect_data_, |
| 1006 | true); |
| 1007 | } |
| 1008 | |
| 1009 | virtual URLRequestJob* MaybeInterceptResponse( |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 1010 | URLRequest* request, NetworkDelegate* network_delegate) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1011 | if (cancel_final_request_) { |
| 1012 | cancel_final_request_ = false; |
| 1013 | did_cancel_final_ = true; |
| 1014 | return new CancelTestJob(request, network_delegate); |
| 1015 | } |
| 1016 | if (!intercept_final_response_) |
| 1017 | return NULL; |
| 1018 | intercept_final_response_ = false; |
| 1019 | did_intercept_final_ = true; |
| 1020 | return new URLRequestTestJob(request, |
| 1021 | network_delegate, |
| 1022 | final_headers_, |
| 1023 | final_data_, |
| 1024 | true); |
| 1025 | } |
| 1026 | |
| 1027 | // Whether to intercept the main request, and if so the response to return. |
| 1028 | bool intercept_main_request_; |
| 1029 | std::string main_headers_; |
| 1030 | std::string main_data_; |
| 1031 | |
| 1032 | // Other actions we take at MaybeIntercept time |
| 1033 | bool restart_main_request_; |
| 1034 | bool cancel_main_request_; |
| 1035 | bool cancel_then_restart_main_request_; |
| 1036 | bool simulate_main_network_error_; |
| 1037 | |
| 1038 | // Whether to intercept redirects, and if so the response to return. |
| 1039 | bool intercept_redirect_; |
| 1040 | std::string redirect_headers_; |
| 1041 | std::string redirect_data_; |
| 1042 | |
| 1043 | // Other actions we can take at MaybeInterceptRedirect time |
| 1044 | bool cancel_redirect_request_; |
| 1045 | |
| 1046 | // Whether to intercept final response, and if so the response to return. |
| 1047 | bool intercept_final_response_; |
| 1048 | std::string final_headers_; |
| 1049 | std::string final_data_; |
| 1050 | |
| 1051 | // Other actions we can take at MaybeInterceptResponse time |
| 1052 | bool cancel_final_request_; |
| 1053 | |
| 1054 | // If we did something or not |
| 1055 | bool did_intercept_main_; |
| 1056 | bool did_restart_main_; |
| 1057 | bool did_cancel_main_; |
| 1058 | bool did_cancel_then_restart_main_; |
| 1059 | bool did_simulate_error_main_; |
| 1060 | bool did_intercept_redirect_; |
| 1061 | bool did_cancel_redirect_; |
| 1062 | bool did_intercept_final_; |
| 1063 | bool did_cancel_final_; |
| 1064 | |
| 1065 | // Static getters for canned response header and data strings |
| 1066 | |
| 1067 | static std::string ok_data() { |
| 1068 | return URLRequestTestJob::test_data_1(); |
| 1069 | } |
| 1070 | |
| 1071 | static std::string ok_headers() { |
| 1072 | return URLRequestTestJob::test_headers(); |
| 1073 | } |
| 1074 | |
| 1075 | static std::string redirect_data() { |
| 1076 | return std::string(); |
| 1077 | } |
| 1078 | |
| 1079 | static std::string redirect_headers() { |
| 1080 | return URLRequestTestJob::test_redirect_headers(); |
| 1081 | } |
| 1082 | |
| 1083 | static std::string error_data() { |
| 1084 | return std::string("ohhh nooooo mr. bill!"); |
| 1085 | } |
| 1086 | |
| 1087 | static std::string error_headers() { |
| 1088 | return URLRequestTestJob::test_error_headers(); |
| 1089 | } |
| 1090 | }; |
| 1091 | |
| 1092 | TEST_F(URLRequestTest, Intercept) { |
| 1093 | TestInterceptor interceptor; |
| 1094 | |
| 1095 | // intercept the main request and respond with a simple response |
| 1096 | interceptor.intercept_main_request_ = true; |
| 1097 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1098 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1099 | |
| 1100 | TestDelegate d; |
| 1101 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1102 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 1103 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 1104 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
| 1105 | req.SetUserData(NULL, user_data0); |
| 1106 | req.SetUserData(&user_data1, user_data1); |
| 1107 | req.SetUserData(&user_data2, user_data2); |
| 1108 | req.set_method("GET"); |
| 1109 | req.Start(); |
| 1110 | MessageLoop::current()->Run(); |
| 1111 | |
| 1112 | // Make sure we can retrieve our specific user data |
| 1113 | EXPECT_EQ(user_data0, req.GetUserData(NULL)); |
| 1114 | EXPECT_EQ(user_data1, req.GetUserData(&user_data1)); |
| 1115 | EXPECT_EQ(user_data2, req.GetUserData(&user_data2)); |
| 1116 | |
| 1117 | // Check the interceptor got called as expected |
| 1118 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1119 | |
| 1120 | // Check we got one good response |
| 1121 | EXPECT_TRUE(req.status().is_success()); |
| 1122 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1123 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1124 | EXPECT_EQ(1, d.response_started_count()); |
| 1125 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1126 | } |
| 1127 | |
| 1128 | TEST_F(URLRequestTest, InterceptRedirect) { |
| 1129 | TestInterceptor interceptor; |
| 1130 | |
| 1131 | // intercept the main request and respond with a redirect |
| 1132 | interceptor.intercept_main_request_ = true; |
| 1133 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1134 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1135 | |
| 1136 | // intercept that redirect and respond a final OK response |
| 1137 | interceptor.intercept_redirect_ = true; |
| 1138 | interceptor.redirect_headers_ = TestInterceptor::ok_headers(); |
| 1139 | interceptor.redirect_data_ = TestInterceptor::ok_data(); |
| 1140 | |
| 1141 | TestDelegate d; |
| 1142 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1143 | req.set_method("GET"); |
| 1144 | req.Start(); |
| 1145 | MessageLoop::current()->Run(); |
| 1146 | |
| 1147 | // Check the interceptor got called as expected |
| 1148 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1149 | EXPECT_TRUE(interceptor.did_intercept_redirect_); |
| 1150 | |
| 1151 | // Check we got one good response |
| 1152 | EXPECT_TRUE(req.status().is_success()); |
| 1153 | if (req.status().is_success()) { |
| 1154 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1155 | } |
| 1156 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1157 | EXPECT_EQ(1, d.response_started_count()); |
| 1158 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1159 | } |
| 1160 | |
| 1161 | TEST_F(URLRequestTest, InterceptServerError) { |
| 1162 | TestInterceptor interceptor; |
| 1163 | |
| 1164 | // intercept the main request to generate a server error response |
| 1165 | interceptor.intercept_main_request_ = true; |
| 1166 | interceptor.main_headers_ = TestInterceptor::error_headers(); |
| 1167 | interceptor.main_data_ = TestInterceptor::error_data(); |
| 1168 | |
| 1169 | // intercept that error and respond with an OK response |
| 1170 | interceptor.intercept_final_response_ = true; |
| 1171 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1172 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1173 | |
| 1174 | TestDelegate d; |
| 1175 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1176 | req.set_method("GET"); |
| 1177 | req.Start(); |
| 1178 | MessageLoop::current()->Run(); |
| 1179 | |
| 1180 | // Check the interceptor got called as expected |
| 1181 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1182 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1183 | |
| 1184 | // Check we got one good response |
| 1185 | EXPECT_TRUE(req.status().is_success()); |
| 1186 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1187 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1188 | EXPECT_EQ(1, d.response_started_count()); |
| 1189 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1190 | } |
| 1191 | |
| 1192 | TEST_F(URLRequestTest, InterceptNetworkError) { |
| 1193 | TestInterceptor interceptor; |
| 1194 | |
| 1195 | // intercept the main request to simulate a network error |
| 1196 | interceptor.simulate_main_network_error_ = true; |
| 1197 | |
| 1198 | // intercept that error and respond with an OK response |
| 1199 | interceptor.intercept_final_response_ = true; |
| 1200 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1201 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1202 | |
| 1203 | TestDelegate d; |
| 1204 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1205 | req.set_method("GET"); |
| 1206 | req.Start(); |
| 1207 | MessageLoop::current()->Run(); |
| 1208 | |
| 1209 | // Check the interceptor got called as expected |
| 1210 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1211 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 1212 | |
| 1213 | // Check we received one good response |
| 1214 | EXPECT_TRUE(req.status().is_success()); |
| 1215 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1216 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1217 | EXPECT_EQ(1, d.response_started_count()); |
| 1218 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1219 | } |
| 1220 | |
| 1221 | TEST_F(URLRequestTest, InterceptRestartRequired) { |
| 1222 | TestInterceptor interceptor; |
| 1223 | |
| 1224 | // restart the main request |
| 1225 | interceptor.restart_main_request_ = true; |
| 1226 | |
| 1227 | // then intercept the new main request and respond with an OK response |
| 1228 | interceptor.intercept_main_request_ = true; |
| 1229 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 1230 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 1231 | |
| 1232 | TestDelegate d; |
| 1233 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1234 | req.set_method("GET"); |
| 1235 | req.Start(); |
| 1236 | MessageLoop::current()->Run(); |
| 1237 | |
| 1238 | // Check the interceptor got called as expected |
| 1239 | EXPECT_TRUE(interceptor.did_restart_main_); |
| 1240 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1241 | |
| 1242 | // Check we received one good response |
| 1243 | EXPECT_TRUE(req.status().is_success()); |
| 1244 | if (req.status().is_success()) { |
| 1245 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 1246 | } |
| 1247 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 1248 | EXPECT_EQ(1, d.response_started_count()); |
| 1249 | EXPECT_EQ(0, d.received_redirect_count()); |
| 1250 | } |
| 1251 | |
| 1252 | TEST_F(URLRequestTest, InterceptRespectsCancelMain) { |
| 1253 | TestInterceptor interceptor; |
| 1254 | |
| 1255 | // intercept the main request and cancel from within the restarted job |
| 1256 | interceptor.cancel_main_request_ = true; |
| 1257 | |
| 1258 | // setup to intercept final response and override it with an OK response |
| 1259 | interceptor.intercept_final_response_ = true; |
| 1260 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1261 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1262 | |
| 1263 | TestDelegate d; |
| 1264 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1265 | req.set_method("GET"); |
| 1266 | req.Start(); |
| 1267 | MessageLoop::current()->Run(); |
| 1268 | |
| 1269 | // Check the interceptor got called as expected |
| 1270 | EXPECT_TRUE(interceptor.did_cancel_main_); |
| 1271 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1272 | |
| 1273 | // Check we see a canceled request |
| 1274 | EXPECT_FALSE(req.status().is_success()); |
| 1275 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1276 | } |
| 1277 | |
| 1278 | TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) { |
| 1279 | TestInterceptor interceptor; |
| 1280 | |
| 1281 | // intercept the main request and respond with a redirect |
| 1282 | interceptor.intercept_main_request_ = true; |
| 1283 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 1284 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 1285 | |
| 1286 | // intercept the redirect and cancel from within that job |
| 1287 | interceptor.cancel_redirect_request_ = true; |
| 1288 | |
| 1289 | // setup to intercept final response and override it with an OK response |
| 1290 | interceptor.intercept_final_response_ = true; |
| 1291 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1292 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1293 | |
| 1294 | TestDelegate d; |
| 1295 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1296 | req.set_method("GET"); |
| 1297 | req.Start(); |
| 1298 | MessageLoop::current()->Run(); |
| 1299 | |
| 1300 | // Check the interceptor got called as expected |
| 1301 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 1302 | EXPECT_TRUE(interceptor.did_cancel_redirect_); |
| 1303 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1304 | |
| 1305 | // Check we see a canceled request |
| 1306 | EXPECT_FALSE(req.status().is_success()); |
| 1307 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1308 | } |
| 1309 | |
| 1310 | TEST_F(URLRequestTest, InterceptRespectsCancelFinal) { |
| 1311 | TestInterceptor interceptor; |
| 1312 | |
| 1313 | // intercept the main request to simulate a network error |
| 1314 | interceptor.simulate_main_network_error_ = true; |
| 1315 | |
| 1316 | // setup to intercept final response and cancel from within that job |
| 1317 | interceptor.cancel_final_request_ = true; |
| 1318 | |
| 1319 | TestDelegate d; |
| 1320 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1321 | req.set_method("GET"); |
| 1322 | req.Start(); |
| 1323 | MessageLoop::current()->Run(); |
| 1324 | |
| 1325 | // Check the interceptor got called as expected |
| 1326 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 1327 | EXPECT_TRUE(interceptor.did_cancel_final_); |
| 1328 | |
| 1329 | // Check we see a canceled request |
| 1330 | EXPECT_FALSE(req.status().is_success()); |
| 1331 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1332 | } |
| 1333 | |
| 1334 | TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { |
| 1335 | TestInterceptor interceptor; |
| 1336 | |
| 1337 | // intercept the main request and cancel then restart from within that job |
| 1338 | interceptor.cancel_then_restart_main_request_ = true; |
| 1339 | |
| 1340 | // setup to intercept final response and override it with an OK response |
| 1341 | interceptor.intercept_final_response_ = true; |
| 1342 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 1343 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 1344 | |
| 1345 | TestDelegate d; |
| 1346 | URLRequest req(GURL("http://test_intercept/foo"), &d, &default_context_); |
| 1347 | req.set_method("GET"); |
| 1348 | req.Start(); |
| 1349 | MessageLoop::current()->Run(); |
| 1350 | |
| 1351 | // Check the interceptor got called as expected |
| 1352 | EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); |
| 1353 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 1354 | |
| 1355 | // Check we see a canceled request |
| 1356 | EXPECT_FALSE(req.status().is_success()); |
| 1357 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1358 | } |
| 1359 | |
| 1360 | // Check that two different URL requests have different identifiers. |
| 1361 | TEST_F(URLRequestTest, Identifiers) { |
| 1362 | TestDelegate d; |
| 1363 | TestURLRequestContext context; |
| 1364 | TestURLRequest req(GURL("http://example.com"), &d, &context); |
| 1365 | TestURLRequest other_req(GURL("http://example.com"), &d, &context); |
| 1366 | |
| 1367 | ASSERT_NE(req.identifier(), other_req.identifier()); |
| 1368 | } |
| 1369 | |
| 1370 | // Check that a failure to connect to the proxy is reported to the network |
| 1371 | // delegate. |
| 1372 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
| 1373 | MockHostResolver host_resolver; |
| 1374 | host_resolver.rules()->AddSimulatedFailure("*"); |
| 1375 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 1376 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1377 | TestURLRequestContextWithProxy context("myproxy:70", &network_delegate); |
| 1378 | |
| 1379 | TestDelegate d; |
| 1380 | URLRequest req(GURL("http://example.com"), &d, &context); |
| 1381 | req.set_method("GET"); |
| 1382 | |
| 1383 | req.Start(); |
| 1384 | MessageLoop::current()->Run(); |
| 1385 | |
| 1386 | // Check we see a failed request. |
| 1387 | EXPECT_FALSE(req.status().is_success()); |
| 1388 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 1389 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error()); |
| 1390 | |
| 1391 | EXPECT_EQ(1, network_delegate.error_count()); |
| 1392 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
| 1393 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 1394 | } |
| 1395 | |
| 1396 | // Make sure that net::NetworkDelegate::NotifyCompleted is called if |
| 1397 | // content is empty. |
| 1398 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
| 1399 | TestDelegate d; |
| 1400 | URLRequest req(GURL("data:,"), &d, &default_context_); |
| 1401 | req.Start(); |
| 1402 | MessageLoop::current()->Run(); |
| 1403 | EXPECT_EQ("", d.data_received()); |
| 1404 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
| 1405 | } |
| 1406 | |
| 1407 | // TODO(droger): Support TestServer on iOS (see http://crbug.com/148666). |
| 1408 | #if !defined(OS_IOS) |
| 1409 | // A subclass of TestServer that uses a statically-configured hostname. This is |
| 1410 | // to work around mysterious failures in chrome_frame_net_tests. See: |
| 1411 | // http://crbug.com/114369 |
| 1412 | class LocalHttpTestServer : public TestServer { |
| 1413 | public: |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1414 | explicit LocalHttpTestServer(const base::FilePath& document_root) |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1415 | : TestServer(TestServer::TYPE_HTTP, |
| 1416 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 1417 | document_root) {} |
| 1418 | LocalHttpTestServer() |
| 1419 | : TestServer(TestServer::TYPE_HTTP, |
| 1420 | ScopedCustomUrlRequestTestHttpHost::value(), |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1421 | base::FilePath()) {} |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1422 | }; |
| 1423 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 1424 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1425 | LocalHttpTestServer test_server; |
| 1426 | ASSERT_TRUE(test_server.Start()); |
| 1427 | |
| 1428 | TestURLRequestContext context; |
| 1429 | scoped_refptr<DelayedCookieMonster> delayed_cm = |
| 1430 | new DelayedCookieMonster(); |
| 1431 | scoped_refptr<CookieStore> cookie_store = delayed_cm; |
| 1432 | context.set_cookie_store(delayed_cm); |
| 1433 | |
| 1434 | // Set up a cookie. |
| 1435 | { |
| 1436 | TestNetworkDelegate network_delegate; |
| 1437 | context.set_network_delegate(&network_delegate); |
| 1438 | TestDelegate d; |
| 1439 | URLRequest req( |
| 1440 | test_server.GetURL("set-cookie?CookieToNotSend=1"), &d, &context); |
| 1441 | req.Start(); |
| 1442 | MessageLoop::current()->Run(); |
| 1443 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1444 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1445 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 1446 | } |
| 1447 | |
| 1448 | // Verify that the cookie is set. |
| 1449 | { |
| 1450 | TestNetworkDelegate network_delegate; |
| 1451 | context.set_network_delegate(&network_delegate); |
| 1452 | TestDelegate d; |
| 1453 | URLRequest req(test_server.GetURL("echoheader?Cookie"), &d, &context); |
| 1454 | req.Start(); |
| 1455 | MessageLoop::current()->Run(); |
| 1456 | |
| 1457 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 1458 | != std::string::npos); |
| 1459 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1460 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1461 | } |
| 1462 | } |
| 1463 | |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 1464 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1465 | LocalHttpTestServer test_server; |
| 1466 | ASSERT_TRUE(test_server.Start()); |
| 1467 | |
| 1468 | // Set up a cookie. |
| 1469 | { |
| 1470 | TestNetworkDelegate network_delegate; |
| 1471 | default_context_.set_network_delegate(&network_delegate); |
| 1472 | TestDelegate d; |
| 1473 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), |
| 1474 | &d, |
| 1475 | &default_context_); |
| 1476 | req.Start(); |
| 1477 | MessageLoop::current()->Run(); |
| 1478 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1479 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1480 | } |
| 1481 | |
| 1482 | // Verify that the cookie is set. |
| 1483 | { |
| 1484 | TestNetworkDelegate network_delegate; |
| 1485 | default_context_.set_network_delegate(&network_delegate); |
| 1486 | TestDelegate d; |
| 1487 | URLRequest req( |
| 1488 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1489 | req.Start(); |
| 1490 | MessageLoop::current()->Run(); |
| 1491 | |
| 1492 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 1493 | != std::string::npos); |
| 1494 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1495 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1496 | } |
| 1497 | |
| 1498 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 1499 | { |
| 1500 | TestNetworkDelegate network_delegate; |
| 1501 | default_context_.set_network_delegate(&network_delegate); |
| 1502 | TestDelegate d; |
| 1503 | URLRequest req( |
| 1504 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1505 | req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES); |
| 1506 | req.Start(); |
| 1507 | MessageLoop::current()->Run(); |
| 1508 | |
| 1509 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 1510 | == std::string::npos); |
| 1511 | |
| 1512 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
| 1513 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1514 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
| 1519 | LocalHttpTestServer test_server; |
| 1520 | ASSERT_TRUE(test_server.Start()); |
| 1521 | |
| 1522 | // Set up a cookie. |
| 1523 | { |
| 1524 | TestNetworkDelegate network_delegate; |
| 1525 | default_context_.set_network_delegate(&network_delegate); |
| 1526 | TestDelegate d; |
| 1527 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
| 1528 | &d, |
| 1529 | &default_context_); |
| 1530 | req.Start(); |
| 1531 | MessageLoop::current()->Run(); |
| 1532 | |
| 1533 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1534 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1535 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
| 1536 | } |
| 1537 | |
| 1538 | // Try to set-up another cookie and update the previous cookie. |
| 1539 | { |
| 1540 | TestNetworkDelegate network_delegate; |
| 1541 | default_context_.set_network_delegate(&network_delegate); |
| 1542 | TestDelegate d; |
| 1543 | URLRequest req( |
| 1544 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
| 1545 | &d, |
| 1546 | &default_context_); |
| 1547 | req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES); |
| 1548 | req.Start(); |
| 1549 | |
| 1550 | MessageLoop::current()->Run(); |
| 1551 | |
| 1552 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
| 1553 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1554 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1555 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 1556 | } |
| 1557 | |
| 1558 | // Verify the cookies weren't saved or updated. |
| 1559 | { |
| 1560 | TestNetworkDelegate network_delegate; |
| 1561 | default_context_.set_network_delegate(&network_delegate); |
| 1562 | TestDelegate d; |
| 1563 | URLRequest req( |
| 1564 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1565 | req.Start(); |
| 1566 | MessageLoop::current()->Run(); |
| 1567 | |
| 1568 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 1569 | == std::string::npos); |
| 1570 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 1571 | != std::string::npos); |
| 1572 | |
| 1573 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1574 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1575 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 1576 | } |
| 1577 | } |
| 1578 | |
| 1579 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
| 1580 | LocalHttpTestServer test_server; |
| 1581 | ASSERT_TRUE(test_server.Start()); |
| 1582 | |
| 1583 | // Set up a cookie. |
| 1584 | { |
| 1585 | TestNetworkDelegate network_delegate; |
| 1586 | default_context_.set_network_delegate(&network_delegate); |
| 1587 | TestDelegate d; |
| 1588 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), |
| 1589 | &d, |
| 1590 | &default_context_); |
| 1591 | req.Start(); |
| 1592 | MessageLoop::current()->Run(); |
| 1593 | |
| 1594 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1595 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1596 | } |
| 1597 | |
| 1598 | // Verify that the cookie is set. |
| 1599 | { |
| 1600 | TestNetworkDelegate network_delegate; |
| 1601 | default_context_.set_network_delegate(&network_delegate); |
| 1602 | TestDelegate d; |
| 1603 | URLRequest req( |
| 1604 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1605 | req.Start(); |
| 1606 | MessageLoop::current()->Run(); |
| 1607 | |
| 1608 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 1609 | != std::string::npos); |
| 1610 | |
| 1611 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1612 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1613 | } |
| 1614 | |
| 1615 | // Verify that the cookie isn't sent. |
| 1616 | { |
| 1617 | TestNetworkDelegate network_delegate; |
| 1618 | default_context_.set_network_delegate(&network_delegate); |
| 1619 | TestDelegate d; |
| 1620 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
| 1621 | URLRequest req( |
| 1622 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1623 | req.Start(); |
| 1624 | MessageLoop::current()->Run(); |
| 1625 | |
| 1626 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 1627 | == std::string::npos); |
| 1628 | |
| 1629 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
| 1630 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
| 1635 | LocalHttpTestServer test_server; |
| 1636 | ASSERT_TRUE(test_server.Start()); |
| 1637 | |
| 1638 | // Set up a cookie. |
| 1639 | { |
| 1640 | TestNetworkDelegate network_delegate; |
| 1641 | default_context_.set_network_delegate(&network_delegate); |
| 1642 | TestDelegate d; |
| 1643 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
| 1644 | &d, |
| 1645 | &default_context_); |
| 1646 | req.Start(); |
| 1647 | MessageLoop::current()->Run(); |
| 1648 | |
| 1649 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1650 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1651 | } |
| 1652 | |
| 1653 | // Try to set-up another cookie and update the previous cookie. |
| 1654 | { |
| 1655 | TestNetworkDelegate network_delegate; |
| 1656 | default_context_.set_network_delegate(&network_delegate); |
| 1657 | TestDelegate d; |
| 1658 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
| 1659 | URLRequest req( |
| 1660 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
| 1661 | &d, |
| 1662 | &default_context_); |
| 1663 | req.Start(); |
| 1664 | |
| 1665 | MessageLoop::current()->Run(); |
| 1666 | |
| 1667 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1668 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 1669 | } |
| 1670 | |
| 1671 | // Verify the cookies weren't saved or updated. |
| 1672 | { |
| 1673 | TestNetworkDelegate network_delegate; |
| 1674 | default_context_.set_network_delegate(&network_delegate); |
| 1675 | TestDelegate d; |
| 1676 | URLRequest req( |
| 1677 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1678 | req.Start(); |
| 1679 | MessageLoop::current()->Run(); |
| 1680 | |
| 1681 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 1682 | == std::string::npos); |
| 1683 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 1684 | != std::string::npos); |
| 1685 | |
| 1686 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1687 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
| 1692 | LocalHttpTestServer test_server; |
| 1693 | ASSERT_TRUE(test_server.Start()); |
| 1694 | |
| 1695 | // Set up an empty cookie. |
| 1696 | { |
| 1697 | TestNetworkDelegate network_delegate; |
| 1698 | default_context_.set_network_delegate(&network_delegate); |
| 1699 | TestDelegate d; |
| 1700 | URLRequest req(test_server.GetURL("set-cookie"), &d, &default_context_); |
| 1701 | req.Start(); |
| 1702 | MessageLoop::current()->Run(); |
| 1703 | |
| 1704 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1705 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1706 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
| 1711 | LocalHttpTestServer test_server; |
| 1712 | ASSERT_TRUE(test_server.Start()); |
| 1713 | |
| 1714 | // Set up a cookie. |
| 1715 | { |
| 1716 | TestNetworkDelegate network_delegate; |
| 1717 | default_context_.set_network_delegate(&network_delegate); |
| 1718 | TestDelegate d; |
| 1719 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), |
| 1720 | &d, |
| 1721 | &default_context_); |
| 1722 | req.Start(); |
| 1723 | MessageLoop::current()->Run(); |
| 1724 | |
| 1725 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1726 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1727 | } |
| 1728 | |
| 1729 | // Verify that the cookie is set. |
| 1730 | { |
| 1731 | TestNetworkDelegate network_delegate; |
| 1732 | default_context_.set_network_delegate(&network_delegate); |
| 1733 | TestDelegate d; |
| 1734 | URLRequest req( |
| 1735 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1736 | req.Start(); |
| 1737 | MessageLoop::current()->Run(); |
| 1738 | |
| 1739 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 1740 | != std::string::npos); |
| 1741 | |
| 1742 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1743 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1744 | } |
| 1745 | |
| 1746 | // Verify that the cookie isn't sent. |
| 1747 | { |
| 1748 | TestNetworkDelegate network_delegate; |
| 1749 | default_context_.set_network_delegate(&network_delegate); |
| 1750 | TestDelegate d; |
| 1751 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
| 1752 | URLRequest req( |
| 1753 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1754 | req.Start(); |
| 1755 | MessageLoop::current()->Run(); |
| 1756 | |
| 1757 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 1758 | == std::string::npos); |
| 1759 | |
| 1760 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
| 1761 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
| 1766 | LocalHttpTestServer test_server; |
| 1767 | ASSERT_TRUE(test_server.Start()); |
| 1768 | |
| 1769 | // Set up a cookie. |
| 1770 | { |
| 1771 | TestNetworkDelegate network_delegate; |
| 1772 | default_context_.set_network_delegate(&network_delegate); |
| 1773 | TestDelegate d; |
| 1774 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), |
| 1775 | &d, |
| 1776 | &default_context_); |
| 1777 | req.Start(); |
| 1778 | MessageLoop::current()->Run(); |
| 1779 | |
| 1780 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1781 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1782 | } |
| 1783 | |
| 1784 | // Try to set-up another cookie and update the previous cookie. |
| 1785 | { |
| 1786 | TestNetworkDelegate network_delegate; |
| 1787 | default_context_.set_network_delegate(&network_delegate); |
| 1788 | TestDelegate d; |
| 1789 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
| 1790 | URLRequest req( |
| 1791 | test_server.GetURL("set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), |
| 1792 | &d, |
| 1793 | &default_context_); |
| 1794 | req.Start(); |
| 1795 | |
| 1796 | MessageLoop::current()->Run(); |
| 1797 | |
| 1798 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1799 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
| 1800 | } |
| 1801 | |
| 1802 | // Verify the cookies weren't saved or updated. |
| 1803 | { |
| 1804 | TestNetworkDelegate network_delegate; |
| 1805 | default_context_.set_network_delegate(&network_delegate); |
| 1806 | TestDelegate d; |
| 1807 | URLRequest req( |
| 1808 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1809 | req.Start(); |
| 1810 | MessageLoop::current()->Run(); |
| 1811 | |
| 1812 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 1813 | == std::string::npos); |
| 1814 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 1815 | != std::string::npos); |
| 1816 | |
| 1817 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 1818 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 1819 | } |
| 1820 | } |
| 1821 | |
[email protected] | 5095cd7 | 2012-11-01 10:29:16 | [diff] [blame] | 1822 | // FixedDateNetworkDelegate swaps out the server's HTTP Date response header |
| 1823 | // value for the |fixed_date| argument given to the constructor. |
| 1824 | class FixedDateNetworkDelegate : public TestNetworkDelegate { |
| 1825 | public: |
| 1826 | explicit FixedDateNetworkDelegate(const std::string& fixed_date) |
| 1827 | : fixed_date_(fixed_date) {} |
| 1828 | virtual ~FixedDateNetworkDelegate() {} |
| 1829 | |
| 1830 | // net::NetworkDelegate implementation |
| 1831 | virtual int OnHeadersReceived( |
| 1832 | net::URLRequest* request, |
| 1833 | const net::CompletionCallback& callback, |
| 1834 | const net::HttpResponseHeaders* original_response_headers, |
| 1835 | scoped_refptr<net::HttpResponseHeaders>* override_response_headers) |
| 1836 | OVERRIDE; |
| 1837 | |
| 1838 | private: |
| 1839 | std::string fixed_date_; |
| 1840 | |
| 1841 | DISALLOW_COPY_AND_ASSIGN(FixedDateNetworkDelegate); |
| 1842 | }; |
| 1843 | |
| 1844 | int FixedDateNetworkDelegate::OnHeadersReceived( |
| 1845 | net::URLRequest* request, |
| 1846 | const net::CompletionCallback& callback, |
| 1847 | const net::HttpResponseHeaders* original_response_headers, |
| 1848 | scoped_refptr<net::HttpResponseHeaders>* override_response_headers) { |
| 1849 | net::HttpResponseHeaders* new_response_headers = |
| 1850 | new net::HttpResponseHeaders(original_response_headers->raw_headers()); |
| 1851 | |
| 1852 | new_response_headers->RemoveHeader("Date"); |
| 1853 | new_response_headers->AddHeader("Date: " + fixed_date_); |
| 1854 | |
| 1855 | *override_response_headers = new_response_headers; |
| 1856 | return TestNetworkDelegate::OnHeadersReceived(request, |
| 1857 | callback, |
| 1858 | original_response_headers, |
| 1859 | override_response_headers); |
| 1860 | } |
| 1861 | |
| 1862 | // Test that cookie expiration times are adjusted for server/client clock |
| 1863 | // skew and that we handle incorrect timezone specifier "UTC" in HTTP Date |
| 1864 | // headers by defaulting to GMT. (crbug.com/135131) |
| 1865 | TEST_F(URLRequestTest, AcceptClockSkewCookieWithWrongDateTimezone) { |
| 1866 | LocalHttpTestServer test_server; |
| 1867 | ASSERT_TRUE(test_server.Start()); |
| 1868 | |
| 1869 | // Set up an expired cookie. |
| 1870 | { |
| 1871 | TestNetworkDelegate network_delegate; |
| 1872 | default_context_.set_network_delegate(&network_delegate); |
| 1873 | TestDelegate d; |
| 1874 | URLRequest req(test_server.GetURL( |
| 1875 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
| 1876 | &d, |
| 1877 | &default_context_); |
| 1878 | req.Start(); |
| 1879 | MessageLoop::current()->Run(); |
| 1880 | } |
| 1881 | // Verify that the cookie is not set. |
| 1882 | { |
| 1883 | TestNetworkDelegate network_delegate; |
| 1884 | default_context_.set_network_delegate(&network_delegate); |
| 1885 | TestDelegate d; |
| 1886 | URLRequest req( |
| 1887 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1888 | req.Start(); |
| 1889 | MessageLoop::current()->Run(); |
| 1890 | |
| 1891 | EXPECT_TRUE(d.data_received().find("StillGood=1") == std::string::npos); |
| 1892 | } |
| 1893 | // Set up a cookie with clock skew and "UTC" HTTP Date timezone specifier. |
| 1894 | { |
| 1895 | FixedDateNetworkDelegate network_delegate("18-Apr-1977 22:49:13 UTC"); |
| 1896 | default_context_.set_network_delegate(&network_delegate); |
| 1897 | TestDelegate d; |
| 1898 | URLRequest req(test_server.GetURL( |
| 1899 | "set-cookie?StillGood=1;expires=Mon,18-Apr-1977,22:50:13,GMT"), |
| 1900 | &d, |
| 1901 | &default_context_); |
| 1902 | req.Start(); |
| 1903 | MessageLoop::current()->Run(); |
| 1904 | } |
| 1905 | // Verify that the cookie is set. |
| 1906 | { |
| 1907 | TestNetworkDelegate network_delegate; |
| 1908 | default_context_.set_network_delegate(&network_delegate); |
| 1909 | TestDelegate d; |
| 1910 | URLRequest req( |
| 1911 | test_server.GetURL("echoheader?Cookie"), &d, &default_context_); |
| 1912 | req.Start(); |
| 1913 | MessageLoop::current()->Run(); |
| 1914 | |
| 1915 | EXPECT_TRUE(d.data_received().find("StillGood=1") != std::string::npos); |
| 1916 | } |
| 1917 | } |
| 1918 | |
| 1919 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 1920 | // Check that it is impossible to change the referrer in the extra headers of |
| 1921 | // an URLRequest. |
| 1922 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
| 1923 | LocalHttpTestServer test_server; |
| 1924 | ASSERT_TRUE(test_server.Start()); |
| 1925 | |
| 1926 | // If extra headers contain referer and the request contains a referer, |
| 1927 | // only the latter shall be respected. |
| 1928 | { |
| 1929 | TestDelegate d; |
| 1930 | URLRequest req( |
| 1931 | test_server.GetURL("echoheader?Referer"), &d, &default_context_); |
| 1932 | req.set_referrer("http://foo.com/"); |
| 1933 | |
| 1934 | HttpRequestHeaders headers; |
| 1935 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
| 1936 | req.SetExtraRequestHeaders(headers); |
| 1937 | |
| 1938 | req.Start(); |
| 1939 | MessageLoop::current()->Run(); |
| 1940 | |
| 1941 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 1942 | } |
| 1943 | |
| 1944 | // If extra headers contain a referer but the request does not, no referer |
| 1945 | // shall be sent in the header. |
| 1946 | { |
| 1947 | TestDelegate d; |
| 1948 | URLRequest req( |
| 1949 | test_server.GetURL("echoheader?Referer"), &d, &default_context_); |
| 1950 | |
| 1951 | HttpRequestHeaders headers; |
| 1952 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
| 1953 | req.SetExtraRequestHeaders(headers); |
| 1954 | req.set_load_flags(LOAD_VALIDATE_CACHE); |
| 1955 | |
| 1956 | req.Start(); |
| 1957 | MessageLoop::current()->Run(); |
| 1958 | |
| 1959 | EXPECT_EQ("None", d.data_received()); |
| 1960 | } |
| 1961 | } |
| 1962 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 1963 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1964 | public: |
| 1965 | URLRequestTestHTTP() |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 1966 | : test_server_(base::FilePath(FILE_PATH_LITERAL( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1967 | "net/data/url_request_unittest"))) { |
| 1968 | } |
| 1969 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 1970 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 1971 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 1972 | // |request_method| is the method to use for the initial request. |
| 1973 | // |redirect_method| is the method that is expected to be used for the second |
| 1974 | // request, after redirection. |
| 1975 | // If |include_data| is true, data is uploaded with the request. The |
| 1976 | // response body is expected to match it exactly, if and only if |
| 1977 | // |request_method| == |redirect_method|. |
| 1978 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 1979 | const std::string& request_method, |
| 1980 | const std::string& redirect_method, |
| 1981 | bool include_data) { |
| 1982 | static const char kData[] = "hello world"; |
| 1983 | TestDelegate d; |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 1984 | URLRequest req(redirect_url, &d, &default_context_); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 1985 | req.set_method(request_method); |
| 1986 | if (include_data) { |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 1987 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 1988 | HttpRequestHeaders headers; |
| 1989 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 1990 | base::UintToString(arraysize(kData) - 1)); |
| 1991 | req.SetExtraRequestHeaders(headers); |
| 1992 | } |
| 1993 | req.Start(); |
| 1994 | MessageLoop::current()->Run(); |
| 1995 | EXPECT_EQ(redirect_method, req.method()); |
| 1996 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 1997 | EXPECT_EQ(OK, req.status().error()); |
| 1998 | if (include_data) { |
| 1999 | if (request_method == redirect_method) { |
| 2000 | EXPECT_EQ(kData, d.data_received()); |
| 2001 | } else { |
| 2002 | EXPECT_NE(kData, d.data_received()); |
| 2003 | } |
| 2004 | } |
| 2005 | if (HasFailure()) |
| 2006 | LOG(WARNING) << "Request method was: " << request_method; |
| 2007 | } |
| 2008 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2009 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2010 | const int kMsgSize = 20000; // multiple of 10 |
| 2011 | const int kIterations = 50; |
[email protected] | f43b89f3 | 2012-05-01 19:39:48 | [diff] [blame] | 2012 | char* uploadBytes = new char[kMsgSize+1]; |
| 2013 | char* ptr = uploadBytes; |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2014 | char marker = 'a'; |
| 2015 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 2016 | memcpy(ptr, "----------", 10); |
| 2017 | ptr += 10; |
| 2018 | if (idx % 100 == 0) { |
| 2019 | ptr--; |
| 2020 | *ptr++ = marker; |
| 2021 | if (++marker > 'z') |
| 2022 | marker = 'a'; |
| 2023 | } |
| 2024 | } |
| 2025 | uploadBytes[kMsgSize] = '\0'; |
| 2026 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2027 | for (int i = 0; i < kIterations; ++i) { |
| 2028 | TestDelegate d; |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2029 | URLRequest r(test_server_.GetURL("echo"), &d, &default_context_); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2030 | r.set_method(method.c_str()); |
| 2031 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 2032 | r.set_upload(make_scoped_ptr(CreateSimpleUploadData(uploadBytes))); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2033 | |
| 2034 | r.Start(); |
| 2035 | EXPECT_TRUE(r.is_pending()); |
| 2036 | |
| 2037 | MessageLoop::current()->Run(); |
| 2038 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2039 | ASSERT_EQ(1, d.response_started_count()) |
| 2040 | << "request failed: " << r.status().status() |
| 2041 | << ", os error: " << r.status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2042 | |
| 2043 | EXPECT_FALSE(d.received_data_before_response()); |
| 2044 | EXPECT_EQ(uploadBytes, d.data_received()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 2045 | } |
| 2046 | delete[] uploadBytes; |
| 2047 | } |
| 2048 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2049 | void AddChunksToUpload(URLRequest* r) { |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 2050 | r->AppendChunkToUpload("a", 1, false); |
| 2051 | r->AppendChunkToUpload("bcd", 3, false); |
| 2052 | r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); |
| 2053 | r->AppendChunkToUpload("\r\n\r\n", 4, false); |
| 2054 | r->AppendChunkToUpload("0", 1, false); |
| 2055 | r->AppendChunkToUpload("2323", 4, true); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2056 | } |
| 2057 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2058 | void VerifyReceivedDataMatchesChunks(URLRequest* r, TestDelegate* d) { |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2059 | // This should match the chunks sent by AddChunksToUpload(). |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2060 | const std::string expected_data = |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2061 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 2062 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2063 | ASSERT_EQ(1, d->response_started_count()) |
| 2064 | << "request failed: " << r->status().status() |
| 2065 | << ", os error: " << r->status().error(); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2066 | |
| 2067 | EXPECT_FALSE(d->received_data_before_response()); |
| 2068 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 2069 | EXPECT_EQ(expected_data.size(), static_cast<size_t>(d->bytes_received())); |
| 2070 | EXPECT_EQ(expected_data, d->data_received()); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 2071 | } |
| 2072 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2073 | bool DoManyCookiesRequest(int num_cookies) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2074 | TestDelegate d; |
| 2075 | URLRequest r(test_server_.GetURL("set-many-cookies?" + |
| 2076 | base::IntToString(num_cookies)), |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2077 | &d, |
| 2078 | &default_context_); |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2079 | |
| 2080 | r.Start(); |
| 2081 | EXPECT_TRUE(r.is_pending()); |
| 2082 | |
| 2083 | MessageLoop::current()->Run(); |
| 2084 | |
| 2085 | bool is_success = r.status().is_success(); |
| 2086 | |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 2087 | if (!is_success) { |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2088 | // Requests handled by ChromeFrame send a less precise error message, |
| 2089 | // ERR_CONNECTION_ABORTED. |
| 2090 | EXPECT_TRUE(r.status().error() == ERR_RESPONSE_HEADERS_TOO_BIG || |
| 2091 | r.status().error() == ERR_CONNECTION_ABORTED); |
| 2092 | // The test server appears to be unable to handle subsequent requests |
| 2093 | // after this error is triggered. Force it to restart. |
| 2094 | EXPECT_TRUE(test_server_.Stop()); |
| 2095 | EXPECT_TRUE(test_server_.Start()); |
| 2096 | } |
| 2097 | |
| 2098 | return is_success; |
| 2099 | } |
| 2100 | |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2101 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2102 | }; |
| 2103 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2104 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 2105 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 2106 | // The HTTPTestServer will return a 302 response, which we should not |
| 2107 | // follow. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 2108 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2109 | ASSERT_TRUE(test_server_.Start()); |
| 2110 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2111 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2112 | TestURLRequestContextWithProxy context( |
| 2113 | test_server_.host_port_pair().ToString(), |
| 2114 | &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2115 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2116 | TestDelegate d; |
| 2117 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2118 | URLRequest r(GURL("https://www.redirect.com/"), &d, &context); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2119 | r.Start(); |
| 2120 | EXPECT_TRUE(r.is_pending()); |
| 2121 | |
| 2122 | MessageLoop::current()->Run(); |
| 2123 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2124 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2125 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 2126 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 2127 | // We should not have followed the redirect. |
| 2128 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2129 | } |
| 2130 | } |
| 2131 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2132 | // This is the same as the previous test, but checks that the network delegate |
| 2133 | // registers the error. |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame^] | 2134 | TEST_F(URLRequestTestHTTP, NetworkDelegateTunnelConnectionFailed) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2135 | ASSERT_TRUE(test_server_.Start()); |
| 2136 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2137 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2138 | TestURLRequestContextWithProxy context( |
| 2139 | test_server_.host_port_pair().ToString(), |
| 2140 | &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2141 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2142 | TestDelegate d; |
| 2143 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2144 | URLRequest r(GURL("https://www.redirect.com/"), &d, &context); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2145 | r.Start(); |
| 2146 | EXPECT_TRUE(r.is_pending()); |
| 2147 | |
| 2148 | MessageLoop::current()->Run(); |
| 2149 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2150 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2151 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2152 | EXPECT_EQ(1, d.response_started_count()); |
| 2153 | // We should not have followed the redirect. |
| 2154 | EXPECT_EQ(0, d.received_redirect_count()); |
| 2155 | |
| 2156 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2157 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2161 | // Tests that we can block and asynchronously return OK in various stages. |
| 2162 | TEST_F(URLRequestTestHTTP, NetworkDelegateBlockAsynchronously) { |
| 2163 | static const BlockingNetworkDelegate::Stage blocking_stages[] = { |
| 2164 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 2165 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 2166 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED |
| 2167 | }; |
| 2168 | static const size_t blocking_stages_length = arraysize(blocking_stages); |
| 2169 | |
| 2170 | ASSERT_TRUE(test_server_.Start()); |
| 2171 | |
| 2172 | TestDelegate d; |
| 2173 | BlockingNetworkDelegate network_delegate( |
| 2174 | BlockingNetworkDelegate::USER_CALLBACK); |
| 2175 | network_delegate.set_block_on( |
| 2176 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST | |
| 2177 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS | |
| 2178 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
| 2179 | |
| 2180 | TestURLRequestContext context(true); |
| 2181 | context.set_network_delegate(&network_delegate); |
| 2182 | context.Init(); |
| 2183 | |
| 2184 | { |
| 2185 | URLRequest r(test_server_.GetURL("empty.html"), &d, &context); |
| 2186 | |
| 2187 | r.Start(); |
| 2188 | for (size_t i = 0; i < blocking_stages_length; ++i) { |
| 2189 | MessageLoop::current()->Run(); |
| 2190 | EXPECT_EQ(blocking_stages[i], |
| 2191 | network_delegate.stage_blocked_for_callback()); |
| 2192 | network_delegate.DoCallback(OK); |
| 2193 | } |
| 2194 | MessageLoop::current()->Run(); |
| 2195 | EXPECT_EQ(200, r.GetResponseCode()); |
| 2196 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2197 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2198 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2199 | } |
| 2200 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2201 | } |
| 2202 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2203 | // Tests that the network delegate can block and cancel a request. |
| 2204 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
| 2205 | ASSERT_TRUE(test_server_.Start()); |
| 2206 | |
| 2207 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2208 | BlockingNetworkDelegate network_delegate( |
| 2209 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2210 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
| 2211 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2212 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2213 | TestURLRequestContextWithProxy context( |
| 2214 | test_server_.host_port_pair().ToString(), |
| 2215 | &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2216 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2217 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2218 | URLRequest r(test_server_.GetURL(""), &d, &context); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2219 | |
| 2220 | r.Start(); |
| 2221 | MessageLoop::current()->Run(); |
| 2222 | |
| 2223 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2224 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2225 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2226 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2227 | } |
| 2228 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2229 | } |
| 2230 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2231 | // Helper function for NetworkDelegateCancelRequestAsynchronously and |
| 2232 | // NetworkDelegateCancelRequestSynchronously. Sets up a blocking network |
| 2233 | // delegate operating in |block_mode| and a request for |url|. It blocks the |
| 2234 | // request in |stage| and cancels it with ERR_BLOCKED_BY_CLIENT. |
| 2235 | void NetworkDelegateCancelRequest(BlockingNetworkDelegate::BlockMode block_mode, |
| 2236 | BlockingNetworkDelegate::Stage stage, |
| 2237 | const GURL& url) { |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2238 | TestDelegate d; |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2239 | BlockingNetworkDelegate network_delegate(block_mode); |
| 2240 | network_delegate.set_retval(ERR_BLOCKED_BY_CLIENT); |
| 2241 | network_delegate.set_block_on(stage); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2242 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2243 | TestURLRequestContext context(true); |
| 2244 | context.set_network_delegate(&network_delegate); |
| 2245 | context.Init(); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2246 | |
| 2247 | { |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2248 | URLRequest r(url, &d, &context); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2249 | |
| 2250 | r.Start(); |
| 2251 | MessageLoop::current()->Run(); |
| 2252 | |
| 2253 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2254 | EXPECT_EQ(ERR_BLOCKED_BY_CLIENT, r.status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 2255 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2256 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2257 | } |
| 2258 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2259 | } |
| 2260 | |
[email protected] | b4438d3 | 2012-09-27 06:15:30 | [diff] [blame] | 2261 | // The following 3 tests check that the network delegate can cancel a request |
| 2262 | // synchronously in various stages of the request. |
| 2263 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously1) { |
| 2264 | ASSERT_TRUE(test_server_.Start()); |
| 2265 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2266 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 2267 | test_server_.GetURL("")); |
| 2268 | } |
| 2269 | |
| 2270 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously2) { |
| 2271 | ASSERT_TRUE(test_server_.Start()); |
| 2272 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2273 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 2274 | test_server_.GetURL("")); |
| 2275 | } |
| 2276 | |
| 2277 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously3) { |
| 2278 | ASSERT_TRUE(test_server_.Start()); |
| 2279 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::SYNCHRONOUS, |
| 2280 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 2281 | test_server_.GetURL("")); |
| 2282 | } |
| 2283 | |
| 2284 | // The following 3 tests check that the network delegate can cancel a request |
| 2285 | // asynchronously in various stages of the request. |
| 2286 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously1) { |
| 2287 | ASSERT_TRUE(test_server_.Start()); |
| 2288 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2289 | BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 2290 | test_server_.GetURL("")); |
| 2291 | } |
| 2292 | |
| 2293 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously2) { |
| 2294 | ASSERT_TRUE(test_server_.Start()); |
| 2295 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2296 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 2297 | test_server_.GetURL("")); |
| 2298 | } |
| 2299 | |
| 2300 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestAsynchronously3) { |
| 2301 | ASSERT_TRUE(test_server_.Start()); |
| 2302 | NetworkDelegateCancelRequest(BlockingNetworkDelegate::AUTO_CALLBACK, |
| 2303 | BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 2304 | test_server_.GetURL("")); |
| 2305 | } |
| 2306 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2307 | // Tests that the network delegate can block and redirect a request to a new |
| 2308 | // URL. |
| 2309 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
| 2310 | ASSERT_TRUE(test_server_.Start()); |
| 2311 | |
| 2312 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2313 | BlockingNetworkDelegate network_delegate( |
| 2314 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2315 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2316 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2317 | network_delegate.set_redirect_url(redirect_url); |
| 2318 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2319 | TestURLRequestContextWithProxy context( |
| 2320 | test_server_.host_port_pair().ToString(), |
| 2321 | &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2322 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2323 | { |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 2324 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2325 | URLRequest r(original_url, &d, &context); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2326 | |
| 2327 | r.Start(); |
| 2328 | MessageLoop::current()->Run(); |
| 2329 | |
| 2330 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2331 | EXPECT_EQ(0, r.status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2332 | EXPECT_EQ(redirect_url, r.url()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 2333 | EXPECT_EQ(original_url, r.original_url()); |
| 2334 | EXPECT_EQ(2U, r.url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 2335 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2336 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2337 | } |
| 2338 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2339 | } |
| 2340 | |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2341 | // Tests that the network delegate can block and redirect a request to a new |
| 2342 | // URL by setting a redirect_url and returning in OnBeforeURLRequest directly. |
| 2343 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestSynchronously) { |
| 2344 | ASSERT_TRUE(test_server_.Start()); |
| 2345 | |
| 2346 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2347 | BlockingNetworkDelegate network_delegate( |
| 2348 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2349 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 2350 | network_delegate.set_redirect_url(redirect_url); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2351 | |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2352 | TestURLRequestContextWithProxy context( |
| 2353 | test_server_.host_port_pair().ToString(), |
| 2354 | &network_delegate); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2355 | |
| 2356 | { |
| 2357 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2358 | URLRequest r(original_url, &d, &context); |
[email protected] | b813ed7 | 2012-04-05 08:21:36 | [diff] [blame] | 2359 | |
| 2360 | r.Start(); |
| 2361 | MessageLoop::current()->Run(); |
| 2362 | |
| 2363 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2364 | EXPECT_EQ(0, r.status().error()); |
| 2365 | EXPECT_EQ(redirect_url, r.url()); |
| 2366 | EXPECT_EQ(original_url, r.original_url()); |
| 2367 | EXPECT_EQ(2U, r.url_chain().size()); |
| 2368 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2369 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2370 | } |
| 2371 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2372 | } |
| 2373 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2374 | // Tests that redirects caused by the network delegate preserve POST data. |
| 2375 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 2376 | ASSERT_TRUE(test_server_.Start()); |
| 2377 | |
| 2378 | const char kData[] = "hello world"; |
| 2379 | |
| 2380 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2381 | BlockingNetworkDelegate network_delegate( |
| 2382 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2383 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2384 | GURL redirect_url(test_server_.GetURL("echo")); |
| 2385 | network_delegate.set_redirect_url(redirect_url); |
| 2386 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2387 | TestURLRequestContext context(true); |
| 2388 | context.set_network_delegate(&network_delegate); |
| 2389 | context.Init(); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2390 | |
| 2391 | { |
| 2392 | GURL original_url(test_server_.GetURL("empty.html")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2393 | URLRequest r(original_url, &d, &context); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2394 | r.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 2395 | r.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2396 | HttpRequestHeaders headers; |
| 2397 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 2398 | base::UintToString(arraysize(kData) - 1)); |
| 2399 | r.SetExtraRequestHeaders(headers); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 2400 | r.Start(); |
| 2401 | MessageLoop::current()->Run(); |
| 2402 | |
| 2403 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2404 | EXPECT_EQ(0, r.status().error()); |
| 2405 | EXPECT_EQ(redirect_url, r.url()); |
| 2406 | EXPECT_EQ(original_url, r.original_url()); |
| 2407 | EXPECT_EQ(2U, r.url_chain().size()); |
| 2408 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2409 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2410 | EXPECT_EQ("POST", r.method()); |
| 2411 | EXPECT_EQ(kData, d.data_received()); |
| 2412 | } |
| 2413 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2414 | } |
| 2415 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2416 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2417 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 2418 | // handle the challenge, and is passing the buck along to the |
| 2419 | // URLRequest::Delegate. |
| 2420 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 2421 | ASSERT_TRUE(test_server_.Start()); |
| 2422 | |
| 2423 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2424 | BlockingNetworkDelegate network_delegate( |
| 2425 | BlockingNetworkDelegate::SYNCHRONOUS); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2426 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2427 | TestURLRequestContext context(true); |
| 2428 | context.set_network_delegate(&network_delegate); |
| 2429 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2430 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2431 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2432 | |
| 2433 | { |
| 2434 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2435 | URLRequest r(url, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2436 | r.Start(); |
| 2437 | MessageLoop::current()->Run(); |
| 2438 | |
| 2439 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2440 | EXPECT_EQ(0, r.status().error()); |
| 2441 | EXPECT_EQ(200, r.GetResponseCode()); |
| 2442 | EXPECT_TRUE(d.auth_required_called()); |
| 2443 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2444 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2445 | } |
| 2446 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2447 | } |
| 2448 | |
| 2449 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2450 | // by setting credentials. |
| 2451 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 2452 | ASSERT_TRUE(test_server_.Start()); |
| 2453 | |
| 2454 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2455 | BlockingNetworkDelegate network_delegate( |
| 2456 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 2457 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2458 | network_delegate.set_auth_retval( |
| 2459 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 2460 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2461 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2462 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2463 | TestURLRequestContext context(true); |
| 2464 | context.set_network_delegate(&network_delegate); |
| 2465 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2466 | |
| 2467 | { |
| 2468 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2469 | URLRequest r(url, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2470 | r.Start(); |
| 2471 | MessageLoop::current()->Run(); |
| 2472 | |
| 2473 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2474 | EXPECT_EQ(0, r.status().error()); |
| 2475 | EXPECT_EQ(200, r.GetResponseCode()); |
| 2476 | EXPECT_FALSE(d.auth_required_called()); |
| 2477 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2478 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2479 | } |
| 2480 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2481 | } |
| 2482 | |
| 2483 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 2484 | // by cancelling authentication. |
| 2485 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 2486 | ASSERT_TRUE(test_server_.Start()); |
| 2487 | |
| 2488 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2489 | BlockingNetworkDelegate network_delegate( |
| 2490 | BlockingNetworkDelegate::SYNCHRONOUS); |
| 2491 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2492 | network_delegate.set_auth_retval( |
| 2493 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 2494 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2495 | TestURLRequestContext context(true); |
| 2496 | context.set_network_delegate(&network_delegate); |
| 2497 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2498 | |
| 2499 | { |
| 2500 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2501 | URLRequest r(url, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2502 | r.Start(); |
| 2503 | MessageLoop::current()->Run(); |
| 2504 | |
| 2505 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2506 | EXPECT_EQ(OK, r.status().error()); |
| 2507 | EXPECT_EQ(401, r.GetResponseCode()); |
| 2508 | EXPECT_FALSE(d.auth_required_called()); |
| 2509 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2510 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2511 | } |
| 2512 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2513 | } |
| 2514 | |
| 2515 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2516 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 2517 | // to handle the challenge, and is passing the buck along to the |
| 2518 | // URLRequest::Delegate. |
| 2519 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 2520 | ASSERT_TRUE(test_server_.Start()); |
| 2521 | |
| 2522 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2523 | BlockingNetworkDelegate network_delegate( |
| 2524 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2525 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2526 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2527 | TestURLRequestContext context(true); |
| 2528 | context.set_network_delegate(&network_delegate); |
| 2529 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2530 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2531 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2532 | |
| 2533 | { |
| 2534 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2535 | URLRequest r(url, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2536 | r.Start(); |
| 2537 | MessageLoop::current()->Run(); |
| 2538 | |
| 2539 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2540 | EXPECT_EQ(0, r.status().error()); |
| 2541 | EXPECT_EQ(200, r.GetResponseCode()); |
| 2542 | EXPECT_TRUE(d.auth_required_called()); |
| 2543 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2544 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2545 | } |
| 2546 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2547 | } |
| 2548 | |
| 2549 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2550 | // by setting credentials. |
| 2551 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 2552 | ASSERT_TRUE(test_server_.Start()); |
| 2553 | |
| 2554 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2555 | BlockingNetworkDelegate network_delegate( |
| 2556 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2557 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2558 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2559 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 2560 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2561 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2562 | network_delegate.set_auth_credentials(auth_credentials); |
| 2563 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2564 | TestURLRequestContext context(true); |
| 2565 | context.set_network_delegate(&network_delegate); |
| 2566 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2567 | |
| 2568 | { |
| 2569 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2570 | URLRequest r(url, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2571 | r.Start(); |
| 2572 | MessageLoop::current()->Run(); |
| 2573 | |
| 2574 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2575 | EXPECT_EQ(0, r.status().error()); |
| 2576 | |
| 2577 | EXPECT_EQ(200, r.GetResponseCode()); |
| 2578 | EXPECT_FALSE(d.auth_required_called()); |
| 2579 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2580 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2581 | } |
| 2582 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2583 | } |
| 2584 | |
| 2585 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 2586 | // by cancelling authentication. |
| 2587 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 2588 | ASSERT_TRUE(test_server_.Start()); |
| 2589 | |
| 2590 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2591 | BlockingNetworkDelegate network_delegate( |
| 2592 | BlockingNetworkDelegate::AUTO_CALLBACK); |
| 2593 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2594 | network_delegate.set_auth_retval( |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2595 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 2596 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2597 | TestURLRequestContext context(true); |
| 2598 | context.set_network_delegate(&network_delegate); |
| 2599 | context.Init(); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2600 | |
| 2601 | { |
| 2602 | GURL url(test_server_.GetURL("auth-basic")); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2603 | URLRequest r(url, &d, &context); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 2604 | r.Start(); |
| 2605 | MessageLoop::current()->Run(); |
| 2606 | |
| 2607 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 2608 | EXPECT_EQ(OK, r.status().error()); |
| 2609 | EXPECT_EQ(401, r.GetResponseCode()); |
| 2610 | EXPECT_FALSE(d.auth_required_called()); |
| 2611 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2612 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2613 | } |
| 2614 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2615 | } |
| 2616 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2617 | // Tests that we can handle when a network request was canceled while we were |
| 2618 | // waiting for the network delegate. |
| 2619 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 2620 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 2621 | ASSERT_TRUE(test_server_.Start()); |
| 2622 | |
| 2623 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2624 | BlockingNetworkDelegate network_delegate( |
| 2625 | BlockingNetworkDelegate::USER_CALLBACK); |
| 2626 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2627 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2628 | TestURLRequestContext context(true); |
| 2629 | context.set_network_delegate(&network_delegate); |
| 2630 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2631 | |
| 2632 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2633 | URLRequest r(test_server_.GetURL(""), &d, &context); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2634 | |
| 2635 | r.Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2636 | MessageLoop::current()->Run(); |
| 2637 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_URL_REQUEST, |
| 2638 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2639 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2640 | // Cancel before callback. |
| 2641 | r.Cancel(); |
| 2642 | // Ensure that network delegate is notified. |
| 2643 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2644 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2645 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2646 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2647 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2648 | } |
| 2649 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2650 | } |
| 2651 | |
| 2652 | // Tests that we can handle when a network request was canceled while we were |
| 2653 | // waiting for the network delegate. |
| 2654 | // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 2655 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
| 2656 | ASSERT_TRUE(test_server_.Start()); |
| 2657 | |
| 2658 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2659 | BlockingNetworkDelegate network_delegate( |
| 2660 | BlockingNetworkDelegate::USER_CALLBACK); |
| 2661 | network_delegate.set_block_on( |
| 2662 | BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2663 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2664 | TestURLRequestContext context(true); |
| 2665 | context.set_network_delegate(&network_delegate); |
| 2666 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2667 | |
| 2668 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2669 | URLRequest r(test_server_.GetURL(""), &d, &context); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2670 | |
| 2671 | r.Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2672 | MessageLoop::current()->Run(); |
| 2673 | EXPECT_EQ(BlockingNetworkDelegate::ON_BEFORE_SEND_HEADERS, |
| 2674 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2675 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2676 | // Cancel before callback. |
| 2677 | r.Cancel(); |
| 2678 | // Ensure that network delegate is notified. |
| 2679 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2680 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2681 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2682 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2683 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2684 | } |
| 2685 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2686 | } |
| 2687 | |
| 2688 | // Tests that we can handle when a network request was canceled while we were |
| 2689 | // waiting for the network delegate. |
| 2690 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 2691 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 2692 | ASSERT_TRUE(test_server_.Start()); |
| 2693 | |
| 2694 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2695 | BlockingNetworkDelegate network_delegate( |
| 2696 | BlockingNetworkDelegate::USER_CALLBACK); |
| 2697 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_HEADERS_RECEIVED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2698 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2699 | TestURLRequestContext context(true); |
| 2700 | context.set_network_delegate(&network_delegate); |
| 2701 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2702 | |
| 2703 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2704 | URLRequest r(test_server_.GetURL(""), &d, &context); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2705 | |
| 2706 | r.Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2707 | MessageLoop::current()->Run(); |
| 2708 | EXPECT_EQ(BlockingNetworkDelegate::ON_HEADERS_RECEIVED, |
| 2709 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2710 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2711 | // Cancel before callback. |
| 2712 | r.Cancel(); |
| 2713 | // Ensure that network delegate is notified. |
| 2714 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2715 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2716 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2717 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2718 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2719 | } |
| 2720 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2721 | } |
| 2722 | |
| 2723 | // Tests that we can handle when a network request was canceled while we were |
| 2724 | // waiting for the network delegate. |
| 2725 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
[email protected] | bfe8b30 | 2013-02-15 12:16:02 | [diff] [blame] | 2726 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2727 | ASSERT_TRUE(test_server_.Start()); |
| 2728 | |
| 2729 | TestDelegate d; |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2730 | BlockingNetworkDelegate network_delegate( |
| 2731 | BlockingNetworkDelegate::USER_CALLBACK); |
| 2732 | network_delegate.set_block_on(BlockingNetworkDelegate::ON_AUTH_REQUIRED); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2733 | |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2734 | TestURLRequestContext context(true); |
| 2735 | context.set_network_delegate(&network_delegate); |
| 2736 | context.Init(); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2737 | |
| 2738 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2739 | URLRequest r(test_server_.GetURL("auth-basic"), &d, &context); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2740 | |
| 2741 | r.Start(); |
[email protected] | dc5a5cf | 2012-09-26 02:49:30 | [diff] [blame] | 2742 | MessageLoop::current()->Run(); |
| 2743 | EXPECT_EQ(BlockingNetworkDelegate::ON_AUTH_REQUIRED, |
| 2744 | network_delegate.stage_blocked_for_callback()); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 2745 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 2746 | // Cancel before callback. |
| 2747 | r.Cancel(); |
| 2748 | // Ensure that network delegate is notified. |
| 2749 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 2750 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 2751 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 2752 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 2753 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 2754 | } |
| 2755 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 2756 | } |
| 2757 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2758 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 2759 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 2760 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2761 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2762 | ASSERT_TRUE(test_server_.Start()); |
| 2763 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2764 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | d5a4dd6 | 2012-05-23 01:41:04 | [diff] [blame] | 2765 | TestURLRequestContextWithProxy context( |
| 2766 | test_server_.host_port_pair().ToString(), |
| 2767 | &network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2768 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 2769 | TestDelegate d; |
| 2770 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2771 | URLRequest r(GURL("https://www.server-auth.com/"), &d, &context); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 2772 | |
| 2773 | r.Start(); |
| 2774 | EXPECT_TRUE(r.is_pending()); |
| 2775 | |
| 2776 | MessageLoop::current()->Run(); |
| 2777 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2778 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2779 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 2780 | } |
| 2781 | } |
| 2782 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2783 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2784 | ASSERT_TRUE(test_server_.Start()); |
| 2785 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2786 | TestDelegate d; |
| 2787 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2788 | URLRequest r(test_server_.GetURL(""), &d, &default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2789 | |
| 2790 | r.Start(); |
| 2791 | EXPECT_TRUE(r.is_pending()); |
| 2792 | |
| 2793 | MessageLoop::current()->Run(); |
| 2794 | |
| 2795 | EXPECT_EQ(1, d.response_started_count()); |
| 2796 | EXPECT_FALSE(d.received_data_before_response()); |
| 2797 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 2798 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 2799 | r.GetSocketAddress().host()); |
| 2800 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 2801 | r.GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 2802 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2803 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2804 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2805 | } |
| 2806 | |
[email protected] | 263163f | 2012-06-14 22:40:34 | [diff] [blame] | 2807 | // This test has the server send a large number of cookies to the client. |
| 2808 | // To ensure that no number of cookies causes a crash, a galloping binary |
| 2809 | // search is used to estimate that maximum number of cookies that are accepted |
| 2810 | // by the browser. Beyond the maximum number, the request will fail with |
| 2811 | // ERR_RESPONSE_HEADERS_TOO_BIG. |
| 2812 | TEST_F(URLRequestTestHTTP, GetTest_ManyCookies) { |
| 2813 | ASSERT_TRUE(test_server_.Start()); |
| 2814 | |
| 2815 | int lower_bound = 0; |
| 2816 | int upper_bound = 1; |
| 2817 | |
| 2818 | // Double the number of cookies until the response header limits are |
| 2819 | // exceeded. |
| 2820 | while (DoManyCookiesRequest(upper_bound)) { |
| 2821 | lower_bound = upper_bound; |
| 2822 | upper_bound *= 2; |
| 2823 | ASSERT_LT(upper_bound, 1000000); |
| 2824 | } |
| 2825 | |
| 2826 | int tolerance = upper_bound * 0.005; |
| 2827 | if (tolerance < 2) |
| 2828 | tolerance = 2; |
| 2829 | |
| 2830 | // Perform a binary search to find the highest possible number of cookies, |
| 2831 | // within the desired tolerance. |
| 2832 | while (upper_bound - lower_bound >= tolerance) { |
| 2833 | int num_cookies = (lower_bound + upper_bound) / 2; |
| 2834 | |
| 2835 | if (DoManyCookiesRequest(num_cookies)) |
| 2836 | lower_bound = num_cookies; |
| 2837 | else |
| 2838 | upper_bound = num_cookies; |
| 2839 | } |
| 2840 | // Success: the test did not crash. |
| 2841 | } |
| 2842 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 2843 | TEST_F(URLRequestTestHTTP, GetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2844 | ASSERT_TRUE(test_server_.Start()); |
| 2845 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2846 | TestDelegate d; |
| 2847 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2848 | URLRequest r(test_server_.GetURL(""), &d, &default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2849 | |
| 2850 | r.Start(); |
| 2851 | EXPECT_TRUE(r.is_pending()); |
| 2852 | |
| 2853 | MessageLoop::current()->Run(); |
| 2854 | |
| 2855 | EXPECT_EQ(1, d.response_started_count()); |
| 2856 | EXPECT_FALSE(d.received_data_before_response()); |
| 2857 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 2858 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 2859 | r.GetSocketAddress().host()); |
| 2860 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 2861 | r.GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2862 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 2863 | } |
| 2864 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2865 | TEST_F(URLRequestTestHTTP, GetTestLoadTiming) { |
| 2866 | ASSERT_TRUE(test_server_.Start()); |
| 2867 | |
| 2868 | TestDelegate d; |
| 2869 | { |
| 2870 | URLRequest r(test_server_.GetURL(""), &d, &default_context_); |
| 2871 | |
| 2872 | r.Start(); |
| 2873 | EXPECT_TRUE(r.is_pending()); |
| 2874 | |
| 2875 | MessageLoop::current()->Run(); |
| 2876 | |
| 2877 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 2878 | r.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2879 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2880 | |
| 2881 | EXPECT_EQ(1, d.response_started_count()); |
| 2882 | EXPECT_FALSE(d.received_data_before_response()); |
| 2883 | EXPECT_NE(0, d.bytes_received()); |
| 2884 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 2885 | r.GetSocketAddress().host()); |
| 2886 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 2887 | r.GetSocketAddress().port()); |
| 2888 | } |
| 2889 | } |
| 2890 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 2891 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
| 2892 | ASSERT_TRUE(test_server_.Start()); |
| 2893 | |
| 2894 | // Parameter that specifies the Content-Length field in the response: |
| 2895 | // C - Compressed length. |
| 2896 | // U - Uncompressed length. |
| 2897 | // L - Large length (larger than both C & U). |
| 2898 | // M - Medium length (between C & U). |
| 2899 | // S - Small length (smaller than both C & U). |
| 2900 | const char test_parameters[] = "CULMS"; |
| 2901 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 2902 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 2903 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 2904 | // S has too little data, but we seem to accept it. |
| 2905 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 2906 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 2907 | |
| 2908 | for (int i = 0; i < num_tests ; i++) { |
| 2909 | TestDelegate d; |
| 2910 | { |
| 2911 | std::string test_file = |
| 2912 | base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c", |
| 2913 | test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 2914 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 2915 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2916 | TestURLRequestContext context(true); |
| 2917 | context.set_network_delegate(&network_delegate); |
| 2918 | context.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2919 | |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2920 | URLRequest r(test_server_.GetURL(test_file), &d, &context); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 2921 | r.Start(); |
| 2922 | EXPECT_TRUE(r.is_pending()); |
| 2923 | |
| 2924 | MessageLoop::current()->Run(); |
| 2925 | |
| 2926 | EXPECT_EQ(1, d.response_started_count()); |
| 2927 | EXPECT_FALSE(d.received_data_before_response()); |
| 2928 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
| 2929 | << " status = " << r.status().status() |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2930 | << " error = " << r.status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 2931 | if (test_expect_success[i]) { |
| 2932 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()) |
| 2933 | << " Parameter = \"" << test_file << "\""; |
| 2934 | } else { |
| 2935 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | 5543cbb | 2012-04-20 16:35:23 | [diff] [blame] | 2936 | EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, r.status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 2937 | << " Parameter = \"" << test_file << "\""; |
| 2938 | } |
| 2939 | } |
| 2940 | } |
| 2941 | } |
| 2942 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame^] | 2943 | TEST_F(URLRequestTestHTTP, HTTPSToHTTPRedirectNoRefererTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2944 | ASSERT_TRUE(test_server_.Start()); |
| 2945 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 2946 | TestServer https_test_server( |
| 2947 | TestServer::TYPE_HTTPS, TestServer::kLocalhost, |
| 2948 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2949 | ASSERT_TRUE(https_test_server.Start()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 2950 | |
| 2951 | // An https server is sent a request with an https referer, |
| 2952 | // and responds with a redirect to an http url. The http |
| 2953 | // server should not be sent the referer. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2954 | GURL http_destination = test_server_.GetURL(""); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 2955 | TestDelegate d; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 2956 | URLRequest req(https_test_server.GetURL( |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 2957 | "server-redirect?" + http_destination.spec()), &d, &default_context_); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 2958 | req.set_referrer("https://www.referrer.com/"); |
| 2959 | req.Start(); |
| 2960 | MessageLoop::current()->Run(); |
| 2961 | |
| 2962 | EXPECT_EQ(1, d.response_started_count()); |
| 2963 | EXPECT_EQ(1, d.received_redirect_count()); |
| 2964 | EXPECT_EQ(http_destination, req.url()); |
| 2965 | EXPECT_EQ(std::string(), req.referrer()); |
| 2966 | } |
| 2967 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2968 | TEST_F(URLRequestTestHTTP, RedirectLoadTiming) { |
| 2969 | ASSERT_TRUE(test_server_.Start()); |
| 2970 | |
| 2971 | GURL destination_url = test_server_.GetURL(""); |
| 2972 | GURL original_url = test_server_.GetURL( |
| 2973 | "server-redirect?" + destination_url.spec()); |
| 2974 | TestDelegate d; |
| 2975 | URLRequest req(original_url, &d, &default_context_); |
| 2976 | req.Start(); |
| 2977 | MessageLoop::current()->Run(); |
| 2978 | |
| 2979 | EXPECT_EQ(1, d.response_started_count()); |
| 2980 | EXPECT_EQ(1, d.received_redirect_count()); |
| 2981 | EXPECT_EQ(destination_url, req.url()); |
| 2982 | EXPECT_EQ(original_url, req.original_url()); |
| 2983 | ASSERT_EQ(2U, req.url_chain().size()); |
| 2984 | EXPECT_EQ(original_url, req.url_chain()[0]); |
| 2985 | EXPECT_EQ(destination_url, req.url_chain()[1]); |
| 2986 | |
| 2987 | LoadTimingInfo load_timing_info_before_redirect; |
| 2988 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeRedirect( |
| 2989 | &load_timing_info_before_redirect)); |
| 2990 | TestLoadTimingNotReused(load_timing_info_before_redirect, |
| 2991 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 2992 | |
| 2993 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 2994 | req.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 2995 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 2996 | |
| 2997 | // Check that a new socket was used on redirect, since the server does not |
| 2998 | // supposed keep-alive sockets, and that the times before the redirect are |
| 2999 | // before the ones recorded for the second request. |
| 3000 | EXPECT_NE(load_timing_info_before_redirect.socket_log_id, |
| 3001 | load_timing_info.socket_log_id); |
| 3002 | EXPECT_LE(load_timing_info_before_redirect.receive_headers_end, |
| 3003 | load_timing_info.connect_timing.connect_start); |
| 3004 | } |
| 3005 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3006 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
| 3007 | ASSERT_TRUE(test_server_.Start()); |
| 3008 | |
| 3009 | GURL destination_url = test_server_.GetURL(""); |
| 3010 | GURL middle_redirect_url = test_server_.GetURL( |
| 3011 | "server-redirect?" + destination_url.spec()); |
| 3012 | GURL original_url = test_server_.GetURL( |
| 3013 | "server-redirect?" + middle_redirect_url.spec()); |
| 3014 | TestDelegate d; |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 3015 | URLRequest req(original_url, &d, &default_context_); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 3016 | req.Start(); |
| 3017 | MessageLoop::current()->Run(); |
| 3018 | |
| 3019 | EXPECT_EQ(1, d.response_started_count()); |
| 3020 | EXPECT_EQ(2, d.received_redirect_count()); |
| 3021 | EXPECT_EQ(destination_url, req.url()); |
| 3022 | EXPECT_EQ(original_url, req.original_url()); |
| 3023 | ASSERT_EQ(3U, req.url_chain().size()); |
| 3024 | EXPECT_EQ(original_url, req.url_chain()[0]); |
| 3025 | EXPECT_EQ(middle_redirect_url, req.url_chain()[1]); |
| 3026 | EXPECT_EQ(destination_url, req.url_chain()[2]); |
| 3027 | } |
| 3028 | |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 3029 | namespace { |
| 3030 | |
| 3031 | const char kExtraHeader[] = "Allow-Snafu"; |
| 3032 | const char kExtraValue[] = "fubar"; |
| 3033 | |
| 3034 | class RedirectWithAdditionalHeadersDelegate : public TestDelegate { |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 3035 | virtual void OnReceivedRedirect(net::URLRequest* request, |
| 3036 | const GURL& new_url, |
| 3037 | bool* defer_redirect) OVERRIDE { |
[email protected] | 847c0fa9 | 2012-11-06 16:37:42 | [diff] [blame] | 3038 | TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect); |
| 3039 | request->SetExtraRequestHeaderByName(kExtraHeader, kExtraValue, false); |
| 3040 | } |
| 3041 | }; |
| 3042 | |
| 3043 | } // namespace |
| 3044 | |
| 3045 | TEST_F(URLRequestTestHTTP, RedirectWithAdditionalHeadersTest) { |
| 3046 | ASSERT_TRUE(test_server_.Start()); |
| 3047 | |
| 3048 | GURL destination_url = test_server_.GetURL( |
| 3049 | "echoheader?" + std::string(kExtraHeader)); |
| 3050 | GURL original_url = test_server_.GetURL( |
| 3051 | "server-redirect?" + destination_url.spec()); |
| 3052 | RedirectWithAdditionalHeadersDelegate d; |
| 3053 | URLRequest req(original_url, &d, &default_context_); |
| 3054 | req.Start(); |
| 3055 | MessageLoop::current()->Run(); |
| 3056 | |
| 3057 | std::string value; |
| 3058 | const HttpRequestHeaders& headers = req.extra_request_headers(); |
| 3059 | EXPECT_TRUE(headers.GetHeader(kExtraHeader, &value)); |
| 3060 | EXPECT_EQ(kExtraValue, value); |
| 3061 | EXPECT_FALSE(req.is_pending()); |
| 3062 | EXPECT_FALSE(req.is_redirecting()); |
| 3063 | EXPECT_EQ(kExtraValue, d.data_received()); |
| 3064 | } |
| 3065 | |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 3066 | namespace { |
| 3067 | |
| 3068 | const char kExtraHeaderToRemove[] = "To-Be-Removed"; |
| 3069 | |
| 3070 | class RedirectWithHeaderRemovalDelegate : public TestDelegate { |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 3071 | virtual void OnReceivedRedirect(net::URLRequest* request, |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 3072 | const GURL& new_url, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 3073 | bool* defer_redirect) OVERRIDE { |
[email protected] | 251a1b9 | 2012-11-13 11:01:09 | [diff] [blame] | 3074 | TestDelegate::OnReceivedRedirect(request, new_url, defer_redirect); |
| 3075 | request->RemoveRequestHeaderByName(kExtraHeaderToRemove); |
| 3076 | } |
| 3077 | }; |
| 3078 | |
| 3079 | } // namespace |
| 3080 | |
| 3081 | TEST_F(URLRequestTestHTTP, RedirectWithHeaderRemovalTest) { |
| 3082 | ASSERT_TRUE(test_server_.Start()); |
| 3083 | |
| 3084 | GURL destination_url = test_server_.GetURL( |
| 3085 | "echoheader?" + std::string(kExtraHeaderToRemove)); |
| 3086 | GURL original_url = test_server_.GetURL( |
| 3087 | "server-redirect?" + destination_url.spec()); |
| 3088 | RedirectWithHeaderRemovalDelegate d; |
| 3089 | URLRequest req(original_url, &d, &default_context_); |
| 3090 | req.SetExtraRequestHeaderByName(kExtraHeaderToRemove, "dummy", false); |
| 3091 | req.Start(); |
| 3092 | MessageLoop::current()->Run(); |
| 3093 | |
| 3094 | std::string value; |
| 3095 | const HttpRequestHeaders& headers = req.extra_request_headers(); |
| 3096 | EXPECT_FALSE(headers.GetHeader(kExtraHeaderToRemove, &value)); |
| 3097 | EXPECT_FALSE(req.is_pending()); |
| 3098 | EXPECT_FALSE(req.is_redirecting()); |
| 3099 | EXPECT_EQ("None", d.data_received()); |
| 3100 | } |
| 3101 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3102 | TEST_F(URLRequestTestHTTP, CancelTest) { |
| 3103 | TestDelegate d; |
| 3104 | { |
| 3105 | URLRequest r(GURL("http://www.google.com/"), &d, &default_context_); |
| 3106 | |
| 3107 | r.Start(); |
| 3108 | EXPECT_TRUE(r.is_pending()); |
| 3109 | |
| 3110 | r.Cancel(); |
| 3111 | |
| 3112 | MessageLoop::current()->Run(); |
| 3113 | |
| 3114 | // We expect to receive OnResponseStarted even though the request has been |
| 3115 | // cancelled. |
| 3116 | EXPECT_EQ(1, d.response_started_count()); |
| 3117 | EXPECT_EQ(0, d.bytes_received()); |
| 3118 | EXPECT_FALSE(d.received_data_before_response()); |
| 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
| 3123 | ASSERT_TRUE(test_server_.Start()); |
| 3124 | |
| 3125 | TestDelegate d; |
| 3126 | { |
| 3127 | URLRequest r(test_server_.GetURL(""), &d, &default_context_); |
| 3128 | |
| 3129 | d.set_cancel_in_response_started(true); |
| 3130 | |
| 3131 | r.Start(); |
| 3132 | EXPECT_TRUE(r.is_pending()); |
| 3133 | |
| 3134 | MessageLoop::current()->Run(); |
| 3135 | |
| 3136 | EXPECT_EQ(1, d.response_started_count()); |
| 3137 | EXPECT_EQ(0, d.bytes_received()); |
| 3138 | EXPECT_FALSE(d.received_data_before_response()); |
| 3139 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 3140 | } |
| 3141 | } |
| 3142 | |
| 3143 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
| 3144 | ASSERT_TRUE(test_server_.Start()); |
| 3145 | |
| 3146 | TestDelegate d; |
| 3147 | { |
| 3148 | URLRequest r(test_server_.GetURL(""), &d, &default_context_); |
| 3149 | |
| 3150 | d.set_cancel_in_received_data(true); |
| 3151 | |
| 3152 | r.Start(); |
| 3153 | EXPECT_TRUE(r.is_pending()); |
| 3154 | |
| 3155 | MessageLoop::current()->Run(); |
| 3156 | |
| 3157 | EXPECT_EQ(1, d.response_started_count()); |
| 3158 | // There is no guarantee about how much data was received |
| 3159 | // before the cancel was issued. It could have been 0 bytes, |
| 3160 | // or it could have been all the bytes. |
| 3161 | // EXPECT_EQ(0, d.bytes_received()); |
| 3162 | EXPECT_FALSE(d.received_data_before_response()); |
| 3163 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
| 3168 | ASSERT_TRUE(test_server_.Start()); |
| 3169 | |
| 3170 | TestDelegate d; |
| 3171 | { |
| 3172 | URLRequest r(test_server_.GetURL(""), &d, &default_context_); |
| 3173 | |
| 3174 | r.Start(); |
| 3175 | EXPECT_TRUE(r.is_pending()); |
| 3176 | |
| 3177 | // The request will be implicitly canceled when it is destroyed. The |
| 3178 | // test delegate must not post a quit message when this happens because |
| 3179 | // this test doesn't actually have a message loop. The quit message would |
| 3180 | // get put on this thread's message queue and the next test would exit |
| 3181 | // early, causing problems. |
| 3182 | d.set_quit_on_complete(false); |
| 3183 | } |
| 3184 | // expect things to just cleanup properly. |
| 3185 | |
| 3186 | // we won't actually get a received reponse here because we've never run the |
| 3187 | // message loop |
| 3188 | EXPECT_FALSE(d.received_data_before_response()); |
| 3189 | EXPECT_EQ(0, d.bytes_received()); |
| 3190 | } |
| 3191 | |
| 3192 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
| 3193 | ASSERT_TRUE(test_server_.Start()); |
| 3194 | |
| 3195 | // populate cache |
| 3196 | { |
| 3197 | TestDelegate d; |
| 3198 | URLRequest r(test_server_.GetURL("cachetime"), &d, &default_context_); |
| 3199 | r.Start(); |
| 3200 | MessageLoop::current()->Run(); |
| 3201 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 3202 | } |
| 3203 | |
| 3204 | // cancel read from cache (see bug 990242) |
| 3205 | { |
| 3206 | TestDelegate d; |
| 3207 | URLRequest r(test_server_.GetURL("cachetime"), &d, &default_context_); |
| 3208 | r.Start(); |
| 3209 | r.Cancel(); |
| 3210 | MessageLoop::current()->Run(); |
| 3211 | |
| 3212 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 3213 | EXPECT_EQ(1, d.response_started_count()); |
| 3214 | EXPECT_EQ(0, d.bytes_received()); |
| 3215 | EXPECT_FALSE(d.received_data_before_response()); |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | TEST_F(URLRequestTestHTTP, PostTest) { |
| 3220 | ASSERT_TRUE(test_server_.Start()); |
| 3221 | HTTPUploadDataOperationTest("POST"); |
| 3222 | } |
| 3223 | |
| 3224 | TEST_F(URLRequestTestHTTP, PutTest) { |
| 3225 | ASSERT_TRUE(test_server_.Start()); |
| 3226 | HTTPUploadDataOperationTest("PUT"); |
| 3227 | } |
| 3228 | |
| 3229 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
| 3230 | ASSERT_TRUE(test_server_.Start()); |
| 3231 | |
| 3232 | TestDelegate d; |
| 3233 | { |
| 3234 | URLRequest r(test_server_.GetURL("echo"), &d, &default_context_); |
| 3235 | r.set_method("POST"); |
| 3236 | |
| 3237 | r.Start(); |
| 3238 | EXPECT_TRUE(r.is_pending()); |
| 3239 | |
| 3240 | MessageLoop::current()->Run(); |
| 3241 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3242 | ASSERT_EQ(1, d.response_started_count()) |
| 3243 | << "request failed: " << r.status().status() |
| 3244 | << ", error: " << r.status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3245 | |
| 3246 | EXPECT_FALSE(d.received_data_before_response()); |
| 3247 | EXPECT_TRUE(d.data_received().empty()); |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
| 3252 | ASSERT_TRUE(test_server_.Start()); |
| 3253 | |
| 3254 | TestDelegate d; |
| 3255 | { |
| 3256 | URLRequest r(test_server_.GetURL("echo"), &d, &default_context_); |
| 3257 | r.set_method("POST"); |
| 3258 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3259 | base::FilePath dir; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3260 | PathService::Get(base::DIR_EXE, &dir); |
| 3261 | file_util::SetCurrentDirectory(dir); |
| 3262 | |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 3263 | ScopedVector<UploadElementReader> element_readers; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3264 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3265 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3266 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 3267 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 3268 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 3269 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 3270 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 3271 | element_readers.push_back(new UploadFileElementReader( |
[email protected] | 671a714 | 2013-01-10 14:08:07 | [diff] [blame] | 3272 | base::MessageLoopProxy::current(), path, 0, kuint64max, base::Time())); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3273 | |
| 3274 | // This file should just be ignored in the upload stream. |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 3275 | element_readers.push_back(new UploadFileElementReader( |
[email protected] | 671a714 | 2013-01-10 14:08:07 | [diff] [blame] | 3276 | base::MessageLoopProxy::current(), |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3277 | base::FilePath(FILE_PATH_LITERAL( |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3278 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 3279 | 0, kuint64max, base::Time())); |
| 3280 | r.set_upload(make_scoped_ptr(new UploadDataStream(&element_readers, 0))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3281 | |
| 3282 | r.Start(); |
| 3283 | EXPECT_TRUE(r.is_pending()); |
| 3284 | |
| 3285 | MessageLoop::current()->Run(); |
| 3286 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3287 | int64 size = 0; |
| 3288 | ASSERT_EQ(true, file_util::GetFileSize(path, &size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3289 | scoped_array<char> buf(new char[size]); |
| 3290 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3291 | ASSERT_EQ(size, file_util::ReadFile(path, buf.get(), size)); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3292 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3293 | ASSERT_EQ(1, d.response_started_count()) |
| 3294 | << "request failed: " << r.status().status() |
| 3295 | << ", error: " << r.status().error(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3296 | |
| 3297 | EXPECT_FALSE(d.received_data_before_response()); |
| 3298 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3299 | EXPECT_EQ(size, d.bytes_received()); |
| 3300 | EXPECT_EQ(std::string(&buf[0], size), d.data_received()); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
| 3305 | ASSERT_TRUE(test_server_.Start()); |
| 3306 | |
| 3307 | TestDelegate d; |
| 3308 | { |
| 3309 | URLRequest r(test_server_.GetURL("echo"), &d, &default_context_); |
| 3310 | r.EnableChunkedUpload(); |
| 3311 | r.set_method("POST"); |
| 3312 | AddChunksToUpload(&r); |
| 3313 | r.Start(); |
| 3314 | EXPECT_TRUE(r.is_pending()); |
| 3315 | |
| 3316 | MessageLoop::current()->Run(); |
| 3317 | |
| 3318 | VerifyReceivedDataMatchesChunks(&r, &d); |
| 3319 | } |
| 3320 | } |
| 3321 | |
[email protected] | 329b68b | 2012-11-14 17:54:27 | [diff] [blame] | 3322 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataJustAfterStart) { |
| 3323 | ASSERT_TRUE(test_server_.Start()); |
| 3324 | |
| 3325 | TestDelegate d; |
| 3326 | { |
| 3327 | URLRequest r(test_server_.GetURL("echo"), &d, &default_context_); |
| 3328 | r.EnableChunkedUpload(); |
| 3329 | r.set_method("POST"); |
| 3330 | r.Start(); |
| 3331 | EXPECT_TRUE(r.is_pending()); |
| 3332 | AddChunksToUpload(&r); |
| 3333 | MessageLoop::current()->Run(); |
| 3334 | |
| 3335 | VerifyReceivedDataMatchesChunks(&r, &d); |
| 3336 | } |
| 3337 | } |
| 3338 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3339 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
| 3340 | ASSERT_TRUE(test_server_.Start()); |
| 3341 | |
| 3342 | TestDelegate d; |
| 3343 | { |
| 3344 | URLRequest r(test_server_.GetURL("echo"), &d, &default_context_); |
| 3345 | r.EnableChunkedUpload(); |
| 3346 | r.set_method("POST"); |
| 3347 | r.Start(); |
| 3348 | EXPECT_TRUE(r.is_pending()); |
| 3349 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 3350 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3351 | AddChunksToUpload(&r); |
| 3352 | MessageLoop::current()->Run(); |
| 3353 | |
| 3354 | VerifyReceivedDataMatchesChunks(&r, &d); |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
| 3359 | ASSERT_TRUE(test_server_.Start()); |
| 3360 | |
| 3361 | TestDelegate d; |
| 3362 | URLRequest req( |
| 3363 | test_server_.GetURL("files/with-headers.html"), &d, &default_context_); |
| 3364 | req.Start(); |
| 3365 | MessageLoop::current()->Run(); |
| 3366 | |
| 3367 | const HttpResponseHeaders* headers = req.response_headers(); |
| 3368 | |
| 3369 | // Simple sanity check that response_info() accesses the same data. |
| 3370 | EXPECT_EQ(headers, req.response_info().headers.get()); |
| 3371 | |
| 3372 | std::string header; |
| 3373 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 3374 | EXPECT_EQ("private", header); |
| 3375 | |
| 3376 | header.clear(); |
| 3377 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 3378 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 3379 | |
| 3380 | // The response has two "X-Multiple-Entries" headers. |
| 3381 | // This verfies our output has them concatenated together. |
| 3382 | header.clear(); |
| 3383 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 3384 | EXPECT_EQ("a, b", header); |
| 3385 | } |
| 3386 | |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 3387 | TEST_F(URLRequestTestHTTP, ProcessSTS) { |
| 3388 | TestServer::SSLOptions ssl_options; |
| 3389 | TestServer https_test_server( |
| 3390 | TestServer::TYPE_HTTPS, |
| 3391 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3392 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 3393 | ASSERT_TRUE(https_test_server.Start()); |
| 3394 | |
| 3395 | TestDelegate d; |
| 3396 | URLRequest request( |
| 3397 | https_test_server.GetURL("files/hsts-headers.html"), |
| 3398 | &d, |
| 3399 | &default_context_); |
| 3400 | request.Start(); |
| 3401 | MessageLoop::current()->Run(); |
| 3402 | |
| 3403 | TransportSecurityState* security_state = |
| 3404 | default_context_.transport_security_state(); |
| 3405 | bool sni_available = true; |
| 3406 | TransportSecurityState::DomainState domain_state; |
| 3407 | EXPECT_TRUE(security_state->GetDomainState( |
| 3408 | TestServer::kLocalhost, sni_available, &domain_state)); |
| 3409 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
| 3410 | domain_state.upgrade_mode); |
| 3411 | EXPECT_TRUE(domain_state.include_subdomains); |
| 3412 | } |
| 3413 | |
| 3414 | TEST_F(URLRequestTestHTTP, ProcessSTSOnce) { |
| 3415 | TestServer::SSLOptions ssl_options; |
| 3416 | TestServer https_test_server( |
| 3417 | TestServer::TYPE_HTTPS, |
| 3418 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3419 | base::FilePath(FILE_PATH_LITERAL("net/data/url_request_unittest"))); |
[email protected] | 242d856 | 2012-10-30 21:20:46 | [diff] [blame] | 3420 | ASSERT_TRUE(https_test_server.Start()); |
| 3421 | |
| 3422 | TestDelegate d; |
| 3423 | URLRequest request( |
| 3424 | https_test_server.GetURL("files/hsts-multiple-headers.html"), |
| 3425 | &d, |
| 3426 | &default_context_); |
| 3427 | request.Start(); |
| 3428 | MessageLoop::current()->Run(); |
| 3429 | |
| 3430 | // We should have set parameters from the first header, not the second. |
| 3431 | TransportSecurityState* security_state = |
| 3432 | default_context_.transport_security_state(); |
| 3433 | bool sni_available = true; |
| 3434 | TransportSecurityState::DomainState domain_state; |
| 3435 | EXPECT_TRUE(security_state->GetDomainState( |
| 3436 | TestServer::kLocalhost, sni_available, &domain_state)); |
| 3437 | EXPECT_EQ(TransportSecurityState::DomainState::MODE_FORCE_HTTPS, |
| 3438 | domain_state.upgrade_mode); |
| 3439 | EXPECT_FALSE(domain_state.include_subdomains); |
| 3440 | } |
| 3441 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3442 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
| 3443 | ASSERT_TRUE(test_server_.Start()); |
| 3444 | |
| 3445 | TestDelegate d; |
| 3446 | URLRequest req(test_server_.GetURL( |
| 3447 | "files/content-type-normalization.html"), &d, &default_context_); |
| 3448 | req.Start(); |
| 3449 | MessageLoop::current()->Run(); |
| 3450 | |
| 3451 | std::string mime_type; |
| 3452 | req.GetMimeType(&mime_type); |
| 3453 | EXPECT_EQ("text/html", mime_type); |
| 3454 | |
| 3455 | std::string charset; |
| 3456 | req.GetCharset(&charset); |
| 3457 | EXPECT_EQ("utf-8", charset); |
| 3458 | req.Cancel(); |
| 3459 | } |
| 3460 | |
| 3461 | TEST_F(URLRequestTestHTTP, RestrictRedirects) { |
| 3462 | ASSERT_TRUE(test_server_.Start()); |
| 3463 | |
| 3464 | TestDelegate d; |
| 3465 | URLRequest req(test_server_.GetURL( |
| 3466 | "files/redirect-to-file.html"), &d, &default_context_); |
| 3467 | req.Start(); |
| 3468 | MessageLoop::current()->Run(); |
| 3469 | |
| 3470 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 3471 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error()); |
| 3472 | } |
| 3473 | |
| 3474 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
| 3475 | ASSERT_TRUE(test_server_.Start()); |
| 3476 | |
| 3477 | TestDelegate d; |
| 3478 | URLRequest req(test_server_.GetURL( |
| 3479 | "files/redirect-to-invalid-url.html"), &d, &default_context_); |
| 3480 | req.Start(); |
| 3481 | MessageLoop::current()->Run(); |
| 3482 | |
| 3483 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 3484 | EXPECT_EQ(ERR_INVALID_URL, req.status().error()); |
| 3485 | } |
| 3486 | |
| 3487 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
| 3488 | ASSERT_TRUE(test_server_.Start()); |
| 3489 | |
| 3490 | TestDelegate d; |
| 3491 | URLRequest req( |
| 3492 | test_server_.GetURL("echoheader?Referer"), &d, &default_context_); |
| 3493 | req.set_referrer("http://user:[email protected]/"); |
| 3494 | req.Start(); |
| 3495 | MessageLoop::current()->Run(); |
| 3496 | |
| 3497 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 3498 | } |
| 3499 | |
| 3500 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
| 3501 | ASSERT_TRUE(test_server_.Start()); |
| 3502 | |
| 3503 | TestDelegate d; |
| 3504 | { |
| 3505 | d.set_cancel_in_received_redirect(true); |
| 3506 | URLRequest req( |
| 3507 | test_server_.GetURL("files/redirect-test.html"), &d, &default_context_); |
| 3508 | req.Start(); |
| 3509 | MessageLoop::current()->Run(); |
| 3510 | |
| 3511 | EXPECT_EQ(1, d.response_started_count()); |
| 3512 | EXPECT_EQ(0, d.bytes_received()); |
| 3513 | EXPECT_FALSE(d.received_data_before_response()); |
| 3514 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 3515 | } |
| 3516 | } |
| 3517 | |
| 3518 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
| 3519 | ASSERT_TRUE(test_server_.Start()); |
| 3520 | |
| 3521 | TestDelegate d; |
| 3522 | { |
| 3523 | d.set_quit_on_redirect(true); |
| 3524 | URLRequest req( |
| 3525 | test_server_.GetURL("files/redirect-test.html"), &d, &default_context_); |
| 3526 | req.Start(); |
| 3527 | MessageLoop::current()->Run(); |
| 3528 | |
| 3529 | EXPECT_EQ(1, d.received_redirect_count()); |
| 3530 | |
| 3531 | req.FollowDeferredRedirect(); |
| 3532 | MessageLoop::current()->Run(); |
| 3533 | |
| 3534 | EXPECT_EQ(1, d.response_started_count()); |
| 3535 | EXPECT_FALSE(d.received_data_before_response()); |
| 3536 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 3537 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 3538 | base::FilePath path; |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3539 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 3540 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 3541 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 3542 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 3543 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 3544 | |
| 3545 | std::string contents; |
| 3546 | EXPECT_TRUE(file_util::ReadFileToString(path, &contents)); |
| 3547 | EXPECT_EQ(contents, d.data_received()); |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
| 3552 | ASSERT_TRUE(test_server_.Start()); |
| 3553 | |
| 3554 | TestDelegate d; |
| 3555 | { |
| 3556 | d.set_quit_on_redirect(true); |
| 3557 | URLRequest req( |
| 3558 | test_server_.GetURL("files/redirect-test.html"), &d, &default_context_); |
| 3559 | req.Start(); |
| 3560 | MessageLoop::current()->Run(); |
| 3561 | |
| 3562 | EXPECT_EQ(1, d.received_redirect_count()); |
| 3563 | |
| 3564 | req.Cancel(); |
| 3565 | MessageLoop::current()->Run(); |
| 3566 | |
| 3567 | EXPECT_EQ(1, d.response_started_count()); |
| 3568 | EXPECT_EQ(0, d.bytes_received()); |
| 3569 | EXPECT_FALSE(d.received_data_before_response()); |
| 3570 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
| 3575 | ASSERT_TRUE(test_server_.Start()); |
| 3576 | |
| 3577 | // populate the cache |
| 3578 | { |
| 3579 | TestDelegate d; |
| 3580 | URLRequest req( |
| 3581 | test_server_.GetURL("echoheadercache?foo"), &d, &default_context_); |
| 3582 | HttpRequestHeaders headers; |
| 3583 | headers.SetHeader("foo", "1"); |
| 3584 | req.SetExtraRequestHeaders(headers); |
| 3585 | req.Start(); |
| 3586 | MessageLoop::current()->Run(); |
| 3587 | } |
| 3588 | |
| 3589 | // expect a cache hit |
| 3590 | { |
| 3591 | TestDelegate d; |
| 3592 | URLRequest req( |
| 3593 | test_server_.GetURL("echoheadercache?foo"), &d, &default_context_); |
| 3594 | HttpRequestHeaders headers; |
| 3595 | headers.SetHeader("foo", "1"); |
| 3596 | req.SetExtraRequestHeaders(headers); |
| 3597 | req.Start(); |
| 3598 | MessageLoop::current()->Run(); |
| 3599 | |
| 3600 | EXPECT_TRUE(req.was_cached()); |
| 3601 | } |
| 3602 | |
| 3603 | // expect a cache miss |
| 3604 | { |
| 3605 | TestDelegate d; |
| 3606 | URLRequest req( |
| 3607 | test_server_.GetURL("echoheadercache?foo"), &d, &default_context_); |
| 3608 | HttpRequestHeaders headers; |
| 3609 | headers.SetHeader("foo", "2"); |
| 3610 | req.SetExtraRequestHeaders(headers); |
| 3611 | req.Start(); |
| 3612 | MessageLoop::current()->Run(); |
| 3613 | |
| 3614 | EXPECT_FALSE(req.was_cached()); |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
| 3619 | ASSERT_TRUE(test_server_.Start()); |
| 3620 | |
| 3621 | // populate the cache |
| 3622 | { |
| 3623 | TestDelegate d; |
| 3624 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3625 | |
| 3626 | URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_); |
| 3627 | r.Start(); |
| 3628 | |
| 3629 | MessageLoop::current()->Run(); |
| 3630 | |
| 3631 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 3632 | } |
| 3633 | |
| 3634 | // repeat request with end-to-end validation. since auth-basic results in a |
| 3635 | // cachable page, we expect this test to result in a 304. in which case, the |
| 3636 | // response should be fetched from the cache. |
| 3637 | { |
| 3638 | TestDelegate d; |
| 3639 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3640 | |
| 3641 | URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_); |
| 3642 | r.set_load_flags(LOAD_VALIDATE_CACHE); |
| 3643 | r.Start(); |
| 3644 | |
| 3645 | MessageLoop::current()->Run(); |
| 3646 | |
| 3647 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 3648 | |
| 3649 | // Should be the same cached document. |
| 3650 | EXPECT_TRUE(r.was_cached()); |
| 3651 | } |
| 3652 | } |
| 3653 | |
| 3654 | // Check that Set-Cookie headers in 401 responses are respected. |
| 3655 | // http://crbug.com/6450 |
| 3656 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
| 3657 | ASSERT_TRUE(test_server_.Start()); |
| 3658 | |
| 3659 | GURL url_requiring_auth = |
| 3660 | test_server_.GetURL("auth-basic?set-cookie-if-challenged"); |
| 3661 | |
| 3662 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 3663 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 3664 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3665 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3666 | TestURLRequestContext context(true); |
| 3667 | context.set_network_delegate(&network_delegate); |
| 3668 | context.Init(); |
| 3669 | |
| 3670 | TestDelegate d; |
| 3671 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3672 | |
| 3673 | URLRequest r(url_requiring_auth, &d, &context); |
| 3674 | r.Start(); |
| 3675 | |
| 3676 | MessageLoop::current()->Run(); |
| 3677 | |
| 3678 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 3679 | |
| 3680 | // Make sure we sent the cookie in the restarted transaction. |
| 3681 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 3682 | != std::string::npos); |
| 3683 | } |
| 3684 | |
| 3685 | // Same test as above, except this time the restart is initiated earlier |
| 3686 | // (without user intervention since identity is embedded in the URL). |
| 3687 | { |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3688 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3689 | TestURLRequestContext context(true); |
| 3690 | context.set_network_delegate(&network_delegate); |
| 3691 | context.Init(); |
| 3692 | |
| 3693 | TestDelegate d; |
| 3694 | |
| 3695 | GURL::Replacements replacements; |
| 3696 | std::string username("user2"); |
| 3697 | std::string password("secret"); |
| 3698 | replacements.SetUsernameStr(username); |
| 3699 | replacements.SetPasswordStr(password); |
| 3700 | GURL url_with_identity = url_requiring_auth.ReplaceComponents(replacements); |
| 3701 | |
| 3702 | URLRequest r(url_with_identity, &d, &context); |
| 3703 | r.Start(); |
| 3704 | |
| 3705 | MessageLoop::current()->Run(); |
| 3706 | |
| 3707 | EXPECT_TRUE(d.data_received().find("user2/secret") != std::string::npos); |
| 3708 | |
| 3709 | // Make sure we sent the cookie in the restarted transaction. |
| 3710 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 3711 | != std::string::npos); |
| 3712 | } |
| 3713 | } |
| 3714 | |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3715 | // Tests that load timing works as expected with auth and the cache. |
| 3716 | TEST_F(URLRequestTestHTTP, BasicAuthLoadTiming) { |
| 3717 | ASSERT_TRUE(test_server_.Start()); |
| 3718 | |
| 3719 | // populate the cache |
| 3720 | { |
| 3721 | TestDelegate d; |
| 3722 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3723 | |
| 3724 | URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_); |
| 3725 | r.Start(); |
| 3726 | |
| 3727 | MessageLoop::current()->Run(); |
| 3728 | |
| 3729 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 3730 | |
| 3731 | LoadTimingInfo load_timing_info_before_auth; |
| 3732 | EXPECT_TRUE(default_network_delegate_.GetLoadTimingInfoBeforeAuth( |
| 3733 | &load_timing_info_before_auth)); |
| 3734 | TestLoadTimingNotReused(load_timing_info_before_auth, |
| 3735 | CONNECT_TIMING_HAS_DNS_TIMES); |
| 3736 | |
| 3737 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 3738 | r.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3739 | // The test server does not support keep alive sockets, so the second |
| 3740 | // request with auth should use a new socket. |
| 3741 | TestLoadTimingNotReused(load_timing_info, CONNECT_TIMING_HAS_DNS_TIMES); |
| 3742 | EXPECT_NE(load_timing_info_before_auth.socket_log_id, |
| 3743 | load_timing_info.socket_log_id); |
| 3744 | EXPECT_LE(load_timing_info_before_auth.receive_headers_end, |
| 3745 | load_timing_info.connect_timing.connect_start); |
| 3746 | } |
| 3747 | |
| 3748 | // repeat request with end-to-end validation. since auth-basic results in a |
| 3749 | // cachable page, we expect this test to result in a 304. in which case, the |
| 3750 | // response should be fetched from the cache. |
| 3751 | { |
| 3752 | TestDelegate d; |
| 3753 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
| 3754 | |
| 3755 | URLRequest r(test_server_.GetURL("auth-basic"), &d, &default_context_); |
| 3756 | r.set_load_flags(LOAD_VALIDATE_CACHE); |
| 3757 | r.Start(); |
| 3758 | |
| 3759 | MessageLoop::current()->Run(); |
| 3760 | |
| 3761 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 3762 | |
| 3763 | // Should be the same cached document. |
| 3764 | EXPECT_TRUE(r.was_cached()); |
| 3765 | |
| 3766 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 3767 | r.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 3768 | TestLoadTimingNoHttpConnection(load_timing_info); |
| 3769 | } |
| 3770 | } |
| 3771 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3772 | // In this test, we do a POST which the server will 302 redirect. |
| 3773 | // The subsequent transaction should use GET, and should not send the |
| 3774 | // Content-Type header. |
| 3775 | // http://code.google.com/p/chromium/issues/detail?id=843 |
| 3776 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
| 3777 | ASSERT_TRUE(test_server_.Start()); |
| 3778 | |
| 3779 | const char kData[] = "hello world"; |
| 3780 | |
| 3781 | TestDelegate d; |
| 3782 | URLRequest req( |
| 3783 | test_server_.GetURL("files/redirect-to-echoall"), &d, &default_context_); |
| 3784 | req.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 3785 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3786 | |
| 3787 | // Set headers (some of which are specific to the POST). |
| 3788 | HttpRequestHeaders headers; |
| 3789 | headers.AddHeadersFromString( |
| 3790 | "Content-Type: multipart/form-data; " |
| 3791 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 3792 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 3793 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 3794 | "Accept-Language: en-US,en\r\n" |
| 3795 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 3796 | "Content-Length: 11\r\n" |
| 3797 | "Origin: http://localhost:1337/"); |
| 3798 | req.SetExtraRequestHeaders(headers); |
| 3799 | req.Start(); |
| 3800 | MessageLoop::current()->Run(); |
| 3801 | |
| 3802 | std::string mime_type; |
| 3803 | req.GetMimeType(&mime_type); |
| 3804 | EXPECT_EQ("text/html", mime_type); |
| 3805 | |
| 3806 | const std::string& data = d.data_received(); |
| 3807 | |
| 3808 | // Check that the post-specific headers were stripped: |
| 3809 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 3810 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 3811 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 3812 | |
| 3813 | // These extra request headers should not have been stripped. |
| 3814 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 3815 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 3816 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 3817 | } |
| 3818 | |
| 3819 | // The following tests check that we handle mutating the request method for |
| 3820 | // HTTP redirects as expected. |
| 3821 | // See http://crbug.com/56373 and http://crbug.com/102130. |
| 3822 | |
| 3823 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
| 3824 | ASSERT_TRUE(test_server_.Start()); |
| 3825 | |
| 3826 | const GURL url = test_server_.GetURL("files/redirect301-to-echo"); |
| 3827 | |
| 3828 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 3829 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 3830 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 3831 | } |
| 3832 | |
| 3833 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
| 3834 | ASSERT_TRUE(test_server_.Start()); |
| 3835 | |
| 3836 | const GURL url = test_server_.GetURL("files/redirect302-to-echo"); |
| 3837 | |
| 3838 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 3839 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 3840 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 3841 | } |
| 3842 | |
| 3843 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
| 3844 | ASSERT_TRUE(test_server_.Start()); |
| 3845 | |
| 3846 | const GURL url = test_server_.GetURL("files/redirect303-to-echo"); |
| 3847 | |
| 3848 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 3849 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
| 3850 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 3851 | } |
| 3852 | |
| 3853 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
| 3854 | ASSERT_TRUE(test_server_.Start()); |
| 3855 | |
| 3856 | const GURL url = test_server_.GetURL("files/redirect307-to-echo"); |
| 3857 | |
| 3858 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 3859 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
| 3860 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
| 3861 | } |
| 3862 | |
| 3863 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
| 3864 | ASSERT_TRUE(test_server_.Start()); |
| 3865 | |
| 3866 | const char kData[] = "hello world"; |
| 3867 | |
| 3868 | TestDelegate d; |
| 3869 | URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); |
| 3870 | req.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 3871 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3872 | HttpRequestHeaders headers; |
| 3873 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3874 | base::UintToString(arraysize(kData) - 1)); |
| 3875 | req.SetExtraRequestHeaders(headers); |
| 3876 | |
| 3877 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 3878 | &req, default_context_.network_delegate(), test_server_.GetURL("echo"), |
| 3879 | URLRequestRedirectJob::REDIRECT_302_FOUND); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3880 | AddTestInterceptor()->set_main_intercept_job(job); |
| 3881 | |
| 3882 | req.Start(); |
| 3883 | MessageLoop::current()->Run(); |
| 3884 | EXPECT_EQ("GET", req.method()); |
| 3885 | } |
| 3886 | |
| 3887 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 3888 | ASSERT_TRUE(test_server_.Start()); |
| 3889 | |
| 3890 | const char kData[] = "hello world"; |
| 3891 | |
| 3892 | TestDelegate d; |
| 3893 | URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); |
| 3894 | req.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 3895 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3896 | HttpRequestHeaders headers; |
| 3897 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3898 | base::UintToString(arraysize(kData) - 1)); |
| 3899 | req.SetExtraRequestHeaders(headers); |
| 3900 | |
| 3901 | URLRequestRedirectJob* job = new URLRequestRedirectJob( |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 3902 | &req, default_context_.network_delegate(), test_server_.GetURL("echo"), |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3903 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); |
| 3904 | AddTestInterceptor()->set_main_intercept_job(job); |
| 3905 | |
| 3906 | req.Start(); |
| 3907 | MessageLoop::current()->Run(); |
| 3908 | EXPECT_EQ("POST", req.method()); |
| 3909 | EXPECT_EQ(kData, d.data_received()); |
| 3910 | } |
| 3911 | |
| 3912 | // Check that default A-L header is sent. |
| 3913 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
| 3914 | ASSERT_TRUE(test_server_.Start()); |
| 3915 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 3916 | StaticHttpUserAgentSettings settings("en", EmptyString(), EmptyString()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3917 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3918 | TestURLRequestContext context(true); |
| 3919 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 3920 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3921 | context.Init(); |
| 3922 | |
| 3923 | TestDelegate d; |
| 3924 | URLRequest req( |
| 3925 | test_server_.GetURL("echoheader?Accept-Language"), &d, &context); |
| 3926 | req.Start(); |
| 3927 | MessageLoop::current()->Run(); |
| 3928 | EXPECT_EQ("en", d.data_received()); |
| 3929 | } |
| 3930 | |
| 3931 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 3932 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
| 3933 | ASSERT_TRUE(test_server_.Start()); |
| 3934 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 3935 | StaticHttpUserAgentSettings settings( |
| 3936 | EmptyString(), EmptyString(), EmptyString()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 3937 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3938 | TestURLRequestContext context(true); |
| 3939 | context.set_network_delegate(&network_delegate); |
| 3940 | context.Init(); |
| 3941 | // We override the language after initialization because empty entries |
| 3942 | // get overridden by Init(). |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 3943 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 3944 | |
| 3945 | TestDelegate d; |
| 3946 | URLRequest req( |
| 3947 | test_server_.GetURL("echoheader?Accept-Language"), &d, &context); |
| 3948 | req.Start(); |
| 3949 | MessageLoop::current()->Run(); |
| 3950 | EXPECT_EQ("None", d.data_received()); |
| 3951 | } |
| 3952 | |
| 3953 | // Check that if request overrides the A-L header, the default is not appended. |
| 3954 | // See http://crbug.com/20894 |
| 3955 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
| 3956 | ASSERT_TRUE(test_server_.Start()); |
| 3957 | |
| 3958 | TestDelegate d; |
| 3959 | URLRequest req(test_server_.GetURL("echoheader?Accept-Language"), |
| 3960 | &d, |
| 3961 | &default_context_); |
| 3962 | HttpRequestHeaders headers; |
| 3963 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
| 3964 | req.SetExtraRequestHeaders(headers); |
| 3965 | req.Start(); |
| 3966 | MessageLoop::current()->Run(); |
| 3967 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 3968 | } |
| 3969 | |
| 3970 | // Check that default A-E header is sent. |
| 3971 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
| 3972 | ASSERT_TRUE(test_server_.Start()); |
| 3973 | |
| 3974 | TestDelegate d; |
| 3975 | URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), |
| 3976 | &d, |
| 3977 | &default_context_); |
| 3978 | HttpRequestHeaders headers; |
| 3979 | req.SetExtraRequestHeaders(headers); |
| 3980 | req.Start(); |
| 3981 | MessageLoop::current()->Run(); |
| 3982 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 3983 | } |
| 3984 | |
| 3985 | // Check that if request overrides the A-E header, the default is not appended. |
| 3986 | // See http://crbug.com/47381 |
| 3987 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
| 3988 | ASSERT_TRUE(test_server_.Start()); |
| 3989 | |
| 3990 | TestDelegate d; |
| 3991 | URLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), |
| 3992 | &d, |
| 3993 | &default_context_); |
| 3994 | HttpRequestHeaders headers; |
| 3995 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
| 3996 | req.SetExtraRequestHeaders(headers); |
| 3997 | req.Start(); |
| 3998 | MessageLoop::current()->Run(); |
| 3999 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 4000 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 4001 | } |
| 4002 | |
| 4003 | // Check that default A-C header is sent. |
| 4004 | TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) { |
| 4005 | ASSERT_TRUE(test_server_.Start()); |
| 4006 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 4007 | StaticHttpUserAgentSettings settings(EmptyString(), "en", EmptyString()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4008 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4009 | TestURLRequestContext context(true); |
| 4010 | context.set_network_delegate(&network_delegate); |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 4011 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4012 | context.Init(); |
| 4013 | |
| 4014 | TestDelegate d; |
| 4015 | URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), |
| 4016 | &d, |
| 4017 | &context); |
| 4018 | req.Start(); |
| 4019 | MessageLoop::current()->Run(); |
| 4020 | EXPECT_EQ("en", d.data_received()); |
| 4021 | } |
| 4022 | |
| 4023 | // Check that an empty A-C header is not sent. http://crbug.com/77365. |
| 4024 | TEST_F(URLRequestTestHTTP, EmptyAcceptCharset) { |
| 4025 | ASSERT_TRUE(test_server_.Start()); |
| 4026 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 4027 | StaticHttpUserAgentSettings settings( |
| 4028 | EmptyString(), EmptyString(), EmptyString()); |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4029 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4030 | TestURLRequestContext context(true); |
| 4031 | context.set_network_delegate(&network_delegate); |
| 4032 | context.Init(); |
| 4033 | // We override the accepted charset after initialization because empty |
| 4034 | // entries get overridden otherwise. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 4035 | context.set_http_user_agent_settings(&settings); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4036 | |
| 4037 | TestDelegate d; |
| 4038 | URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), |
| 4039 | &d, |
| 4040 | &context); |
| 4041 | req.Start(); |
| 4042 | MessageLoop::current()->Run(); |
| 4043 | EXPECT_EQ("None", d.data_received()); |
| 4044 | } |
| 4045 | |
| 4046 | // Check that if request overrides the A-C header, the default is not appended. |
| 4047 | // See http://crbug.com/20894 |
| 4048 | TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) { |
| 4049 | ASSERT_TRUE(test_server_.Start()); |
| 4050 | |
| 4051 | TestDelegate d; |
| 4052 | URLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), |
| 4053 | &d, |
| 4054 | &default_context_); |
| 4055 | HttpRequestHeaders headers; |
| 4056 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
| 4057 | req.SetExtraRequestHeaders(headers); |
| 4058 | req.Start(); |
| 4059 | MessageLoop::current()->Run(); |
| 4060 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 4061 | } |
| 4062 | |
| 4063 | // Check that default User-Agent header is sent. |
| 4064 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
| 4065 | ASSERT_TRUE(test_server_.Start()); |
| 4066 | |
| 4067 | TestDelegate d; |
| 4068 | URLRequest req(test_server_.GetURL("echoheader?User-Agent"), |
| 4069 | &d, |
| 4070 | &default_context_); |
| 4071 | req.Start(); |
| 4072 | MessageLoop::current()->Run(); |
| 4073 | EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received()); |
| 4074 | } |
| 4075 | |
| 4076 | // Check that if request overrides the User-Agent header, |
| 4077 | // the default is not appended. |
| 4078 | TEST_F(URLRequestTestHTTP, OverrideUserAgent) { |
| 4079 | ASSERT_TRUE(test_server_.Start()); |
| 4080 | |
| 4081 | TestDelegate d; |
| 4082 | URLRequest req(test_server_.GetURL("echoheader?User-Agent"), |
| 4083 | &d, |
| 4084 | &default_context_); |
| 4085 | HttpRequestHeaders headers; |
| 4086 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
| 4087 | req.SetExtraRequestHeaders(headers); |
| 4088 | req.Start(); |
| 4089 | MessageLoop::current()->Run(); |
| 4090 | // If the net tests are being run with ChromeFrame then we need to allow for |
| 4091 | // the 'chromeframe' suffix which is added to the user agent before the |
| 4092 | // closing parentheses. |
| 4093 | EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); |
| 4094 | } |
| 4095 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 4096 | // Check that a NULL HttpUserAgentSettings causes the corresponding empty |
| 4097 | // User-Agent header to be sent but does not send the Accept-Language and |
| 4098 | // Accept-Charset headers. |
| 4099 | TEST_F(URLRequestTestHTTP, EmptyHttpUserAgentSettings) { |
| 4100 | ASSERT_TRUE(test_server_.Start()); |
| 4101 | |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4102 | TestNetworkDelegate network_delegate; // Must outlive URLRequests. |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 4103 | TestURLRequestContext context(true); |
| 4104 | context.set_network_delegate(&network_delegate); |
| 4105 | context.Init(); |
| 4106 | // We override the HttpUserAgentSettings after initialization because empty |
| 4107 | // entries get overridden by Init(). |
| 4108 | context.set_http_user_agent_settings(NULL); |
| 4109 | |
| 4110 | struct { |
| 4111 | const char* request; |
| 4112 | const char* expected_response; |
| 4113 | } tests[] = { { "echoheader?Accept-Language", "None" }, |
| 4114 | { "echoheader?Accept-Charset", "None" }, |
| 4115 | { "echoheader?User-Agent", "" } }; |
| 4116 | |
| 4117 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); i++) { |
| 4118 | TestDelegate d; |
| 4119 | URLRequest req(test_server_.GetURL(tests[i].request), &d, &context); |
| 4120 | req.Start(); |
| 4121 | MessageLoop::current()->Run(); |
| 4122 | EXPECT_EQ(tests[i].expected_response, d.data_received()) |
| 4123 | << " Request = \"" << tests[i].request << "\""; |
| 4124 | } |
| 4125 | } |
| 4126 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 4127 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4128 | public: |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4129 | HTTPSRequestTest() : default_context_(true) { |
| 4130 | default_context_.set_network_delegate(&default_network_delegate_); |
| 4131 | default_context_.Init(); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4132 | } |
| 4133 | virtual ~HTTPSRequestTest() {} |
| 4134 | |
| 4135 | protected: |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4136 | TestNetworkDelegate default_network_delegate_; // Must outlive URLRequest. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4137 | TestURLRequestContext default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 4138 | }; |
| 4139 | |
[email protected] | c044616e | 2013-02-20 02:01:26 | [diff] [blame^] | 4140 | TEST_F(HTTPSRequestTest, HTTPSGetTest) { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 4141 | TestServer test_server(TestServer::TYPE_HTTPS, |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 4142 | TestServer::kLocalhost, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4143 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4144 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 4145 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 4146 | TestDelegate d; |
| 4147 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 4148 | URLRequest r(test_server.GetURL(""), &d, &default_context_); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 4149 | r.Start(); |
| 4150 | EXPECT_TRUE(r.is_pending()); |
| 4151 | |
| 4152 | MessageLoop::current()->Run(); |
| 4153 | |
| 4154 | EXPECT_EQ(1, d.response_started_count()); |
| 4155 | EXPECT_FALSE(d.received_data_before_response()); |
| 4156 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 4157 | CheckSSLInfo(r.ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 4158 | EXPECT_EQ(test_server.host_port_pair().host(), |
| 4159 | r.GetSocketAddress().host()); |
| 4160 | EXPECT_EQ(test_server.host_port_pair().port(), |
| 4161 | r.GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 4162 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 4163 | } |
| 4164 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 4165 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4166 | TestServer::SSLOptions ssl_options( |
| 4167 | TestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 4168 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4169 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4170 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4171 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4172 | |
| 4173 | bool err_allowed = true; |
| 4174 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 4175 | TestDelegate d; |
| 4176 | { |
| 4177 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 4178 | URLRequest r(test_server.GetURL(""), &d, &default_context_); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4179 | |
| 4180 | r.Start(); |
| 4181 | EXPECT_TRUE(r.is_pending()); |
| 4182 | |
| 4183 | MessageLoop::current()->Run(); |
| 4184 | |
| 4185 | EXPECT_EQ(1, d.response_started_count()); |
| 4186 | EXPECT_FALSE(d.received_data_before_response()); |
| 4187 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 4188 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4189 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 4190 | CheckSSLInfo(r.ssl_info()); |
| 4191 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4192 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 4193 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4194 | } |
| 4195 | } |
| 4196 | } |
| 4197 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 4198 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4199 | TestServer::SSLOptions ssl_options( |
| 4200 | TestServer::SSLOptions::CERT_EXPIRED); |
| 4201 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4202 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4203 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4204 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4205 | |
| 4206 | // Iterate from false to true, just so that we do the opposite of the |
| 4207 | // previous test in order to increase test coverage. |
| 4208 | bool err_allowed = false; |
| 4209 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 4210 | TestDelegate d; |
| 4211 | { |
| 4212 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 4213 | URLRequest r(test_server.GetURL(""), &d, &default_context_); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4214 | |
| 4215 | r.Start(); |
| 4216 | EXPECT_TRUE(r.is_pending()); |
| 4217 | |
| 4218 | MessageLoop::current()->Run(); |
| 4219 | |
| 4220 | EXPECT_EQ(1, d.response_started_count()); |
| 4221 | EXPECT_FALSE(d.received_data_before_response()); |
| 4222 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 4223 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4224 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 4225 | CheckSSLInfo(r.ssl_info()); |
| 4226 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4227 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 4228 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 4229 | } |
| 4230 | } |
| 4231 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 4232 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4233 | // Tests TLSv1.1 -> TLSv1 fallback. Verifies that we don't fall back more |
| 4234 | // than necessary. |
| 4235 | TEST_F(HTTPSRequestTest, TLSv1Fallback) { |
| 4236 | uint16 default_version_max = SSLConfigService::default_version_max(); |
| 4237 | // The OpenSSL library in use may not support TLS 1.1. |
| 4238 | #if !defined(USE_OPENSSL) |
| 4239 | EXPECT_GT(default_version_max, SSL_PROTOCOL_VERSION_TLS1); |
| 4240 | #endif |
| 4241 | if (default_version_max <= SSL_PROTOCOL_VERSION_TLS1) |
| 4242 | return; |
| 4243 | |
| 4244 | TestServer::SSLOptions ssl_options( |
| 4245 | TestServer::SSLOptions::CERT_OK); |
| 4246 | ssl_options.tls_intolerant = |
| 4247 | TestServer::SSLOptions::TLS_INTOLERANT_TLS1_1; |
| 4248 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4249 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4250 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4251 | ASSERT_TRUE(test_server.Start()); |
| 4252 | |
| 4253 | TestDelegate d; |
| 4254 | TestURLRequestContext context(true); |
| 4255 | context.Init(); |
| 4256 | d.set_allow_certificate_errors(true); |
| 4257 | URLRequest r(test_server.GetURL(""), &d, &context); |
| 4258 | r.Start(); |
| 4259 | |
| 4260 | MessageLoop::current()->Run(); |
| 4261 | |
| 4262 | EXPECT_EQ(1, d.response_started_count()); |
| 4263 | EXPECT_NE(0, d.bytes_received()); |
| 4264 | EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_TLS1), |
| 4265 | SSLConnectionStatusToVersion(r.ssl_info().connection_status)); |
| 4266 | EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK); |
| 4267 | } |
| 4268 | |
| 4269 | // This tests that a load of www.google.com with a certificate error sets |
| 4270 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 4271 | // the interstitial to be fatal. |
| 4272 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
| 4273 | TestServer::SSLOptions ssl_options( |
| 4274 | TestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 4275 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4276 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4277 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4278 | ASSERT_TRUE(test_server.Start()); |
| 4279 | |
| 4280 | // We require that the URL be www.google.com in order to pick up the |
| 4281 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 4282 | // 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] | 4283 | // testserver. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4284 | |
| 4285 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4286 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4287 | TestURLRequestContext context(true); |
| 4288 | context.set_network_delegate(&network_delegate); |
| 4289 | context.set_host_resolver(&host_resolver); |
| 4290 | TransportSecurityState transport_security_state; |
| 4291 | context.set_transport_security_state(&transport_security_state); |
| 4292 | context.Init(); |
| 4293 | |
| 4294 | TestDelegate d; |
| 4295 | URLRequest r(GURL(StringPrintf("https://www.google.com:%d", |
| 4296 | test_server.host_port_pair().port())), |
| 4297 | &d, |
| 4298 | &context); |
| 4299 | |
| 4300 | r.Start(); |
| 4301 | EXPECT_TRUE(r.is_pending()); |
| 4302 | |
| 4303 | MessageLoop::current()->Run(); |
| 4304 | |
| 4305 | EXPECT_EQ(1, d.response_started_count()); |
| 4306 | EXPECT_FALSE(d.received_data_before_response()); |
| 4307 | EXPECT_TRUE(d.have_certificate_errors()); |
| 4308 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 4309 | } |
| 4310 | |
| 4311 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 4312 | // TransportSecurityState. |
| 4313 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
| 4314 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 4315 | // matter. It just has to be any error. |
| 4316 | TestServer::SSLOptions ssl_options( |
| 4317 | TestServer::SSLOptions::CERT_MISMATCHED_NAME); |
| 4318 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4319 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4320 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4321 | ASSERT_TRUE(test_server.Start()); |
| 4322 | |
| 4323 | // We require that the URL be www.google.com in order to pick up the |
| 4324 | // preloaded and dynamic HSTS and public key pin entries in the |
| 4325 | // TransportSecurityState. This means that we have to use a |
| 4326 | // MockHostResolver in order to direct www.google.com to the testserver. |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4327 | // By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4328 | |
| 4329 | MockHostResolver host_resolver; |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4330 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4331 | TestURLRequestContext context(true); |
| 4332 | context.set_network_delegate(&network_delegate); |
| 4333 | context.set_host_resolver(&host_resolver); |
| 4334 | TransportSecurityState transport_security_state; |
| 4335 | TransportSecurityState::DomainState domain_state; |
| 4336 | EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true, |
| 4337 | &domain_state)); |
| 4338 | context.set_transport_security_state(&transport_security_state); |
| 4339 | context.Init(); |
| 4340 | |
| 4341 | TestDelegate d; |
| 4342 | URLRequest r(GURL(StringPrintf("https://www.google.com:%d", |
| 4343 | test_server.host_port_pair().port())), |
| 4344 | &d, |
| 4345 | &context); |
| 4346 | |
| 4347 | r.Start(); |
| 4348 | EXPECT_TRUE(r.is_pending()); |
| 4349 | |
| 4350 | MessageLoop::current()->Run(); |
| 4351 | |
| 4352 | EXPECT_EQ(1, d.response_started_count()); |
| 4353 | EXPECT_FALSE(d.received_data_before_response()); |
| 4354 | EXPECT_TRUE(d.have_certificate_errors()); |
| 4355 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 4356 | |
| 4357 | // Get a fresh copy of the state, and check that it hasn't been updated. |
| 4358 | TransportSecurityState::DomainState new_domain_state; |
| 4359 | EXPECT_TRUE(transport_security_state.GetDomainState("www.google.com", true, |
| 4360 | &new_domain_state)); |
| 4361 | EXPECT_EQ(new_domain_state.upgrade_mode, domain_state.upgrade_mode); |
| 4362 | EXPECT_EQ(new_domain_state.include_subdomains, |
| 4363 | domain_state.include_subdomains); |
| 4364 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.static_spki_hashes, |
| 4365 | domain_state.static_spki_hashes)); |
| 4366 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes, |
| 4367 | domain_state.dynamic_spki_hashes)); |
| 4368 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_static_spki_hashes, |
| 4369 | domain_state.bad_static_spki_hashes)); |
| 4370 | } |
| 4371 | |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 4372 | // Make sure HSTS preserves a POST request's method and body. |
| 4373 | TEST_F(HTTPSRequestTest, HSTSPreservesPosts) { |
| 4374 | static const char kData[] = "hello world"; |
| 4375 | |
| 4376 | TestServer::SSLOptions ssl_options(TestServer::SSLOptions::CERT_OK); |
| 4377 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4378 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4379 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 4380 | ASSERT_TRUE(test_server.Start()); |
| 4381 | |
| 4382 | |
| 4383 | // Per spec, TransportSecurityState expects a domain name, rather than an IP |
| 4384 | // address, so a MockHostResolver is needed to redirect www.somewhere.com to |
[email protected] | ceefd7fd | 2012-11-29 00:36:24 | [diff] [blame] | 4385 | // the TestServer. By default, MockHostResolver maps all hosts to 127.0.0.1. |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 4386 | MockHostResolver host_resolver; |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 4387 | |
| 4388 | // Force https for www.somewhere.com. |
| 4389 | TransportSecurityState transport_security_state; |
| 4390 | net::TransportSecurityState::DomainState domain_state; |
| 4391 | domain_state.upgrade_expiry = |
| 4392 | domain_state.created + base::TimeDelta::FromDays(1000); |
| 4393 | transport_security_state.EnableHost("www.somewhere.com", domain_state); |
| 4394 | |
| 4395 | TestNetworkDelegate network_delegate; // Must outlive URLRequest. |
| 4396 | |
| 4397 | TestURLRequestContext context(true); |
| 4398 | context.set_host_resolver(&host_resolver); |
| 4399 | context.set_transport_security_state(&transport_security_state); |
| 4400 | context.set_network_delegate(&network_delegate); |
| 4401 | context.Init(); |
| 4402 | |
| 4403 | TestDelegate d; |
| 4404 | // Navigating to https://www.somewhere.com instead of https://127.0.0.1 will |
| 4405 | // cause a certificate error. Ignore the error. |
| 4406 | d.set_allow_certificate_errors(true); |
| 4407 | |
| 4408 | URLRequest req(GURL(StringPrintf("http://www.somewhere.com:%d/echo", |
| 4409 | test_server.host_port_pair().port())), |
| 4410 | &d, |
| 4411 | &context); |
| 4412 | req.set_method("POST"); |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 4413 | req.set_upload(make_scoped_ptr(CreateSimpleUploadData(kData))); |
[email protected] | 8ccc69f | 2012-11-28 19:52:14 | [diff] [blame] | 4414 | |
| 4415 | req.Start(); |
| 4416 | MessageLoop::current()->Run(); |
| 4417 | |
| 4418 | EXPECT_EQ("https", req.url().scheme()); |
| 4419 | EXPECT_EQ("POST", req.method()); |
| 4420 | EXPECT_EQ(kData, d.data_received()); |
| 4421 | } |
| 4422 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4423 | TEST_F(HTTPSRequestTest, SSLv3Fallback) { |
| 4424 | TestServer::SSLOptions ssl_options( |
| 4425 | TestServer::SSLOptions::CERT_OK); |
| 4426 | ssl_options.tls_intolerant = TestServer::SSLOptions::TLS_INTOLERANT_ALL; |
| 4427 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4428 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4429 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4430 | ASSERT_TRUE(test_server.Start()); |
| 4431 | |
| 4432 | TestDelegate d; |
| 4433 | TestURLRequestContext context(true); |
| 4434 | context.Init(); |
| 4435 | d.set_allow_certificate_errors(true); |
| 4436 | URLRequest r(test_server.GetURL(""), &d, &context); |
| 4437 | r.Start(); |
| 4438 | |
| 4439 | MessageLoop::current()->Run(); |
| 4440 | |
| 4441 | EXPECT_EQ(1, d.response_started_count()); |
| 4442 | EXPECT_NE(0, d.bytes_received()); |
| 4443 | EXPECT_EQ(static_cast<int>(SSL_CONNECTION_VERSION_SSL3), |
| 4444 | SSLConnectionStatusToVersion(r.ssl_info().connection_status)); |
| 4445 | EXPECT_TRUE(r.ssl_info().connection_status & SSL_CONNECTION_VERSION_FALLBACK); |
| 4446 | } |
| 4447 | |
| 4448 | namespace { |
| 4449 | |
| 4450 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 4451 | public: |
| 4452 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 4453 | } |
| 4454 | virtual void OnCertificateRequested( |
| 4455 | URLRequest* request, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 4456 | SSLCertRequestInfo* cert_request_info) OVERRIDE { |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4457 | on_certificate_requested_count_++; |
| 4458 | MessageLoop::current()->Quit(); |
| 4459 | } |
| 4460 | int on_certificate_requested_count() { |
| 4461 | return on_certificate_requested_count_; |
| 4462 | } |
| 4463 | private: |
| 4464 | int on_certificate_requested_count_; |
| 4465 | }; |
| 4466 | |
| 4467 | } // namespace |
| 4468 | |
| 4469 | // TODO(davidben): Test the rest of the code. Specifically, |
| 4470 | // - Filtering which certificates to select. |
| 4471 | // - Sending a certificate back. |
| 4472 | // - Getting a certificate request in an SSL renegotiation sending the |
| 4473 | // HTTP request. |
| 4474 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
| 4475 | TestServer::SSLOptions ssl_options; |
| 4476 | ssl_options.request_client_certificate = true; |
| 4477 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4478 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4479 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4480 | ASSERT_TRUE(test_server.Start()); |
| 4481 | |
| 4482 | SSLClientAuthTestDelegate d; |
| 4483 | { |
| 4484 | URLRequest r(test_server.GetURL(""), &d, &default_context_); |
| 4485 | |
| 4486 | r.Start(); |
| 4487 | EXPECT_TRUE(r.is_pending()); |
| 4488 | |
| 4489 | MessageLoop::current()->Run(); |
| 4490 | |
| 4491 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 4492 | EXPECT_FALSE(d.received_data_before_response()); |
| 4493 | EXPECT_EQ(0, d.bytes_received()); |
| 4494 | |
| 4495 | // Send no certificate. |
| 4496 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 4497 | // all platforms so we can test sending a cert as well. |
| 4498 | r.ContinueWithCertificate(NULL); |
| 4499 | |
| 4500 | MessageLoop::current()->Run(); |
| 4501 | |
| 4502 | EXPECT_EQ(1, d.response_started_count()); |
| 4503 | EXPECT_FALSE(d.received_data_before_response()); |
| 4504 | EXPECT_NE(0, d.bytes_received()); |
| 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 4509 | // Test that we attempt a session resume when making two connections to the |
| 4510 | // same host. |
| 4511 | TestServer::SSLOptions ssl_options; |
| 4512 | ssl_options.record_resume = true; |
| 4513 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4514 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4515 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4516 | ASSERT_TRUE(test_server.Start()); |
| 4517 | |
| 4518 | SSLClientSocket::ClearSessionCache(); |
| 4519 | |
| 4520 | { |
| 4521 | TestDelegate d; |
| 4522 | URLRequest r( |
| 4523 | test_server.GetURL("ssl-session-cache"), &d, &default_context_); |
| 4524 | |
| 4525 | r.Start(); |
| 4526 | EXPECT_TRUE(r.is_pending()); |
| 4527 | |
| 4528 | MessageLoop::current()->Run(); |
| 4529 | |
| 4530 | EXPECT_EQ(1, d.response_started_count()); |
| 4531 | } |
| 4532 | |
| 4533 | reinterpret_cast<HttpCache*>(default_context_.http_transaction_factory())-> |
| 4534 | CloseAllConnections(); |
| 4535 | |
| 4536 | { |
| 4537 | TestDelegate d; |
| 4538 | URLRequest r( |
| 4539 | test_server.GetURL("ssl-session-cache"), &d, &default_context_); |
| 4540 | |
| 4541 | r.Start(); |
| 4542 | EXPECT_TRUE(r.is_pending()); |
| 4543 | |
| 4544 | MessageLoop::current()->Run(); |
| 4545 | |
| 4546 | // The response will look like; |
| 4547 | // insert abc |
| 4548 | // lookup abc |
| 4549 | // insert xyz |
| 4550 | // |
| 4551 | // With a newline at the end which makes the split think that there are |
| 4552 | // four lines. |
| 4553 | |
| 4554 | EXPECT_EQ(1, d.response_started_count()); |
| 4555 | std::vector<std::string> lines; |
| 4556 | base::SplitString(d.data_received(), '\n', &lines); |
| 4557 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 4558 | |
| 4559 | std::string session_id; |
| 4560 | |
| 4561 | for (size_t i = 0; i < 2; i++) { |
| 4562 | std::vector<std::string> parts; |
| 4563 | base::SplitString(lines[i], '\t', &parts); |
| 4564 | ASSERT_EQ(2u, parts.size()); |
| 4565 | if (i == 0) { |
| 4566 | EXPECT_EQ("insert", parts[0]); |
| 4567 | session_id = parts[1]; |
| 4568 | } else { |
| 4569 | EXPECT_EQ("lookup", parts[0]); |
| 4570 | EXPECT_EQ(session_id, parts[1]); |
| 4571 | } |
| 4572 | } |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 4577 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 4578 | // differs. |
| 4579 | TestServer::SSLOptions ssl_options; |
| 4580 | ssl_options.record_resume = true; |
| 4581 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4582 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4583 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 4584 | ASSERT_TRUE(test_server.Start()); |
| 4585 | |
| 4586 | SSLClientSocket::ClearSessionCache(); |
| 4587 | |
| 4588 | { |
| 4589 | TestDelegate d; |
| 4590 | URLRequest r( |
| 4591 | test_server.GetURL("ssl-session-cache"), &d, &default_context_); |
| 4592 | |
| 4593 | r.Start(); |
| 4594 | EXPECT_TRUE(r.is_pending()); |
| 4595 | |
| 4596 | MessageLoop::current()->Run(); |
| 4597 | |
| 4598 | EXPECT_EQ(1, d.response_started_count()); |
| 4599 | } |
| 4600 | |
| 4601 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 4602 | HttpNetworkSession::Params params; |
| 4603 | params.host_resolver = default_context_.host_resolver(); |
| 4604 | params.cert_verifier = default_context_.cert_verifier(); |
| 4605 | params.proxy_service = default_context_.proxy_service(); |
| 4606 | params.ssl_config_service = default_context_.ssl_config_service(); |
| 4607 | params.http_auth_handler_factory = |
| 4608 | default_context_.http_auth_handler_factory(); |
| 4609 | params.network_delegate = default_context_.network_delegate(); |
| 4610 | params.http_server_properties = default_context_.http_server_properties(); |
| 4611 | params.ssl_session_cache_shard = "alternate"; |
| 4612 | |
| 4613 | scoped_ptr<net::HttpCache> cache(new net::HttpCache( |
| 4614 | new net::HttpNetworkSession(params), |
| 4615 | net::HttpCache::DefaultBackend::InMemory(0))); |
| 4616 | |
| 4617 | default_context_.set_http_transaction_factory(cache.get()); |
| 4618 | |
| 4619 | { |
| 4620 | TestDelegate d; |
| 4621 | URLRequest r( |
| 4622 | test_server.GetURL("ssl-session-cache"), &d, &default_context_); |
| 4623 | |
| 4624 | r.Start(); |
| 4625 | EXPECT_TRUE(r.is_pending()); |
| 4626 | |
| 4627 | MessageLoop::current()->Run(); |
| 4628 | |
| 4629 | // The response will look like; |
| 4630 | // insert abc |
| 4631 | // insert xyz |
| 4632 | // |
| 4633 | // With a newline at the end which makes the split think that there are |
| 4634 | // three lines. |
| 4635 | |
| 4636 | EXPECT_EQ(1, d.response_started_count()); |
| 4637 | std::vector<std::string> lines; |
| 4638 | base::SplitString(d.data_received(), '\n', &lines); |
| 4639 | ASSERT_EQ(3u, lines.size()); |
| 4640 | |
| 4641 | std::string session_id; |
| 4642 | for (size_t i = 0; i < 2; i++) { |
| 4643 | std::vector<std::string> parts; |
| 4644 | base::SplitString(lines[i], '\t', &parts); |
| 4645 | ASSERT_EQ(2u, parts.size()); |
| 4646 | EXPECT_EQ("insert", parts[0]); |
| 4647 | if (i == 0) { |
| 4648 | session_id = parts[1]; |
| 4649 | } else { |
| 4650 | EXPECT_NE(session_id, parts[1]); |
| 4651 | } |
| 4652 | } |
| 4653 | } |
| 4654 | } |
| 4655 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4656 | class TestSSLConfigService : public SSLConfigService { |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4657 | public: |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4658 | TestSSLConfigService(bool ev_enabled, bool online_rev_checking) |
| 4659 | : ev_enabled_(ev_enabled), |
| 4660 | online_rev_checking_(online_rev_checking) { |
| 4661 | } |
| 4662 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 4663 | // SSLConfigService: |
| 4664 | virtual void GetSSLConfig(SSLConfig* config) OVERRIDE { |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4665 | *config = SSLConfig(); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4666 | config->rev_checking_enabled = online_rev_checking_; |
| 4667 | config->verify_ev_cert = ev_enabled_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4668 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4669 | |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 4670 | protected: |
| 4671 | virtual ~TestSSLConfigService() {} |
| 4672 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4673 | private: |
| 4674 | const bool ev_enabled_; |
| 4675 | const bool online_rev_checking_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4676 | }; |
| 4677 | |
| 4678 | // This the fingerprint of the "Testing CA" certificate used by the testserver. |
| 4679 | // See net/data/ssl/certificates/ocsp-test-root.pem. |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 4680 | static const SHA1HashValue kOCSPTestCertFingerprint = |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4681 | { { 0xf1, 0xad, 0xf6, 0xce, 0x42, 0xac, 0xe7, 0xb4, 0xf4, 0x24, |
| 4682 | 0xdb, 0x1a, 0xf7, 0xa0, 0x9f, 0x09, 0xa1, 0xea, 0xf1, 0x5c } }; |
| 4683 | |
| 4684 | // This is the policy OID contained in the certificates that testserver |
| 4685 | // generates. |
| 4686 | static const char kOCSPTestCertPolicy[] = "1.3.6.1.4.1.11129.2.4.1"; |
| 4687 | |
| 4688 | class HTTPSOCSPTest : public HTTPSRequestTest { |
| 4689 | public: |
| 4690 | HTTPSOCSPTest() |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4691 | : context_(true), |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4692 | ev_test_policy_( |
| 4693 | new ScopedTestEVPolicy(EVRootCAMetadata::GetInstance(), |
| 4694 | kOCSPTestCertFingerprint, |
| 4695 | kOCSPTestCertPolicy)) { |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | virtual void SetUp() OVERRIDE { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4699 | SetupContext(&context_); |
| 4700 | context_.Init(); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4701 | |
| 4702 | scoped_refptr<net::X509Certificate> root_cert = |
| 4703 | ImportCertFromFile(GetTestCertsDirectory(), "ocsp-test-root.pem"); |
| 4704 | CHECK_NE(static_cast<X509Certificate*>(NULL), root_cert); |
| 4705 | test_root_.reset(new ScopedTestRoot(root_cert)); |
| 4706 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 4707 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4708 | SetURLRequestContextForNSSHttpIO(&context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4709 | EnsureNSSHttpIOInit(); |
| 4710 | #endif |
| 4711 | } |
| 4712 | |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4713 | void DoConnection(const TestServer::SSLOptions& ssl_options, |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4714 | CertStatus* out_cert_status) { |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 4715 | // We always overwrite out_cert_status. |
| 4716 | *out_cert_status = 0; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4717 | TestServer test_server(TestServer::TYPE_HTTPS, |
| 4718 | ssl_options, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 4719 | base::FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4720 | ASSERT_TRUE(test_server.Start()); |
| 4721 | |
| 4722 | TestDelegate d; |
| 4723 | d.set_allow_certificate_errors(true); |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 4724 | URLRequest r(test_server.GetURL(""), &d, &context_); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4725 | r.Start(); |
| 4726 | |
| 4727 | MessageLoop::current()->Run(); |
| 4728 | |
| 4729 | EXPECT_EQ(1, d.response_started_count()); |
| 4730 | *out_cert_status = r.ssl_info().cert_status; |
| 4731 | } |
| 4732 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 4733 | virtual ~HTTPSOCSPTest() { |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 4734 | #if defined(USE_NSS) || defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4735 | ShutdownNSSHttpIO(); |
| 4736 | #endif |
| 4737 | } |
| 4738 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4739 | protected: |
| 4740 | // SetupContext configures the URLRequestContext that will be used for making |
| 4741 | // connetions to testserver. This can be overridden in test subclasses for |
| 4742 | // different behaviour. |
| 4743 | virtual void SetupContext(URLRequestContext* context) { |
| 4744 | context->set_ssl_config_service( |
| 4745 | new TestSSLConfigService(true /* check for EV */, |
| 4746 | true /* online revocation checking */)); |
| 4747 | } |
| 4748 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4749 | scoped_ptr<ScopedTestRoot> test_root_; |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 4750 | TestURLRequestContext context_; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4751 | scoped_ptr<ScopedTestEVPolicy> ev_test_policy_; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4752 | }; |
| 4753 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4754 | static CertStatus ExpectedCertStatusForFailedOnlineRevocationCheck() { |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 4755 | #if defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4756 | // Windows can return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION but we don't |
| 4757 | // have that ability on other platforms. |
| 4758 | return CERT_STATUS_UNABLE_TO_CHECK_REVOCATION; |
| 4759 | #else |
| 4760 | return 0; |
| 4761 | #endif |
| 4762 | } |
| 4763 | |
| 4764 | // SystemUsesChromiumEVMetadata returns true iff the current operating system |
| 4765 | // uses Chromium's EV metadata (i.e. EVRootCAMetadata). If it does not, then |
| 4766 | // several tests are effected because our testing EV certificate won't be |
| 4767 | // recognised as EV. |
| 4768 | static bool SystemUsesChromiumEVMetadata() { |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 4769 | #if defined(USE_OPENSSL) |
| 4770 | // http://crbug.com/117478 - OpenSSL does not support EV validation. |
| 4771 | return false; |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 4772 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4773 | // On OS X, we use the system to tell us whether a certificate is EV or not |
| 4774 | // and the system won't recognise our testing root. |
[email protected] | 05454a43 | 2012-03-20 20:04:01 | [diff] [blame] | 4775 | return false; |
| 4776 | #else |
| 4777 | return true; |
| 4778 | #endif |
| 4779 | } |
| 4780 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4781 | static bool SystemSupportsOCSP() { |
[email protected] | 5c50419 | 2012-03-27 19:00:57 | [diff] [blame] | 4782 | #if defined(USE_OPENSSL) |
| 4783 | // http://crbug.com/117478 - OpenSSL does not support OCSP. |
| 4784 | return false; |
| 4785 | #elif defined(OS_WIN) |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4786 | return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 4787 | #elif defined(OS_ANDROID) |
| 4788 | // TODO(jnd): http://crbug.com/117478 - EV verification is not yet supported. |
| 4789 | return false; |
| 4790 | #else |
| 4791 | return true; |
| 4792 | #endif |
| 4793 | } |
| 4794 | |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4795 | TEST_F(HTTPSOCSPTest, Valid) { |
| 4796 | if (!SystemSupportsOCSP()) { |
| 4797 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 4798 | return; |
| 4799 | } |
| 4800 | |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4801 | TestServer::SSLOptions ssl_options(TestServer::SSLOptions::CERT_AUTO); |
| 4802 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_OK; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4803 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 4804 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4805 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4806 | |
| 4807 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 4808 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4809 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 4810 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4811 | |
| 4812 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 4813 | } |
| 4814 | |
| 4815 | TEST_F(HTTPSOCSPTest, Revoked) { |
| 4816 | if (!SystemSupportsOCSP()) { |
| 4817 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 4818 | return; |
| 4819 | } |
| 4820 | |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4821 | TestServer::SSLOptions ssl_options( |
| 4822 | TestServer::SSLOptions::CERT_AUTO); |
| 4823 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4824 | |
| 4825 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4826 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4827 | |
[email protected] | a592c043 | 2012-12-01 18:10:29 | [diff] [blame] | 4828 | #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4829 | // Doesn't pass on OS X yet for reasons that need to be investigated. |
| 4830 | EXPECT_EQ(CERT_STATUS_REVOKED, cert_status & CERT_STATUS_ALL_ERRORS); |
| 4831 | #endif |
| 4832 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 4833 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 4834 | } |
| 4835 | |
| 4836 | TEST_F(HTTPSOCSPTest, Invalid) { |
| 4837 | if (!SystemSupportsOCSP()) { |
| 4838 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 4839 | return; |
| 4840 | } |
| 4841 | |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4842 | TestServer::SSLOptions ssl_options( |
| 4843 | TestServer::SSLOptions::CERT_AUTO); |
| 4844 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID; |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4845 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 4846 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4847 | DoConnection(ssl_options, &cert_status); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4848 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4849 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4850 | cert_status & CERT_STATUS_ALL_ERRORS); |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 4851 | |
| 4852 | // Without a positive OCSP response, we shouldn't show the EV status. |
| 4853 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 4854 | EXPECT_TRUE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 4855 | } |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4856 | |
| 4857 | class HTTPSEVCRLSetTest : public HTTPSOCSPTest { |
| 4858 | protected: |
| 4859 | virtual void SetupContext(URLRequestContext* context) OVERRIDE { |
| 4860 | context->set_ssl_config_service( |
| 4861 | new TestSSLConfigService(true /* check for EV */, |
| 4862 | false /* online revocation checking */)); |
| 4863 | } |
| 4864 | }; |
| 4865 | |
| 4866 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndInvalidOCSP) { |
| 4867 | if (!SystemSupportsOCSP()) { |
| 4868 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 4869 | return; |
| 4870 | } |
| 4871 | |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4872 | TestServer::SSLOptions ssl_options( |
| 4873 | TestServer::SSLOptions::CERT_AUTO); |
| 4874 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4875 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 4876 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 4877 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4878 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4879 | |
| 4880 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 4881 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 4882 | |
| 4883 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4884 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 4885 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | TEST_F(HTTPSEVCRLSetTest, MissingCRLSetAndGoodOCSP) { |
| 4889 | if (!SystemSupportsOCSP()) { |
| 4890 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 4891 | return; |
| 4892 | } |
| 4893 | |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4894 | TestServer::SSLOptions ssl_options( |
| 4895 | TestServer::SSLOptions::CERT_AUTO); |
| 4896 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_OK; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4897 | SSLConfigService::SetCRLSet(scoped_refptr<CRLSet>()); |
| 4898 | |
| 4899 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4900 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4901 | |
| 4902 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 4903 | |
| 4904 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 4905 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4906 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 4907 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSet) { |
| 4911 | if (!SystemSupportsOCSP()) { |
| 4912 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 4913 | return; |
| 4914 | } |
| 4915 | |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4916 | TestServer::SSLOptions ssl_options( |
| 4917 | TestServer::SSLOptions::CERT_AUTO); |
| 4918 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4919 | SSLConfigService::SetCRLSet( |
| 4920 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 4921 | |
| 4922 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4923 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4924 | |
| 4925 | EXPECT_EQ(ExpectedCertStatusForFailedOnlineRevocationCheck(), |
| 4926 | cert_status & CERT_STATUS_ALL_ERRORS); |
| 4927 | |
| 4928 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4929 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 4930 | static_cast<bool>(cert_status & CERT_STATUS_REV_CHECKING_ENABLED)); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | TEST_F(HTTPSEVCRLSetTest, FreshCRLSet) { |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4934 | TestServer::SSLOptions ssl_options( |
| 4935 | TestServer::SSLOptions::CERT_AUTO); |
| 4936 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4937 | SSLConfigService::SetCRLSet( |
| 4938 | scoped_refptr<CRLSet>(CRLSet::EmptyCRLSetForTesting())); |
| 4939 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 4940 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4941 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4942 | |
| 4943 | // With a valid, fresh CRLSet the bad OCSP response shouldn't matter because |
| 4944 | // we wont check it. |
| 4945 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 4946 | |
| 4947 | EXPECT_EQ(SystemUsesChromiumEVMetadata(), |
| 4948 | static_cast<bool>(cert_status & CERT_STATUS_IS_EV)); |
| 4949 | |
| 4950 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 4951 | } |
| 4952 | |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4953 | TEST_F(HTTPSEVCRLSetTest, ExpiredCRLSetAndRevokedNonEVCert) { |
| 4954 | // Test that when EV verification is requested, but online revocation |
| 4955 | // checking is disabled, and the leaf certificate is not in fact EV, that |
| 4956 | // no revocation checking actually happens. |
| 4957 | if (!SystemSupportsOCSP()) { |
| 4958 | LOG(WARNING) << "Skipping test because system doesn't support OCSP"; |
| 4959 | return; |
| 4960 | } |
| 4961 | |
| 4962 | // Unmark the certificate's OID as EV, which should disable revocation |
| 4963 | // checking (as per the user preference) |
| 4964 | ev_test_policy_.reset(); |
| 4965 | |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4966 | TestServer::SSLOptions ssl_options( |
| 4967 | TestServer::SSLOptions::CERT_AUTO); |
| 4968 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_REVOKED; |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4969 | SSLConfigService::SetCRLSet( |
| 4970 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 4971 | |
| 4972 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4973 | DoConnection(ssl_options, &cert_status); |
[email protected] | b6f2de3 | 2012-08-17 04:35:08 | [diff] [blame] | 4974 | |
| 4975 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 4976 | |
| 4977 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 4978 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 4979 | } |
| 4980 | |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4981 | class HTTPSCRLSetTest : public HTTPSOCSPTest { |
| 4982 | protected: |
| 4983 | virtual void SetupContext(URLRequestContext* context) OVERRIDE { |
| 4984 | context->set_ssl_config_service( |
| 4985 | new TestSSLConfigService(false /* check for EV */, |
| 4986 | false /* online revocation checking */)); |
| 4987 | } |
| 4988 | }; |
| 4989 | |
| 4990 | TEST_F(HTTPSCRLSetTest, ExpiredCRLSet) { |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4991 | TestServer::SSLOptions ssl_options( |
| 4992 | TestServer::SSLOptions::CERT_AUTO); |
| 4993 | ssl_options.ocsp_status = TestServer::SSLOptions::OCSP_INVALID; |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4994 | SSLConfigService::SetCRLSet( |
| 4995 | scoped_refptr<CRLSet>(CRLSet::ExpiredCRLSetForTesting())); |
| 4996 | |
[email protected] | 924e9f9 | 2012-12-16 22:00:53 | [diff] [blame] | 4997 | CertStatus cert_status; |
[email protected] | 295e5cd | 2012-08-23 01:05:12 | [diff] [blame] | 4998 | DoConnection(ssl_options, &cert_status); |
[email protected] | a13234c | 2012-03-20 21:45:02 | [diff] [blame] | 4999 | |
| 5000 | // If we're not trying EV verification then, even if the CRLSet has expired, |
| 5001 | // we don't fall back to online revocation checks. |
| 5002 | EXPECT_EQ(0u, cert_status & CERT_STATUS_ALL_ERRORS); |
| 5003 | EXPECT_FALSE(cert_status & CERT_STATUS_IS_EV); |
| 5004 | EXPECT_FALSE(cert_status & CERT_STATUS_REV_CHECKING_ENABLED); |
| 5005 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5006 | #endif // !defined(OS_IOS) |
[email protected] | dffe824 | 2012-03-20 15:14:27 | [diff] [blame] | 5007 | |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5008 | #if !defined(DISABLE_FTP_SUPPORT) |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 5009 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5010 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 5011 | URLRequestTestFTP() |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5012 | : test_server_(TestServer::TYPE_FTP, TestServer::kLocalhost, |
| 5013 | base::FilePath()) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5014 | } |
| 5015 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 5016 | protected: |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 5017 | TestServer test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 5018 | }; |
| 5019 | |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 5020 | // Make sure an FTP request using an unsafe ports fails. |
[email protected] | f2f31b3 | 2013-01-16 23:24:09 | [diff] [blame] | 5021 | TEST_F(URLRequestTestFTP, UnsafePort) { |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 5022 | ASSERT_TRUE(test_server_.Start()); |
| 5023 | |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 5024 | URLRequestJobFactoryImpl job_factory; |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 5025 | |
| 5026 | GURL url("ftp://127.0.0.1:7"); |
| 5027 | FtpProtocolHandler ftp_protocol_handler( |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 5028 | default_context_.ftp_transaction_factory(), |
| 5029 | default_context_.ftp_auth_cache()); |
| 5030 | job_factory.SetProtocolHandler( |
| 5031 | "ftp", |
[email protected] | 9f17046 | 2012-08-24 01:06:58 | [diff] [blame] | 5032 | new FtpProtocolHandler(default_context_.ftp_transaction_factory(), |
[email protected] | d2a13318 | 2012-08-05 16:44:08 | [diff] [blame] | 5033 | default_context_.ftp_auth_cache())); |
| 5034 | default_context_.set_job_factory(&job_factory); |
| 5035 | |
| 5036 | TestDelegate d; |
| 5037 | { |
| 5038 | URLRequest r(url, &d, &default_context_); |
| 5039 | r.Start(); |
| 5040 | EXPECT_TRUE(r.is_pending()); |
| 5041 | |
| 5042 | MessageLoop::current()->Run(); |
| 5043 | |
| 5044 | EXPECT_FALSE(r.is_pending()); |
| 5045 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
| 5046 | EXPECT_EQ(ERR_UNSAFE_PORT, r.status().error()); |
| 5047 | } |
| 5048 | } |
| 5049 | |
[email protected] | 5accf733 | 2009-11-24 03:41:38 | [diff] [blame] | 5050 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5051 | TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5052 | ASSERT_TRUE(test_server_.Start()); |
| 5053 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 5054 | TestDelegate d; |
| 5055 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5056 | URLRequest r(test_server_.GetURL("/"), &d, &default_context_); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 5057 | r.Start(); |
| 5058 | EXPECT_TRUE(r.is_pending()); |
| 5059 | |
| 5060 | MessageLoop::current()->Run(); |
| 5061 | |
| 5062 | EXPECT_FALSE(r.is_pending()); |
| 5063 | EXPECT_EQ(1, d.response_started_count()); |
| 5064 | EXPECT_FALSE(d.received_data_before_response()); |
| 5065 | EXPECT_LT(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 5066 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 5067 | r.GetSocketAddress().host()); |
| 5068 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 5069 | r.GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 5070 | } |
| 5071 | } |
| 5072 | |
[email protected] | 7df7001 | 2010-02-04 00:09:55 | [diff] [blame] | 5073 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5074 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5075 | ASSERT_TRUE(test_server_.Start()); |
| 5076 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5077 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5078 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 5079 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5080 | TestDelegate d; |
| 5081 | { |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5082 | URLRequest r(test_server_.GetURL("/LICENSE"), &d, &default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5083 | r.Start(); |
| 5084 | EXPECT_TRUE(r.is_pending()); |
| 5085 | |
| 5086 | MessageLoop::current()->Run(); |
| 5087 | |
| 5088 | int64 file_size = 0; |
| 5089 | file_util::GetFileSize(app_path, &file_size); |
| 5090 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 5091 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5092 | EXPECT_EQ(1, d.response_started_count()); |
| 5093 | EXPECT_FALSE(d.received_data_before_response()); |
| 5094 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 5095 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 5096 | r.GetSocketAddress().host()); |
| 5097 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 5098 | r.GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5099 | } |
| 5100 | } |
| 5101 | |
[email protected] | e9ecbd1 | 2009-12-20 18:44:40 | [diff] [blame] | 5102 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5103 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5104 | ASSERT_TRUE(test_server_.Start()); |
| 5105 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5106 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5107 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 5108 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5109 | TestDelegate d; |
| 5110 | { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5111 | URLRequest r( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5112 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5113 | &d, |
| 5114 | &default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5115 | r.Start(); |
| 5116 | EXPECT_TRUE(r.is_pending()); |
| 5117 | |
| 5118 | MessageLoop::current()->Run(); |
| 5119 | |
| 5120 | int64 file_size = 0; |
| 5121 | file_util::GetFileSize(app_path, &file_size); |
| 5122 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 5123 | EXPECT_FALSE(r.is_pending()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 5124 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 5125 | r.GetSocketAddress().host()); |
| 5126 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 5127 | r.GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5128 | EXPECT_EQ(1, d.response_started_count()); |
| 5129 | EXPECT_FALSE(d.received_data_before_response()); |
| 5130 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5131 | |
| 5132 | LoadTimingInfo load_timing_info; |
[email protected] | 0a70391 | 2013-02-02 04:25:17 | [diff] [blame] | 5133 | r.GetLoadTimingInfo(&load_timing_info); |
[email protected] | 58e32bb | 2013-01-21 18:23:25 | [diff] [blame] | 5134 | TestLoadTimingNoHttpConnection(load_timing_info); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 5138 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5139 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5140 | ASSERT_TRUE(test_server_.Start()); |
| 5141 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5142 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5143 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 5144 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5145 | TestDelegate d; |
| 5146 | { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5147 | URLRequest r( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5148 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 5149 | "chrome", |
| 5150 | "wrong_password"), |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5151 | &d, |
| 5152 | &default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5153 | r.Start(); |
| 5154 | EXPECT_TRUE(r.is_pending()); |
| 5155 | |
| 5156 | MessageLoop::current()->Run(); |
| 5157 | |
| 5158 | int64 file_size = 0; |
| 5159 | file_util::GetFileSize(app_path, &file_size); |
| 5160 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 5161 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5162 | EXPECT_EQ(1, d.response_started_count()); |
| 5163 | EXPECT_FALSE(d.received_data_before_response()); |
| 5164 | EXPECT_EQ(d.bytes_received(), 0); |
| 5165 | } |
| 5166 | } |
| 5167 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 5168 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5169 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5170 | ASSERT_TRUE(test_server_.Start()); |
| 5171 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5172 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 5173 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 5174 | app_path = app_path.AppendASCII("LICENSE"); |
| 5175 | TestDelegate d; |
| 5176 | // Set correct login credentials. The delegate will be asked for them when |
| 5177 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5178 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 5179 | { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5180 | URLRequest r( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5181 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 5182 | "chrome", |
| 5183 | "wrong_password"), |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5184 | &d, |
| 5185 | &default_context_); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 5186 | r.Start(); |
| 5187 | EXPECT_TRUE(r.is_pending()); |
| 5188 | |
| 5189 | MessageLoop::current()->Run(); |
| 5190 | |
| 5191 | int64 file_size = 0; |
| 5192 | file_util::GetFileSize(app_path, &file_size); |
| 5193 | |
| 5194 | EXPECT_FALSE(r.is_pending()); |
| 5195 | EXPECT_EQ(1, d.response_started_count()); |
| 5196 | EXPECT_FALSE(d.received_data_before_response()); |
| 5197 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 5198 | } |
| 5199 | } |
| 5200 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 5201 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5202 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5203 | ASSERT_TRUE(test_server_.Start()); |
| 5204 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5205 | base::FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5206 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 5207 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5208 | TestDelegate d; |
| 5209 | { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5210 | URLRequest r( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5211 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 5212 | "wrong_user", |
| 5213 | "chrome"), |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5214 | &d, |
| 5215 | &default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5216 | r.Start(); |
| 5217 | EXPECT_TRUE(r.is_pending()); |
| 5218 | |
| 5219 | MessageLoop::current()->Run(); |
| 5220 | |
| 5221 | int64 file_size = 0; |
| 5222 | file_util::GetFileSize(app_path, &file_size); |
| 5223 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 5224 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 5225 | EXPECT_EQ(1, d.response_started_count()); |
| 5226 | EXPECT_FALSE(d.received_data_before_response()); |
| 5227 | EXPECT_EQ(d.bytes_received(), 0); |
| 5228 | } |
| 5229 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 5230 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 5231 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5232 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5233 | ASSERT_TRUE(test_server_.Start()); |
| 5234 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5235 | base::FilePath app_path; |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 5236 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 5237 | app_path = app_path.AppendASCII("LICENSE"); |
| 5238 | TestDelegate d; |
| 5239 | // Set correct login credentials. The delegate will be asked for them when |
| 5240 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5241 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 5242 | { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5243 | URLRequest r( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5244 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 5245 | "wrong_user", |
| 5246 | "chrome"), |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5247 | &d, |
| 5248 | &default_context_); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 5249 | r.Start(); |
| 5250 | EXPECT_TRUE(r.is_pending()); |
| 5251 | |
| 5252 | MessageLoop::current()->Run(); |
| 5253 | |
| 5254 | int64 file_size = 0; |
| 5255 | file_util::GetFileSize(app_path, &file_size); |
| 5256 | |
| 5257 | EXPECT_FALSE(r.is_pending()); |
| 5258 | EXPECT_EQ(1, d.response_started_count()); |
| 5259 | EXPECT_FALSE(d.received_data_before_response()); |
| 5260 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 5261 | } |
| 5262 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 5263 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 5264 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5265 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5266 | ASSERT_TRUE(test_server_.Start()); |
| 5267 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5268 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 5269 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 5270 | app_path = app_path.AppendASCII("LICENSE"); |
| 5271 | |
| 5272 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 5273 | { |
| 5274 | // Pass correct login identity in the URL. |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5275 | URLRequest r( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5276 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 5277 | "chrome", |
| 5278 | "chrome"), |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5279 | d.get(), |
| 5280 | &default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 5281 | r.Start(); |
| 5282 | EXPECT_TRUE(r.is_pending()); |
| 5283 | |
| 5284 | MessageLoop::current()->Run(); |
| 5285 | |
| 5286 | int64 file_size = 0; |
| 5287 | file_util::GetFileSize(app_path, &file_size); |
| 5288 | |
| 5289 | EXPECT_FALSE(r.is_pending()); |
| 5290 | EXPECT_EQ(1, d->response_started_count()); |
| 5291 | EXPECT_FALSE(d->received_data_before_response()); |
| 5292 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 5293 | } |
| 5294 | |
| 5295 | d.reset(new TestDelegate); |
| 5296 | { |
| 5297 | // This request should use cached identity from previous request. |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5298 | URLRequest r(test_server_.GetURL("/LICENSE"), d.get(), &default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 5299 | r.Start(); |
| 5300 | EXPECT_TRUE(r.is_pending()); |
| 5301 | |
| 5302 | MessageLoop::current()->Run(); |
| 5303 | |
| 5304 | int64 file_size = 0; |
| 5305 | file_util::GetFileSize(app_path, &file_size); |
| 5306 | |
| 5307 | EXPECT_FALSE(r.is_pending()); |
| 5308 | EXPECT_EQ(1, d->response_started_count()); |
| 5309 | EXPECT_FALSE(d->received_data_before_response()); |
| 5310 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 5311 | } |
| 5312 | } |
| 5313 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 5314 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 5315 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5316 | ASSERT_TRUE(test_server_.Start()); |
| 5317 | |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 5318 | base::FilePath app_path; |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 5319 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 5320 | app_path = app_path.AppendASCII("LICENSE"); |
| 5321 | |
| 5322 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 5323 | // Set correct login credentials. The delegate will be asked for them when |
| 5324 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 5325 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 5326 | { |
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 5327 | URLRequest r( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 5328 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 5329 | "chrome", |
| 5330 | "wrong_password"), |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5331 | d.get(), |
| 5332 | &default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 5333 | r.Start(); |
| 5334 | EXPECT_TRUE(r.is_pending()); |
| 5335 | |
| 5336 | MessageLoop::current()->Run(); |
| 5337 | |
| 5338 | int64 file_size = 0; |
| 5339 | file_util::GetFileSize(app_path, &file_size); |
| 5340 | |
| 5341 | EXPECT_FALSE(r.is_pending()); |
| 5342 | EXPECT_EQ(1, d->response_started_count()); |
| 5343 | EXPECT_FALSE(d->received_data_before_response()); |
| 5344 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 5345 | } |
| 5346 | |
| 5347 | // Use a new delegate without explicit credentials. The cached ones should be |
| 5348 | // used. |
| 5349 | d.reset(new TestDelegate); |
| 5350 | { |
| 5351 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 5352 | // ones. |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 5353 | URLRequest r(test_server_.GetURL("/LICENSE"), d.get(), &default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 5354 | r.Start(); |
| 5355 | EXPECT_TRUE(r.is_pending()); |
| 5356 | |
| 5357 | MessageLoop::current()->Run(); |
| 5358 | |
| 5359 | int64 file_size = 0; |
| 5360 | file_util::GetFileSize(app_path, &file_size); |
| 5361 | |
| 5362 | EXPECT_FALSE(r.is_pending()); |
| 5363 | EXPECT_EQ(1, d->response_started_count()); |
| 5364 | EXPECT_FALSE(d->received_data_before_response()); |
| 5365 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 5366 | } |
| 5367 | } |
[email protected] | 316c1e5e | 2012-09-12 15:17:44 | [diff] [blame] | 5368 | #endif // !defined(DISABLE_FTP_SUPPORT) |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 5369 | |
| 5370 | } // namespace net |