[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" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 30 | #include "net/base/load_flags.h" |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 31 | #include "net/base/mock_host_resolver.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 32 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 33 | #include "net/base/net_log.h" |
| 34 | #include "net/base/net_log_unittest.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 35 | #include "net/base/net_module.h" |
| 36 | #include "net/base/net_util.h" |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 37 | #include "net/base/ssl_connection_status_flags.h" |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 38 | #include "net/base/upload_data.h" |
[email protected] | aa84a7e | 2012-03-15 21:29:06 | [diff] [blame^] | 39 | #include "net/cookies/cookie_monster.h" |
| 40 | #include "net/cookies/cookie_store_test_helpers.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 41 | #include "net/disk_cache/disk_cache.h" |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 42 | #include "net/ftp/ftp_network_layer.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 43 | #include "net/http/http_cache.h" |
| 44 | #include "net/http/http_network_layer.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 45 | #include "net/http/http_network_session.h" |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 46 | #include "net/http/http_request_headers.h" |
[email protected] | 319d9e6f | 2009-02-18 19:47:21 | [diff] [blame] | 47 | #include "net/http/http_response_headers.h" |
[email protected] | 63de95b | 2008-12-10 04:11:27 | [diff] [blame] | 48 | #include "net/proxy/proxy_service.h" |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 49 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 1b9565c | 2010-07-21 01:19:31 | [diff] [blame] | 50 | #include "net/test/test_server.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 51 | #include "net/url_request/url_request.h" |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 52 | #include "net/url_request/url_request_file_dir_job.h" |
[email protected] | bcb84f8b | 2009-08-31 16:20:14 | [diff] [blame] | 53 | #include "net/url_request/url_request_http_job.h" |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 54 | #include "net/url_request/url_request_job_factory.h" |
| 55 | #include "net/url_request/url_request_redirect_job.h" |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 56 | #include "net/url_request/url_request_test_job.h" |
[email protected] | d2db029 | 2011-01-26 20:23:44 | [diff] [blame] | 57 | #include "net/url_request/url_request_test_util.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 58 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 23887f04f | 2008-12-02 19:20:15 | [diff] [blame] | 59 | #include "testing/platform_test.h" |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 60 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 61 | using base::Time; |
| 62 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 63 | namespace net { |
| 64 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 65 | namespace { |
| 66 | |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 67 | // A subclass of TestServer that uses a statically-configured hostname. This is |
| 68 | // to work around mysterious failures in chrome_frame_net_tests. See: |
| 69 | // http://crbug.com/114369 |
| 70 | class LocalHttpTestServer : public TestServer { |
| 71 | public: |
| 72 | explicit LocalHttpTestServer(const FilePath& document_root) |
| 73 | : TestServer(TestServer::TYPE_HTTP, |
| 74 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 75 | document_root) {} |
| 76 | LocalHttpTestServer() |
| 77 | : TestServer(TestServer::TYPE_HTTP, |
| 78 | ScopedCustomUrlRequestTestHttpHost::value(), |
| 79 | FilePath()) {} |
| 80 | }; |
| 81 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 82 | const string16 kChrome(ASCIIToUTF16("chrome")); |
| 83 | const string16 kSecret(ASCIIToUTF16("secret")); |
| 84 | const string16 kUser(ASCIIToUTF16("user")); |
| 85 | |
[email protected] | 8a16266e | 2009-09-10 21:08:39 | [diff] [blame] | 86 | base::StringPiece TestNetResourceProvider(int key) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 87 | return "header"; |
| 88 | } |
| 89 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 90 | // Do a case-insensitive search through |haystack| for |needle|. |
| 91 | bool ContainsString(const std::string& haystack, const char* needle) { |
| 92 | std::string::const_iterator it = |
| 93 | std::search(haystack.begin(), |
| 94 | haystack.end(), |
| 95 | needle, |
| 96 | needle + strlen(needle), |
[email protected] | 07f1cee | 2010-11-03 03:53:35 | [diff] [blame] | 97 | base::CaseInsensitiveCompare<char>()); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 98 | return it != haystack.end(); |
| 99 | } |
| 100 | |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 101 | void FillBuffer(char* buffer, size_t len) { |
| 102 | static bool called = false; |
| 103 | if (!called) { |
| 104 | called = true; |
| 105 | int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 106 | srand(seed); |
| 107 | } |
| 108 | |
| 109 | for (size_t i = 0; i < len; i++) { |
| 110 | buffer[i] = static_cast<char>(rand()); |
| 111 | if (!buffer[i]) |
| 112 | buffer[i] = 'g'; |
| 113 | } |
| 114 | } |
| 115 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 116 | scoped_refptr<UploadData> CreateSimpleUploadData(const char* data) { |
| 117 | scoped_refptr<UploadData> upload(new UploadData); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 118 | upload->AppendBytes(data, strlen(data)); |
| 119 | return upload; |
| 120 | } |
| 121 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 122 | // Verify that the SSLInfo of a successful SSL connection has valid values. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 123 | void CheckSSLInfo(const SSLInfo& ssl_info) { |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 124 | // Allow ChromeFrame fake SSLInfo to get through. |
[email protected] | c679be2 | 2010-08-29 15:20:05 | [diff] [blame] | 125 | if (ssl_info.cert.get() && |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 126 | ssl_info.cert.get()->issuer().GetDisplayName() == "Chrome Internal") { |
| 127 | // -1 means unknown. |
| 128 | EXPECT_EQ(ssl_info.security_bits, -1); |
[email protected] | c679be2 | 2010-08-29 15:20:05 | [diff] [blame] | 129 | return; |
[email protected] | 89af3d9 | 2010-10-15 20:31:37 | [diff] [blame] | 130 | } |
| 131 | |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 132 | // -1 means unknown. 0 means no encryption. |
| 133 | EXPECT_GT(ssl_info.security_bits, 0); |
| 134 | |
| 135 | // The cipher suite TLS_NULL_WITH_NULL_NULL (0) must not be negotiated. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 136 | int cipher_suite = SSLConnectionStatusToCipherSuite( |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 137 | ssl_info.connection_status); |
| 138 | EXPECT_NE(0, cipher_suite); |
| 139 | } |
| 140 | |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 141 | bool FingerprintsEqual(const FingerprintVector& a, const FingerprintVector& b) { |
| 142 | size_t size = a.size(); |
| 143 | |
| 144 | if (size != b.size()) |
| 145 | return false; |
| 146 | |
| 147 | for (size_t i = 0; i < size; ++i) { |
| 148 | if (!a[i].Equals(b[i])) |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | return true; |
| 153 | } |
| 154 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 155 | } // namespace |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 156 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 157 | // A network delegate that blocks requests, optionally cancelling or redirecting |
| 158 | // them. |
| 159 | class BlockingNetworkDelegate : public TestNetworkDelegate { |
| 160 | public: |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 161 | BlockingNetworkDelegate() |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 162 | : retval_(ERR_IO_PENDING), |
| 163 | callback_retval_(OK), |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 164 | auth_retval_(NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING), |
| 165 | auth_callback_retval_( |
| 166 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION), |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 167 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 168 | |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 169 | void set_retval(int retval) { retval_ = retval; } |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 170 | void set_callback_retval(int retval) { callback_retval_ = retval; } |
| 171 | void set_redirect_url(const GURL& url) { redirect_url_ = url; } |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 172 | void set_auth_retval(NetworkDelegate::AuthRequiredResponse retval) { |
| 173 | auth_retval_ = retval; } |
| 174 | void set_auth_callback_retval(NetworkDelegate::AuthRequiredResponse retval) { |
| 175 | auth_callback_retval_ = retval; } |
| 176 | void set_auth_credentials(const AuthCredentials& auth_credentials) { |
| 177 | auth_credentials_ = auth_credentials; |
| 178 | } |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 179 | |
| 180 | private: |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 181 | // TestNetworkDelegate implementation. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 182 | virtual int OnBeforeURLRequest(URLRequest* request, |
| 183 | const CompletionCallback& callback, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 184 | GURL* new_url) OVERRIDE { |
[email protected] | bb445ce | 2011-05-03 18:23:57 | [diff] [blame] | 185 | if (redirect_url_ == request->url()) { |
| 186 | // We've already seen this request and redirected elsewhere. |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 187 | return OK; |
[email protected] | bb445ce | 2011-05-03 18:23:57 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 190 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 191 | |
| 192 | if (!redirect_url_.is_empty()) |
| 193 | *new_url = redirect_url_; |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 194 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 195 | if (retval_ != ERR_IO_PENDING) |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 196 | return retval_; |
| 197 | |
[email protected] | 8523ba5 | 2011-05-22 19:00:58 | [diff] [blame] | 198 | MessageLoop::current()->PostTask( |
| 199 | FROM_HERE, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 200 | base::Bind(&BlockingNetworkDelegate::DoCallback, |
| 201 | weak_factory_.GetWeakPtr(), callback)); |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 202 | return ERR_IO_PENDING; |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 205 | virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 206 | URLRequest* request, |
| 207 | const AuthChallengeInfo& auth_info, |
| 208 | const AuthCallback& callback, |
| 209 | AuthCredentials* credentials) OVERRIDE { |
| 210 | TestNetworkDelegate::OnAuthRequired(request, auth_info, callback, |
| 211 | credentials); |
| 212 | switch (auth_retval_) { |
| 213 | case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: |
| 214 | break; |
| 215 | case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: |
| 216 | *credentials = auth_credentials_; |
| 217 | case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: |
| 218 | break; |
| 219 | case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: |
| 220 | MessageLoop::current()->PostTask( |
| 221 | FROM_HERE, |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 222 | base::Bind(&BlockingNetworkDelegate::DoAuthCallback, |
| 223 | weak_factory_.GetWeakPtr(), callback, credentials)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | return auth_retval_; |
| 227 | } |
| 228 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 229 | void DoCallback(const CompletionCallback& callback) { |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 230 | callback.Run(callback_retval_); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 231 | } |
| 232 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 233 | void DoAuthCallback(const AuthCallback& callback, |
| 234 | AuthCredentials* credentials) { |
| 235 | if (auth_callback_retval_ == |
| 236 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH) { |
| 237 | *credentials = auth_credentials_; |
| 238 | } |
| 239 | callback.Run(auth_callback_retval_); |
| 240 | } |
| 241 | |
| 242 | |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 243 | int retval_; |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 244 | int callback_retval_; |
| 245 | GURL redirect_url_; |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 246 | NetworkDelegate::AuthRequiredResponse auth_retval_; |
| 247 | NetworkDelegate::AuthRequiredResponse auth_callback_retval_; |
| 248 | AuthCredentials auth_credentials_; |
[email protected] | 084262c | 2011-12-01 21:12:47 | [diff] [blame] | 249 | base::WeakPtrFactory<BlockingNetworkDelegate> weak_factory_; |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 250 | }; |
| 251 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 252 | // A network delegate that allows blocking requests until a callback function is |
| 253 | // called. |
| 254 | class BlockingNetworkDelegateWithManualCallback : public TestNetworkDelegate { |
| 255 | public: |
| 256 | enum State { |
| 257 | NOT_BLOCKED = 0, |
| 258 | ON_BEFORE_URL_REQUEST = 1 << 0, |
| 259 | ON_BEFORE_SEND_HEADERS = 1 << 1, |
| 260 | ON_HEADERS_RECEIVED = 1 << 2, |
| 261 | ON_AUTH_REQUIRED = 1 << 3 |
| 262 | }; |
| 263 | |
| 264 | BlockingNetworkDelegateWithManualCallback() |
| 265 | : block_on_(0), |
| 266 | state_(NOT_BLOCKED) { |
| 267 | } |
| 268 | |
| 269 | // Activates blocking on |state|. |
| 270 | void BlockOn(State state) { |
| 271 | block_on_ |= state; |
| 272 | } |
| 273 | |
| 274 | void DoCallback(int rv) { |
| 275 | ASSERT_NE(NOT_BLOCKED, state_); |
| 276 | CompletionCallback callback = callback_; |
| 277 | Reset(); |
| 278 | callback.Run(rv); |
| 279 | } |
| 280 | |
| 281 | void DoAuthCallback(NetworkDelegate::AuthRequiredResponse response) { |
| 282 | ASSERT_EQ(ON_AUTH_REQUIRED, state_); |
| 283 | AuthCallback auth_callback = auth_callback_; |
| 284 | Reset(); |
| 285 | auth_callback.Run(response); |
| 286 | } |
| 287 | |
| 288 | // Runs the message loop until |state| is reached. |
| 289 | void WaitForState(State state) { |
| 290 | while (state_ != state) |
| 291 | MessageLoop::current()->RunAllPending(); |
| 292 | } |
| 293 | |
| 294 | private: |
| 295 | // TestNetworkDelegate implementation. |
| 296 | virtual int OnBeforeURLRequest(URLRequest* request, |
| 297 | const CompletionCallback& callback, |
| 298 | GURL* new_url) OVERRIDE { |
| 299 | TestNetworkDelegate::OnBeforeURLRequest(request, callback, new_url); |
| 300 | if ((block_on_ & ON_BEFORE_URL_REQUEST) == 0) { |
| 301 | return OK; |
| 302 | } else { |
| 303 | state_ = ON_BEFORE_URL_REQUEST; |
| 304 | callback_ = callback; |
| 305 | return ERR_IO_PENDING; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | virtual int OnBeforeSendHeaders(URLRequest* request, |
| 310 | const CompletionCallback& callback, |
| 311 | HttpRequestHeaders* headers) OVERRIDE { |
| 312 | TestNetworkDelegate::OnBeforeSendHeaders(request, callback, headers); |
| 313 | if ((block_on_ & ON_BEFORE_SEND_HEADERS) == 0) { |
| 314 | return OK; |
| 315 | } else { |
| 316 | state_ = ON_BEFORE_SEND_HEADERS; |
| 317 | callback_ = callback; |
| 318 | return ERR_IO_PENDING; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | virtual int OnHeadersReceived( |
| 323 | URLRequest* request, |
| 324 | const CompletionCallback& callback, |
| 325 | HttpResponseHeaders* original_response_headers, |
| 326 | scoped_refptr<HttpResponseHeaders>* override_response_headers) |
| 327 | OVERRIDE { |
| 328 | TestNetworkDelegate::OnHeadersReceived( |
| 329 | request, callback, original_response_headers, |
| 330 | override_response_headers); |
| 331 | if ((block_on_ & ON_HEADERS_RECEIVED) == 0) { |
| 332 | return OK; |
| 333 | } else { |
| 334 | state_ = ON_HEADERS_RECEIVED; |
| 335 | callback_ = callback; |
| 336 | return ERR_IO_PENDING; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | virtual NetworkDelegate::AuthRequiredResponse OnAuthRequired( |
| 341 | URLRequest* request, |
| 342 | const AuthChallengeInfo& auth_info, |
| 343 | const AuthCallback& callback, |
| 344 | AuthCredentials* credentials) OVERRIDE { |
| 345 | if ((block_on_ & ON_AUTH_REQUIRED) == 0) { |
| 346 | return NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; |
| 347 | } else { |
| 348 | state_ = ON_AUTH_REQUIRED; |
| 349 | auth_callback_ = callback; |
| 350 | return NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void Reset() { |
| 355 | state_ = NOT_BLOCKED; |
| 356 | callback_.Reset(); |
| 357 | auth_callback_.Reset(); |
| 358 | } |
| 359 | |
| 360 | int block_on_; // Bit mask on which states to block. |
| 361 | State state_; |
| 362 | CompletionCallback callback_; |
| 363 | AuthCallback auth_callback_; |
| 364 | }; |
| 365 | |
| 366 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 367 | // A simple Interceptor that returns a pre-built URLRequestJob one time. |
| 368 | class TestJobInterceptor : public URLRequestJobFactory::Interceptor { |
| 369 | public: |
| 370 | TestJobInterceptor() |
| 371 | : main_intercept_job_(NULL) { |
| 372 | } |
| 373 | |
| 374 | virtual URLRequestJob* MaybeIntercept(URLRequest* request) const OVERRIDE { |
| 375 | URLRequestJob* job = main_intercept_job_; |
| 376 | main_intercept_job_ = NULL; |
| 377 | return job; |
| 378 | } |
| 379 | |
| 380 | virtual URLRequestJob* MaybeInterceptRedirect( |
| 381 | const GURL& location, URLRequest* request) const OVERRIDE { |
| 382 | return NULL; |
| 383 | } |
| 384 | |
| 385 | virtual URLRequestJob* MaybeInterceptResponse( |
| 386 | URLRequest* request) const OVERRIDE { |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | void set_main_intercept_job(URLRequestJob* job) { |
| 391 | main_intercept_job_ = job; |
| 392 | } |
| 393 | |
| 394 | private: |
| 395 | mutable URLRequestJob* main_intercept_job_; |
| 396 | }; |
| 397 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 398 | // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
| 399 | class URLRequestTest : public PlatformTest { |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 400 | public: |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 401 | URLRequestTest() : default_context_(new TestURLRequestContext(true)) { |
| 402 | default_context_->set_network_delegate(&default_network_delegate_); |
| 403 | default_context_->Init(); |
| 404 | } |
| 405 | |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 406 | static void SetUpTestCase() { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 407 | URLRequest::AllowFileAccess(); |
[email protected] | abb2609 | 2010-11-11 22:19:00 | [diff] [blame] | 408 | } |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 409 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 410 | // Adds the TestJobInterceptor to the default context. |
| 411 | TestJobInterceptor* AddTestInterceptor() { |
| 412 | TestJobInterceptor* interceptor = new TestJobInterceptor(); |
| 413 | default_context_->set_job_factory(&job_factory_); |
| 414 | job_factory_.AddInterceptor(interceptor); |
| 415 | return interceptor; |
| 416 | } |
| 417 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 418 | protected: |
| 419 | TestNetworkDelegate default_network_delegate_; // must outlive URLRequest |
| 420 | scoped_refptr<TestURLRequestContext> default_context_; |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 421 | URLRequestJobFactory job_factory_; |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 422 | }; |
| 423 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 424 | class URLRequestTestHTTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 425 | public: |
| 426 | URLRequestTestHTTP() |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 427 | : test_server_(FilePath(FILE_PATH_LITERAL( |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 428 | "net/data/url_request_unittest"))) { |
| 429 | } |
| 430 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 431 | protected: |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 432 | // Requests |redirect_url|, which must return a HTTP 3xx redirect. |
| 433 | // |request_method| is the method to use for the initial request. |
| 434 | // |redirect_method| is the method that is expected to be used for the second |
| 435 | // request, after redirection. |
| 436 | // If |include_data| is true, data is uploaded with the request. The |
| 437 | // response body is expected to match it exactly, if and only if |
| 438 | // |request_method| == |redirect_method|. |
| 439 | void HTTPRedirectMethodTest(const GURL& redirect_url, |
| 440 | const std::string& request_method, |
| 441 | const std::string& redirect_method, |
| 442 | bool include_data) { |
| 443 | static const char kData[] = "hello world"; |
| 444 | TestDelegate d; |
| 445 | TestURLRequest req(redirect_url, &d); |
| 446 | req.set_context(default_context_); |
| 447 | req.set_method(request_method); |
| 448 | if (include_data) { |
| 449 | req.set_upload(CreateSimpleUploadData(kData).get()); |
| 450 | HttpRequestHeaders headers; |
| 451 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 452 | base::UintToString(arraysize(kData) - 1)); |
| 453 | req.SetExtraRequestHeaders(headers); |
| 454 | } |
| 455 | req.Start(); |
| 456 | MessageLoop::current()->Run(); |
| 457 | EXPECT_EQ(redirect_method, req.method()); |
| 458 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 459 | EXPECT_EQ(OK, req.status().error()); |
| 460 | if (include_data) { |
| 461 | if (request_method == redirect_method) { |
| 462 | EXPECT_EQ(kData, d.data_received()); |
| 463 | } else { |
| 464 | EXPECT_NE(kData, d.data_received()); |
| 465 | } |
| 466 | } |
| 467 | if (HasFailure()) |
| 468 | LOG(WARNING) << "Request method was: " << request_method; |
| 469 | } |
| 470 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 471 | void HTTPUploadDataOperationTest(const std::string& method) { |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 472 | const int kMsgSize = 20000; // multiple of 10 |
| 473 | const int kIterations = 50; |
| 474 | char *uploadBytes = new char[kMsgSize+1]; |
| 475 | char *ptr = uploadBytes; |
| 476 | char marker = 'a'; |
| 477 | for (int idx = 0; idx < kMsgSize/10; idx++) { |
| 478 | memcpy(ptr, "----------", 10); |
| 479 | ptr += 10; |
| 480 | if (idx % 100 == 0) { |
| 481 | ptr--; |
| 482 | *ptr++ = marker; |
| 483 | if (++marker > 'z') |
| 484 | marker = 'a'; |
| 485 | } |
| 486 | } |
| 487 | uploadBytes[kMsgSize] = '\0'; |
| 488 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 489 | for (int i = 0; i < kIterations; ++i) { |
| 490 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 491 | URLRequest r(test_server_.GetURL("echo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 492 | r.set_context(default_context_); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 493 | r.set_method(method.c_str()); |
| 494 | |
| 495 | r.AppendBytesToUpload(uploadBytes, kMsgSize); |
| 496 | |
| 497 | r.Start(); |
| 498 | EXPECT_TRUE(r.is_pending()); |
| 499 | |
| 500 | MessageLoop::current()->Run(); |
| 501 | |
| 502 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 503 | (int) r.status().status() << ", os error: " << r.status().error(); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 504 | |
| 505 | EXPECT_FALSE(d.received_data_before_response()); |
| 506 | EXPECT_EQ(uploadBytes, d.data_received()); |
| 507 | EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize), 0); |
| 508 | EXPECT_EQ(d.data_received().compare(uploadBytes), 0); |
| 509 | } |
| 510 | delete[] uploadBytes; |
| 511 | } |
| 512 | |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 513 | void AddChunksToUpload(TestURLRequest* r) { |
[email protected] | 0c9bf87 | 2011-03-04 17:53:22 | [diff] [blame] | 514 | r->AppendChunkToUpload("a", 1, false); |
| 515 | r->AppendChunkToUpload("bcd", 3, false); |
| 516 | r->AppendChunkToUpload("this is a longer chunk than before.", 35, false); |
| 517 | r->AppendChunkToUpload("\r\n\r\n", 4, false); |
| 518 | r->AppendChunkToUpload("0", 1, false); |
| 519 | r->AppendChunkToUpload("2323", 4, true); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void VerifyReceivedDataMatchesChunks(TestURLRequest* r, TestDelegate* d) { |
| 523 | // This should match the chunks sent by AddChunksToUpload(). |
| 524 | const char* expected_data = |
| 525 | "abcdthis is a longer chunk than before.\r\n\r\n02323"; |
| 526 | |
| 527 | ASSERT_EQ(1, d->response_started_count()) << "request failed: " << |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 528 | (int) r->status().status() << ", os error: " << r->status().error(); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 529 | |
| 530 | EXPECT_FALSE(d->received_data_before_response()); |
| 531 | |
| 532 | ASSERT_EQ(strlen(expected_data), static_cast<size_t>(d->bytes_received())); |
| 533 | EXPECT_EQ(0, memcmp(d->data_received().c_str(), expected_data, |
| 534 | strlen(expected_data))); |
| 535 | } |
| 536 | |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 537 | LocalHttpTestServer test_server_; |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 538 | }; |
| 539 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 540 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 541 | // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 542 | // The HTTPTestServer will return a 302 response, which we should not |
| 543 | // follow. |
[email protected] | 8eb127e | 2011-09-16 10:47:47 | [diff] [blame] | 544 | TEST_F(URLRequestTestHTTP, ProxyTunnelRedirectTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 545 | ASSERT_TRUE(test_server_.Start()); |
| 546 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 547 | TestNetworkDelegate network_delegate; // must outlive URLRequest |
| 548 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 549 | context->SetProxyFromString(test_server_.host_port_pair().ToString()); |
| 550 | context->set_network_delegate(&network_delegate); |
| 551 | context->Init(); |
| 552 | |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 553 | TestDelegate d; |
| 554 | { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 555 | URLRequest r(GURL("https://www.redirect.com/"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 556 | r.set_context(context); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 557 | r.Start(); |
| 558 | EXPECT_TRUE(r.is_pending()); |
| 559 | |
| 560 | MessageLoop::current()->Run(); |
| 561 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 562 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 563 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 564 | EXPECT_EQ(1, d.response_started_count()); |
[email protected] | d1ec5908 | 2009-02-11 02:48:15 | [diff] [blame] | 565 | // We should not have followed the redirect. |
| 566 | EXPECT_EQ(0, d.received_redirect_count()); |
| 567 | } |
| 568 | } |
| 569 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 570 | // This is the same as the previous test, but checks that the network delegate |
| 571 | // registers the error. |
[email protected] | 4841b77 | 2011-11-04 10:36:06 | [diff] [blame] | 572 | // This test was disabled because it made chrome_frame_net_tests hang |
| 573 | // (see bug 102991). |
| 574 | TEST_F(URLRequestTestHTTP, DISABLED_NetworkDelegateTunnelConnectionFailed) { |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 575 | ASSERT_TRUE(test_server_.Start()); |
| 576 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 577 | TestNetworkDelegate network_delegate; // must outlive URLRequest |
| 578 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 579 | context->SetProxyFromString(test_server_.host_port_pair().ToString()); |
| 580 | context->set_network_delegate(&network_delegate); |
| 581 | context->Init(); |
| 582 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 583 | TestDelegate d; |
| 584 | { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 585 | URLRequest r(GURL("https://www.redirect.com/"), &d); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 586 | r.set_context(context); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 587 | r.Start(); |
| 588 | EXPECT_TRUE(r.is_pending()); |
| 589 | |
| 590 | MessageLoop::current()->Run(); |
| 591 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 592 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 593 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 594 | EXPECT_EQ(1, d.response_started_count()); |
| 595 | // We should not have followed the redirect. |
| 596 | EXPECT_EQ(0, d.received_redirect_count()); |
| 597 | |
| 598 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 599 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 603 | // Tests that the network delegate can block and cancel a request. |
| 604 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequest) { |
| 605 | ASSERT_TRUE(test_server_.Start()); |
| 606 | |
| 607 | TestDelegate d; |
| 608 | BlockingNetworkDelegate network_delegate; |
| 609 | network_delegate.set_callback_retval(ERR_EMPTY_RESPONSE); |
| 610 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 611 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 612 | context->SetProxyFromString(test_server_.host_port_pair().ToString()); |
| 613 | context->set_network_delegate(&network_delegate); |
| 614 | context->Init(); |
| 615 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 616 | { |
| 617 | TestURLRequest r(test_server_.GetURL(""), &d); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 618 | r.set_context(context); |
| 619 | |
| 620 | r.Start(); |
| 621 | MessageLoop::current()->Run(); |
| 622 | |
| 623 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 624 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 625 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 626 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 627 | } |
| 628 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 629 | } |
| 630 | |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 631 | // Tests that the network delegate can cancel a request synchronously. |
| 632 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelRequestSynchronously) { |
| 633 | ASSERT_TRUE(test_server_.Start()); |
| 634 | |
| 635 | TestDelegate d; |
| 636 | BlockingNetworkDelegate network_delegate; |
| 637 | network_delegate.set_retval(ERR_EMPTY_RESPONSE); |
| 638 | |
| 639 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 640 | context->SetProxyFromString(test_server_.host_port_pair().ToString()); |
| 641 | context->set_network_delegate(&network_delegate); |
| 642 | context->Init(); |
| 643 | |
| 644 | { |
| 645 | TestURLRequest r(test_server_.GetURL(""), &d); |
| 646 | r.set_context(context); |
| 647 | |
| 648 | r.Start(); |
| 649 | MessageLoop::current()->Run(); |
| 650 | |
| 651 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 652 | EXPECT_EQ(ERR_EMPTY_RESPONSE, r.status().error()); |
[email protected] | 3cd384c60 | 2011-08-31 16:12:36 | [diff] [blame] | 653 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 654 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 655 | } |
| 656 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 657 | } |
| 658 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 659 | // Tests that the network delegate can block and redirect a request to a new |
| 660 | // URL. |
| 661 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequest) { |
| 662 | ASSERT_TRUE(test_server_.Start()); |
| 663 | |
| 664 | TestDelegate d; |
| 665 | BlockingNetworkDelegate network_delegate; |
| 666 | GURL redirect_url(test_server_.GetURL("simple.html")); |
| 667 | network_delegate.set_redirect_url(redirect_url); |
| 668 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 669 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 670 | context->SetProxyFromString(test_server_.host_port_pair().ToString()); |
| 671 | context->set_network_delegate(&network_delegate); |
| 672 | context->Init(); |
| 673 | |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 674 | { |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 675 | GURL original_url(test_server_.GetURL("empty.html")); |
| 676 | TestURLRequest r(original_url, &d); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 677 | r.set_context(context); |
| 678 | |
| 679 | r.Start(); |
| 680 | MessageLoop::current()->Run(); |
| 681 | |
| 682 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 683 | EXPECT_EQ(0, r.status().error()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 684 | EXPECT_EQ(redirect_url, r.url()); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 685 | EXPECT_EQ(original_url, r.original_url()); |
| 686 | EXPECT_EQ(2U, r.url_chain().size()); |
[email protected] | 4c76d7c | 2011-04-15 19:14:12 | [diff] [blame] | 687 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 688 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 689 | } |
| 690 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 691 | } |
| 692 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 693 | // Tests that redirects caused by the network delegate preserve POST data. |
| 694 | TEST_F(URLRequestTestHTTP, NetworkDelegateRedirectRequestPost) { |
| 695 | ASSERT_TRUE(test_server_.Start()); |
| 696 | |
| 697 | const char kData[] = "hello world"; |
| 698 | |
| 699 | TestDelegate d; |
| 700 | BlockingNetworkDelegate network_delegate; |
| 701 | GURL redirect_url(test_server_.GetURL("echo")); |
| 702 | network_delegate.set_redirect_url(redirect_url); |
| 703 | |
| 704 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 705 | context->set_network_delegate(&network_delegate); |
| 706 | context->Init(); |
| 707 | |
| 708 | { |
| 709 | GURL original_url(test_server_.GetURL("empty.html")); |
| 710 | TestURLRequest r(original_url, &d); |
| 711 | r.set_context(context); |
| 712 | r.set_method("POST"); |
| 713 | r.set_upload(CreateSimpleUploadData(kData).get()); |
| 714 | HttpRequestHeaders headers; |
| 715 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 716 | base::UintToString(arraysize(kData) - 1)); |
| 717 | r.SetExtraRequestHeaders(headers); |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 718 | r.Start(); |
| 719 | MessageLoop::current()->Run(); |
| 720 | |
| 721 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 722 | EXPECT_EQ(0, r.status().error()); |
| 723 | EXPECT_EQ(redirect_url, r.url()); |
| 724 | EXPECT_EQ(original_url, r.original_url()); |
| 725 | EXPECT_EQ(2U, r.url_chain().size()); |
| 726 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 727 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 728 | EXPECT_EQ("POST", r.method()); |
| 729 | EXPECT_EQ(kData, d.data_received()); |
| 730 | } |
| 731 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 732 | } |
| 733 | |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 734 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 735 | // by taking no action. This indicates that the NetworkDelegate does not want to |
| 736 | // handle the challenge, and is passing the buck along to the |
| 737 | // URLRequest::Delegate. |
| 738 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncNoAction) { |
| 739 | ASSERT_TRUE(test_server_.Start()); |
| 740 | |
| 741 | TestDelegate d; |
| 742 | BlockingNetworkDelegate network_delegate; |
| 743 | network_delegate.set_auth_retval( |
| 744 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); |
| 745 | |
| 746 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 747 | context->set_network_delegate(&network_delegate); |
| 748 | context->Init(); |
| 749 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 750 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 751 | |
| 752 | { |
| 753 | GURL url(test_server_.GetURL("auth-basic")); |
| 754 | TestURLRequest r(url, &d); |
| 755 | r.set_context(context); |
| 756 | r.Start(); |
| 757 | MessageLoop::current()->Run(); |
| 758 | |
| 759 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 760 | EXPECT_EQ(0, r.status().error()); |
| 761 | EXPECT_EQ(200, r.GetResponseCode()); |
| 762 | EXPECT_TRUE(d.auth_required_called()); |
| 763 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 764 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 765 | } |
| 766 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 767 | } |
| 768 | |
| 769 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 770 | // by setting credentials. |
| 771 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncSetAuth) { |
| 772 | ASSERT_TRUE(test_server_.Start()); |
| 773 | |
| 774 | TestDelegate d; |
| 775 | BlockingNetworkDelegate network_delegate; |
| 776 | network_delegate.set_auth_retval( |
| 777 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 778 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 779 | network_delegate.set_auth_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 780 | |
| 781 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 782 | context->set_network_delegate(&network_delegate); |
| 783 | context->Init(); |
| 784 | |
| 785 | { |
| 786 | GURL url(test_server_.GetURL("auth-basic")); |
| 787 | TestURLRequest r(url, &d); |
| 788 | r.set_context(context); |
| 789 | r.Start(); |
| 790 | MessageLoop::current()->Run(); |
| 791 | |
| 792 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 793 | EXPECT_EQ(0, r.status().error()); |
| 794 | EXPECT_EQ(200, r.GetResponseCode()); |
| 795 | EXPECT_FALSE(d.auth_required_called()); |
| 796 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 797 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 798 | } |
| 799 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 800 | } |
| 801 | |
| 802 | // Tests that the network delegate can synchronously complete OnAuthRequired |
| 803 | // by cancelling authentication. |
| 804 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredSyncCancel) { |
| 805 | ASSERT_TRUE(test_server_.Start()); |
| 806 | |
| 807 | TestDelegate d; |
| 808 | BlockingNetworkDelegate network_delegate; |
| 809 | network_delegate.set_auth_retval( |
| 810 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 811 | |
| 812 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 813 | context->set_network_delegate(&network_delegate); |
| 814 | context->Init(); |
| 815 | |
| 816 | { |
| 817 | GURL url(test_server_.GetURL("auth-basic")); |
| 818 | TestURLRequest r(url, &d); |
| 819 | r.set_context(context); |
| 820 | r.Start(); |
| 821 | MessageLoop::current()->Run(); |
| 822 | |
| 823 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 824 | EXPECT_EQ(OK, r.status().error()); |
| 825 | EXPECT_EQ(401, r.GetResponseCode()); |
| 826 | EXPECT_FALSE(d.auth_required_called()); |
| 827 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 828 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 829 | } |
| 830 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 831 | } |
| 832 | |
| 833 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 834 | // by taking no action. This indicates that the NetworkDelegate does not want |
| 835 | // to handle the challenge, and is passing the buck along to the |
| 836 | // URLRequest::Delegate. |
| 837 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncNoAction) { |
| 838 | ASSERT_TRUE(test_server_.Start()); |
| 839 | |
| 840 | TestDelegate d; |
| 841 | BlockingNetworkDelegate network_delegate; |
| 842 | network_delegate.set_auth_retval( |
| 843 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); |
| 844 | network_delegate.set_auth_callback_retval( |
| 845 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION); |
| 846 | |
| 847 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 848 | context->set_network_delegate(&network_delegate); |
| 849 | context->Init(); |
| 850 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 851 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 852 | |
| 853 | { |
| 854 | GURL url(test_server_.GetURL("auth-basic")); |
| 855 | TestURLRequest r(url, &d); |
| 856 | r.set_context(context); |
| 857 | r.Start(); |
| 858 | MessageLoop::current()->Run(); |
| 859 | |
| 860 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 861 | EXPECT_EQ(0, r.status().error()); |
| 862 | EXPECT_EQ(200, r.GetResponseCode()); |
| 863 | EXPECT_TRUE(d.auth_required_called()); |
| 864 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 865 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 866 | } |
| 867 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 868 | } |
| 869 | |
| 870 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 871 | // by setting credentials. |
| 872 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncSetAuth) { |
| 873 | ASSERT_TRUE(test_server_.Start()); |
| 874 | |
| 875 | TestDelegate d; |
| 876 | BlockingNetworkDelegate network_delegate; |
| 877 | network_delegate.set_auth_retval( |
| 878 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); |
| 879 | network_delegate.set_auth_callback_retval( |
| 880 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH); |
| 881 | |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 882 | AuthCredentials auth_credentials(kUser, kSecret); |
[email protected] | c2911d7 | 2011-10-03 22:16:36 | [diff] [blame] | 883 | network_delegate.set_auth_credentials(auth_credentials); |
| 884 | |
| 885 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 886 | context->set_network_delegate(&network_delegate); |
| 887 | context->Init(); |
| 888 | |
| 889 | { |
| 890 | GURL url(test_server_.GetURL("auth-basic")); |
| 891 | TestURLRequest r(url, &d); |
| 892 | r.set_context(context); |
| 893 | r.Start(); |
| 894 | MessageLoop::current()->Run(); |
| 895 | |
| 896 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 897 | EXPECT_EQ(0, r.status().error()); |
| 898 | |
| 899 | EXPECT_EQ(200, r.GetResponseCode()); |
| 900 | EXPECT_FALSE(d.auth_required_called()); |
| 901 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 902 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 903 | } |
| 904 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 905 | } |
| 906 | |
| 907 | // Tests that the network delegate can asynchronously complete OnAuthRequired |
| 908 | // by cancelling authentication. |
| 909 | TEST_F(URLRequestTestHTTP, NetworkDelegateOnAuthRequiredAsyncCancel) { |
| 910 | ASSERT_TRUE(test_server_.Start()); |
| 911 | |
| 912 | TestDelegate d; |
| 913 | BlockingNetworkDelegate network_delegate; |
| 914 | network_delegate.set_auth_retval( |
| 915 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING); |
| 916 | network_delegate.set_auth_callback_retval( |
| 917 | NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH); |
| 918 | |
| 919 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 920 | context->set_network_delegate(&network_delegate); |
| 921 | context->Init(); |
| 922 | |
| 923 | { |
| 924 | GURL url(test_server_.GetURL("auth-basic")); |
| 925 | TestURLRequest r(url, &d); |
| 926 | r.set_context(context); |
| 927 | r.Start(); |
| 928 | MessageLoop::current()->Run(); |
| 929 | |
| 930 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 931 | EXPECT_EQ(OK, r.status().error()); |
| 932 | EXPECT_EQ(401, r.GetResponseCode()); |
| 933 | EXPECT_FALSE(d.auth_required_called()); |
| 934 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 935 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 936 | } |
| 937 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 938 | } |
| 939 | |
[email protected] | 9045b882 | 2012-01-13 20:35:35 | [diff] [blame] | 940 | // Tests that we can handle when a network request was canceled while we were |
| 941 | // waiting for the network delegate. |
| 942 | // Part 1: Request is cancelled while waiting for OnBeforeURLRequest callback. |
| 943 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting1) { |
| 944 | ASSERT_TRUE(test_server_.Start()); |
| 945 | |
| 946 | TestDelegate d; |
| 947 | BlockingNetworkDelegateWithManualCallback network_delegate; |
| 948 | network_delegate.BlockOn( |
| 949 | BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); |
| 950 | |
| 951 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 952 | context->set_network_delegate(&network_delegate); |
| 953 | context->Init(); |
| 954 | |
| 955 | { |
| 956 | TestURLRequest r(test_server_.GetURL(""), &d); |
| 957 | r.set_context(context); |
| 958 | |
| 959 | r.Start(); |
| 960 | network_delegate.WaitForState( |
| 961 | BlockingNetworkDelegateWithManualCallback::ON_BEFORE_URL_REQUEST); |
| 962 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 963 | // Cancel before callback. |
| 964 | r.Cancel(); |
| 965 | // Ensure that network delegate is notified. |
| 966 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 967 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 968 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 969 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 970 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 971 | } |
| 972 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 973 | } |
| 974 | |
| 975 | // Tests that we can handle when a network request was canceled while we were |
| 976 | // waiting for the network delegate. |
| 977 | // Part 2: Request is cancelled while waiting for OnBeforeSendHeaders callback. |
| 978 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting2) { |
| 979 | ASSERT_TRUE(test_server_.Start()); |
| 980 | |
| 981 | TestDelegate d; |
| 982 | BlockingNetworkDelegateWithManualCallback network_delegate; |
| 983 | network_delegate.BlockOn( |
| 984 | BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); |
| 985 | |
| 986 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 987 | context->set_network_delegate(&network_delegate); |
| 988 | context->Init(); |
| 989 | |
| 990 | { |
| 991 | TestURLRequest r(test_server_.GetURL(""), &d); |
| 992 | r.set_context(context); |
| 993 | |
| 994 | r.Start(); |
| 995 | network_delegate.WaitForState( |
| 996 | BlockingNetworkDelegateWithManualCallback::ON_BEFORE_SEND_HEADERS); |
| 997 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 998 | // Cancel before callback. |
| 999 | r.Cancel(); |
| 1000 | // Ensure that network delegate is notified. |
| 1001 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 1002 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 1003 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 1004 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 1005 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 1006 | } |
| 1007 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 1008 | } |
| 1009 | |
| 1010 | // Tests that we can handle when a network request was canceled while we were |
| 1011 | // waiting for the network delegate. |
| 1012 | // Part 3: Request is cancelled while waiting for OnHeadersReceived callback. |
| 1013 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting3) { |
| 1014 | ASSERT_TRUE(test_server_.Start()); |
| 1015 | |
| 1016 | TestDelegate d; |
| 1017 | BlockingNetworkDelegateWithManualCallback network_delegate; |
| 1018 | network_delegate.BlockOn( |
| 1019 | BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); |
| 1020 | |
| 1021 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 1022 | context->set_network_delegate(&network_delegate); |
| 1023 | context->Init(); |
| 1024 | |
| 1025 | { |
| 1026 | TestURLRequest r(test_server_.GetURL(""), &d); |
| 1027 | r.set_context(context); |
| 1028 | |
| 1029 | r.Start(); |
| 1030 | network_delegate.WaitForState( |
| 1031 | BlockingNetworkDelegateWithManualCallback::ON_HEADERS_RECEIVED); |
| 1032 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 1033 | // Cancel before callback. |
| 1034 | r.Cancel(); |
| 1035 | // Ensure that network delegate is notified. |
| 1036 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 1037 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 1038 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 1039 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 1040 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 1041 | } |
| 1042 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 1043 | } |
| 1044 | |
| 1045 | // Tests that we can handle when a network request was canceled while we were |
| 1046 | // waiting for the network delegate. |
| 1047 | // Part 4: Request is cancelled while waiting for OnAuthRequired callback. |
| 1048 | TEST_F(URLRequestTestHTTP, NetworkDelegateCancelWhileWaiting4) { |
| 1049 | ASSERT_TRUE(test_server_.Start()); |
| 1050 | |
| 1051 | TestDelegate d; |
| 1052 | BlockingNetworkDelegateWithManualCallback network_delegate; |
| 1053 | network_delegate.BlockOn( |
| 1054 | BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); |
| 1055 | |
| 1056 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 1057 | context->set_network_delegate(&network_delegate); |
| 1058 | context->Init(); |
| 1059 | |
| 1060 | { |
| 1061 | TestURLRequest r(test_server_.GetURL("auth-basic"), &d); |
| 1062 | r.set_context(context); |
| 1063 | |
| 1064 | r.Start(); |
| 1065 | network_delegate.WaitForState( |
| 1066 | BlockingNetworkDelegateWithManualCallback::ON_AUTH_REQUIRED); |
| 1067 | EXPECT_EQ(0, network_delegate.completed_requests()); |
| 1068 | // Cancel before callback. |
| 1069 | r.Cancel(); |
| 1070 | // Ensure that network delegate is notified. |
| 1071 | EXPECT_EQ(1, network_delegate.completed_requests()); |
| 1072 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 1073 | EXPECT_EQ(ERR_ABORTED, r.status().error()); |
| 1074 | EXPECT_EQ(1, network_delegate.created_requests()); |
| 1075 | EXPECT_EQ(0, network_delegate.destroyed_requests()); |
| 1076 | } |
| 1077 | EXPECT_EQ(1, network_delegate.destroyed_requests()); |
| 1078 | } |
| 1079 | |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1080 | // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 1081 | // issuing a CONNECT request with the magic host name "www.server-auth.com". |
| 1082 | // The HTTPTestServer will return a 401 response, which we should balk at. |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 1083 | TEST_F(URLRequestTestHTTP, UnexpectedServerAuthTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1084 | ASSERT_TRUE(test_server_.Start()); |
| 1085 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1086 | TestNetworkDelegate network_delegate; // must outlive URLRequest |
| 1087 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 1088 | context->SetProxyFromString(test_server_.host_port_pair().ToString()); |
| 1089 | context->set_network_delegate(&network_delegate); |
| 1090 | context->Init(); |
| 1091 | |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 1092 | TestDelegate d; |
| 1093 | { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1094 | URLRequest r(GURL("https://www.server-auth.com/"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1095 | r.set_context(context); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 1096 | |
| 1097 | r.Start(); |
| 1098 | EXPECT_TRUE(r.is_pending()); |
| 1099 | |
| 1100 | MessageLoop::current()->Run(); |
| 1101 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1102 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 1103 | EXPECT_EQ(ERR_TUNNEL_CONNECTION_FAILED, r.status().error()); |
[email protected] | dc65178 | 2009-02-14 01:45:08 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 1107 | TEST_F(URLRequestTestHTTP, GetTest_NoCache) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1108 | ASSERT_TRUE(test_server_.Start()); |
| 1109 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1110 | TestDelegate d; |
| 1111 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1112 | TestURLRequest r(test_server_.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1113 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1114 | |
| 1115 | r.Start(); |
| 1116 | EXPECT_TRUE(r.is_pending()); |
| 1117 | |
| 1118 | MessageLoop::current()->Run(); |
| 1119 | |
| 1120 | EXPECT_EQ(1, d.response_started_count()); |
| 1121 | EXPECT_FALSE(d.received_data_before_response()); |
| 1122 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 1123 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 1124 | r.GetSocketAddress().host()); |
| 1125 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 1126 | r.GetSocketAddress().port()); |
[email protected] | c31a5459 | 2009-09-04 02:36:16 | [diff] [blame] | 1127 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1128 | // TODO(eroman): Add back the NetLog tests... |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1129 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1130 | } |
| 1131 | |
[email protected] | b8929021 | 2009-08-14 22:37:35 | [diff] [blame] | 1132 | TEST_F(URLRequestTestHTTP, GetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1133 | ASSERT_TRUE(test_server_.Start()); |
| 1134 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1135 | TestDelegate d; |
| 1136 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1137 | TestURLRequest r(test_server_.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1138 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1139 | |
| 1140 | r.Start(); |
| 1141 | EXPECT_TRUE(r.is_pending()); |
| 1142 | |
| 1143 | MessageLoop::current()->Run(); |
| 1144 | |
| 1145 | EXPECT_EQ(1, d.response_started_count()); |
| 1146 | EXPECT_FALSE(d.received_data_before_response()); |
| 1147 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 1148 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 1149 | r.GetSocketAddress().host()); |
| 1150 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 1151 | r.GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1152 | } |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 1153 | } |
| 1154 | |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 1155 | TEST_F(URLRequestTestHTTP, GetZippedTest) { |
| 1156 | ASSERT_TRUE(test_server_.Start()); |
| 1157 | |
| 1158 | // Parameter that specifies the Content-Length field in the response: |
| 1159 | // C - Compressed length. |
| 1160 | // U - Uncompressed length. |
| 1161 | // L - Large length (larger than both C & U). |
| 1162 | // M - Medium length (between C & U). |
| 1163 | // S - Small length (smaller than both C & U). |
| 1164 | const char test_parameters[] = "CULMS"; |
| 1165 | const int num_tests = arraysize(test_parameters)- 1; // Skip NULL. |
| 1166 | // C & U should be OK. |
[email protected] | f0e2bf4 | 2011-07-22 21:21:44 | [diff] [blame] | 1167 | // L & M are larger than the data sent, and show an error. |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 1168 | // S has too little data, but we seem to accept it. |
| 1169 | const bool test_expect_success[num_tests] = |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1170 | { true, true, false, false, true }; |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 1171 | |
| 1172 | for (int i = 0; i < num_tests ; i++) { |
| 1173 | TestDelegate d; |
| 1174 | { |
| 1175 | std::string test_file = |
| 1176 | base::StringPrintf("compressedfiles/BullRunSpeech.txt?%c", |
| 1177 | test_parameters[i]); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 1178 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1179 | TestNetworkDelegate network_delegate; // must outlive URLRequest |
| 1180 | scoped_refptr<TestURLRequestContext> context( |
| 1181 | new TestURLRequestContext(true)); |
| 1182 | context->set_network_delegate(&network_delegate); |
| 1183 | context->Init(); |
| 1184 | |
| 1185 | TestURLRequest r(test_server_.GetURL(test_file), &d); |
| 1186 | r.set_context(context); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 1187 | r.Start(); |
| 1188 | EXPECT_TRUE(r.is_pending()); |
| 1189 | |
| 1190 | MessageLoop::current()->Run(); |
| 1191 | |
| 1192 | EXPECT_EQ(1, d.response_started_count()); |
| 1193 | EXPECT_FALSE(d.received_data_before_response()); |
| 1194 | VLOG(1) << " Received " << d.bytes_received() << " bytes" |
| 1195 | << " status = " << r.status().status() |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 1196 | << " error = " << r.status().error(); |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 1197 | if (test_expect_success[i]) { |
| 1198 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()) |
| 1199 | << " Parameter = \"" << test_file << "\""; |
| 1200 | } else { |
| 1201 | EXPECT_EQ(URLRequestStatus::FAILED, r.status().status()); |
[email protected] | f001bd6a | 2011-12-08 04:31:37 | [diff] [blame] | 1202 | EXPECT_EQ(-100, r.status().error()) |
[email protected] | aad6357 | 2011-05-24 20:14:39 | [diff] [blame] | 1203 | << " Parameter = \"" << test_file << "\""; |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | |
[email protected] | 4841b77 | 2011-11-04 10:36:06 | [diff] [blame] | 1209 | // This test was disabled because it made chrome_frame_net_tests hang |
| 1210 | // (see bug 102991). |
| 1211 | TEST_F(URLRequestTestHTTP, DISABLED_HTTPSToHTTPRedirectNoRefererTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1212 | ASSERT_TRUE(test_server_.Start()); |
| 1213 | |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 1214 | TestServer https_test_server(TestServer::TYPE_HTTPS, |
| 1215 | TestServer::kLocalhost, |
| 1216 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1217 | ASSERT_TRUE(https_test_server.Start()); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 1218 | |
| 1219 | // An https server is sent a request with an https referer, |
| 1220 | // and responds with a redirect to an http url. The http |
| 1221 | // server should not be sent the referer. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1222 | GURL http_destination = test_server_.GetURL(""); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 1223 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1224 | TestURLRequest req(https_test_server.GetURL( |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 1225 | "server-redirect?" + http_destination.spec()), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1226 | req.set_context(default_context_); |
[email protected] | 7844480a | 2009-12-16 21:18:58 | [diff] [blame] | 1227 | req.set_referrer("https://www.referrer.com/"); |
| 1228 | req.Start(); |
| 1229 | MessageLoop::current()->Run(); |
| 1230 | |
| 1231 | EXPECT_EQ(1, d.response_started_count()); |
| 1232 | EXPECT_EQ(1, d.received_redirect_count()); |
| 1233 | EXPECT_EQ(http_destination, req.url()); |
| 1234 | EXPECT_EQ(std::string(), req.referrer()); |
| 1235 | } |
| 1236 | |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 1237 | TEST_F(URLRequestTestHTTP, MultipleRedirectTest) { |
| 1238 | ASSERT_TRUE(test_server_.Start()); |
| 1239 | |
| 1240 | GURL destination_url = test_server_.GetURL(""); |
| 1241 | GURL middle_redirect_url = test_server_.GetURL( |
| 1242 | "server-redirect?" + destination_url.spec()); |
| 1243 | GURL original_url = test_server_.GetURL( |
| 1244 | "server-redirect?" + middle_redirect_url.spec()); |
| 1245 | TestDelegate d; |
| 1246 | TestURLRequest req(original_url, &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1247 | req.set_context(default_context_); |
[email protected] | 8f1ac08 | 2011-04-19 21:14:13 | [diff] [blame] | 1248 | req.Start(); |
| 1249 | MessageLoop::current()->Run(); |
| 1250 | |
| 1251 | EXPECT_EQ(1, d.response_started_count()); |
| 1252 | EXPECT_EQ(2, d.received_redirect_count()); |
| 1253 | EXPECT_EQ(destination_url, req.url()); |
| 1254 | EXPECT_EQ(original_url, req.original_url()); |
| 1255 | ASSERT_EQ(3U, req.url_chain().size()); |
| 1256 | EXPECT_EQ(original_url, req.url_chain()[0]); |
| 1257 | EXPECT_EQ(middle_redirect_url, req.url_chain()[1]); |
| 1258 | EXPECT_EQ(destination_url, req.url_chain()[2]); |
| 1259 | } |
| 1260 | |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 1261 | class HTTPSRequestTest : public testing::Test { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1262 | public: |
| 1263 | HTTPSRequestTest() : default_context_(new TestURLRequestContext(true)) { |
| 1264 | default_context_->set_network_delegate(&default_network_delegate_); |
| 1265 | default_context_->Init(); |
| 1266 | } |
| 1267 | virtual ~HTTPSRequestTest() {} |
| 1268 | |
| 1269 | protected: |
| 1270 | TestNetworkDelegate default_network_delegate_; // must outlive URLRequest |
| 1271 | scoped_refptr<TestURLRequestContext> default_context_; |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 1272 | }; |
| 1273 | |
[email protected] | 4841b77 | 2011-11-04 10:36:06 | [diff] [blame] | 1274 | // This test was disabled because it made chrome_frame_net_tests hang |
| 1275 | // (see bug 102991). |
| 1276 | TEST_F(HTTPSRequestTest, DISABLED_HTTPSGetTest) { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1277 | TestServer test_server(TestServer::TYPE_HTTPS, |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 1278 | TestServer::kLocalhost, |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1279 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1280 | ASSERT_TRUE(test_server.Start()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 1281 | |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 1282 | TestDelegate d; |
| 1283 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1284 | TestURLRequest r(test_server.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1285 | r.set_context(default_context_); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 1286 | r.Start(); |
| 1287 | EXPECT_TRUE(r.is_pending()); |
| 1288 | |
| 1289 | MessageLoop::current()->Run(); |
| 1290 | |
| 1291 | EXPECT_EQ(1, d.response_started_count()); |
| 1292 | EXPECT_FALSE(d.received_data_before_response()); |
| 1293 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 1294 | CheckSSLInfo(r.ssl_info()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 1295 | EXPECT_EQ(test_server.host_port_pair().host(), |
| 1296 | r.GetSocketAddress().host()); |
| 1297 | EXPECT_EQ(test_server.host_port_pair().port(), |
| 1298 | r.GetSocketAddress().port()); |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 1299 | } |
[email protected] | ea22458 | 2008-12-07 20:25:46 | [diff] [blame] | 1300 | } |
| 1301 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 1302 | TEST_F(HTTPSRequestTest, HTTPSMismatchedTest) { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1303 | TestServer::HTTPSOptions https_options( |
| 1304 | TestServer::HTTPSOptions::CERT_MISMATCHED_NAME); |
| 1305 | TestServer test_server(https_options, |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1306 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1307 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1308 | |
| 1309 | bool err_allowed = true; |
| 1310 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 1311 | TestDelegate d; |
| 1312 | { |
| 1313 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1314 | TestURLRequest r(test_server.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1315 | r.set_context(default_context_); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1316 | |
| 1317 | r.Start(); |
| 1318 | EXPECT_TRUE(r.is_pending()); |
| 1319 | |
| 1320 | MessageLoop::current()->Run(); |
| 1321 | |
| 1322 | EXPECT_EQ(1, d.response_started_count()); |
| 1323 | EXPECT_FALSE(d.received_data_before_response()); |
| 1324 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 1325 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1326 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 1327 | CheckSSLInfo(r.ssl_info()); |
| 1328 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1329 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 1330 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | |
[email protected] | 5774ada | 2010-07-15 06:30:54 | [diff] [blame] | 1335 | TEST_F(HTTPSRequestTest, HTTPSExpiredTest) { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1336 | TestServer::HTTPSOptions https_options( |
| 1337 | TestServer::HTTPSOptions::CERT_EXPIRED); |
| 1338 | TestServer test_server(https_options, |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1339 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1340 | ASSERT_TRUE(test_server.Start()); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1341 | |
| 1342 | // Iterate from false to true, just so that we do the opposite of the |
| 1343 | // previous test in order to increase test coverage. |
| 1344 | bool err_allowed = false; |
| 1345 | for (int i = 0; i < 2 ; i++, err_allowed = !err_allowed) { |
| 1346 | TestDelegate d; |
| 1347 | { |
| 1348 | d.set_allow_certificate_errors(err_allowed); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1349 | TestURLRequest r(test_server.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1350 | r.set_context(default_context_); |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1351 | |
| 1352 | r.Start(); |
| 1353 | EXPECT_TRUE(r.is_pending()); |
| 1354 | |
| 1355 | MessageLoop::current()->Run(); |
| 1356 | |
| 1357 | EXPECT_EQ(1, d.response_started_count()); |
| 1358 | EXPECT_FALSE(d.received_data_before_response()); |
| 1359 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 1360 | if (err_allowed) { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1361 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 1362 | CheckSSLInfo(r.ssl_info()); |
| 1363 | } else { |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1364 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 96adadb | 2010-08-28 01:16:17 | [diff] [blame] | 1365 | } |
[email protected] | bacff65 | 2009-03-31 17:50:33 | [diff] [blame] | 1366 | } |
| 1367 | } |
| 1368 | } |
[email protected] | 73e0bba | 2009-02-19 22:57:09 | [diff] [blame] | 1369 | |
[email protected] | 46d117e | 2012-01-18 01:53:14 | [diff] [blame] | 1370 | // This tests that a load of www.google.com with a certificate error sets |
| 1371 | // the |certificate_errors_are_fatal| flag correctly. This flag will cause |
| 1372 | // the interstitial to be fatal. |
[email protected] | dc694c3 | 2011-11-29 20:00:49 | [diff] [blame] | 1373 | TEST_F(HTTPSRequestTest, HTTPSPreloadedHSTSTest) { |
| 1374 | TestServer::HTTPSOptions https_options( |
| 1375 | TestServer::HTTPSOptions::CERT_MISMATCHED_NAME); |
| 1376 | TestServer test_server(https_options, |
| 1377 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 1378 | ASSERT_TRUE(test_server.Start()); |
| 1379 | |
| 1380 | // We require that the URL be www.google.com in order to pick up the |
| 1381 | // preloaded HSTS entries in the TransportSecurityState. This means that we |
| 1382 | // have to use a MockHostResolver in order to direct www.google.com to the |
| 1383 | // testserver. |
| 1384 | |
| 1385 | MockHostResolver host_resolver; |
| 1386 | host_resolver.rules()->AddRule("www.google.com", "127.0.0.1"); |
| 1387 | TestNetworkDelegate network_delegate; // must outlive URLRequest |
| 1388 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 1389 | context->set_network_delegate(&network_delegate); |
| 1390 | context->set_host_resolver(&host_resolver); |
| 1391 | TransportSecurityState transport_security_state(""); |
| 1392 | context->set_transport_security_state(&transport_security_state); |
| 1393 | context->Init(); |
| 1394 | |
| 1395 | TestDelegate d; |
| 1396 | TestURLRequest r(GURL(StringPrintf("https://www.google.com:%d", |
| 1397 | test_server.host_port_pair().port())), |
| 1398 | &d); |
| 1399 | r.set_context(context); |
| 1400 | |
| 1401 | r.Start(); |
| 1402 | EXPECT_TRUE(r.is_pending()); |
| 1403 | |
| 1404 | MessageLoop::current()->Run(); |
| 1405 | |
| 1406 | EXPECT_EQ(1, d.response_started_count()); |
| 1407 | EXPECT_FALSE(d.received_data_before_response()); |
| 1408 | EXPECT_TRUE(d.have_certificate_errors()); |
[email protected] | 46d117e | 2012-01-18 01:53:14 | [diff] [blame] | 1409 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
[email protected] | dc694c3 | 2011-11-29 20:00:49 | [diff] [blame] | 1410 | } |
| 1411 | |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 1412 | // This tests that cached HTTPS page loads do not cause any updates to the |
| 1413 | // TransportSecurityState. |
[email protected] | 8805685 | 2012-02-15 05:18:20 | [diff] [blame] | 1414 | TEST_F(HTTPSRequestTest, HTTPSErrorsNoClobberTSSTest) { |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 1415 | // The actual problem -- CERT_MISMATCHED_NAME in this case -- doesn't |
| 1416 | // matter. It just has to be any error. |
| 1417 | TestServer::HTTPSOptions https_options( |
| 1418 | TestServer::HTTPSOptions::CERT_MISMATCHED_NAME); |
| 1419 | TestServer test_server(https_options, |
| 1420 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 1421 | ASSERT_TRUE(test_server.Start()); |
| 1422 | |
| 1423 | // We require that the URL be www.google.com in order to pick up the |
| 1424 | // preloaded and dynamic HSTS and public key pin entries in the |
| 1425 | // TransportSecurityState. This means that we have to use a |
| 1426 | // MockHostResolver in order to direct www.google.com to the testserver. |
| 1427 | |
| 1428 | MockHostResolver host_resolver; |
| 1429 | host_resolver.rules()->AddRule("www.google.com", "127.0.0.1"); |
| 1430 | TestNetworkDelegate network_delegate; // must outlive URLRequest |
| 1431 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 1432 | context->set_network_delegate(&network_delegate); |
| 1433 | context->set_host_resolver(&host_resolver); |
| 1434 | TransportSecurityState transport_security_state(""); |
| 1435 | TransportSecurityState::DomainState domain_state; |
| 1436 | EXPECT_TRUE(transport_security_state.HasMetadata(&domain_state, |
| 1437 | "www.google.com", true)); |
| 1438 | context->set_transport_security_state(&transport_security_state); |
| 1439 | context->Init(); |
| 1440 | |
| 1441 | TestDelegate d; |
| 1442 | TestURLRequest r(GURL(StringPrintf("https://www.google.com:%d", |
| 1443 | test_server.host_port_pair().port())), |
| 1444 | &d); |
| 1445 | r.set_context(context); |
| 1446 | |
| 1447 | r.Start(); |
| 1448 | EXPECT_TRUE(r.is_pending()); |
| 1449 | |
| 1450 | MessageLoop::current()->Run(); |
| 1451 | |
| 1452 | EXPECT_EQ(1, d.response_started_count()); |
| 1453 | EXPECT_FALSE(d.received_data_before_response()); |
| 1454 | EXPECT_TRUE(d.have_certificate_errors()); |
| 1455 | EXPECT_TRUE(d.certificate_errors_are_fatal()); |
| 1456 | |
| 1457 | // Get a fresh copy of the state, and check that it hasn't been updated. |
| 1458 | TransportSecurityState::DomainState new_domain_state; |
| 1459 | EXPECT_TRUE(transport_security_state.HasMetadata(&new_domain_state, |
| 1460 | "www.google.com", true)); |
| 1461 | EXPECT_EQ(new_domain_state.mode, domain_state.mode); |
[email protected] | 69d7ff44 | 2012-02-13 22:41:27 | [diff] [blame] | 1462 | EXPECT_EQ(new_domain_state.include_subdomains, |
| 1463 | domain_state.include_subdomains); |
| 1464 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.preloaded_spki_hashes, |
| 1465 | domain_state.preloaded_spki_hashes)); |
| 1466 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.dynamic_spki_hashes, |
| 1467 | domain_state.dynamic_spki_hashes)); |
| 1468 | EXPECT_TRUE(FingerprintsEqual(new_domain_state.bad_preloaded_spki_hashes, |
| 1469 | domain_state.bad_preloaded_spki_hashes)); |
| 1470 | } |
| 1471 | |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 1472 | namespace { |
| 1473 | |
| 1474 | class SSLClientAuthTestDelegate : public TestDelegate { |
| 1475 | public: |
| 1476 | SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { |
| 1477 | } |
| 1478 | virtual void OnCertificateRequested( |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1479 | URLRequest* request, |
| 1480 | SSLCertRequestInfo* cert_request_info) { |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 1481 | on_certificate_requested_count_++; |
| 1482 | MessageLoop::current()->Quit(); |
| 1483 | } |
| 1484 | int on_certificate_requested_count() { |
| 1485 | return on_certificate_requested_count_; |
| 1486 | } |
| 1487 | private: |
| 1488 | int on_certificate_requested_count_; |
| 1489 | }; |
| 1490 | |
| 1491 | } // namespace |
| 1492 | |
| 1493 | // TODO(davidben): Test the rest of the code. Specifically, |
| 1494 | // - Filtering which certificates to select. |
| 1495 | // - Sending a certificate back. |
| 1496 | // - Getting a certificate request in an SSL renegotiation sending the |
| 1497 | // HTTP request. |
| 1498 | TEST_F(HTTPSRequestTest, ClientAuthTest) { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1499 | TestServer::HTTPSOptions https_options; |
[email protected] | 34759995 | 2010-10-28 11:57:36 | [diff] [blame] | 1500 | https_options.request_client_certificate = true; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1501 | TestServer test_server(https_options, |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1502 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1503 | ASSERT_TRUE(test_server.Start()); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 1504 | |
| 1505 | SSLClientAuthTestDelegate d; |
| 1506 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1507 | TestURLRequest r(test_server.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1508 | r.set_context(default_context_); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 1509 | |
| 1510 | r.Start(); |
| 1511 | EXPECT_TRUE(r.is_pending()); |
| 1512 | |
| 1513 | MessageLoop::current()->Run(); |
| 1514 | |
| 1515 | EXPECT_EQ(1, d.on_certificate_requested_count()); |
| 1516 | EXPECT_FALSE(d.received_data_before_response()); |
| 1517 | EXPECT_EQ(0, d.bytes_received()); |
[email protected] | 65a3b91 | 2010-08-21 05:46:58 | [diff] [blame] | 1518 | |
| 1519 | // Send no certificate. |
| 1520 | // TODO(davidben): Get temporary client cert import (with keys) working on |
| 1521 | // all platforms so we can test sending a cert as well. |
| 1522 | r.ContinueWithCertificate(NULL); |
| 1523 | |
| 1524 | MessageLoop::current()->Run(); |
| 1525 | |
| 1526 | EXPECT_EQ(1, d.response_started_count()); |
| 1527 | EXPECT_FALSE(d.received_data_before_response()); |
| 1528 | EXPECT_NE(0, d.bytes_received()); |
[email protected] | 8df162a | 2010-08-07 01:10:02 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
[email protected] | c3456bb | 2011-12-12 22:22:19 | [diff] [blame] | 1532 | TEST_F(HTTPSRequestTest, ResumeTest) { |
| 1533 | // Test that we attempt a session resume when making two connections to the |
| 1534 | // same host. |
| 1535 | TestServer::HTTPSOptions https_options; |
| 1536 | https_options.record_resume = true; |
| 1537 | TestServer test_server(https_options, |
| 1538 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 1539 | ASSERT_TRUE(test_server.Start()); |
| 1540 | |
| 1541 | SSLClientSocket::ClearSessionCache(); |
| 1542 | |
| 1543 | { |
| 1544 | TestDelegate d; |
| 1545 | TestURLRequest r(test_server.GetURL("ssl-session-cache"), &d); |
| 1546 | r.set_context(default_context_); |
| 1547 | |
| 1548 | r.Start(); |
| 1549 | EXPECT_TRUE(r.is_pending()); |
| 1550 | |
| 1551 | MessageLoop::current()->Run(); |
| 1552 | |
| 1553 | EXPECT_EQ(1, d.response_started_count()); |
| 1554 | } |
| 1555 | |
| 1556 | reinterpret_cast<HttpCache*>(default_context_->http_transaction_factory())-> |
| 1557 | CloseAllConnections(); |
| 1558 | |
| 1559 | { |
| 1560 | TestDelegate d; |
| 1561 | TestURLRequest r(test_server.GetURL("ssl-session-cache"), &d); |
| 1562 | r.set_context(default_context_); |
| 1563 | |
| 1564 | r.Start(); |
| 1565 | EXPECT_TRUE(r.is_pending()); |
| 1566 | |
| 1567 | MessageLoop::current()->Run(); |
| 1568 | |
| 1569 | // The response will look like; |
| 1570 | // insert abc |
| 1571 | // lookup abc |
| 1572 | // insert xyz |
| 1573 | // |
| 1574 | // With a newline at the end which makes the split think that there are |
| 1575 | // four lines. |
| 1576 | |
| 1577 | EXPECT_EQ(1, d.response_started_count()); |
| 1578 | std::vector<std::string> lines; |
| 1579 | base::SplitString(d.data_received(), '\n', &lines); |
| 1580 | ASSERT_EQ(4u, lines.size()) << d.data_received(); |
| 1581 | |
| 1582 | std::string session_id; |
| 1583 | |
| 1584 | for (size_t i = 0; i < 2; i++) { |
| 1585 | std::vector<std::string> parts; |
| 1586 | base::SplitString(lines[i], '\t', &parts); |
| 1587 | ASSERT_EQ(2u, parts.size()); |
| 1588 | if (i == 0) { |
| 1589 | EXPECT_EQ("insert", parts[0]); |
| 1590 | session_id = parts[1]; |
| 1591 | } else { |
| 1592 | EXPECT_EQ("lookup", parts[0]); |
| 1593 | EXPECT_EQ(session_id, parts[1]); |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | TEST_F(HTTPSRequestTest, SSLSessionCacheShardTest) { |
| 1600 | // Test that sessions aren't resumed when the value of ssl_session_cache_shard |
| 1601 | // differs. |
| 1602 | TestServer::HTTPSOptions https_options; |
| 1603 | https_options.record_resume = true; |
| 1604 | TestServer test_server(https_options, |
| 1605 | FilePath(FILE_PATH_LITERAL("net/data/ssl"))); |
| 1606 | ASSERT_TRUE(test_server.Start()); |
| 1607 | |
| 1608 | SSLClientSocket::ClearSessionCache(); |
| 1609 | |
| 1610 | { |
| 1611 | TestDelegate d; |
| 1612 | TestURLRequest r(test_server.GetURL("ssl-session-cache"), &d); |
| 1613 | r.set_context(default_context_); |
| 1614 | |
| 1615 | r.Start(); |
| 1616 | EXPECT_TRUE(r.is_pending()); |
| 1617 | |
| 1618 | MessageLoop::current()->Run(); |
| 1619 | |
| 1620 | EXPECT_EQ(1, d.response_started_count()); |
| 1621 | } |
| 1622 | |
| 1623 | // Now create a new HttpCache with a different ssl_session_cache_shard value. |
| 1624 | HttpNetworkSession::Params params; |
| 1625 | params.host_resolver = default_context_->host_resolver(); |
| 1626 | params.cert_verifier = default_context_->cert_verifier(); |
| 1627 | params.proxy_service = default_context_->proxy_service(); |
| 1628 | params.ssl_config_service = default_context_->ssl_config_service(); |
| 1629 | params.http_auth_handler_factory = |
| 1630 | default_context_->http_auth_handler_factory(); |
| 1631 | params.network_delegate = default_context_->network_delegate(); |
| 1632 | params.http_server_properties = default_context_->http_server_properties(); |
| 1633 | params.ssl_session_cache_shard = "alternate"; |
| 1634 | |
| 1635 | scoped_ptr<net::HttpCache> cache(new net::HttpCache( |
| 1636 | new net::HttpNetworkSession(params), |
| 1637 | net::HttpCache::DefaultBackend::InMemory(0))); |
| 1638 | |
| 1639 | default_context_->set_http_transaction_factory(cache.get()); |
| 1640 | |
| 1641 | { |
| 1642 | TestDelegate d; |
| 1643 | TestURLRequest r(test_server.GetURL("ssl-session-cache"), &d); |
| 1644 | r.set_context(default_context_); |
| 1645 | |
| 1646 | r.Start(); |
| 1647 | EXPECT_TRUE(r.is_pending()); |
| 1648 | |
| 1649 | MessageLoop::current()->Run(); |
| 1650 | |
| 1651 | // The response will look like; |
| 1652 | // insert abc |
| 1653 | // insert xyz |
| 1654 | // |
| 1655 | // With a newline at the end which makes the split think that there are |
| 1656 | // three lines. |
| 1657 | |
| 1658 | EXPECT_EQ(1, d.response_started_count()); |
| 1659 | std::vector<std::string> lines; |
| 1660 | base::SplitString(d.data_received(), '\n', &lines); |
| 1661 | ASSERT_EQ(3u, lines.size()); |
| 1662 | |
| 1663 | std::string session_id; |
| 1664 | for (size_t i = 0; i < 2; i++) { |
| 1665 | std::vector<std::string> parts; |
| 1666 | base::SplitString(lines[i], '\t', &parts); |
| 1667 | ASSERT_EQ(2u, parts.size()); |
| 1668 | EXPECT_EQ("insert", parts[0]); |
| 1669 | if (i == 0) { |
| 1670 | session_id = parts[1]; |
| 1671 | } else { |
| 1672 | EXPECT_NE(session_id, parts[1]); |
| 1673 | } |
| 1674 | } |
| 1675 | } |
| 1676 | } |
| 1677 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1678 | TEST_F(URLRequestTestHTTP, CancelTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1679 | TestDelegate d; |
| 1680 | { |
| 1681 | TestURLRequest r(GURL("http://www.google.com/"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1682 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1683 | |
| 1684 | r.Start(); |
| 1685 | EXPECT_TRUE(r.is_pending()); |
| 1686 | |
| 1687 | r.Cancel(); |
| 1688 | |
| 1689 | MessageLoop::current()->Run(); |
| 1690 | |
| 1691 | // We expect to receive OnResponseStarted even though the request has been |
| 1692 | // cancelled. |
| 1693 | EXPECT_EQ(1, d.response_started_count()); |
| 1694 | EXPECT_EQ(0, d.bytes_received()); |
| 1695 | EXPECT_FALSE(d.received_data_before_response()); |
| 1696 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1697 | } |
| 1698 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1699 | TEST_F(URLRequestTestHTTP, CancelTest2) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1700 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1701 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1702 | TestDelegate d; |
| 1703 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1704 | TestURLRequest r(test_server_.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1705 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1706 | |
| 1707 | d.set_cancel_in_response_started(true); |
| 1708 | |
| 1709 | r.Start(); |
| 1710 | EXPECT_TRUE(r.is_pending()); |
| 1711 | |
| 1712 | MessageLoop::current()->Run(); |
| 1713 | |
| 1714 | EXPECT_EQ(1, d.response_started_count()); |
| 1715 | EXPECT_EQ(0, d.bytes_received()); |
| 1716 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1717 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1718 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1719 | } |
| 1720 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1721 | TEST_F(URLRequestTestHTTP, CancelTest3) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1722 | ASSERT_TRUE(test_server_.Start()); |
| 1723 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1724 | TestDelegate d; |
| 1725 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1726 | TestURLRequest r(test_server_.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1727 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1728 | |
| 1729 | d.set_cancel_in_received_data(true); |
| 1730 | |
| 1731 | r.Start(); |
| 1732 | EXPECT_TRUE(r.is_pending()); |
| 1733 | |
| 1734 | MessageLoop::current()->Run(); |
| 1735 | |
| 1736 | EXPECT_EQ(1, d.response_started_count()); |
| 1737 | // There is no guarantee about how much data was received |
| 1738 | // before the cancel was issued. It could have been 0 bytes, |
| 1739 | // or it could have been all the bytes. |
| 1740 | // EXPECT_EQ(0, d.bytes_received()); |
| 1741 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1742 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1743 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1744 | } |
| 1745 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1746 | TEST_F(URLRequestTestHTTP, CancelTest4) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1747 | ASSERT_TRUE(test_server_.Start()); |
| 1748 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1749 | TestDelegate d; |
| 1750 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1751 | TestURLRequest r(test_server_.GetURL(""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1752 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1753 | |
| 1754 | r.Start(); |
| 1755 | EXPECT_TRUE(r.is_pending()); |
| 1756 | |
| 1757 | // The request will be implicitly canceled when it is destroyed. The |
| 1758 | // test delegate must not post a quit message when this happens because |
| 1759 | // this test doesn't actually have a message loop. The quit message would |
| 1760 | // get put on this thread's message queue and the next test would exit |
| 1761 | // early, causing problems. |
| 1762 | d.set_quit_on_complete(false); |
| 1763 | } |
| 1764 | // expect things to just cleanup properly. |
| 1765 | |
| 1766 | // we won't actually get a received reponse here because we've never run the |
| 1767 | // message loop |
| 1768 | EXPECT_FALSE(d.received_data_before_response()); |
| 1769 | EXPECT_EQ(0, d.bytes_received()); |
| 1770 | } |
| 1771 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1772 | TEST_F(URLRequestTestHTTP, CancelTest5) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1773 | ASSERT_TRUE(test_server_.Start()); |
| 1774 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1775 | // populate cache |
| 1776 | { |
| 1777 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1778 | URLRequest r(test_server_.GetURL("cachetime"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1779 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1780 | r.Start(); |
| 1781 | MessageLoop::current()->Run(); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1782 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | // cancel read from cache (see bug 990242) |
| 1786 | { |
| 1787 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1788 | URLRequest r(test_server_.GetURL("cachetime"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1789 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1790 | r.Start(); |
| 1791 | r.Cancel(); |
| 1792 | MessageLoop::current()->Run(); |
| 1793 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1794 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1795 | EXPECT_EQ(1, d.response_started_count()); |
| 1796 | EXPECT_EQ(0, d.bytes_received()); |
| 1797 | EXPECT_FALSE(d.received_data_before_response()); |
| 1798 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1799 | } |
| 1800 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1801 | TEST_F(URLRequestTestHTTP, PostTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1802 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 1803 | HTTPUploadDataOperationTest("POST"); |
| 1804 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1805 | |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 1806 | TEST_F(URLRequestTestHTTP, PutTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1807 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | 762d2db | 2010-01-11 19:03:01 | [diff] [blame] | 1808 | HTTPUploadDataOperationTest("PUT"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1809 | } |
| 1810 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1811 | TEST_F(URLRequestTestHTTP, PostEmptyTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1812 | ASSERT_TRUE(test_server_.Start()); |
| 1813 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1814 | TestDelegate d; |
| 1815 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1816 | TestURLRequest r(test_server_.GetURL("echo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1817 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1818 | r.set_method("POST"); |
| 1819 | |
| 1820 | r.Start(); |
| 1821 | EXPECT_TRUE(r.is_pending()); |
| 1822 | |
| 1823 | MessageLoop::current()->Run(); |
| 1824 | |
| 1825 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 1826 | (int) r.status().status() << ", error: " << r.status().error(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1827 | |
| 1828 | EXPECT_FALSE(d.received_data_before_response()); |
| 1829 | EXPECT_TRUE(d.data_received().empty()); |
| 1830 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1831 | } |
| 1832 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 1833 | TEST_F(URLRequestTestHTTP, PostFileTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1834 | ASSERT_TRUE(test_server_.Start()); |
| 1835 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1836 | TestDelegate d; |
| 1837 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 1838 | TestURLRequest r(test_server_.GetURL("echo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1839 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1840 | r.set_method("POST"); |
| 1841 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 1842 | FilePath dir; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1843 | PathService::Get(base::DIR_EXE, &dir); |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 1844 | file_util::SetCurrentDirectory(dir); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1845 | |
[email protected] | 2b2e84a2c | 2012-01-12 01:56:16 | [diff] [blame] | 1846 | scoped_refptr<UploadData> upload_data(new UploadData); |
| 1847 | |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 1848 | FilePath path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1849 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 1850 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 1851 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 1852 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 1853 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
[email protected] | 2b2e84a2c | 2012-01-12 01:56:16 | [diff] [blame] | 1854 | upload_data->AppendFileRange(path, 0, kuint64max, base::Time()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1855 | |
| 1856 | // This file should just be ignored in the upload stream. |
[email protected] | 2b2e84a2c | 2012-01-12 01:56:16 | [diff] [blame] | 1857 | upload_data->AppendFileRange( |
| 1858 | FilePath(FILE_PATH_LITERAL( |
| 1859 | "c:\\path\\to\\non\\existant\\file.randomness.12345")), |
| 1860 | 0, kuint64max, base::Time()); |
| 1861 | r.set_upload(upload_data); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1862 | |
| 1863 | r.Start(); |
| 1864 | EXPECT_TRUE(r.is_pending()); |
| 1865 | |
| 1866 | MessageLoop::current()->Run(); |
| 1867 | |
[email protected] | 10a1fe9 | 2008-11-04 21:47:02 | [diff] [blame] | 1868 | int64 longsize; |
| 1869 | ASSERT_EQ(true, file_util::GetFileSize(path, &longsize)); |
| 1870 | int size = static_cast<int>(longsize); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1871 | scoped_array<char> buf(new char[size]); |
| 1872 | |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 1873 | int size_read = static_cast<int>(file_util::ReadFile(path, |
| 1874 | buf.get(), size)); |
[email protected] | eac0709a | 2008-11-04 21:00:46 | [diff] [blame] | 1875 | ASSERT_EQ(size, size_read); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1876 | |
| 1877 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 1878 | (int) r.status().status() << ", error: " << r.status().error(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1879 | |
| 1880 | EXPECT_FALSE(d.received_data_before_response()); |
| 1881 | |
| 1882 | ASSERT_EQ(size, d.bytes_received()); |
| 1883 | EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size)); |
| 1884 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1885 | } |
| 1886 | |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 1887 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataBeforeStart) { |
| 1888 | ASSERT_TRUE(test_server_.Start()); |
| 1889 | |
| 1890 | TestDelegate d; |
| 1891 | { |
| 1892 | TestURLRequest r(test_server_.GetURL("echo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1893 | r.set_context(default_context_); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 1894 | r.EnableChunkedUpload(); |
| 1895 | r.set_method("POST"); |
| 1896 | AddChunksToUpload(&r); |
| 1897 | r.Start(); |
| 1898 | EXPECT_TRUE(r.is_pending()); |
| 1899 | |
| 1900 | MessageLoop::current()->Run(); |
| 1901 | |
| 1902 | VerifyReceivedDataMatchesChunks(&r, &d); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | TEST_F(URLRequestTestHTTP, TestPostChunkedDataAfterStart) { |
| 1907 | ASSERT_TRUE(test_server_.Start()); |
| 1908 | |
| 1909 | TestDelegate d; |
| 1910 | { |
| 1911 | TestURLRequest r(test_server_.GetURL("echo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1912 | r.set_context(default_context_); |
[email protected] | 699efe60 | 2011-01-25 07:17:11 | [diff] [blame] | 1913 | r.EnableChunkedUpload(); |
| 1914 | r.set_method("POST"); |
| 1915 | r.Start(); |
| 1916 | EXPECT_TRUE(r.is_pending()); |
| 1917 | |
| 1918 | MessageLoop::current()->RunAllPending(); |
| 1919 | AddChunksToUpload(&r); |
| 1920 | MessageLoop::current()->Run(); |
| 1921 | |
| 1922 | VerifyReceivedDataMatchesChunks(&r, &d); |
| 1923 | } |
| 1924 | } |
| 1925 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 1926 | TEST_F(URLRequestTest, AboutBlankTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1927 | TestDelegate d; |
| 1928 | { |
| 1929 | TestURLRequest r(GURL("about:blank"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1930 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1931 | |
| 1932 | r.Start(); |
| 1933 | EXPECT_TRUE(r.is_pending()); |
| 1934 | |
| 1935 | MessageLoop::current()->Run(); |
| 1936 | |
| 1937 | EXPECT_TRUE(!r.is_pending()); |
| 1938 | EXPECT_FALSE(d.received_data_before_response()); |
| 1939 | EXPECT_EQ(d.bytes_received(), 0); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 1940 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 1941 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1942 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1943 | } |
| 1944 | |
[email protected] | fcb7fd9 | 2009-10-22 04:18:58 | [diff] [blame] | 1945 | TEST_F(URLRequestTest, DataURLImageTest) { |
| 1946 | TestDelegate d; |
| 1947 | { |
| 1948 | // Use our nice little Chrome logo. |
| 1949 | TestURLRequest r(GURL( |
| 1950 | "data:image/png;base64," |
| 1951 | "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADVklEQVQ4jX2TfUwUBBjG3" |
| 1952 | "w1y+HGcd9dxhXR8T4awOccJGgOSWclHImznLkTlSw0DDQXkrmgYgbUYnlQTqQxIEVxitD" |
| 1953 | "5UMCATRA1CEEg+Qjw3bWDxIauJv/5oumqs39/P827vnucRmYN0gyF01GI5MpCVdW0gO7t" |
| 1954 | "vNC+vqSEtbZefk5NuLv1jdJ46p/zw0HeH4+PHr3h7c1mjoV2t5rKzMx1+fg9bAgK6zHq9" |
| 1955 | "cU5z+LpA3xOtx34+vTeT21onRuzssC3zxbbSwC13d/pFuC7CkIMDxQpF7r/MWq12UctI1" |
| 1956 | "dWWm99ypqSYmRUBdKem8MkrO/kgaTt1O7YzlpzE5GIVd0WYUqt57yWf2McHTObYPbVD+Z" |
| 1957 | "wbtlLTVMZ3BW+TnLyXLaWtmEq6WJVbT3HBh3Svj2HQQcm43XwmtoYM6vVKleh0uoWvnzW" |
| 1958 | "3v3MpidruPTQPf0bia7sJOtBM0ufTWNvus/nkDFHF9ZS+uYVjRUasMeHUmyLYtcklTvzW" |
| 1959 | "GFZnNOXczThvpKIzjcahSqIzkvDLayDq6D3eOjtBbNUEIZYyqsvj4V4wY92eNJ4IoyhTb" |
| 1960 | "xXX1T5xsV9tm9r4TQwHLiZw/pdDZJea8TKmsmR/K0uLh/GwnCHghTja6lPhphezPfO5/5" |
| 1961 | "MrVvMzNaI3+ERHfrFzPKQukrQGI4d/3EFD/3E2mVNYvi4at7CXWREaxZGD+3hg28zD3gV" |
| 1962 | "Md6q5c8GdosynKmSeRuGzpjyl1/9UDGtPR5HeaKT8Wjo17WXk579BXVUhN64ehF9fhRtq" |
| 1963 | "/uxxZKzNiZFGD0wRC3NFROZ5mwIPL/96K/rKMMLrIzF9uhHr+/sYH7DAbwlgC4J+R2Z7F" |
| 1964 | "Ux1qLnV7MGF40smVSoJ/jvHRfYhQeUJd/SnYtGWhPHR0Sz+GE2F2yth0B36Vcz2KpnufB" |
| 1965 | "JbsysjjW4kblBUiIjiURUWqJY65zxbnTy57GQyH58zgy0QBtTQv5gH15XMdKkYu+TGaJM" |
| 1966 | "nlm2O34uI4b9tflqp1+QEFGzoW/ulmcofcpkZCYJhDfSpme7QcrHa+Xfji8paEQkTkSfm" |
| 1967 | "moRWRNZr/F1KfVMjW+IKEnv2FwZfKdzt0BQR6lClcZR0EfEXEfv/G6W9iLiIyCoReV5En" |
| 1968 | "hORIBHx+ufPj/gLB/zGI/G4Bk0AAAAASUVORK5CYII="), |
| 1969 | &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1970 | r.set_context(default_context_); |
[email protected] | fcb7fd9 | 2009-10-22 04:18:58 | [diff] [blame] | 1971 | |
| 1972 | r.Start(); |
| 1973 | EXPECT_TRUE(r.is_pending()); |
| 1974 | |
| 1975 | MessageLoop::current()->Run(); |
| 1976 | |
| 1977 | EXPECT_TRUE(!r.is_pending()); |
| 1978 | EXPECT_FALSE(d.received_data_before_response()); |
| 1979 | EXPECT_EQ(d.bytes_received(), 911); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 1980 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 1981 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
[email protected] | fcb7fd9 | 2009-10-22 04:18:58 | [diff] [blame] | 1982 | } |
| 1983 | } |
| 1984 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 1985 | TEST_F(URLRequestTest, FileTest) { |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 1986 | FilePath app_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1987 | PathService::Get(base::FILE_EXE, &app_path); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 1988 | GURL app_url = FilePathToFileURL(app_path); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1989 | |
| 1990 | TestDelegate d; |
| 1991 | { |
[email protected] | b9e04f0 | 2008-11-27 04:03:57 | [diff] [blame] | 1992 | TestURLRequest r(app_url, &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 1993 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 1994 | |
| 1995 | r.Start(); |
| 1996 | EXPECT_TRUE(r.is_pending()); |
| 1997 | |
| 1998 | MessageLoop::current()->Run(); |
| 1999 | |
[email protected] | b77280c | 2009-03-20 17:27:47 | [diff] [blame] | 2000 | int64 file_size = -1; |
| 2001 | EXPECT_TRUE(file_util::GetFileSize(app_path, &file_size)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2002 | |
| 2003 | EXPECT_TRUE(!r.is_pending()); |
| 2004 | EXPECT_EQ(1, d.response_started_count()); |
| 2005 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 2006 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 2007 | EXPECT_EQ("", r.GetSocketAddress().host()); |
| 2008 | EXPECT_EQ(0, r.GetSocketAddress().port()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2009 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2010 | } |
| 2011 | |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2012 | TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 2013 | const size_t buffer_size = 4000; |
| 2014 | scoped_array<char> buffer(new char[buffer_size]); |
| 2015 | FillBuffer(buffer.get(), buffer_size); |
| 2016 | |
| 2017 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 2018 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2019 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 2020 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2021 | |
| 2022 | int64 file_size; |
| 2023 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 2024 | |
| 2025 | const size_t first_byte_position = 500; |
| 2026 | const size_t last_byte_position = buffer_size - first_byte_position; |
| 2027 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 2028 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 2029 | buffer.get() + last_byte_position + 1); |
| 2030 | |
| 2031 | TestDelegate d; |
| 2032 | { |
| 2033 | TestURLRequest r(temp_url, &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2034 | r.set_context(default_context_); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2035 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2036 | HttpRequestHeaders headers; |
| 2037 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 2038 | base::StringPrintf( |
| 2039 | "bytes=%" PRIuS "-%" PRIuS, |
| 2040 | first_byte_position, last_byte_position)); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 2041 | r.SetExtraRequestHeaders(headers); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2042 | r.Start(); |
| 2043 | EXPECT_TRUE(r.is_pending()); |
| 2044 | |
| 2045 | MessageLoop::current()->Run(); |
| 2046 | EXPECT_TRUE(!r.is_pending()); |
| 2047 | EXPECT_EQ(1, d.response_started_count()); |
| 2048 | EXPECT_FALSE(d.received_data_before_response()); |
| 2049 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 2050 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 2051 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 2052 | } |
| 2053 | |
| 2054 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2055 | } |
| 2056 | |
| 2057 | TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 2058 | const size_t buffer_size = 4000; |
| 2059 | scoped_array<char> buffer(new char[buffer_size]); |
| 2060 | FillBuffer(buffer.get(), buffer_size); |
| 2061 | |
| 2062 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 2063 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2064 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 2065 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2066 | |
| 2067 | int64 file_size; |
| 2068 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 2069 | |
| 2070 | const size_t first_byte_position = 500; |
| 2071 | const size_t last_byte_position = buffer_size - 1; |
| 2072 | const size_t content_length = last_byte_position - first_byte_position + 1; |
| 2073 | std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 2074 | buffer.get() + last_byte_position + 1); |
| 2075 | |
| 2076 | TestDelegate d; |
| 2077 | { |
| 2078 | TestURLRequest r(temp_url, &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2079 | r.set_context(default_context_); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2080 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2081 | HttpRequestHeaders headers; |
| 2082 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 2083 | base::StringPrintf("bytes=%" PRIuS "-", |
| 2084 | first_byte_position)); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 2085 | r.SetExtraRequestHeaders(headers); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2086 | r.Start(); |
| 2087 | EXPECT_TRUE(r.is_pending()); |
| 2088 | |
| 2089 | MessageLoop::current()->Run(); |
| 2090 | EXPECT_TRUE(!r.is_pending()); |
| 2091 | EXPECT_EQ(1, d.response_started_count()); |
| 2092 | EXPECT_FALSE(d.received_data_before_response()); |
| 2093 | EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 2094 | // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 2095 | EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 2096 | } |
| 2097 | |
| 2098 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 2102 | const size_t buffer_size = 400000; |
| 2103 | scoped_array<char> buffer(new char[buffer_size]); |
| 2104 | FillBuffer(buffer.get(), buffer_size); |
| 2105 | |
| 2106 | FilePath temp_path; |
[email protected] | 33edeab | 2009-08-18 16:07:55 | [diff] [blame] | 2107 | EXPECT_TRUE(file_util::CreateTemporaryFile(&temp_path)); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2108 | GURL temp_url = FilePathToFileURL(temp_path); |
[email protected] | 1e5ae86 | 2009-10-14 22:14:53 | [diff] [blame] | 2109 | EXPECT_TRUE(file_util::WriteFile(temp_path, buffer.get(), buffer_size)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2110 | |
| 2111 | int64 file_size; |
| 2112 | EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 2113 | |
| 2114 | TestDelegate d; |
| 2115 | { |
| 2116 | TestURLRequest r(temp_url, &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2117 | r.set_context(default_context_); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2118 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2119 | HttpRequestHeaders headers; |
| 2120 | headers.SetHeader(HttpRequestHeaders::kRange, |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 2121 | "bytes=0-0,10-200,200-300"); |
| 2122 | r.SetExtraRequestHeaders(headers); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2123 | r.Start(); |
| 2124 | EXPECT_TRUE(r.is_pending()); |
| 2125 | |
| 2126 | MessageLoop::current()->Run(); |
| 2127 | EXPECT_TRUE(d.request_failed()); |
| 2128 | } |
| 2129 | |
| 2130 | EXPECT_TRUE(file_util::Delete(temp_path, false)); |
[email protected] | 661376a | 2009-04-29 02:04:23 | [diff] [blame] | 2131 | } |
| 2132 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 2133 | TEST_F(URLRequestTest, InvalidUrlTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2134 | TestDelegate d; |
| 2135 | { |
| 2136 | TestURLRequest r(GURL("invalid url"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2137 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2138 | |
| 2139 | r.Start(); |
| 2140 | EXPECT_TRUE(r.is_pending()); |
| 2141 | |
| 2142 | MessageLoop::current()->Run(); |
| 2143 | EXPECT_TRUE(d.request_failed()); |
| 2144 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2145 | } |
| 2146 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2147 | TEST_F(URLRequestTestHTTP, ResponseHeadersTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2148 | ASSERT_TRUE(test_server_.Start()); |
| 2149 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2150 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2151 | TestURLRequest req(test_server_.GetURL("files/with-headers.html"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2152 | req.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2153 | req.Start(); |
| 2154 | MessageLoop::current()->Run(); |
| 2155 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2156 | const HttpResponseHeaders* headers = req.response_headers(); |
[email protected] | 589deddb | 2009-10-05 23:41:40 | [diff] [blame] | 2157 | |
| 2158 | // Simple sanity check that response_info() accesses the same data. |
| 2159 | EXPECT_EQ(headers, req.response_info().headers.get()); |
| 2160 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2161 | std::string header; |
| 2162 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 2163 | EXPECT_EQ("private", header); |
| 2164 | |
| 2165 | header.clear(); |
| 2166 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 2167 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 2168 | |
| 2169 | // The response has two "X-Multiple-Entries" headers. |
| 2170 | // This verfies our output has them concatenated together. |
| 2171 | header.clear(); |
| 2172 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 2173 | EXPECT_EQ("a, b", header); |
| 2174 | } |
| 2175 | |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 2176 | #if defined(OS_WIN) |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 2177 | TEST_F(URLRequestTest, ResolveShortcutTest) { |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2178 | FilePath app_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2179 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2180 | app_path = app_path.AppendASCII("net"); |
| 2181 | app_path = app_path.AppendASCII("data"); |
| 2182 | app_path = app_path.AppendASCII("url_request_unittest"); |
| 2183 | app_path = app_path.AppendASCII("with-headers.html"); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2184 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2185 | std::wstring lnk_path = app_path.value() + L".lnk"; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2186 | |
| 2187 | HRESULT result; |
| 2188 | IShellLink *shell = NULL; |
| 2189 | IPersistFile *persist = NULL; |
| 2190 | |
| 2191 | CoInitialize(NULL); |
| 2192 | // Temporarily create a shortcut for test |
| 2193 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 2194 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 2195 | reinterpret_cast<LPVOID*>(&shell)); |
[email protected] | d149ce8 | 2009-07-01 23:57:02 | [diff] [blame] | 2196 | ASSERT_TRUE(SUCCEEDED(result)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2197 | result = shell->QueryInterface(IID_IPersistFile, |
[email protected] | 5d7b373e | 2009-09-02 07:19:03 | [diff] [blame] | 2198 | reinterpret_cast<LPVOID*>(&persist)); |
[email protected] | d149ce8 | 2009-07-01 23:57:02 | [diff] [blame] | 2199 | ASSERT_TRUE(SUCCEEDED(result)); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2200 | result = shell->SetPath(app_path.value().c_str()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2201 | EXPECT_TRUE(SUCCEEDED(result)); |
| 2202 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 2203 | EXPECT_TRUE(SUCCEEDED(result)); |
| 2204 | result = persist->Save(lnk_path.c_str(), TRUE); |
| 2205 | EXPECT_TRUE(SUCCEEDED(result)); |
| 2206 | if (persist) |
| 2207 | persist->Release(); |
| 2208 | if (shell) |
| 2209 | shell->Release(); |
| 2210 | |
| 2211 | TestDelegate d; |
| 2212 | { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2213 | TestURLRequest r(FilePathToFileURL(FilePath(lnk_path)), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2214 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2215 | |
| 2216 | r.Start(); |
| 2217 | EXPECT_TRUE(r.is_pending()); |
| 2218 | |
| 2219 | MessageLoop::current()->Run(); |
| 2220 | |
| 2221 | WIN32_FILE_ATTRIBUTE_DATA data; |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 2222 | GetFileAttributesEx(app_path.value().c_str(), |
| 2223 | GetFileExInfoStandard, &data); |
| 2224 | HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2225 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 2226 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 2227 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 2228 | scoped_array<char> buffer(new char[data.nFileSizeLow]); |
| 2229 | DWORD read_size; |
| 2230 | BOOL result; |
| 2231 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 2232 | &read_size, NULL); |
| 2233 | std::string content(buffer.get(), read_size); |
| 2234 | CloseHandle(file); |
| 2235 | |
| 2236 | EXPECT_TRUE(!r.is_pending()); |
| 2237 | EXPECT_EQ(1, d.received_redirect_count()); |
| 2238 | EXPECT_EQ(content, d.data_received()); |
| 2239 | } |
| 2240 | |
| 2241 | // Clean the shortcut |
| 2242 | DeleteFile(lnk_path.c_str()); |
| 2243 | CoUninitialize(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2244 | } |
[email protected] | 9396b25 | 2008-09-29 17:29:38 | [diff] [blame] | 2245 | #endif // defined(OS_WIN) |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2246 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2247 | TEST_F(URLRequestTestHTTP, ContentTypeNormalizationTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2248 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2249 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2250 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2251 | TestURLRequest req(test_server_.GetURL( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2252 | "files/content-type-normalization.html"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2253 | req.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2254 | req.Start(); |
| 2255 | MessageLoop::current()->Run(); |
| 2256 | |
| 2257 | std::string mime_type; |
| 2258 | req.GetMimeType(&mime_type); |
| 2259 | EXPECT_EQ("text/html", mime_type); |
| 2260 | |
| 2261 | std::string charset; |
| 2262 | req.GetCharset(&charset); |
| 2263 | EXPECT_EQ("utf-8", charset); |
| 2264 | req.Cancel(); |
| 2265 | } |
| 2266 | |
[email protected] | 7a0bb4bf | 2008-11-19 21:41:48 | [diff] [blame] | 2267 | TEST_F(URLRequestTest, FileDirCancelTest) { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2268 | // Put in mock resource provider. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2269 | NetModule::SetResourceProvider(TestNetResourceProvider); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2270 | |
| 2271 | TestDelegate d; |
| 2272 | { |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 2273 | FilePath file_path; |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2274 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
[email protected] | 72cbd32 | 2009-04-07 10:17:12 | [diff] [blame] | 2275 | file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
| 2276 | file_path = file_path.Append(FILE_PATH_LITERAL("data")); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2277 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2278 | TestURLRequest req(FilePathToFileURL(file_path), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2279 | req.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2280 | req.Start(); |
| 2281 | EXPECT_TRUE(req.is_pending()); |
| 2282 | |
| 2283 | d.set_cancel_in_received_data_pending(true); |
| 2284 | |
| 2285 | MessageLoop::current()->Run(); |
| 2286 | } |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2287 | |
| 2288 | // Take out mock resource provider. |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2289 | NetModule::SetResourceProvider(NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2290 | } |
| 2291 | |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 2292 | TEST_F(URLRequestTest, FileDirRedirectNoCrash) { |
| 2293 | // There is an implicit redirect when loading a file path that matches a |
| 2294 | // directory and does not end with a slash. Ensure that following such |
| 2295 | // redirects does not crash. See http://crbug.com/18686. |
| 2296 | |
| 2297 | FilePath path; |
| 2298 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 2299 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 2300 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 2301 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 2302 | |
| 2303 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2304 | TestURLRequest req(FilePathToFileURL(path), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2305 | req.set_context(default_context_); |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 2306 | req.Start(); |
| 2307 | MessageLoop::current()->Run(); |
| 2308 | |
[email protected] | 0db5577 | 2010-06-14 22:27:44 | [diff] [blame] | 2309 | ASSERT_EQ(1, d.received_redirect_count()); |
| 2310 | ASSERT_LT(0, d.bytes_received()); |
| 2311 | ASSERT_FALSE(d.request_failed()); |
| 2312 | ASSERT_TRUE(req.status().is_success()); |
[email protected] | 7886a8c | 2009-08-21 04:11:09 | [diff] [blame] | 2313 | } |
| 2314 | |
[email protected] | 0db5577 | 2010-06-14 22:27:44 | [diff] [blame] | 2315 | #if defined(OS_WIN) |
| 2316 | // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
| 2317 | TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
| 2318 | TestDelegate d; |
| 2319 | TestURLRequest req(GURL("file:///"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2320 | req.set_context(default_context_); |
[email protected] | 0db5577 | 2010-06-14 22:27:44 | [diff] [blame] | 2321 | req.Start(); |
| 2322 | MessageLoop::current()->Run(); |
| 2323 | |
| 2324 | ASSERT_EQ(1, d.received_redirect_count()); |
| 2325 | ASSERT_FALSE(req.status().is_success()); |
| 2326 | } |
| 2327 | #endif |
| 2328 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2329 | TEST_F(URLRequestTestHTTP, RestrictRedirects) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2330 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 2331 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2332 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2333 | TestURLRequest req(test_server_.GetURL( |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2334 | "files/redirect-to-file.html"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2335 | req.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2336 | req.Start(); |
| 2337 | MessageLoop::current()->Run(); |
| 2338 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2339 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2340 | EXPECT_EQ(ERR_UNSAFE_REDIRECT, req.status().error()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2341 | } |
| 2342 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2343 | TEST_F(URLRequestTestHTTP, RedirectToInvalidURL) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2344 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 2345 | |
| 2346 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2347 | TestURLRequest req(test_server_.GetURL( |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 2348 | "files/redirect-to-invalid-url.html"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2349 | req.set_context(default_context_); |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 2350 | req.Start(); |
| 2351 | MessageLoop::current()->Run(); |
| 2352 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2353 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 2354 | EXPECT_EQ(ERR_INVALID_URL, req.status().error()); |
[email protected] | 4e66ed1 | 2009-07-21 23:38:42 | [diff] [blame] | 2355 | } |
| 2356 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2357 | TEST_F(URLRequestTestHTTP, NoUserPassInReferrer) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2358 | ASSERT_TRUE(test_server_.Start()); |
| 2359 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2360 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2361 | TestURLRequest req(test_server_.GetURL("echoheader?Referer"), &d); |
| 2362 | req.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2363 | req.set_referrer("http://user:[email protected]/"); |
| 2364 | req.Start(); |
| 2365 | MessageLoop::current()->Run(); |
| 2366 | |
| 2367 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 2368 | } |
| 2369 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2370 | TEST_F(URLRequestTestHTTP, CancelRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2371 | ASSERT_TRUE(test_server_.Start()); |
| 2372 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2373 | TestDelegate d; |
| 2374 | { |
| 2375 | d.set_cancel_in_received_redirect(true); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2376 | TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d); |
| 2377 | req.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2378 | req.Start(); |
| 2379 | MessageLoop::current()->Run(); |
| 2380 | |
| 2381 | EXPECT_EQ(1, d.response_started_count()); |
| 2382 | EXPECT_EQ(0, d.bytes_received()); |
| 2383 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2384 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2385 | } |
| 2386 | } |
| 2387 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2388 | TEST_F(URLRequestTestHTTP, DeferredRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2389 | ASSERT_TRUE(test_server_.Start()); |
| 2390 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 2391 | TestDelegate d; |
| 2392 | { |
| 2393 | d.set_quit_on_redirect(true); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2394 | TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d); |
| 2395 | req.set_context(default_context_); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 2396 | req.Start(); |
| 2397 | MessageLoop::current()->Run(); |
| 2398 | |
| 2399 | EXPECT_EQ(1, d.received_redirect_count()); |
| 2400 | |
| 2401 | req.FollowDeferredRedirect(); |
| 2402 | MessageLoop::current()->Run(); |
| 2403 | |
| 2404 | EXPECT_EQ(1, d.response_started_count()); |
| 2405 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2406 | EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 2407 | |
| 2408 | FilePath path; |
| 2409 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 2410 | path = path.Append(FILE_PATH_LITERAL("net")); |
| 2411 | path = path.Append(FILE_PATH_LITERAL("data")); |
| 2412 | path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 2413 | path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 2414 | |
| 2415 | std::string contents; |
| 2416 | EXPECT_TRUE(file_util::ReadFileToString(path, &contents)); |
| 2417 | EXPECT_EQ(contents, d.data_received()); |
| 2418 | } |
| 2419 | } |
| 2420 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2421 | TEST_F(URLRequestTestHTTP, CancelDeferredRedirect) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2422 | ASSERT_TRUE(test_server_.Start()); |
| 2423 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 2424 | TestDelegate d; |
| 2425 | { |
| 2426 | d.set_quit_on_redirect(true); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2427 | TestURLRequest req(test_server_.GetURL("files/redirect-test.html"), &d); |
| 2428 | req.set_context(default_context_); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 2429 | req.Start(); |
| 2430 | MessageLoop::current()->Run(); |
| 2431 | |
| 2432 | EXPECT_EQ(1, d.received_redirect_count()); |
| 2433 | |
| 2434 | req.Cancel(); |
| 2435 | MessageLoop::current()->Run(); |
| 2436 | |
| 2437 | EXPECT_EQ(1, d.response_started_count()); |
| 2438 | EXPECT_EQ(0, d.bytes_received()); |
| 2439 | EXPECT_FALSE(d.received_data_before_response()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2440 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 2444 | TEST_F(URLRequestTestHTTP, VaryHeader) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2445 | ASSERT_TRUE(test_server_.Start()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2446 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2447 | // populate the cache |
| 2448 | { |
| 2449 | TestDelegate d; |
[email protected] | 1bc869d5 | 2011-04-07 17:52:05 | [diff] [blame] | 2450 | URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2451 | req.set_context(default_context_); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2452 | HttpRequestHeaders headers; |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 2453 | headers.SetHeader("foo", "1"); |
| 2454 | req.SetExtraRequestHeaders(headers); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2455 | req.Start(); |
| 2456 | MessageLoop::current()->Run(); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2457 | } |
| 2458 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2459 | // expect a cache hit |
| 2460 | { |
| 2461 | TestDelegate d; |
[email protected] | 1bc869d5 | 2011-04-07 17:52:05 | [diff] [blame] | 2462 | URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2463 | req.set_context(default_context_); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2464 | HttpRequestHeaders headers; |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 2465 | headers.SetHeader("foo", "1"); |
| 2466 | req.SetExtraRequestHeaders(headers); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2467 | req.Start(); |
| 2468 | MessageLoop::current()->Run(); |
| 2469 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 2470 | EXPECT_TRUE(req.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | // expect a cache miss |
| 2474 | { |
| 2475 | TestDelegate d; |
[email protected] | 1bc869d5 | 2011-04-07 17:52:05 | [diff] [blame] | 2476 | URLRequest req(test_server_.GetURL("echoheadercache?foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2477 | req.set_context(default_context_); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2478 | HttpRequestHeaders headers; |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 2479 | headers.SetHeader("foo", "2"); |
| 2480 | req.SetExtraRequestHeaders(headers); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2481 | req.Start(); |
| 2482 | MessageLoop::current()->Run(); |
| 2483 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 2484 | EXPECT_FALSE(req.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2485 | } |
| 2486 | } |
| 2487 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 2488 | TEST_F(URLRequestTestHTTP, BasicAuth) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2489 | ASSERT_TRUE(test_server_.Start()); |
| 2490 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2491 | // populate the cache |
| 2492 | { |
| 2493 | TestDelegate d; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2494 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2495 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2496 | URLRequest r(test_server_.GetURL("auth-basic"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2497 | r.set_context(default_context_); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2498 | r.Start(); |
| 2499 | |
| 2500 | MessageLoop::current()->Run(); |
| 2501 | |
| 2502 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2503 | } |
| 2504 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2505 | // repeat request with end-to-end validation. since auth-basic results in a |
| 2506 | // cachable page, we expect this test to result in a 304. in which case, the |
| 2507 | // response should be fetched from the cache. |
| 2508 | { |
| 2509 | TestDelegate d; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2510 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2511 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2512 | URLRequest r(test_server_.GetURL("auth-basic"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2513 | r.set_context(default_context_); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2514 | r.set_load_flags(LOAD_VALIDATE_CACHE); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2515 | r.Start(); |
| 2516 | |
| 2517 | MessageLoop::current()->Run(); |
| 2518 | |
| 2519 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 2520 | |
[email protected] | cb4ff9d | 2009-09-04 22:51:53 | [diff] [blame] | 2521 | // Should be the same cached document. |
| 2522 | EXPECT_TRUE(r.was_cached()); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 2523 | } |
| 2524 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2525 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2526 | // Check that Set-Cookie headers in 401 responses are respected. |
| 2527 | // http://crbug.com/6450 |
[email protected] | be6fca6c | 2010-01-30 21:48:57 | [diff] [blame] | 2528 | TEST_F(URLRequestTestHTTP, BasicAuthWithCookies) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2529 | ASSERT_TRUE(test_server_.Start()); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2530 | |
| 2531 | GURL url_requiring_auth = |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2532 | test_server_.GetURL("auth-basic?set-cookie-if-challenged"); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2533 | |
| 2534 | // Request a page that will give a 401 containing a Set-Cookie header. |
| 2535 | // Verify that when the transaction is restarted, it includes the new cookie. |
| 2536 | { |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2537 | TestNetworkDelegate network_delegate; // must outlive URLRequest |
| 2538 | scoped_refptr<TestURLRequestContext> context( |
| 2539 | new TestURLRequestContext(true)); |
| 2540 | context->set_network_delegate(&network_delegate); |
| 2541 | context->Init(); |
| 2542 | |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2543 | TestDelegate d; |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 2544 | d.set_credentials(AuthCredentials(kUser, kSecret)); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2545 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2546 | URLRequest r(url_requiring_auth, &d); |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2547 | r.set_context(context); |
| 2548 | r.Start(); |
| 2549 | |
| 2550 | MessageLoop::current()->Run(); |
| 2551 | |
| 2552 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 2553 | |
| 2554 | // Make sure we sent the cookie in the restarted transaction. |
| 2555 | EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 2556 | != std::string::npos); |
| 2557 | } |
[email protected] | 0757e770 | 2009-03-27 04:00:22 | [diff] [blame] | 2558 | } |
| 2559 | |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 2560 | TEST_F(URLRequestTest, DelayedCookieCallback) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2561 | LocalHttpTestServer test_server; |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 2562 | ASSERT_TRUE(test_server.Start()); |
| 2563 | |
| 2564 | scoped_refptr<URLRequestContext> context(new TestURLRequestContext()); |
| 2565 | scoped_refptr<DelayedCookieMonster> delayed_cm = |
| 2566 | new DelayedCookieMonster(); |
| 2567 | scoped_refptr<CookieStore> cookie_store = delayed_cm; |
| 2568 | context->set_cookie_store(delayed_cm); |
| 2569 | |
| 2570 | // Set up a cookie. |
| 2571 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2572 | TestNetworkDelegate network_delegate; |
| 2573 | context->set_network_delegate(&network_delegate); |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 2574 | TestDelegate d; |
| 2575 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d); |
| 2576 | req.set_context(context); |
| 2577 | req.Start(); |
| 2578 | MessageLoop::current()->Run(); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2579 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2580 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2581 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | // Verify that the cookie is set. |
| 2585 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2586 | TestNetworkDelegate network_delegate; |
| 2587 | context->set_network_delegate(&network_delegate); |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 2588 | TestDelegate d; |
| 2589 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
| 2590 | req.set_context(context); |
| 2591 | req.Start(); |
| 2592 | MessageLoop::current()->Run(); |
| 2593 | |
| 2594 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2595 | != std::string::npos); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2596 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2597 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 54f4c936 | 2011-07-25 21:54:46 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | |
[email protected] | be6fca6c | 2010-01-30 21:48:57 | [diff] [blame] | 2601 | TEST_F(URLRequestTest, DoNotSendCookies) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2602 | LocalHttpTestServer test_server; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2603 | ASSERT_TRUE(test_server.Start()); |
| 2604 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2605 | // Set up a cookie. |
| 2606 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2607 | TestNetworkDelegate network_delegate; |
| 2608 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2609 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2610 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2611 | req.set_context(default_context_); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2612 | req.Start(); |
| 2613 | MessageLoop::current()->Run(); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2614 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2615 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | // Verify that the cookie is set. |
| 2619 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2620 | TestNetworkDelegate network_delegate; |
| 2621 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2622 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2623 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2624 | req.set_context(default_context_); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2625 | req.Start(); |
| 2626 | MessageLoop::current()->Run(); |
| 2627 | |
| 2628 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2629 | != std::string::npos); |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2630 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2631 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | // Verify that the cookie isn't sent when LOAD_DO_NOT_SEND_COOKIES is set. |
| 2635 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2636 | TestNetworkDelegate network_delegate; |
| 2637 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2638 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2639 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2640 | req.set_load_flags(LOAD_DO_NOT_SEND_COOKIES); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2641 | req.set_context(default_context_); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2642 | req.Start(); |
| 2643 | MessageLoop::current()->Run(); |
| 2644 | |
| 2645 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2646 | == std::string::npos); |
[email protected] | abf03a0 | 2010-03-12 05:02:13 | [diff] [blame] | 2647 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 2648 | // LOAD_DO_NOT_SEND_COOKIES does not trigger OnGetCookies. |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2649 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2650 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2651 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2652 | |
| 2653 | default_context_->set_network_delegate(&default_network_delegate_); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2654 | } |
| 2655 | |
[email protected] | be6fca6c | 2010-01-30 21:48:57 | [diff] [blame] | 2656 | TEST_F(URLRequestTest, DoNotSaveCookies) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2657 | LocalHttpTestServer test_server; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2658 | ASSERT_TRUE(test_server.Start()); |
| 2659 | |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2660 | // Set up a cookie. |
| 2661 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2662 | TestNetworkDelegate network_delegate; |
| 2663 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2664 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2665 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d); |
| 2666 | req.set_context(default_context_); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2667 | req.Start(); |
| 2668 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2669 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2670 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2671 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2672 | EXPECT_EQ(1, network_delegate.set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | // Try to set-up another cookie and update the previous cookie. |
| 2676 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2677 | TestNetworkDelegate network_delegate; |
| 2678 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2679 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2680 | URLRequest req(test_server.GetURL( |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2681 | "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2682 | req.set_load_flags(LOAD_DO_NOT_SAVE_COOKIES); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2683 | req.set_context(default_context_); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2684 | req.Start(); |
| 2685 | |
| 2686 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2687 | |
[email protected] | 9fb83e8 | 2010-07-02 18:24:55 | [diff] [blame] | 2688 | // LOAD_DO_NOT_SAVE_COOKIES does not trigger OnSetCookie. |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2689 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2690 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2691 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | // Verify the cookies weren't saved or updated. |
| 2695 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2696 | TestNetworkDelegate network_delegate; |
| 2697 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2698 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2699 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2700 | req.set_context(default_context_); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2701 | req.Start(); |
| 2702 | MessageLoop::current()->Run(); |
| 2703 | |
| 2704 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2705 | == std::string::npos); |
| 2706 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2707 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2708 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2709 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2710 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2711 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2712 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2713 | |
| 2714 | default_context_->set_network_delegate(&default_network_delegate_); |
[email protected] | 861fcd5 | 2009-08-26 02:33:46 | [diff] [blame] | 2715 | } |
| 2716 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2717 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2718 | LocalHttpTestServer test_server; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2719 | ASSERT_TRUE(test_server.Start()); |
| 2720 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2721 | // Set up a cookie. |
| 2722 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2723 | TestNetworkDelegate network_delegate; |
| 2724 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2725 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2726 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2727 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2728 | req.Start(); |
| 2729 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2730 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2731 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2732 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | // Verify that the cookie is set. |
| 2736 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2737 | TestNetworkDelegate network_delegate; |
| 2738 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2739 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2740 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2741 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2742 | req.Start(); |
| 2743 | MessageLoop::current()->Run(); |
| 2744 | |
| 2745 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2746 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2747 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2748 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2749 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | // Verify that the cookie isn't sent. |
| 2753 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2754 | TestNetworkDelegate network_delegate; |
| 2755 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2756 | TestDelegate d; |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2757 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2758 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2759 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2760 | req.Start(); |
| 2761 | MessageLoop::current()->Run(); |
| 2762 | |
| 2763 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2764 | == std::string::npos); |
| 2765 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2766 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
| 2767 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2768 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2769 | |
| 2770 | default_context_->set_network_delegate(&default_network_delegate_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2774 | LocalHttpTestServer test_server; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2775 | ASSERT_TRUE(test_server.Start()); |
| 2776 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2777 | // Set up a cookie. |
| 2778 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2779 | TestNetworkDelegate network_delegate; |
| 2780 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2781 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2782 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d); |
| 2783 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2784 | req.Start(); |
| 2785 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2786 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2787 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2788 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | // Try to set-up another cookie and update the previous cookie. |
| 2792 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2793 | TestNetworkDelegate network_delegate; |
| 2794 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2795 | TestDelegate d; |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2796 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2797 | URLRequest req(test_server.GetURL( |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2798 | "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2799 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2800 | req.Start(); |
| 2801 | |
| 2802 | MessageLoop::current()->Run(); |
| 2803 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2804 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2805 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | |
| 2809 | // Verify the cookies weren't saved or updated. |
| 2810 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2811 | TestNetworkDelegate network_delegate; |
| 2812 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2813 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2814 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2815 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2816 | req.Start(); |
| 2817 | MessageLoop::current()->Run(); |
| 2818 | |
| 2819 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2820 | == std::string::npos); |
| 2821 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2822 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2823 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2824 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2825 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2826 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2827 | |
| 2828 | default_context_->set_network_delegate(&default_network_delegate_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2829 | } |
| 2830 | |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 2831 | TEST_F(URLRequestTest, DoNotSaveEmptyCookies) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2832 | LocalHttpTestServer test_server; |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 2833 | ASSERT_TRUE(test_server.Start()); |
| 2834 | |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 2835 | // Set up an empty cookie. |
| 2836 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2837 | TestNetworkDelegate network_delegate; |
| 2838 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 2839 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2840 | URLRequest req(test_server.GetURL("set-cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2841 | req.set_context(default_context_); |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 2842 | req.Start(); |
| 2843 | MessageLoop::current()->Run(); |
| 2844 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2845 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2846 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
| 2847 | EXPECT_EQ(0, network_delegate.set_cookie_count()); |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 2848 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2849 | |
| 2850 | default_context_->set_network_delegate(&default_network_delegate_); |
[email protected] | 2adf288 | 2010-09-27 08:30:37 | [diff] [blame] | 2851 | } |
| 2852 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2853 | TEST_F(URLRequestTest, DoNotSendCookies_ViaPolicy_Async) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2854 | LocalHttpTestServer test_server; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2855 | ASSERT_TRUE(test_server.Start()); |
| 2856 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2857 | // Set up a cookie. |
| 2858 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2859 | TestNetworkDelegate network_delegate; |
| 2860 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2861 | TestDelegate d; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2862 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotSend=1"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2863 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2864 | req.Start(); |
| 2865 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2866 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2867 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2868 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2869 | } |
| 2870 | |
| 2871 | // Verify that the cookie is set. |
| 2872 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2873 | TestNetworkDelegate network_delegate; |
| 2874 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2875 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2876 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2877 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2878 | req.Start(); |
| 2879 | MessageLoop::current()->Run(); |
| 2880 | |
| 2881 | EXPECT_TRUE(d.data_received().find("CookieToNotSend=1") |
| 2882 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2883 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2884 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2885 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | // Verify that the cookie isn't sent. |
| 2889 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2890 | TestNetworkDelegate network_delegate; |
| 2891 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2892 | TestDelegate d; |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2893 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_GET_COOKIES); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2894 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2895 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2896 | req.Start(); |
| 2897 | MessageLoop::current()->Run(); |
| 2898 | |
| 2899 | EXPECT_TRUE(d.data_received().find("Cookie: CookieToNotSend=1") |
| 2900 | == std::string::npos); |
| 2901 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2902 | EXPECT_EQ(1, network_delegate.blocked_get_cookies_count()); |
| 2903 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2904 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2905 | |
| 2906 | default_context_->set_network_delegate(&default_network_delegate_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | TEST_F(URLRequestTest, DoNotSaveCookies_ViaPolicy_Async) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2910 | LocalHttpTestServer test_server; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2911 | ASSERT_TRUE(test_server.Start()); |
| 2912 | |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2913 | // Set up a cookie. |
| 2914 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2915 | TestNetworkDelegate network_delegate; |
| 2916 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2917 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2918 | URLRequest req(test_server.GetURL("set-cookie?CookieToNotUpdate=2"), &d); |
| 2919 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2920 | req.Start(); |
| 2921 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2922 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2923 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2924 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | // Try to set-up another cookie and update the previous cookie. |
| 2928 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2929 | TestNetworkDelegate network_delegate; |
| 2930 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2931 | TestDelegate d; |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2932 | network_delegate.set_cookie_options(TestNetworkDelegate::NO_SET_COOKIE); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2933 | URLRequest req(test_server.GetURL( |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2934 | "set-cookie?CookieToNotSave=1&CookieToNotUpdate=1"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2935 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2936 | req.Start(); |
| 2937 | |
| 2938 | MessageLoop::current()->Run(); |
| 2939 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2940 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2941 | EXPECT_EQ(2, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | // Verify the cookies weren't saved or updated. |
| 2945 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2946 | TestNetworkDelegate network_delegate; |
| 2947 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2948 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2949 | TestURLRequest req(test_server.GetURL("echoheader?Cookie"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2950 | req.set_context(default_context_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2951 | req.Start(); |
| 2952 | MessageLoop::current()->Run(); |
| 2953 | |
| 2954 | EXPECT_TRUE(d.data_received().find("CookieToNotSave=1") |
| 2955 | == std::string::npos); |
| 2956 | EXPECT_TRUE(d.data_received().find("CookieToNotUpdate=2") |
| 2957 | != std::string::npos); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2958 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2959 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2960 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2961 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2962 | |
| 2963 | default_context_->set_network_delegate(&default_network_delegate_); |
[email protected] | 3460228 | 2010-02-03 22:14:15 | [diff] [blame] | 2964 | } |
| 2965 | |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 2966 | void CheckCookiePolicyCallback(bool* was_run, const CookieList& cookies) { |
| 2967 | EXPECT_EQ(1U, cookies.size()); |
| 2968 | EXPECT_FALSE(cookies[0].IsPersistent()); |
| 2969 | *was_run = true; |
[email protected] | 72d27ce | 2011-12-09 00:41:12 | [diff] [blame] | 2970 | MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 2971 | } |
| 2972 | |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 2973 | TEST_F(URLRequestTest, CookiePolicy_ForceSession) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 2974 | LocalHttpTestServer test_server; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 2975 | ASSERT_TRUE(test_server.Start()); |
| 2976 | |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 2977 | // Set up a cookie. |
| 2978 | { |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2979 | TestNetworkDelegate network_delegate; |
| 2980 | default_context_->set_network_delegate(&network_delegate); |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 2981 | TestDelegate d; |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2982 | network_delegate.set_cookie_options(TestNetworkDelegate::FORCE_SESSION); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 2983 | URLRequest req(test_server.GetURL( |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 2984 | "set-cookie?A=1;expires=\"Fri, 05 Feb 2010 23:42:01 GMT\""), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 2985 | req.set_context(default_context_); |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 2986 | req.Start(); // Triggers an asynchronous cookie policy check. |
| 2987 | |
| 2988 | MessageLoop::current()->Run(); |
[email protected] | 3dbb80b | 2010-02-09 22:41:20 | [diff] [blame] | 2989 | |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2990 | EXPECT_EQ(0, network_delegate.blocked_get_cookies_count()); |
| 2991 | EXPECT_EQ(0, network_delegate.blocked_set_cookie_count()); |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 2992 | } |
[email protected] | 9c8ae8c | 2012-03-09 13:13:35 | [diff] [blame] | 2993 | default_context_->set_network_delegate(&default_network_delegate_); |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 2994 | |
| 2995 | // Now, check the cookie store. |
[email protected] | 218aa6a1 | 2011-09-13 17:38:38 | [diff] [blame] | 2996 | bool was_run = false; |
| 2997 | default_context_->cookie_store()->GetCookieMonster()->GetAllCookiesAsync( |
| 2998 | base::Bind(&CheckCookiePolicyCallback, &was_run)); |
| 2999 | MessageLoop::current()->RunAllPending(); |
| 3000 | DCHECK(was_run); |
[email protected] | 4f79b3f | 2010-02-05 04:27:47 | [diff] [blame] | 3001 | } |
| 3002 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 3003 | // In this test, we do a POST which the server will 302 redirect. |
| 3004 | // The subsequent transaction should use GET, and should not send the |
| 3005 | // Content-Type header. |
| 3006 | // http://code.google.com/p/chromium/issues/detail?id=843 |
[email protected] | 3731462 | 2009-08-17 20:29:39 | [diff] [blame] | 3007 | TEST_F(URLRequestTestHTTP, Post302RedirectGet) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3008 | ASSERT_TRUE(test_server_.Start()); |
| 3009 | |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 3010 | const char kData[] = "hello world"; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3011 | |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 3012 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3013 | TestURLRequest req(test_server_.GetURL("files/redirect-to-echoall"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3014 | req.set_context(default_context_); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 3015 | req.set_method("POST"); |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 3016 | req.set_upload(CreateSimpleUploadData(kData)); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 3017 | |
| 3018 | // Set headers (some of which are specific to the POST). |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3019 | HttpRequestHeaders headers; |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 3020 | headers.AddHeadersFromString( |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3021 | "Content-Type: multipart/form-data; " |
| 3022 | "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 3023 | "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 3024 | "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 3025 | "Accept-Language: en-US,en\r\n" |
| 3026 | "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
[email protected] | 195e77d | 2009-07-23 19:10:23 | [diff] [blame] | 3027 | "Content-Length: 11\r\n" |
[email protected] | a86c97cc | 2009-06-24 21:26:27 | [diff] [blame] | 3028 | "Origin: http://localhost:1337/"); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 3029 | req.SetExtraRequestHeaders(headers); |
[email protected] | 71c64f6 | 2008-11-15 04:36:51 | [diff] [blame] | 3030 | req.Start(); |
| 3031 | MessageLoop::current()->Run(); |
| 3032 | |
| 3033 | std::string mime_type; |
| 3034 | req.GetMimeType(&mime_type); |
| 3035 | EXPECT_EQ("text/html", mime_type); |
| 3036 | |
| 3037 | const std::string& data = d.data_received(); |
| 3038 | |
| 3039 | // Check that the post-specific headers were stripped: |
| 3040 | EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 3041 | EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 3042 | EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 3043 | |
| 3044 | // These extra request headers should not have been stripped. |
| 3045 | EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 3046 | EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 3047 | EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 3048 | } |
| 3049 | |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3050 | // The following tests check that we handle mutating the request method for |
[email protected] | bbb011f6 | 2011-11-01 02:32:05 | [diff] [blame] | 3051 | // HTTP redirects as expected. |
| 3052 | // See http://crbug.com/56373 and http://crbug.com/102130. |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3053 | |
| 3054 | TEST_F(URLRequestTestHTTP, Redirect301Tests) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3055 | ASSERT_TRUE(test_server_.Start()); |
| 3056 | |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3057 | const GURL url = test_server_.GetURL("files/redirect301-to-echo"); |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3058 | |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3059 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 3060 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
[email protected] | bbb011f6 | 2011-11-01 02:32:05 | [diff] [blame] | 3061 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | TEST_F(URLRequestTestHTTP, Redirect302Tests) { |
| 3065 | ASSERT_TRUE(test_server_.Start()); |
| 3066 | |
| 3067 | const GURL url = test_server_.GetURL("files/redirect302-to-echo"); |
| 3068 | |
| 3069 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 3070 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
[email protected] | bbb011f6 | 2011-11-01 02:32:05 | [diff] [blame] | 3071 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | TEST_F(URLRequestTestHTTP, Redirect303Tests) { |
| 3075 | ASSERT_TRUE(test_server_.Start()); |
| 3076 | |
| 3077 | const GURL url = test_server_.GetURL("files/redirect303-to-echo"); |
| 3078 | |
| 3079 | HTTPRedirectMethodTest(url, "POST", "GET", true); |
| 3080 | HTTPRedirectMethodTest(url, "PUT", "GET", true); |
[email protected] | bbb011f6 | 2011-11-01 02:32:05 | [diff] [blame] | 3081 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
[email protected] | 2118496 | 2011-10-26 00:50:30 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | TEST_F(URLRequestTestHTTP, Redirect307Tests) { |
| 3085 | ASSERT_TRUE(test_server_.Start()); |
| 3086 | |
| 3087 | const GURL url = test_server_.GetURL("files/redirect307-to-echo"); |
| 3088 | |
| 3089 | HTTPRedirectMethodTest(url, "POST", "POST", true); |
| 3090 | HTTPRedirectMethodTest(url, "PUT", "PUT", true); |
[email protected] | bbb011f6 | 2011-11-01 02:32:05 | [diff] [blame] | 3091 | HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); |
[email protected] | 140932f | 2008-12-12 03:58:06 | [diff] [blame] | 3092 | } |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3093 | |
[email protected] | 3c5ca8c | 2011-09-29 01:14:51 | [diff] [blame] | 3094 | TEST_F(URLRequestTestHTTP, InterceptPost302RedirectGet) { |
| 3095 | ASSERT_TRUE(test_server_.Start()); |
| 3096 | |
| 3097 | const char kData[] = "hello world"; |
| 3098 | |
| 3099 | TestDelegate d; |
| 3100 | TestURLRequest req(test_server_.GetURL("empty.html"), &d); |
| 3101 | req.set_context(default_context_); |
| 3102 | req.set_method("POST"); |
| 3103 | req.set_upload(CreateSimpleUploadData(kData).get()); |
| 3104 | HttpRequestHeaders headers; |
| 3105 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3106 | base::UintToString(arraysize(kData) - 1)); |
| 3107 | req.SetExtraRequestHeaders(headers); |
| 3108 | |
| 3109 | URLRequestRedirectJob* job = |
| 3110 | new URLRequestRedirectJob(&req, test_server_.GetURL("echo")); |
| 3111 | AddTestInterceptor()->set_main_intercept_job(job); |
| 3112 | |
| 3113 | req.Start(); |
| 3114 | MessageLoop::current()->Run(); |
| 3115 | EXPECT_EQ("GET", req.method()); |
| 3116 | } |
| 3117 | |
| 3118 | TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 3119 | ASSERT_TRUE(test_server_.Start()); |
| 3120 | |
| 3121 | const char kData[] = "hello world"; |
| 3122 | |
| 3123 | TestDelegate d; |
| 3124 | TestURLRequest req(test_server_.GetURL("empty.html"), &d); |
| 3125 | req.set_context(default_context_); |
| 3126 | req.set_method("POST"); |
| 3127 | req.set_upload(CreateSimpleUploadData(kData).get()); |
| 3128 | HttpRequestHeaders headers; |
| 3129 | headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3130 | base::UintToString(arraysize(kData) - 1)); |
| 3131 | req.SetExtraRequestHeaders(headers); |
| 3132 | |
| 3133 | URLRequestRedirectJob* job = |
| 3134 | new URLRequestRedirectJob(&req, test_server_.GetURL("echo")); |
| 3135 | job->set_redirect_code( |
| 3136 | URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); |
| 3137 | AddTestInterceptor()->set_main_intercept_job(job); |
| 3138 | |
| 3139 | req.Start(); |
| 3140 | MessageLoop::current()->Run(); |
| 3141 | EXPECT_EQ("POST", req.method()); |
| 3142 | EXPECT_EQ(kData, d.data_received()); |
| 3143 | } |
| 3144 | |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3145 | // Custom URLRequestJobs for use with interceptor tests |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3146 | class RestartTestJob : public URLRequestTestJob { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3147 | public: |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3148 | explicit RestartTestJob(URLRequest* request) |
| 3149 | : URLRequestTestJob(request, true) {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3150 | protected: |
| 3151 | virtual void StartAsync() { |
| 3152 | this->NotifyRestartRequired(); |
| 3153 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 3154 | private: |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 3155 | ~RestartTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3156 | }; |
| 3157 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3158 | class CancelTestJob : public URLRequestTestJob { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3159 | public: |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3160 | explicit CancelTestJob(URLRequest* request) |
| 3161 | : URLRequestTestJob(request, true) {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3162 | protected: |
| 3163 | virtual void StartAsync() { |
| 3164 | request_->Cancel(); |
| 3165 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 3166 | private: |
| 3167 | ~CancelTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3168 | }; |
| 3169 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3170 | class CancelThenRestartTestJob : public URLRequestTestJob { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3171 | public: |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3172 | explicit CancelThenRestartTestJob(URLRequest* request) |
| 3173 | : URLRequestTestJob(request, true) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3174 | } |
| 3175 | protected: |
| 3176 | virtual void StartAsync() { |
| 3177 | request_->Cancel(); |
| 3178 | this->NotifyRestartRequired(); |
| 3179 | } |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 3180 | private: |
| 3181 | ~CancelThenRestartTestJob() {} |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3182 | }; |
| 3183 | |
| 3184 | // An Interceptor for use with interceptor tests |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3185 | class TestInterceptor : URLRequest::Interceptor { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3186 | public: |
| 3187 | TestInterceptor() |
| 3188 | : intercept_main_request_(false), restart_main_request_(false), |
| 3189 | cancel_main_request_(false), cancel_then_restart_main_request_(false), |
| 3190 | simulate_main_network_error_(false), |
| 3191 | intercept_redirect_(false), cancel_redirect_request_(false), |
| 3192 | intercept_final_response_(false), cancel_final_request_(false), |
| 3193 | did_intercept_main_(false), did_restart_main_(false), |
| 3194 | did_cancel_main_(false), did_cancel_then_restart_main_(false), |
| 3195 | did_simulate_error_main_(false), |
| 3196 | did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 3197 | did_intercept_final_(false), did_cancel_final_(false) { |
[email protected] | e353940 | 2011-07-19 09:31:08 | [diff] [blame] | 3198 | URLRequest::Deprecated::RegisterRequestInterceptor(this); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | ~TestInterceptor() { |
[email protected] | e353940 | 2011-07-19 09:31:08 | [diff] [blame] | 3202 | URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3203 | } |
| 3204 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3205 | virtual URLRequestJob* MaybeIntercept(URLRequest* request) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3206 | if (restart_main_request_) { |
| 3207 | restart_main_request_ = false; |
| 3208 | did_restart_main_ = true; |
| 3209 | return new RestartTestJob(request); |
| 3210 | } |
| 3211 | if (cancel_main_request_) { |
| 3212 | cancel_main_request_ = false; |
| 3213 | did_cancel_main_ = true; |
| 3214 | return new CancelTestJob(request); |
| 3215 | } |
| 3216 | if (cancel_then_restart_main_request_) { |
| 3217 | cancel_then_restart_main_request_ = false; |
| 3218 | did_cancel_then_restart_main_ = true; |
| 3219 | return new CancelThenRestartTestJob(request); |
| 3220 | } |
| 3221 | if (simulate_main_network_error_) { |
| 3222 | simulate_main_network_error_ = false; |
| 3223 | did_simulate_error_main_ = true; |
| 3224 | // will error since the requeted url is not one of its canned urls |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3225 | return new URLRequestTestJob(request, true); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3226 | } |
| 3227 | if (!intercept_main_request_) |
| 3228 | return NULL; |
| 3229 | intercept_main_request_ = false; |
| 3230 | did_intercept_main_ = true; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3231 | return new URLRequestTestJob(request, |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 3232 | main_headers_, |
| 3233 | main_data_, |
| 3234 | true); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3235 | } |
| 3236 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3237 | virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request, |
[email protected] | 9acd869e | 2010-12-11 10:18:59 | [diff] [blame] | 3238 | const GURL& location) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3239 | if (cancel_redirect_request_) { |
| 3240 | cancel_redirect_request_ = false; |
| 3241 | did_cancel_redirect_ = true; |
| 3242 | return new CancelTestJob(request); |
| 3243 | } |
| 3244 | if (!intercept_redirect_) |
| 3245 | return NULL; |
| 3246 | intercept_redirect_ = false; |
| 3247 | did_intercept_redirect_ = true; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3248 | return new URLRequestTestJob(request, |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 3249 | redirect_headers_, |
| 3250 | redirect_data_, |
| 3251 | true); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3252 | } |
| 3253 | |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3254 | virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) { |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3255 | if (cancel_final_request_) { |
| 3256 | cancel_final_request_ = false; |
| 3257 | did_cancel_final_ = true; |
| 3258 | return new CancelTestJob(request); |
| 3259 | } |
| 3260 | if (!intercept_final_response_) |
| 3261 | return NULL; |
| 3262 | intercept_final_response_ = false; |
| 3263 | did_intercept_final_ = true; |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3264 | return new URLRequestTestJob(request, |
[email protected] | cbe04ef | 2011-01-11 00:13:24 | [diff] [blame] | 3265 | final_headers_, |
| 3266 | final_data_, |
| 3267 | true); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | // Whether to intercept the main request, and if so the response to return. |
| 3271 | bool intercept_main_request_; |
| 3272 | std::string main_headers_; |
| 3273 | std::string main_data_; |
| 3274 | |
| 3275 | // Other actions we take at MaybeIntercept time |
| 3276 | bool restart_main_request_; |
| 3277 | bool cancel_main_request_; |
| 3278 | bool cancel_then_restart_main_request_; |
| 3279 | bool simulate_main_network_error_; |
| 3280 | |
| 3281 | // Whether to intercept redirects, and if so the response to return. |
| 3282 | bool intercept_redirect_; |
| 3283 | std::string redirect_headers_; |
| 3284 | std::string redirect_data_; |
| 3285 | |
| 3286 | // Other actions we can take at MaybeInterceptRedirect time |
| 3287 | bool cancel_redirect_request_; |
| 3288 | |
| 3289 | // Whether to intercept final response, and if so the response to return. |
| 3290 | bool intercept_final_response_; |
| 3291 | std::string final_headers_; |
| 3292 | std::string final_data_; |
| 3293 | |
| 3294 | // Other actions we can take at MaybeInterceptResponse time |
| 3295 | bool cancel_final_request_; |
| 3296 | |
| 3297 | // If we did something or not |
| 3298 | bool did_intercept_main_; |
| 3299 | bool did_restart_main_; |
| 3300 | bool did_cancel_main_; |
| 3301 | bool did_cancel_then_restart_main_; |
| 3302 | bool did_simulate_error_main_; |
| 3303 | bool did_intercept_redirect_; |
| 3304 | bool did_cancel_redirect_; |
| 3305 | bool did_intercept_final_; |
| 3306 | bool did_cancel_final_; |
| 3307 | |
| 3308 | // Static getters for canned response header and data strings |
| 3309 | |
| 3310 | static std::string ok_data() { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3311 | return URLRequestTestJob::test_data_1(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | static std::string ok_headers() { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3315 | return URLRequestTestJob::test_headers(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | static std::string redirect_data() { |
| 3319 | return std::string(); |
| 3320 | } |
| 3321 | |
| 3322 | static std::string redirect_headers() { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3323 | return URLRequestTestJob::test_redirect_headers(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | static std::string error_data() { |
| 3327 | return std::string("ohhh nooooo mr. bill!"); |
| 3328 | } |
| 3329 | |
| 3330 | static std::string error_headers() { |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3331 | return URLRequestTestJob::test_error_headers(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3332 | } |
| 3333 | }; |
| 3334 | |
| 3335 | TEST_F(URLRequestTest, Intercept) { |
| 3336 | TestInterceptor interceptor; |
| 3337 | |
| 3338 | // intercept the main request and respond with a simple response |
| 3339 | interceptor.intercept_main_request_ = true; |
| 3340 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 3341 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 3342 | |
| 3343 | TestDelegate d; |
| 3344 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3345 | req.set_context(default_context_); |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 3346 | base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data(); |
| 3347 | base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data(); |
| 3348 | base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data(); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3349 | req.SetUserData(NULL, user_data0); |
| 3350 | req.SetUserData(&user_data1, user_data1); |
| 3351 | req.SetUserData(&user_data2, user_data2); |
| 3352 | req.set_method("GET"); |
| 3353 | req.Start(); |
| 3354 | MessageLoop::current()->Run(); |
| 3355 | |
| 3356 | // Make sure we can retrieve our specific user data |
| 3357 | EXPECT_EQ(user_data0, req.GetUserData(NULL)); |
| 3358 | EXPECT_EQ(user_data1, req.GetUserData(&user_data1)); |
| 3359 | EXPECT_EQ(user_data2, req.GetUserData(&user_data2)); |
| 3360 | |
| 3361 | // Check the interceptor got called as expected |
| 3362 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 3363 | |
| 3364 | // Check we got one good response |
| 3365 | EXPECT_TRUE(req.status().is_success()); |
| 3366 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 3367 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 3368 | EXPECT_EQ(1, d.response_started_count()); |
| 3369 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3370 | } |
| 3371 | |
| 3372 | TEST_F(URLRequestTest, InterceptRedirect) { |
| 3373 | TestInterceptor interceptor; |
| 3374 | |
| 3375 | // intercept the main request and respond with a redirect |
| 3376 | interceptor.intercept_main_request_ = true; |
| 3377 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 3378 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 3379 | |
| 3380 | // intercept that redirect and respond a final OK response |
| 3381 | interceptor.intercept_redirect_ = true; |
| 3382 | interceptor.redirect_headers_ = TestInterceptor::ok_headers(); |
| 3383 | interceptor.redirect_data_ = TestInterceptor::ok_data(); |
| 3384 | |
| 3385 | TestDelegate d; |
| 3386 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3387 | req.set_context(default_context_); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3388 | req.set_method("GET"); |
| 3389 | req.Start(); |
| 3390 | MessageLoop::current()->Run(); |
| 3391 | |
| 3392 | // Check the interceptor got called as expected |
| 3393 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 3394 | EXPECT_TRUE(interceptor.did_intercept_redirect_); |
| 3395 | |
| 3396 | // Check we got one good response |
| 3397 | EXPECT_TRUE(req.status().is_success()); |
[email protected] | 4463782 | 2009-08-27 17:01:11 | [diff] [blame] | 3398 | if (req.status().is_success()) { |
| 3399 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 3400 | } |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3401 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 3402 | EXPECT_EQ(1, d.response_started_count()); |
| 3403 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3404 | } |
| 3405 | |
| 3406 | TEST_F(URLRequestTest, InterceptServerError) { |
| 3407 | TestInterceptor interceptor; |
| 3408 | |
| 3409 | // intercept the main request to generate a server error response |
| 3410 | interceptor.intercept_main_request_ = true; |
| 3411 | interceptor.main_headers_ = TestInterceptor::error_headers(); |
| 3412 | interceptor.main_data_ = TestInterceptor::error_data(); |
| 3413 | |
| 3414 | // intercept that error and respond with an OK response |
| 3415 | interceptor.intercept_final_response_ = true; |
| 3416 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 3417 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 3418 | |
| 3419 | TestDelegate d; |
| 3420 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3421 | req.set_context(default_context_); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3422 | req.set_method("GET"); |
| 3423 | req.Start(); |
| 3424 | MessageLoop::current()->Run(); |
| 3425 | |
| 3426 | // Check the interceptor got called as expected |
| 3427 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 3428 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 3429 | |
| 3430 | // Check we got one good response |
| 3431 | EXPECT_TRUE(req.status().is_success()); |
| 3432 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 3433 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 3434 | EXPECT_EQ(1, d.response_started_count()); |
| 3435 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3436 | } |
| 3437 | |
| 3438 | TEST_F(URLRequestTest, InterceptNetworkError) { |
| 3439 | TestInterceptor interceptor; |
| 3440 | |
| 3441 | // intercept the main request to simulate a network error |
| 3442 | interceptor.simulate_main_network_error_ = true; |
| 3443 | |
| 3444 | // intercept that error and respond with an OK response |
| 3445 | interceptor.intercept_final_response_ = true; |
| 3446 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 3447 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 3448 | |
| 3449 | TestDelegate d; |
| 3450 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3451 | req.set_context(default_context_); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3452 | req.set_method("GET"); |
| 3453 | req.Start(); |
| 3454 | MessageLoop::current()->Run(); |
| 3455 | |
| 3456 | // Check the interceptor got called as expected |
| 3457 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 3458 | EXPECT_TRUE(interceptor.did_intercept_final_); |
| 3459 | |
| 3460 | // Check we received one good response |
| 3461 | EXPECT_TRUE(req.status().is_success()); |
| 3462 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 3463 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 3464 | EXPECT_EQ(1, d.response_started_count()); |
| 3465 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3466 | } |
| 3467 | |
| 3468 | TEST_F(URLRequestTest, InterceptRestartRequired) { |
| 3469 | TestInterceptor interceptor; |
| 3470 | |
| 3471 | // restart the main request |
| 3472 | interceptor.restart_main_request_ = true; |
| 3473 | |
| 3474 | // then intercept the new main request and respond with an OK response |
| 3475 | interceptor.intercept_main_request_ = true; |
| 3476 | interceptor.main_headers_ = TestInterceptor::ok_headers(); |
| 3477 | interceptor.main_data_ = TestInterceptor::ok_data(); |
| 3478 | |
| 3479 | TestDelegate d; |
| 3480 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3481 | req.set_context(default_context_); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3482 | req.set_method("GET"); |
| 3483 | req.Start(); |
| 3484 | MessageLoop::current()->Run(); |
| 3485 | |
| 3486 | // Check the interceptor got called as expected |
| 3487 | EXPECT_TRUE(interceptor.did_restart_main_); |
| 3488 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 3489 | |
| 3490 | // Check we received one good response |
| 3491 | EXPECT_TRUE(req.status().is_success()); |
[email protected] | 4463782 | 2009-08-27 17:01:11 | [diff] [blame] | 3492 | if (req.status().is_success()) { |
| 3493 | EXPECT_EQ(200, req.response_headers()->response_code()); |
| 3494 | } |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3495 | EXPECT_EQ(TestInterceptor::ok_data(), d.data_received()); |
| 3496 | EXPECT_EQ(1, d.response_started_count()); |
| 3497 | EXPECT_EQ(0, d.received_redirect_count()); |
| 3498 | } |
| 3499 | |
| 3500 | TEST_F(URLRequestTest, InterceptRespectsCancelMain) { |
| 3501 | TestInterceptor interceptor; |
| 3502 | |
| 3503 | // intercept the main request and cancel from within the restarted job |
| 3504 | interceptor.cancel_main_request_ = true; |
| 3505 | |
| 3506 | // setup to intercept final response and override it with an OK response |
| 3507 | interceptor.intercept_final_response_ = true; |
| 3508 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 3509 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 3510 | |
| 3511 | TestDelegate d; |
| 3512 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3513 | req.set_context(default_context_); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3514 | req.set_method("GET"); |
| 3515 | req.Start(); |
| 3516 | MessageLoop::current()->Run(); |
| 3517 | |
| 3518 | // Check the interceptor got called as expected |
| 3519 | EXPECT_TRUE(interceptor.did_cancel_main_); |
| 3520 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 3521 | |
| 3522 | // Check we see a canceled request |
| 3523 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3524 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) { |
| 3528 | TestInterceptor interceptor; |
| 3529 | |
| 3530 | // intercept the main request and respond with a redirect |
| 3531 | interceptor.intercept_main_request_ = true; |
| 3532 | interceptor.main_headers_ = TestInterceptor::redirect_headers(); |
| 3533 | interceptor.main_data_ = TestInterceptor::redirect_data(); |
| 3534 | |
| 3535 | // intercept the redirect and cancel from within that job |
| 3536 | interceptor.cancel_redirect_request_ = true; |
| 3537 | |
| 3538 | // setup to intercept final response and override it with an OK response |
| 3539 | interceptor.intercept_final_response_ = true; |
| 3540 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 3541 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 3542 | |
| 3543 | TestDelegate d; |
| 3544 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3545 | req.set_context(default_context_); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3546 | req.set_method("GET"); |
| 3547 | req.Start(); |
| 3548 | MessageLoop::current()->Run(); |
| 3549 | |
| 3550 | // Check the interceptor got called as expected |
| 3551 | EXPECT_TRUE(interceptor.did_intercept_main_); |
| 3552 | EXPECT_TRUE(interceptor.did_cancel_redirect_); |
| 3553 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 3554 | |
| 3555 | // Check we see a canceled request |
| 3556 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3557 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | TEST_F(URLRequestTest, InterceptRespectsCancelFinal) { |
| 3561 | TestInterceptor interceptor; |
| 3562 | |
| 3563 | // intercept the main request to simulate a network error |
| 3564 | interceptor.simulate_main_network_error_ = true; |
| 3565 | |
| 3566 | // setup to intercept final response and cancel from within that job |
| 3567 | interceptor.cancel_final_request_ = true; |
| 3568 | |
| 3569 | TestDelegate d; |
| 3570 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3571 | req.set_context(default_context_); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3572 | req.set_method("GET"); |
| 3573 | req.Start(); |
| 3574 | MessageLoop::current()->Run(); |
| 3575 | |
| 3576 | // Check the interceptor got called as expected |
| 3577 | EXPECT_TRUE(interceptor.did_simulate_error_main_); |
| 3578 | EXPECT_TRUE(interceptor.did_cancel_final_); |
| 3579 | |
| 3580 | // Check we see a canceled request |
| 3581 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3582 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) { |
| 3586 | TestInterceptor interceptor; |
| 3587 | |
| 3588 | // intercept the main request and cancel then restart from within that job |
| 3589 | interceptor.cancel_then_restart_main_request_ = true; |
| 3590 | |
| 3591 | // setup to intercept final response and override it with an OK response |
| 3592 | interceptor.intercept_final_response_ = true; |
| 3593 | interceptor.final_headers_ = TestInterceptor::ok_headers(); |
| 3594 | interceptor.final_data_ = TestInterceptor::ok_data(); |
| 3595 | |
| 3596 | TestDelegate d; |
| 3597 | TestURLRequest req(GURL("http://test_intercept/foo"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3598 | req.set_context(default_context_); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3599 | req.set_method("GET"); |
| 3600 | req.Start(); |
| 3601 | MessageLoop::current()->Run(); |
| 3602 | |
| 3603 | // Check the interceptor got called as expected |
| 3604 | EXPECT_TRUE(interceptor.did_cancel_then_restart_main_); |
| 3605 | EXPECT_FALSE(interceptor.did_intercept_final_); |
| 3606 | |
| 3607 | // Check we see a canceled request |
| 3608 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3609 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
[email protected] | a5c713f | 2009-04-16 21:05:47 | [diff] [blame] | 3610 | } |
| 3611 | |
[email protected] | f4f2f51 | 2011-02-17 09:28:45 | [diff] [blame] | 3612 | // Check that two different URL requests have different identifiers. |
| 3613 | TEST_F(URLRequestTest, Identifiers) { |
| 3614 | TestDelegate d; |
| 3615 | TestURLRequest req(GURL("http://example.com"), &d); |
| 3616 | TestURLRequest other_req(GURL("http://example.com"), &d); |
| 3617 | |
| 3618 | ASSERT_NE(req.identifier(), other_req.identifier()); |
| 3619 | } |
| 3620 | |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3621 | // Check that a failure to connect to the proxy is reported to the network |
| 3622 | // delegate. |
| 3623 | TEST_F(URLRequestTest, NetworkDelegateProxyError) { |
[email protected] | e74aa63 | 2011-07-14 17:10:16 | [diff] [blame] | 3624 | MockHostResolver host_resolver; |
| 3625 | host_resolver.rules()->AddSimulatedFailure("*"); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3626 | |
| 3627 | TestNetworkDelegate network_delegate; // must outlive URLRequests |
| 3628 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3629 | context->set_network_delegate(&network_delegate); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3630 | context->SetProxyFromString("myproxy:70"); |
[email protected] | e74aa63 | 2011-07-14 17:10:16 | [diff] [blame] | 3631 | context->set_host_resolver(&host_resolver); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3632 | context->Init(); |
| 3633 | |
| 3634 | TestDelegate d; |
| 3635 | TestURLRequest req(GURL("http://example.com"), &d); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3636 | req.set_context(context); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3637 | req.set_method("GET"); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3638 | |
| 3639 | req.Start(); |
| 3640 | MessageLoop::current()->Run(); |
| 3641 | |
| 3642 | // Check we see a failed request. |
| 3643 | EXPECT_FALSE(req.status().is_success()); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3644 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 3645 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, req.status().error()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3646 | |
| 3647 | EXPECT_EQ(1, network_delegate.error_count()); |
[email protected] | d0cc35b | 2011-09-08 12:02:05 | [diff] [blame] | 3648 | EXPECT_EQ(ERR_PROXY_CONNECTION_FAILED, network_delegate.last_error()); |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 3649 | EXPECT_EQ(1, network_delegate.completed_requests()); |
[email protected] | 8202d0c | 2011-02-23 08:31:14 | [diff] [blame] | 3650 | } |
| 3651 | |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 3652 | // Check that it is impossible to change the referrer in the extra headers of |
| 3653 | // an URLRequest. |
| 3654 | TEST_F(URLRequestTest, DoNotOverrideReferrer) { |
[email protected] | 1700c6a | 2012-02-22 18:07:07 | [diff] [blame] | 3655 | LocalHttpTestServer test_server; |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 3656 | ASSERT_TRUE(test_server.Start()); |
| 3657 | |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 3658 | // If extra headers contain referer and the request contains a referer, |
| 3659 | // only the latter shall be respected. |
| 3660 | { |
| 3661 | TestDelegate d; |
| 3662 | TestURLRequest req(test_server.GetURL("echoheader?Referer"), &d); |
| 3663 | req.set_referrer("http://foo.com/"); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3664 | req.set_context(default_context_); |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 3665 | |
| 3666 | HttpRequestHeaders headers; |
| 3667 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
| 3668 | req.SetExtraRequestHeaders(headers); |
| 3669 | |
| 3670 | req.Start(); |
| 3671 | MessageLoop::current()->Run(); |
| 3672 | |
| 3673 | EXPECT_EQ("http://foo.com/", d.data_received()); |
| 3674 | } |
| 3675 | |
| 3676 | // If extra headers contain a referer but the request does not, no referer |
| 3677 | // shall be sent in the header. |
| 3678 | { |
| 3679 | TestDelegate d; |
| 3680 | TestURLRequest req(test_server.GetURL("echoheader?Referer"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3681 | req.set_context(default_context_); |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 3682 | |
| 3683 | HttpRequestHeaders headers; |
| 3684 | headers.SetHeader(HttpRequestHeaders::kReferer, "http://bar.com/"); |
| 3685 | req.SetExtraRequestHeaders(headers); |
[email protected] | 52c28759 | 2011-06-28 17:39:10 | [diff] [blame] | 3686 | req.set_load_flags(LOAD_VALIDATE_CACHE); |
[email protected] | c1045010 | 2011-06-27 09:06:16 | [diff] [blame] | 3687 | |
| 3688 | req.Start(); |
| 3689 | MessageLoop::current()->Run(); |
| 3690 | |
| 3691 | EXPECT_EQ("None", d.data_received()); |
| 3692 | } |
| 3693 | } |
| 3694 | |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 3695 | // Make sure that net::NetworkDelegate::NotifyCompleted is called if |
| 3696 | // content is empty. |
| 3697 | TEST_F(URLRequestTest, RequestCompletionForEmptyResponse) { |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 3698 | TestDelegate d; |
| 3699 | TestURLRequest req(GURL("data:,"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3700 | req.set_context(new TestURLRequestContext()); |
| 3701 | req.set_context(default_context_); |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 3702 | req.Start(); |
| 3703 | MessageLoop::current()->Run(); |
| 3704 | EXPECT_EQ("", d.data_received()); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3705 | EXPECT_EQ(1, default_network_delegate_.completed_requests()); |
[email protected] | a83dd33 | 2011-07-13 10:41:01 | [diff] [blame] | 3706 | } |
| 3707 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 3708 | class URLRequestTestFTP : public URLRequestTest { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3709 | public: |
[email protected] | d9fca99a | 2012-02-24 16:16:20 | [diff] [blame] | 3710 | URLRequestTestFTP() |
| 3711 | : test_server_(TestServer::TYPE_FTP, TestServer::kLocalhost, FilePath()) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3712 | } |
| 3713 | |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 3714 | protected: |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 3715 | TestServer test_server_; |
[email protected] | b89ca03 | 2009-08-31 21:41:31 | [diff] [blame] | 3716 | }; |
| 3717 | |
[email protected] | 5accf733 | 2009-11-24 03:41:38 | [diff] [blame] | 3718 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3719 | TEST_F(URLRequestTestFTP, DISABLED_FTPDirectoryListing) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3720 | ASSERT_TRUE(test_server_.Start()); |
| 3721 | |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 3722 | TestDelegate d; |
| 3723 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3724 | TestURLRequest r(test_server_.GetURL("/"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3725 | r.set_context(default_context_); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 3726 | r.Start(); |
| 3727 | EXPECT_TRUE(r.is_pending()); |
| 3728 | |
| 3729 | MessageLoop::current()->Run(); |
| 3730 | |
| 3731 | EXPECT_FALSE(r.is_pending()); |
| 3732 | EXPECT_EQ(1, d.response_started_count()); |
| 3733 | EXPECT_FALSE(d.received_data_before_response()); |
| 3734 | EXPECT_LT(0, d.bytes_received()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3735 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 3736 | r.GetSocketAddress().host()); |
| 3737 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 3738 | r.GetSocketAddress().port()); |
[email protected] | a25e90e | 2009-09-09 17:05:37 | [diff] [blame] | 3739 | } |
| 3740 | } |
| 3741 | |
[email protected] | 7df7001 | 2010-02-04 00:09:55 | [diff] [blame] | 3742 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3743 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTestAnonymous) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3744 | ASSERT_TRUE(test_server_.Start()); |
| 3745 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 3746 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3747 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 3748 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3749 | TestDelegate d; |
| 3750 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3751 | TestURLRequest r(test_server_.GetURL("/LICENSE"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3752 | r.set_context(default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3753 | r.Start(); |
| 3754 | EXPECT_TRUE(r.is_pending()); |
| 3755 | |
| 3756 | MessageLoop::current()->Run(); |
| 3757 | |
| 3758 | int64 file_size = 0; |
| 3759 | file_util::GetFileSize(app_path, &file_size); |
| 3760 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 3761 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3762 | EXPECT_EQ(1, d.response_started_count()); |
| 3763 | EXPECT_FALSE(d.received_data_before_response()); |
| 3764 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3765 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 3766 | r.GetSocketAddress().host()); |
| 3767 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 3768 | r.GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3769 | } |
| 3770 | } |
| 3771 | |
[email protected] | e9ecbd1 | 2009-12-20 18:44:40 | [diff] [blame] | 3772 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3773 | TEST_F(URLRequestTestFTP, DISABLED_FTPGetTest) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3774 | ASSERT_TRUE(test_server_.Start()); |
| 3775 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 3776 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3777 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 3778 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3779 | TestDelegate d; |
| 3780 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3781 | TestURLRequest r( |
| 3782 | test_server_.GetURLWithUserAndPassword("/LICENSE", "chrome", "chrome"), |
| 3783 | &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3784 | r.set_context(default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3785 | r.Start(); |
| 3786 | EXPECT_TRUE(r.is_pending()); |
| 3787 | |
| 3788 | MessageLoop::current()->Run(); |
| 3789 | |
| 3790 | int64 file_size = 0; |
| 3791 | file_util::GetFileSize(app_path, &file_size); |
| 3792 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 3793 | EXPECT_FALSE(r.is_pending()); |
[email protected] | 6d81b48 | 2011-02-22 19:47:19 | [diff] [blame] | 3794 | EXPECT_EQ(test_server_.host_port_pair().host(), |
| 3795 | r.GetSocketAddress().host()); |
| 3796 | EXPECT_EQ(test_server_.host_port_pair().port(), |
| 3797 | r.GetSocketAddress().port()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3798 | EXPECT_EQ(1, d.response_started_count()); |
| 3799 | EXPECT_FALSE(d.received_data_before_response()); |
| 3800 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 3801 | } |
| 3802 | } |
| 3803 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 3804 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3805 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPassword) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3806 | ASSERT_TRUE(test_server_.Start()); |
| 3807 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 3808 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3809 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 3810 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3811 | TestDelegate d; |
| 3812 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3813 | TestURLRequest r( |
| 3814 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 3815 | "chrome", |
| 3816 | "wrong_password"), |
| 3817 | &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3818 | r.set_context(default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3819 | r.Start(); |
| 3820 | EXPECT_TRUE(r.is_pending()); |
| 3821 | |
| 3822 | MessageLoop::current()->Run(); |
| 3823 | |
| 3824 | int64 file_size = 0; |
| 3825 | file_util::GetFileSize(app_path, &file_size); |
| 3826 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 3827 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3828 | EXPECT_EQ(1, d.response_started_count()); |
| 3829 | EXPECT_FALSE(d.received_data_before_response()); |
| 3830 | EXPECT_EQ(d.bytes_received(), 0); |
| 3831 | } |
| 3832 | } |
| 3833 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 3834 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3835 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongPasswordRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3836 | ASSERT_TRUE(test_server_.Start()); |
| 3837 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 3838 | FilePath app_path; |
| 3839 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 3840 | app_path = app_path.AppendASCII("LICENSE"); |
| 3841 | TestDelegate d; |
| 3842 | // Set correct login credentials. The delegate will be asked for them when |
| 3843 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3844 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 3845 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3846 | TestURLRequest r( |
| 3847 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 3848 | "chrome", |
| 3849 | "wrong_password"), |
| 3850 | &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3851 | r.set_context(default_context_); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 3852 | r.Start(); |
| 3853 | EXPECT_TRUE(r.is_pending()); |
| 3854 | |
| 3855 | MessageLoop::current()->Run(); |
| 3856 | |
| 3857 | int64 file_size = 0; |
| 3858 | file_util::GetFileSize(app_path, &file_size); |
| 3859 | |
| 3860 | EXPECT_FALSE(r.is_pending()); |
| 3861 | EXPECT_EQ(1, d.response_started_count()); |
| 3862 | EXPECT_FALSE(d.received_data_before_response()); |
| 3863 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 3864 | } |
| 3865 | } |
| 3866 | |
[email protected] | 49abd65 | 2010-08-05 05:04:53 | [diff] [blame] | 3867 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3868 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUser) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3869 | ASSERT_TRUE(test_server_.Start()); |
| 3870 | |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 3871 | FilePath app_path; |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3872 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
[email protected] | 399b870 | 2009-05-01 20:34:02 | [diff] [blame] | 3873 | app_path = app_path.AppendASCII("LICENSE"); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3874 | TestDelegate d; |
| 3875 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3876 | TestURLRequest r( |
| 3877 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 3878 | "wrong_user", |
| 3879 | "chrome"), |
| 3880 | &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3881 | r.set_context(default_context_); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3882 | r.Start(); |
| 3883 | EXPECT_TRUE(r.is_pending()); |
| 3884 | |
| 3885 | MessageLoop::current()->Run(); |
| 3886 | |
| 3887 | int64 file_size = 0; |
| 3888 | file_util::GetFileSize(app_path, &file_size); |
| 3889 | |
[email protected] | ba2f334 | 2009-07-30 18:08:42 | [diff] [blame] | 3890 | EXPECT_FALSE(r.is_pending()); |
[email protected] | dd26501 | 2009-01-08 20:45:27 | [diff] [blame] | 3891 | EXPECT_EQ(1, d.response_started_count()); |
| 3892 | EXPECT_FALSE(d.received_data_before_response()); |
| 3893 | EXPECT_EQ(d.bytes_received(), 0); |
| 3894 | } |
| 3895 | } |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 3896 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 3897 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3898 | TEST_F(URLRequestTestFTP, DISABLED_FTPCheckWrongUserRestart) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3899 | ASSERT_TRUE(test_server_.Start()); |
| 3900 | |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 3901 | FilePath app_path; |
| 3902 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 3903 | app_path = app_path.AppendASCII("LICENSE"); |
| 3904 | TestDelegate d; |
| 3905 | // Set correct login credentials. The delegate will be asked for them when |
| 3906 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3907 | d.set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 3908 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3909 | TestURLRequest r( |
| 3910 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 3911 | "wrong_user", |
| 3912 | "chrome"), |
| 3913 | &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3914 | r.set_context(default_context_); |
[email protected] | 8b8a197d | 2009-08-26 15:57:58 | [diff] [blame] | 3915 | r.Start(); |
| 3916 | EXPECT_TRUE(r.is_pending()); |
| 3917 | |
| 3918 | MessageLoop::current()->Run(); |
| 3919 | |
| 3920 | int64 file_size = 0; |
| 3921 | file_util::GetFileSize(app_path, &file_size); |
| 3922 | |
| 3923 | EXPECT_FALSE(r.is_pending()); |
| 3924 | EXPECT_EQ(1, d.response_started_count()); |
| 3925 | EXPECT_FALSE(d.received_data_before_response()); |
| 3926 | EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
| 3927 | } |
| 3928 | } |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 3929 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 3930 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3931 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheURLCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3932 | ASSERT_TRUE(test_server_.Start()); |
| 3933 | |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 3934 | FilePath app_path; |
| 3935 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 3936 | app_path = app_path.AppendASCII("LICENSE"); |
| 3937 | |
| 3938 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 3939 | { |
| 3940 | // Pass correct login identity in the URL. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3941 | TestURLRequest r( |
| 3942 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 3943 | "chrome", |
| 3944 | "chrome"), |
| 3945 | d.get()); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3946 | r.set_context(default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 3947 | r.Start(); |
| 3948 | EXPECT_TRUE(r.is_pending()); |
| 3949 | |
| 3950 | MessageLoop::current()->Run(); |
| 3951 | |
| 3952 | int64 file_size = 0; |
| 3953 | file_util::GetFileSize(app_path, &file_size); |
| 3954 | |
| 3955 | EXPECT_FALSE(r.is_pending()); |
| 3956 | EXPECT_EQ(1, d->response_started_count()); |
| 3957 | EXPECT_FALSE(d->received_data_before_response()); |
| 3958 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 3959 | } |
| 3960 | |
| 3961 | d.reset(new TestDelegate); |
| 3962 | { |
| 3963 | // This request should use cached identity from previous request. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3964 | TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get()); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3965 | r.set_context(default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 3966 | r.Start(); |
| 3967 | EXPECT_TRUE(r.is_pending()); |
| 3968 | |
| 3969 | MessageLoop::current()->Run(); |
| 3970 | |
| 3971 | int64 file_size = 0; |
| 3972 | file_util::GetFileSize(app_path, &file_size); |
| 3973 | |
| 3974 | EXPECT_FALSE(r.is_pending()); |
| 3975 | EXPECT_EQ(1, d->response_started_count()); |
| 3976 | EXPECT_FALSE(d->received_data_before_response()); |
| 3977 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 3978 | } |
| 3979 | } |
| 3980 | |
[email protected] | cde4e80d | 2009-10-16 19:58:15 | [diff] [blame] | 3981 | // Flaky, see http://crbug.com/25045. |
[email protected] | 9ef1d98e | 2012-02-14 00:34:12 | [diff] [blame] | 3982 | TEST_F(URLRequestTestFTP, DISABLED_FTPCacheLoginBoxCredentials) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3983 | ASSERT_TRUE(test_server_.Start()); |
| 3984 | |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 3985 | FilePath app_path; |
| 3986 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 3987 | app_path = app_path.AppendASCII("LICENSE"); |
| 3988 | |
| 3989 | scoped_ptr<TestDelegate> d(new TestDelegate); |
| 3990 | // Set correct login credentials. The delegate will be asked for them when |
| 3991 | // the initial login with wrong credentials will fail. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 3992 | d->set_credentials(AuthCredentials(kChrome, kChrome)); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 3993 | { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 3994 | TestURLRequest r( |
| 3995 | test_server_.GetURLWithUserAndPassword("/LICENSE", |
| 3996 | "chrome", |
| 3997 | "wrong_password"), |
| 3998 | d.get()); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 3999 | r.set_context(default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 4000 | r.Start(); |
| 4001 | EXPECT_TRUE(r.is_pending()); |
| 4002 | |
| 4003 | MessageLoop::current()->Run(); |
| 4004 | |
| 4005 | int64 file_size = 0; |
| 4006 | file_util::GetFileSize(app_path, &file_size); |
| 4007 | |
| 4008 | EXPECT_FALSE(r.is_pending()); |
| 4009 | EXPECT_EQ(1, d->response_started_count()); |
| 4010 | EXPECT_FALSE(d->received_data_before_response()); |
| 4011 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 4012 | } |
| 4013 | |
| 4014 | // Use a new delegate without explicit credentials. The cached ones should be |
| 4015 | // used. |
| 4016 | d.reset(new TestDelegate); |
| 4017 | { |
| 4018 | // Don't pass wrong credentials in the URL, they would override valid cached |
| 4019 | // ones. |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4020 | TestURLRequest r(test_server_.GetURL("/LICENSE"), d.get()); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4021 | r.set_context(default_context_); |
[email protected] | 60a3df5 | 2009-09-22 16:13:24 | [diff] [blame] | 4022 | r.Start(); |
| 4023 | EXPECT_TRUE(r.is_pending()); |
| 4024 | |
| 4025 | MessageLoop::current()->Run(); |
| 4026 | |
| 4027 | int64 file_size = 0; |
| 4028 | file_util::GetFileSize(app_path, &file_size); |
| 4029 | |
| 4030 | EXPECT_FALSE(r.is_pending()); |
| 4031 | EXPECT_EQ(1, d->response_started_count()); |
| 4032 | EXPECT_FALSE(d->received_data_before_response()); |
| 4033 | EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
| 4034 | } |
| 4035 | } |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4036 | |
| 4037 | // Check that default A-L header is sent. |
| 4038 | TEST_F(URLRequestTestHTTP, DefaultAcceptLanguage) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4039 | ASSERT_TRUE(test_server_.Start()); |
| 4040 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4041 | TestNetworkDelegate network_delegate; // must outlive URLRequests |
| 4042 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 4043 | context->set_network_delegate(&network_delegate); |
| 4044 | context->set_accept_language("en"); |
| 4045 | context->Init(); |
| 4046 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4047 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4048 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d); |
[email protected] | 6dae6b8 | 2011-03-30 00:35:34 | [diff] [blame] | 4049 | req.set_context(context); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4050 | req.Start(); |
| 4051 | MessageLoop::current()->Run(); |
[email protected] | 6dae6b8 | 2011-03-30 00:35:34 | [diff] [blame] | 4052 | EXPECT_EQ("en", d.data_received()); |
| 4053 | } |
| 4054 | |
| 4055 | // Check that an empty A-L header is not sent. http://crbug.com/77365. |
| 4056 | TEST_F(URLRequestTestHTTP, EmptyAcceptLanguage) { |
| 4057 | ASSERT_TRUE(test_server_.Start()); |
| 4058 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4059 | TestNetworkDelegate network_delegate; // must outlive URLRequests |
| 4060 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 4061 | context->set_network_delegate(&network_delegate); |
| 4062 | context->Init(); |
| 4063 | // We override the language after initialization because empty entries |
| 4064 | // get overridden by Init(). |
[email protected] | 6dae6b8 | 2011-03-30 00:35:34 | [diff] [blame] | 4065 | context->set_accept_language(""); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4066 | |
| 4067 | TestDelegate d; |
| 4068 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d); |
[email protected] | 6dae6b8 | 2011-03-30 00:35:34 | [diff] [blame] | 4069 | req.set_context(context); |
| 4070 | req.Start(); |
| 4071 | MessageLoop::current()->Run(); |
| 4072 | EXPECT_EQ("None", d.data_received()); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | // Check that if request overrides the A-L header, the default is not appended. |
| 4076 | // See http://crbug.com/20894 |
| 4077 | TEST_F(URLRequestTestHTTP, OverrideAcceptLanguage) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4078 | ASSERT_TRUE(test_server_.Start()); |
| 4079 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4080 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4081 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Language"), &d); |
| 4082 | req.set_context(default_context_); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 4083 | HttpRequestHeaders headers; |
| 4084 | headers.SetHeader(HttpRequestHeaders::kAcceptLanguage, "ru"); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 4085 | req.SetExtraRequestHeaders(headers); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4086 | req.Start(); |
| 4087 | MessageLoop::current()->Run(); |
| 4088 | EXPECT_EQ(std::string("ru"), d.data_received()); |
| 4089 | } |
| 4090 | |
[email protected] | c7bef94c | 2011-06-21 18:05:51 | [diff] [blame] | 4091 | // Check that default A-E header is sent. |
| 4092 | TEST_F(URLRequestTestHTTP, DefaultAcceptEncoding) { |
| 4093 | ASSERT_TRUE(test_server_.Start()); |
| 4094 | |
| 4095 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4096 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), &d); |
| 4097 | req.set_context(default_context_); |
[email protected] | c7bef94c | 2011-06-21 18:05:51 | [diff] [blame] | 4098 | HttpRequestHeaders headers; |
| 4099 | req.SetExtraRequestHeaders(headers); |
| 4100 | req.Start(); |
| 4101 | MessageLoop::current()->Run(); |
| 4102 | EXPECT_TRUE(ContainsString(d.data_received(), "gzip")); |
| 4103 | } |
| 4104 | |
| 4105 | // Check that if request overrides the A-E header, the default is not appended. |
| 4106 | // See http://crbug.com/47381 |
| 4107 | TEST_F(URLRequestTestHTTP, OverrideAcceptEncoding) { |
| 4108 | ASSERT_TRUE(test_server_.Start()); |
| 4109 | |
| 4110 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4111 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Encoding"), &d); |
| 4112 | req.set_context(default_context_); |
[email protected] | c7bef94c | 2011-06-21 18:05:51 | [diff] [blame] | 4113 | HttpRequestHeaders headers; |
| 4114 | headers.SetHeader(HttpRequestHeaders::kAcceptEncoding, "identity"); |
| 4115 | req.SetExtraRequestHeaders(headers); |
| 4116 | req.Start(); |
| 4117 | MessageLoop::current()->Run(); |
| 4118 | EXPECT_FALSE(ContainsString(d.data_received(), "gzip")); |
| 4119 | EXPECT_TRUE(ContainsString(d.data_received(), "identity")); |
| 4120 | } |
| 4121 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4122 | // Check that default A-C header is sent. |
| 4123 | TEST_F(URLRequestTestHTTP, DefaultAcceptCharset) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4124 | ASSERT_TRUE(test_server_.Start()); |
| 4125 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4126 | TestNetworkDelegate network_delegate; // must outlive URLRequests |
| 4127 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 4128 | context->set_network_delegate(&network_delegate); |
| 4129 | context->set_accept_charset("en"); |
| 4130 | context->Init(); |
| 4131 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4132 | TestDelegate d; |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4133 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d); |
[email protected] | 6dae6b8 | 2011-03-30 00:35:34 | [diff] [blame] | 4134 | req.set_context(context); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4135 | req.Start(); |
| 4136 | MessageLoop::current()->Run(); |
[email protected] | 6dae6b8 | 2011-03-30 00:35:34 | [diff] [blame] | 4137 | EXPECT_EQ("en", d.data_received()); |
| 4138 | } |
| 4139 | |
| 4140 | // Check that an empty A-C header is not sent. http://crbug.com/77365. |
| 4141 | TEST_F(URLRequestTestHTTP, EmptyAcceptCharset) { |
| 4142 | ASSERT_TRUE(test_server_.Start()); |
| 4143 | |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4144 | TestNetworkDelegate network_delegate; // must outlive URLRequests |
| 4145 | scoped_refptr<TestURLRequestContext> context(new TestURLRequestContext(true)); |
| 4146 | context->set_network_delegate(&network_delegate); |
| 4147 | context->Init(); |
| 4148 | // We override the accepted charset after initialization because empty |
| 4149 | // entries get overridden otherwise. |
[email protected] | 6dae6b8 | 2011-03-30 00:35:34 | [diff] [blame] | 4150 | context->set_accept_charset(""); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4151 | |
| 4152 | TestDelegate d; |
| 4153 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d); |
[email protected] | 6dae6b8 | 2011-03-30 00:35:34 | [diff] [blame] | 4154 | req.set_context(context); |
| 4155 | req.Start(); |
| 4156 | MessageLoop::current()->Run(); |
| 4157 | EXPECT_EQ("None", d.data_received()); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | // Check that if request overrides the A-C header, the default is not appended. |
| 4161 | // See http://crbug.com/20894 |
| 4162 | TEST_F(URLRequestTestHTTP, OverrideAcceptCharset) { |
[email protected] | 95409e1 | 2010-08-17 20:07:11 | [diff] [blame] | 4163 | ASSERT_TRUE(test_server_.Start()); |
| 4164 | |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4165 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4166 | TestURLRequest req(test_server_.GetURL("echoheader?Accept-Charset"), &d); |
| 4167 | req.set_context(default_context_); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 4168 | HttpRequestHeaders headers; |
| 4169 | headers.SetHeader(HttpRequestHeaders::kAcceptCharset, "koi-8r"); |
[email protected] | 88e6b6f3 | 2010-05-07 23:14:25 | [diff] [blame] | 4170 | req.SetExtraRequestHeaders(headers); |
[email protected] | 09a362d3 | 2009-09-24 18:01:33 | [diff] [blame] | 4171 | req.Start(); |
| 4172 | MessageLoop::current()->Run(); |
| 4173 | EXPECT_EQ(std::string("koi-8r"), d.data_received()); |
| 4174 | } |
[email protected] | 50ba404b | 2011-01-21 13:38:45 | [diff] [blame] | 4175 | |
| 4176 | // Check that default User-Agent header is sent. |
| 4177 | TEST_F(URLRequestTestHTTP, DefaultUserAgent) { |
| 4178 | ASSERT_TRUE(test_server_.Start()); |
| 4179 | |
| 4180 | TestDelegate d; |
| 4181 | TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d); |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4182 | req.set_context(default_context_); |
[email protected] | 50ba404b | 2011-01-21 13:38:45 | [diff] [blame] | 4183 | req.Start(); |
| 4184 | MessageLoop::current()->Run(); |
| 4185 | EXPECT_EQ(req.context()->GetUserAgent(req.url()), d.data_received()); |
| 4186 | } |
| 4187 | |
| 4188 | // Check that if request overrides the User-Agent header, |
| 4189 | // the default is not appended. |
| 4190 | TEST_F(URLRequestTestHTTP, OverrideUserAgent) { |
| 4191 | ASSERT_TRUE(test_server_.Start()); |
| 4192 | |
| 4193 | TestDelegate d; |
[email protected] | 87a09a9 | 2011-07-14 15:50:50 | [diff] [blame] | 4194 | TestURLRequest req(test_server_.GetURL("echoheader?User-Agent"), &d); |
| 4195 | req.set_context(default_context_); |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 4196 | HttpRequestHeaders headers; |
| 4197 | headers.SetHeader(HttpRequestHeaders::kUserAgent, "Lynx (textmode)"); |
[email protected] | 50ba404b | 2011-01-21 13:38:45 | [diff] [blame] | 4198 | req.SetExtraRequestHeaders(headers); |
| 4199 | req.Start(); |
| 4200 | MessageLoop::current()->Run(); |
[email protected] | ebbad60 | 2011-01-21 23:30:47 | [diff] [blame] | 4201 | // If the net tests are being run with ChromeFrame then we need to allow for |
[email protected] | 0464e16a | 2011-02-04 22:56:57 | [diff] [blame] | 4202 | // the 'chromeframe' suffix which is added to the user agent before the |
| 4203 | // closing parentheses. |
| 4204 | EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); |
[email protected] | 50ba404b | 2011-01-21 13:38:45 | [diff] [blame] | 4205 | } |
[email protected] | 7461a40 | 2011-03-24 23:19:51 | [diff] [blame] | 4206 | |
| 4207 | } // namespace net |